|
@@ -1,9 +1,10 @@
|
|
|
-import { defineComponent, onMounted, reactive, watch } from "vue";
|
|
|
+import { defineComponent, onMounted, onUnmounted, reactive, watch } from "vue";
|
|
|
import { api_musicSheetCreationWav2mp3, api_musicSheetSave, api_subjectList } from "../../api";
|
|
|
-import { NButton, NForm, NFormItem, NIcon, NModal, NSelect, NSpace } from "naive-ui";
|
|
|
+import { NButton, NForm, NFormItem, NIcon, NModal, NSelect, NSpace, useMessage } from "naive-ui";
|
|
|
import styles from "./index.module.less";
|
|
|
import { Close } from "@vicons/ionicons5";
|
|
|
import { SelectMixedOption } from "naive-ui/es/select/src/interface";
|
|
|
+import { api_uploadFile } from "/src/utils/uploadFile";
|
|
|
|
|
|
export default defineComponent({
|
|
|
name: "UploadToResources",
|
|
@@ -19,14 +20,20 @@ export default defineComponent({
|
|
|
},
|
|
|
emits: ["update:show"],
|
|
|
setup(props, { emit }) {
|
|
|
+ const message = useMessage();
|
|
|
const model = reactive({
|
|
|
subjects: [] as SelectMixedOption[],
|
|
|
saveLoading: false,
|
|
|
+ productOpen: false,
|
|
|
+ productIfameSrc: "",
|
|
|
});
|
|
|
const froms = reactive({
|
|
|
subjectId: null,
|
|
|
isPublic: 0,
|
|
|
mp3: "",
|
|
|
+ musicImg: "",
|
|
|
+ musicSvg: "",
|
|
|
+ musicJianSvg: "",
|
|
|
});
|
|
|
const getSubjects = async () => {
|
|
|
const { data } = await api_subjectList();
|
|
@@ -37,14 +44,63 @@ export default defineComponent({
|
|
|
};
|
|
|
});
|
|
|
};
|
|
|
+
|
|
|
+ const handleProductResult = (res: MessageEvent) => {
|
|
|
+ const data = res.data;
|
|
|
+ if (data?.api === "webApi_renderSvg") {
|
|
|
+ let imgs: any = [];
|
|
|
+ try {
|
|
|
+ imgs = JSON.parse(data.product);
|
|
|
+ } catch (error) {
|
|
|
+ console.log("🚀 ~ error:", error);
|
|
|
+ }
|
|
|
+ imgs = imgs.filter((item: any) => item.base64);
|
|
|
+ if (imgs.length === 3) {
|
|
|
+ message.success("生成成功");
|
|
|
+ handleUploadImg(imgs);
|
|
|
+ } else {
|
|
|
+ message.error("生成失败");
|
|
|
+ }
|
|
|
+ console.log("🚀 ~ 上传之前", [...imgs]);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ const handleUploadImg = async (imgs: any[]) => {
|
|
|
+ for (let i = 0; i < imgs.length; i++) {
|
|
|
+ const fileName = `${Date.now()}p${i}.png`;
|
|
|
+ const file = dataURLtoFile(imgs[i].base64, fileName);
|
|
|
+ imgs[i].url = await api_uploadFile(file, fileName, () => {});
|
|
|
+ }
|
|
|
+ froms.musicImg = imgs[0]?.url || "";
|
|
|
+ froms.musicSvg = imgs[1]?.url || "";
|
|
|
+ froms.musicJianSvg = imgs[2]?.url || "";
|
|
|
+ model.productOpen = false;
|
|
|
+ imgs = [];
|
|
|
+ handleSubmit();
|
|
|
+ };
|
|
|
+ /** base64转file */
|
|
|
+ const dataURLtoFile = (dataurl: string, filename: string) => {
|
|
|
+ let arr = dataurl.split(",") || [],
|
|
|
+ mime = arr[0].match(/:(.*?);/)?.[1],
|
|
|
+ bstr = atob(arr[1]),
|
|
|
+ n = bstr.length,
|
|
|
+ u8arr = new Uint8Array(n);
|
|
|
+ while (n--) {
|
|
|
+ u8arr[n] = bstr.charCodeAt(n);
|
|
|
+ }
|
|
|
+ return new File([u8arr], filename, { type: mime });
|
|
|
+ };
|
|
|
onMounted(() => {
|
|
|
getSubjects();
|
|
|
+ window.addEventListener("message", handleProductResult);
|
|
|
+ });
|
|
|
+ onUnmounted(() => {
|
|
|
+ window.removeEventListener("message", handleProductResult);
|
|
|
});
|
|
|
watch(
|
|
|
() => props.item,
|
|
|
() => {
|
|
|
console.log(props.item);
|
|
|
- froms.subjectId = props.item.subjectId ?? null;
|
|
|
+ froms.subjectId = props.item.subjectId ?? null;
|
|
|
}
|
|
|
);
|
|
|
|
|
@@ -66,9 +122,9 @@ export default defineComponent({
|
|
|
track: "",
|
|
|
},
|
|
|
],
|
|
|
- musicImg: props.item.coverImg,
|
|
|
- musicSvg: "",
|
|
|
- musicJianSvg: "",
|
|
|
+ musicImg: froms.musicImg,
|
|
|
+ musicSvg: froms.musicSvg,
|
|
|
+ musicJianSvg: froms.musicJianSvg,
|
|
|
extConfigJson: "",
|
|
|
});
|
|
|
};
|
|
@@ -77,9 +133,25 @@ export default defineComponent({
|
|
|
froms.mp3 = data;
|
|
|
};
|
|
|
|
|
|
- const handleUpload = async () => {
|
|
|
- model.saveLoading = true;
|
|
|
+ /** 自动生成图片 */
|
|
|
+ const handleAutoProduct = () => {
|
|
|
+ const xml = props.item.xml;
|
|
|
+ if (!xml) {
|
|
|
+ message.error("没有生成xml文件");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const origin = /(localhost|192)/.test(location.host)
|
|
|
+ ? "https://test.lexiaoya.cn"
|
|
|
+ : location.origin;
|
|
|
+ model.productIfameSrc = `${origin}/instrument/#/product-img?xmlUrl=${xml}&productXmlImg=1`;
|
|
|
+ model.productOpen = true;
|
|
|
+ };
|
|
|
|
|
|
+ const handleUpload = () => {
|
|
|
+ model.saveLoading = true;
|
|
|
+ handleAutoProduct();
|
|
|
+ };
|
|
|
+ const handleSubmit = async () => {
|
|
|
await wav2mp3();
|
|
|
await createMusic();
|
|
|
setTimeout(() => {
|
|
@@ -88,58 +160,61 @@ export default defineComponent({
|
|
|
}, 1000);
|
|
|
};
|
|
|
return () => (
|
|
|
- <NModal autoFocus={false} show={props.show} onUpdate:show={(val) => emit("update:show", val)}>
|
|
|
- <div class={styles.setbox}>
|
|
|
- <div class={styles.head}>
|
|
|
- <div>上传到我的资源</div>
|
|
|
- <NButton
|
|
|
- class={styles.close}
|
|
|
- quaternary
|
|
|
- circle
|
|
|
- size="small"
|
|
|
- onClick={() => emit("update:show", false)}
|
|
|
- >
|
|
|
- <NIcon component={Close} size={18} />
|
|
|
- </NButton>
|
|
|
- </div>
|
|
|
- <NForm class={styles.form} labelPlacement="left">
|
|
|
- <NFormItem label="可用声部">
|
|
|
- <NSelect
|
|
|
- to="body"
|
|
|
- placeholder="请选择素材可用乐器"
|
|
|
- options={model.subjects}
|
|
|
- v-model:value={froms.subjectId}
|
|
|
- ></NSelect>
|
|
|
- </NFormItem>
|
|
|
- <NFormItem label="是否公开">
|
|
|
- <NSpace class={styles.checkbox} wrapItem={false}>
|
|
|
- <NButton
|
|
|
- secondary
|
|
|
- bordered={false}
|
|
|
- type={froms.isPublic === 1 ? "primary" : "default"}
|
|
|
- onClick={() => (froms.isPublic = 1)}
|
|
|
- >
|
|
|
- 公开
|
|
|
- </NButton>
|
|
|
- <NButton
|
|
|
- secondary
|
|
|
- bordered={false}
|
|
|
- type={froms.isPublic === 0 ? "primary" : "default"}
|
|
|
- onClick={() => (froms.isPublic = 0)}
|
|
|
- >
|
|
|
- 不公开
|
|
|
- </NButton>
|
|
|
- </NSpace>
|
|
|
- </NFormItem>
|
|
|
- </NForm>
|
|
|
- <div class={styles.btns}>
|
|
|
- <NButton onClick={() => emit("update:show", false)}>取消</NButton>
|
|
|
- <NButton type="primary" loading={model.saveLoading} onClick={() => handleUpload()}>
|
|
|
- 确定
|
|
|
- </NButton>
|
|
|
+ <>
|
|
|
+ <NModal autoFocus={false} show={props.show} onUpdate:show={(val) => emit("update:show", val)}>
|
|
|
+ <div class={styles.setbox}>
|
|
|
+ <div class={styles.head}>
|
|
|
+ <div>上传到我的资源</div>
|
|
|
+ <NButton
|
|
|
+ class={styles.close}
|
|
|
+ quaternary
|
|
|
+ circle
|
|
|
+ size="small"
|
|
|
+ onClick={() => emit("update:show", false)}
|
|
|
+ >
|
|
|
+ <NIcon component={Close} size={18} />
|
|
|
+ </NButton>
|
|
|
+ </div>
|
|
|
+ <NForm class={styles.form} labelPlacement="left">
|
|
|
+ <NFormItem label="可用声部">
|
|
|
+ <NSelect
|
|
|
+ to="body"
|
|
|
+ placeholder="请选择素材可用乐器"
|
|
|
+ options={model.subjects}
|
|
|
+ v-model:value={froms.subjectId}
|
|
|
+ ></NSelect>
|
|
|
+ </NFormItem>
|
|
|
+ <NFormItem label="是否公开">
|
|
|
+ <NSpace class={styles.checkbox} wrapItem={false}>
|
|
|
+ <NButton
|
|
|
+ secondary
|
|
|
+ bordered={false}
|
|
|
+ type={froms.isPublic === 1 ? "primary" : "default"}
|
|
|
+ onClick={() => (froms.isPublic = 1)}
|
|
|
+ >
|
|
|
+ 公开
|
|
|
+ </NButton>
|
|
|
+ <NButton
|
|
|
+ secondary
|
|
|
+ bordered={false}
|
|
|
+ type={froms.isPublic === 0 ? "primary" : "default"}
|
|
|
+ onClick={() => (froms.isPublic = 0)}
|
|
|
+ >
|
|
|
+ 不公开
|
|
|
+ </NButton>
|
|
|
+ </NSpace>
|
|
|
+ </NFormItem>
|
|
|
+ </NForm>
|
|
|
+ <div class={styles.btns}>
|
|
|
+ <NButton onClick={() => emit("update:show", false)}>取消</NButton>
|
|
|
+ <NButton type="primary" loading={model.saveLoading} onClick={() => handleUpload()}>
|
|
|
+ 确定
|
|
|
+ </NButton>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
- </div>
|
|
|
- </NModal>
|
|
|
+ </NModal>
|
|
|
+ { model.productOpen && <iframe class={styles.productIframe} src={model.productIfameSrc}></iframe>}
|
|
|
+ </>
|
|
|
);
|
|
|
},
|
|
|
});
|