download.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. console.log(
  48. list.value[acitveIndex.value],
  49. 'list.value[acitveIndex.value]'
  50. )
  51. const tempCanvas = await imgToCanvas(
  52. list.value[acitveIndex.value] as any
  53. )
  54. const titleCanvas = addMusicTitle(tempCanvas, {
  55. title: props.musicSheetName,
  56. size: 18
  57. })
  58. const canvas = await addWatermark(titleCanvas, '酷乐秀')
  59. image.value = convasToImg(canvas)
  60. await saveImg()
  61. }
  62. }
  63. const saveImg = async () => {
  64. Toast.loading({
  65. message: '图片生成中...',
  66. forbidClick: true
  67. })
  68. setTimeout(() => {
  69. saveLoading.value = false
  70. }, 100)
  71. const res = await promisefiyPostMessage({
  72. api: 'savePicture',
  73. content: {
  74. base64: image.value
  75. }
  76. })
  77. if (res?.content?.status === 'success') {
  78. Toast.success('已保存到相册')
  79. } else {
  80. Toast.fail('保存失败')
  81. }
  82. }
  83. return () => {
  84. return (
  85. <div class={styles.downloadContainer}>
  86. <div class={styles.musicContainer}>
  87. <h2>{props.musicSheetName}</h2>
  88. <div class={styles.musicImg}>
  89. <Swipe
  90. ref={swipeRef}
  91. showIndicators={false}
  92. loop={false}
  93. onChange={(index: number) => {
  94. acitveIndex.value = index
  95. image.value = ''
  96. }}
  97. >
  98. {list.value.length > 0 &&
  99. list.value.map((img: any) => (
  100. <SwipeItem>
  101. <Image src={img} />
  102. </SwipeItem>
  103. ))}
  104. </Swipe>
  105. </div>
  106. </div>
  107. <div class={styles.buttonGroup}>
  108. <div class={styles.num}>
  109. <span class={styles.page}>
  110. {acitveIndex.value + 1}/{list.value.length}
  111. </span>
  112. <span class={styles.countPage}>(共{list.value.length}页)</span>
  113. </div>
  114. <Button
  115. type="primary"
  116. color="linear-gradient(270deg, #FF204B 0%, #FE5B71 100%)"
  117. class={styles.downloadBtn}
  118. block
  119. round
  120. onClick={() => onSaveImg()}
  121. loading={saveLoading.value}
  122. loadingText={'加载中...'}
  123. >
  124. 下载当前页面
  125. </Button>
  126. </div>
  127. </div>
  128. )
  129. }
  130. }
  131. })