index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { defineComponent, onMounted, reactive, ref } from 'vue'
  2. import styles from './index.module.less'
  3. import {
  4. Button,
  5. Grid,
  6. GridItem,
  7. Icon,
  8. Image,
  9. closeToast,
  10. showFailToast,
  11. showLoadingToast,
  12. showSuccessToast,
  13. showToast
  14. } from 'vant'
  15. import iconDownload from './images/icon-download.png'
  16. import iconFirend from './images/icon-friend.png'
  17. import iconWeChat from './images/icon-wechat.png'
  18. import iconFriendRing from './images/icon-friend-ring.png'
  19. // import iconLink from './images/icon-link.png';
  20. import iconLogo from './images/icon-logo.png'
  21. import shareBg from './images/share-bg.png'
  22. import audioPan from '../images/audio-pan.png'
  23. import smallLogo from '@common/images/logo.png'
  24. import musicBg from './images/music-bg.png'
  25. import QRCode from 'qrcode'
  26. import { promisefiyPostMessage, postMessage } from '@/helpers/native-message'
  27. import html2canvas from 'html2canvas'
  28. export default defineComponent({
  29. name: 'sahre-model',
  30. props: {
  31. musicDetail: {
  32. type: Object,
  33. default: () => {}
  34. }
  35. },
  36. emits: ['close'],
  37. setup(props, { emit }) {
  38. const canvasRef = ref()
  39. const state = reactive({
  40. saveLoading: false,
  41. image: null as any
  42. })
  43. const saveImg = async () => {
  44. showLoadingToast({
  45. message: '图片生成中...',
  46. forbidClick: true
  47. })
  48. setTimeout(() => {
  49. state.saveLoading = false
  50. }, 100)
  51. const res = await promisefiyPostMessage({
  52. api: 'savePicture',
  53. content: {
  54. base64: state.image
  55. }
  56. })
  57. if (res?.content?.status === 'success') {
  58. showSuccessToast('已保存到相册')
  59. } else {
  60. showFailToast('保存失败')
  61. }
  62. }
  63. const onSaveWe = async (type: string) => {
  64. showLoadingToast({
  65. message: '图片生成中...',
  66. forbidClick: true
  67. })
  68. setTimeout(() => {
  69. state.saveLoading = false
  70. }, 100)
  71. const res = await promisefiyPostMessage({
  72. api: 'shareTripartite',
  73. content: {
  74. title: '',
  75. desc: '',
  76. image: state.image,
  77. video: '',
  78. type: 'image',
  79. shareType: type
  80. }
  81. })
  82. if (res?.content?.status) {
  83. showSuccessToast('分享成功')
  84. } else {
  85. showFailToast('分享失败')
  86. }
  87. }
  88. const onSavePath = (type: string) => {
  89. // 判断是否在保存中...
  90. if (state.saveLoading) {
  91. return
  92. }
  93. state.saveLoading = true
  94. // 判断是否已经生成图片
  95. if (state.image) {
  96. if (type) {
  97. onSaveWe(type)
  98. } else {
  99. saveImg()
  100. }
  101. } else {
  102. const container: any = document.getElementById('shareContent')
  103. html2canvas(container, {
  104. allowTaint: true,
  105. useCORS: true,
  106. backgroundColor: null
  107. })
  108. .then(async (canvas) => {
  109. const url = canvas.toDataURL('image/png')
  110. state.image = url
  111. if (type) {
  112. onSaveWe(type)
  113. } else {
  114. saveImg()
  115. }
  116. })
  117. .catch(() => {
  118. closeToast()
  119. state.saveLoading = false
  120. })
  121. }
  122. }
  123. onMounted(() => {
  124. const canvas = canvasRef.value
  125. QRCode.toCanvas(
  126. canvas,
  127. location.origin + location.pathname + '#/shareCreation?id=' + props.musicDetail.id,
  128. {
  129. margin: 1
  130. },
  131. (error: any) => {
  132. if (error) console.log(error)
  133. console.log('success')
  134. }
  135. )
  136. })
  137. return () => (
  138. <div class={styles.shareModel}>
  139. <div class={styles.shareContent} id="shareContent">
  140. <img src={shareBg} class={styles.shareBg} />
  141. <div class={styles.share_content__title}>
  142. <p class={styles.large}>我在管乐团发布了演奏作品</p>
  143. <p class={styles.small}>赶快扫码看看吧!</p>
  144. </div>
  145. <div class={[styles.share_music__container, styles.sectionFile]}>
  146. <div class={styles.uploadImg}>
  147. <img src={audioPan} class={styles.audioPan} crossorigin="anonymous" />
  148. <img
  149. src={
  150. props.musicDetail.img
  151. ? props.musicDetail.img + '?t' + new Date().getTime()
  152. : musicBg
  153. }
  154. class={styles.muploader}
  155. crossorigin="anonymous"
  156. />
  157. </div>
  158. <div class={styles.musicDetail}>
  159. <p class={[styles.musicName, 'van-ellipsis']}>{props.musicDetail.musicSheetName}</p>
  160. <p class={styles.username}>演奏者:{props.musicDetail.username}</p>
  161. </div>
  162. </div>
  163. <div class={styles.downloadSection}>
  164. <div class={styles.qrcode}>
  165. <canvas ref={canvasRef} class={styles.qrcodeCanvas}></canvas>
  166. <img src={smallLogo} class={styles.qrcodeLogo} />
  167. </div>
  168. <div class={styles.qrtips}>
  169. <p class={styles.tip}>温馨提示:保存图片到相册或长按识别二维码进入查看喔~</p>
  170. <img src={iconLogo} class={styles.iconLogo} />
  171. {/* <p class={styles.downTip}>扫码下载音乐数字课堂App</p> */}
  172. </div>
  173. </div>
  174. </div>
  175. <div class={styles.shareBottom}>
  176. <Icon name="cross" class={styles.iconClose} onClick={() => emit('close')} />
  177. <div class={styles.share__header}>海报已生成!快来分享吧!</div>
  178. <Grid columnNum={5} border={false} class={styles.gridSection}>
  179. <GridItem icon={iconDownload} text="保存本地" onClick={() => onSavePath('')}></GridItem>
  180. {/* <GridItem icon={iconFirend} text="群聊"></GridItem> */}
  181. <GridItem
  182. icon={iconWeChat}
  183. text="微信好友"
  184. onClick={() => onSavePath('wechat')}
  185. ></GridItem>
  186. <GridItem
  187. icon={iconFriendRing}
  188. text="朋友圈"
  189. onClick={() => onSavePath('wechat_circle')}
  190. ></GridItem>
  191. {/* <GridItem icon={iconLink} text="复制链接"></GridItem> */}
  192. </Grid>
  193. <div class={[styles.btn, 'van-hairline--top']} onClick={() => emit('close')}>
  194. 取消
  195. </div>
  196. </div>
  197. </div>
  198. )
  199. }
  200. })