download.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { defineComponent, reactive, ref, watch } from 'vue'
  2. import styles from './download.module.less'
  3. import { postMessage, promisefiyPostMessage } from '@/helpers/native-message'
  4. import {
  5. addMusicTitle,
  6. addWatermark,
  7. convasToImg,
  8. imgToCanvas
  9. } from './imageFunction'
  10. import { Button, Swipe, SwipeItem, Toast, Image } from 'vant'
  11. export default defineComponent({
  12. name: 'download',
  13. props: {
  14. imgList: {
  15. type: Array,
  16. default: () => []
  17. },
  18. musicSheetName: {
  19. type: String,
  20. default: ''
  21. }
  22. },
  23. setup(props) {
  24. const list = ref(props.imgList)
  25. const swipeRef = ref()
  26. watch(
  27. () => props.imgList,
  28. (val: any) => {
  29. list.value = val
  30. acitveIndex.value = 0
  31. if (swipeRef.value) swipeRef.value.swipeTo(0)
  32. }
  33. )
  34. const acitveIndex = ref(0)
  35. const saveLoading = ref<boolean>(false)
  36. const image = ref('')
  37. const onSaveImg = async () => {
  38. // 判断是否在保存中...
  39. if (saveLoading.value) {
  40. return
  41. }
  42. saveLoading.value = true
  43. // 判断是否已经生成图片
  44. if (image.value) {
  45. saveImg()
  46. } else {
  47. const tempCanvas = await imgToCanvas(
  48. list.value[acitveIndex.value] as any
  49. )
  50. const titleCanvas = addMusicTitle(tempCanvas, {
  51. title: props.musicSheetName,
  52. size: 20
  53. })
  54. const canvas = await addWatermark(titleCanvas, '酷乐秀')
  55. image.value = convasToImg(canvas)
  56. await saveImg()
  57. }
  58. }
  59. const saveImg = async () => {
  60. Toast.loading({
  61. message: '图片生成中...',
  62. forbidClick: true
  63. })
  64. setTimeout(() => {
  65. saveLoading.value = false
  66. }, 100)
  67. const res = await promisefiyPostMessage({
  68. api: 'savePicture',
  69. content: {
  70. base64: image.value
  71. }
  72. })
  73. if (res?.content?.status === 'success') {
  74. Toast.success('已保存到相册')
  75. } else {
  76. Toast.fail('保存失败')
  77. }
  78. }
  79. return () => {
  80. return (
  81. <div class={styles.downloadContainer}>
  82. <div class={styles.musicContainer}>
  83. <h2>{props.musicSheetName}</h2>
  84. <div class={styles.musicImg}>
  85. <Swipe
  86. ref={swipeRef}
  87. showIndicators={false}
  88. loop={false}
  89. onChange={(index: number) => {
  90. acitveIndex.value = index
  91. image.value = ''
  92. }}
  93. >
  94. {list.value.length > 0 &&
  95. list.value.map((img: any) => (
  96. <SwipeItem>
  97. <Image src={img} />
  98. </SwipeItem>
  99. ))}
  100. </Swipe>
  101. </div>
  102. </div>
  103. <div class={styles.buttonGroup}>
  104. <div class={styles.num}>
  105. <span class={styles.page}>
  106. {acitveIndex.value + 1}/{list.value.length}
  107. </span>
  108. <span class={styles.countPage}>(共{list.value.length}页)</span>
  109. </div>
  110. <Button
  111. type="primary"
  112. color="linear-gradient(180deg, #59E5D5 0%, #2DC7AA 100%)"
  113. class={styles.downloadBtn}
  114. block
  115. round
  116. onClick={() => onSaveImg()}
  117. loading={saveLoading.value}
  118. loadingText={'加载中...'}
  119. >
  120. 下载当前页面
  121. </Button>
  122. </div>
  123. </div>
  124. )
  125. }
  126. }
  127. })