index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Button, Cell, Swipe, SwipeItem, Toast } from 'vant'
  2. import { defineComponent } from 'vue'
  3. import styles from './index.module.less'
  4. import { postMessage, promisefiyPostMessage } from '@/helpers/native-message'
  5. import html2canvas from 'html2canvas'
  6. import ShareItem from './share-item'
  7. import request from '@/helpers/request'
  8. export default defineComponent({
  9. name: 'col-share',
  10. props: {
  11. teacherId: {
  12. type: Number
  13. },
  14. shareUrl: {
  15. type: String,
  16. default: ''
  17. }
  18. },
  19. data() {
  20. return {
  21. image: null as any,
  22. codeUrl: ''
  23. }
  24. },
  25. async mounted() {
  26. try {
  27. const shortRes = await request.post('/api-teacher/sysConfig/shortURL', {
  28. requestType: 'form',
  29. data: {
  30. orginURL: this.shareUrl
  31. }
  32. })
  33. this.codeUrl = shortRes.data
  34. this.$nextTick(async () => {
  35. const container: any = document.getElementById(
  36. 'share-preview-container'
  37. )
  38. html2canvas(container, { allowTaint: true, useCORS: true }).then(
  39. canvas => {
  40. const url = canvas.toDataURL('image/png')
  41. this.image = url
  42. }
  43. )
  44. })
  45. } catch {
  46. //
  47. }
  48. },
  49. methods: {
  50. async onSaveImg() {
  51. const res = await promisefiyPostMessage({
  52. api: 'savePicture',
  53. content: {
  54. base64: this.image
  55. }
  56. })
  57. if (res?.content?.status === 'success') {
  58. Toast.success('保存成功')
  59. } else {
  60. Toast.fail('保存失败')
  61. }
  62. },
  63. async shareShow() {
  64. const image = this.image
  65. if (image) {
  66. postMessage(
  67. {
  68. api: 'shareAchievements',
  69. content: {
  70. title: '我在酷乐秀使用小酷Ai练习乐器',
  71. desc: '酷乐秀小酷Ai帮助我自主练习乐器,真的太好用啦!每天都要坚持练习哦~',
  72. image,
  73. video: '',
  74. type: 'image'
  75. }
  76. },
  77. (res: any) => {
  78. if (res && res.content) {
  79. Toast(
  80. res.content.message ||
  81. (res.content.status ? '分享成功' : '分享失败')
  82. )
  83. }
  84. }
  85. )
  86. }
  87. }
  88. },
  89. render() {
  90. return (
  91. <>
  92. {this.codeUrl && (
  93. <>
  94. <div class={styles.shareContainer}>
  95. <Swipe
  96. showIndicators={false}
  97. loop={false}
  98. style={{ borderRadius: '10px', overflow: 'hidden' }}
  99. >
  100. <SwipeItem>
  101. <ShareItem
  102. teacherId={this.teacherId}
  103. shareUrl={this.codeUrl}
  104. />
  105. </SwipeItem>
  106. <SwipeItem>
  107. <ShareItem
  108. teacherId={this.teacherId}
  109. shareUrl={this.codeUrl}
  110. />
  111. </SwipeItem>
  112. <SwipeItem>
  113. <ShareItem
  114. teacherId={this.teacherId}
  115. shareUrl={this.codeUrl}
  116. />
  117. </SwipeItem>
  118. </Swipe>
  119. </div>
  120. <div class={['btnGroup', styles.shareGroupBtn]}>
  121. <Button type="primary" round onClick={this.onSaveImg}>
  122. 保存图片
  123. </Button>
  124. <Button
  125. type="primary"
  126. round
  127. onClick={() => {
  128. this.shareShow()
  129. }}
  130. >
  131. 立即分享
  132. </Button>
  133. </div>
  134. </>
  135. )}
  136. </>
  137. )
  138. }
  139. })