music-create-img.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { defineComponent, onMounted, onUnmounted, reactive } from 'vue'
  2. import styles from './index.module.less'
  3. import { api_uploadFile } from '@/plugins/uploadFile'
  4. import { useMessage } from 'naive-ui'
  5. export default defineComponent({
  6. name: 'music-create-img',
  7. props: {
  8. xmlFileUrl: {
  9. type: String,
  10. default: ''
  11. }
  12. },
  13. emits: ['close', 'confirm'],
  14. setup(props, { emit }) {
  15. const message = useMessage()
  16. const state = reactive({
  17. productOpen: false,
  18. productIfameSrc: ''
  19. })
  20. /** 自动生成图片 */
  21. const forms = reactive({
  22. musicImg: '', // 五线谱图片
  23. musicFirstImg: '', //首调图片
  24. musicJianImg: '' // 简谱固定调
  25. })
  26. const handleAutoProduct = () => {
  27. if (!props.xmlFileUrl) {
  28. message.error('请先上传XML')
  29. return
  30. }
  31. const apiUrls = {
  32. dev: 'https://dev.kt.colexiu.com',
  33. test: 'https://test.lexiaoya.cn',
  34. online: 'https://mec.colexiu.com'
  35. }
  36. const environment = location.origin.includes('//dev')
  37. ? 'dev'
  38. : location.origin.includes('//test')
  39. ? 'test'
  40. : location.origin.includes('//mec')
  41. ? 'online'
  42. : 'dev'
  43. const apiUrl = apiUrls[environment]
  44. const prefix = /(localhost|192)/.test(location.host) ? 'https://dev.kt.colexiu.com' : apiUrl
  45. const xmlFileUrl = encodeURIComponent(props.xmlFileUrl)
  46. state.productIfameSrc =
  47. prefix + `/instrument/#/product-img?xmlUrl=${xmlFileUrl}&isCreateImg=true&isCbs=true`
  48. // state.productOpen = true
  49. }
  50. const handleProductResult = (res: MessageEvent) => {
  51. const data = res.data
  52. if (data?.api === 'webApi_renderSvg') {
  53. let imgs: any = []
  54. try {
  55. imgs = JSON.parse(data.product)
  56. } catch (error) {
  57. console.log('🚀 ~ error:', error)
  58. }
  59. imgs = imgs.filter((item: any) => item.base64)
  60. if (imgs.length === 3) {
  61. message.success('图片生成成功')
  62. handleUploadImg(imgs)
  63. } else {
  64. message.error('图片生成失败')
  65. }
  66. // console.log('🚀 ~ 上传之前', [...imgs])
  67. }
  68. }
  69. const handleUploadImg = async (imgs: any[]) => {
  70. for (let i = 0; i < imgs.length; i++) {
  71. const file = dataURLtoFile(imgs[i].base64, `${Date.now()}p${i}.png`)
  72. imgs[i].url = await api_uploadFile(file, () => {})
  73. }
  74. forms.musicImg = imgs[0]?.url || ''
  75. forms.musicFirstImg = imgs[1]?.url || ''
  76. forms.musicJianImg = imgs[2]?.url || ''
  77. // state.productOpen = false
  78. imgs = []
  79. emit('close')
  80. emit('confirm', forms)
  81. }
  82. /** base64转file */
  83. const dataURLtoFile = (dataurl: string, filename: string) => {
  84. let arr = dataurl.split(',') || [],
  85. mime = arr[0].match(/:(.*?);/)?.[1],
  86. bstr = atob(arr[1]),
  87. n = bstr.length,
  88. u8arr = new Uint8Array(n)
  89. while (n--) {
  90. u8arr[n] = bstr.charCodeAt(n)
  91. }
  92. return new File([u8arr], filename, { type: mime })
  93. }
  94. onMounted(() => {
  95. handleAutoProduct()
  96. window.addEventListener('message', handleProductResult)
  97. })
  98. onUnmounted(() => {
  99. window.removeEventListener('message', handleProductResult)
  100. })
  101. return () => <iframe class={styles.productIframe} src={state.productIfameSrc}></iframe>
  102. }
  103. })