index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 +
  128. location.pathname +
  129. '#/shareCreation?id=' +
  130. props.musicDetail.id,
  131. {
  132. margin: 1
  133. },
  134. (error: any) => {
  135. if (error) console.log(error);
  136. console.log('success');
  137. }
  138. );
  139. });
  140. return () => (
  141. <div class={styles.shareModel}>
  142. <div class={styles.shareContent} id="shareContent">
  143. <img src={shareBg} class={styles.shareBg} />
  144. <div class={styles.share_content__title}>
  145. <p class={styles.large}>我在音乐数字课堂发布了演奏作品</p>
  146. <p class={styles.small}>赶快扫码看看吧!</p>
  147. </div>
  148. <div class={[styles.share_music__container, styles.sectionFile]}>
  149. <div class={styles.uploadImg}>
  150. <img
  151. src={audioPan}
  152. class={styles.audioPan}
  153. crossorigin="anonymous"
  154. />
  155. <img
  156. src={
  157. props.musicDetail.img
  158. ? props.musicDetail.img + '?t' + new Date().getTime()
  159. : musicBg
  160. }
  161. class={styles.muploader}
  162. crossorigin="anonymous"
  163. />
  164. </div>
  165. <div class={styles.musicDetail}>
  166. <p class={[styles.musicName, 'van-ellipsis']}>
  167. {props.musicDetail.musicSheetName}
  168. </p>
  169. <p class={styles.username}>
  170. 演奏者:{props.musicDetail.username}
  171. </p>
  172. </div>
  173. </div>
  174. <div class={styles.downloadSection}>
  175. <div class={styles.qrcode}>
  176. <canvas ref={canvasRef} class={styles.qrcodeCanvas}></canvas>
  177. <img src={smallLogo} class={styles.qrcodeLogo} />
  178. </div>
  179. <div class={styles.qrtips}>
  180. <p class={styles.tip}>
  181. 温馨提示:保存图片到相册或长按识别二维码进入查看喔~
  182. </p>
  183. <img src={iconLogo} class={styles.iconLogo} />
  184. <p class={styles.downTip}>扫码下载音乐数字课堂App</p>
  185. </div>
  186. </div>
  187. </div>
  188. <div class={styles.shareBottom}>
  189. <Icon
  190. name="cross"
  191. class={styles.iconClose}
  192. onClick={() => emit('close')}
  193. />
  194. <div class={styles.share__header}>海报已生成!快来分享吧!</div>
  195. <Grid columnNum={5} border={false} class={styles.gridSection}>
  196. <GridItem
  197. icon={iconDownload}
  198. text="保存本地"
  199. onClick={() => onSavePath('')}></GridItem>
  200. {/* <GridItem icon={iconFirend} text="群聊"></GridItem> */}
  201. <GridItem
  202. icon={iconWeChat}
  203. text="微信好友"
  204. onClick={() => onSavePath('wechat')}></GridItem>
  205. <GridItem
  206. icon={iconFriendRing}
  207. text="朋友圈"
  208. onClick={() => onSavePath('wechat_circle')}></GridItem>
  209. {/* <GridItem icon={iconLink} text="复制链接"></GridItem> */}
  210. </Grid>
  211. <div
  212. class={[styles.btn, 'van-hairline--top']}
  213. onClick={() => emit('close')}>
  214. 取消
  215. </div>
  216. </div>
  217. </div>
  218. );
  219. }
  220. });