index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { Button, Cell, Swipe, SwipeItem, Toast } from 'vant'
  2. import { defineComponent, PropType } 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. shareType: {
  19. // 分享类型
  20. type: String as PropType<'' | 'video' | 'music' | 'live'>,
  21. default: ''
  22. }
  23. },
  24. data() {
  25. return {
  26. image: null as any,
  27. codeUrl: ''
  28. }
  29. },
  30. async mounted() {
  31. try {
  32. // const shortRes = await request.post('/api-teacher/sysConfig/shortURL', {
  33. // requestType: 'form',
  34. // data: {
  35. // orginURL: this.shareUrl
  36. // }
  37. // })
  38. // this.codeUrl = shortRes.data
  39. this.codeUrl = this.shareUrl
  40. } catch {
  41. //
  42. }
  43. },
  44. methods: {
  45. async onSaveImg() {
  46. const container: any = document.getElementById('share-preview-container')
  47. html2canvas(container, { allowTaint: true, useCORS: true }).then(
  48. async canvas => {
  49. const url = canvas.toDataURL('image/png')
  50. this.image = url
  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. )
  64. },
  65. async shareShow() {
  66. const container: any = document.getElementById('share-preview-container')
  67. html2canvas(container, { allowTaint: true, useCORS: true }).then(
  68. async canvas => {
  69. const url = canvas.toDataURL('image/png')
  70. this.image = url
  71. const image = this.image
  72. if (image) {
  73. postMessage(
  74. {
  75. api: 'shareAchievements',
  76. content: {
  77. title: '我在酷乐秀使用小酷Ai练习乐器',
  78. desc: '酷乐秀小酷Ai帮助我自主练习乐器,真的太好用啦!每天都要坚持练习哦~',
  79. image,
  80. video: '',
  81. type: 'image'
  82. }
  83. },
  84. (res: any) => {
  85. if (res && res.content) {
  86. Toast(
  87. res.content.message ||
  88. (res.content.status ? '分享成功' : '分享失败')
  89. )
  90. }
  91. }
  92. )
  93. }
  94. }
  95. )
  96. }
  97. },
  98. render() {
  99. return (
  100. <>
  101. {this.codeUrl && (
  102. <>
  103. <div class={styles.shareContainer} id="share-preview-container">
  104. <Swipe showIndicators={false} loop={false}>
  105. <SwipeItem>
  106. <ShareItem teacherId={this.teacherId} shareUrl={this.codeUrl}>
  107. {this.$slots.default && this.$slots.default()}
  108. </ShareItem>
  109. </SwipeItem>
  110. <SwipeItem>
  111. <ShareItem
  112. teacherId={this.teacherId}
  113. shareUrl={this.codeUrl}
  114. showType="yellow"
  115. >
  116. {this.$slots.default && this.$slots.default()}
  117. </ShareItem>
  118. </SwipeItem>
  119. <SwipeItem>
  120. <ShareItem
  121. teacherId={this.teacherId}
  122. shareUrl={this.codeUrl}
  123. showType="pink"
  124. >
  125. {this.$slots.default && this.$slots.default()}
  126. </ShareItem>
  127. </SwipeItem>
  128. </Swipe>
  129. </div>
  130. <div class={['btnGroup', styles.shareGroupBtn]}>
  131. <Button type="primary" plain round onClick={this.onSaveImg}>
  132. 保存图片
  133. </Button>
  134. <Button
  135. type="primary"
  136. round
  137. onClick={() => {
  138. this.shareShow()
  139. }}
  140. >
  141. 立即分享
  142. </Button>
  143. </div>
  144. </>
  145. )}
  146. </>
  147. )
  148. }
  149. })