| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- // module/chats/components/guide-analysis/guide-analysis.ts
- import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
- import { upload } from "../../../../lib/request/upload";
- Component({
- behaviors: [PageContainerBehavior],
- properties: {
- visible: { type: Boolean, value: false },
- },
- data: {
- uploadList: [
- {
- target: "tongueImgUrl",
- required: true,
- label: "舌面图",
- src: "../../assets/tongue-1.png",
- },
- {
- target: "tongueBackImgUrl",
- required: true,
- label: "舌下图",
- src: "../../assets/tongue-2.png",
- },
- {
- target: "faceImgUrl",
- required: false,
- label: "正面面部图",
- src: "../../assets/face-1.png",
- },
- ],
- thumbnail: [] as string[],
- original: [] as string[],
- _queue: {} as AnyObject,
- },
- methods: {
- handle(event: WechatMiniprogram.TouchEvent) {
- const { handle, index } = event.mark as AnyObject;
- switch (handle) {
- case "preview":
- break;
- case "upload":
- this._chooseMedia(index).then((src) => src && this._uploadMedia(index, src));
- break;
- case "upload:delete":
- this._deleteMedia(index);
- break;
- }
- },
- _chooseMedia(index: number) {
- return wx.chooseMedia({
- count: 1,
- mediaType: ["image"],
- sourceType: ["album", "camera"],
- camera: "front",
- }).then((res) => {
- const src = res.tempFiles[0].tempFilePath;
- this.setData({ [`thumbnail.${index}`]: src });
- return src;
- }).catch(() => null);
- },
- _deleteMedia(index: number) {
- this.setData({
- [`thumbnail.${index}`]: "",
- [`original.${index}`]: "",
- });
- },
- _uploadMedia(index: number, src?: string) {
- this.setData({ [`_queue.${index}`]: true });
- src ??= this.data.thumbnail[index];
- upload({
- params: { name: "file", file: src! },
- transform({ data }) {
- return (<any>data).url;
- },
- }).then(
- (src) => {
- this.setData({ [`original.${index}`]: src });
- },
- (error) => {
- wx.showToast({ title: error?.errMsg ?? "上传失败", icon: "error" });
- this.setData({
- [`thumbnail.${index}`]: "",
- [`original.${index}`]: "",
- });
- }
- ).then(() => {
- this.setData({ [`_queue.${index}`]: false });
- });
- },
- onCancel() {
- this.triggerEvent('stop');
- },
- onSubmit() {
- const data = {
- thumbnail: [] as any,
- source: [] as any,
- };
- for (let index = 0; index < this.data.uploadList.length; index++) {
- const item = this.data.uploadList[index];
- if (this.data._queue[index]) return wx.showToast({ title: `请等待图片上传完毕`, icon: 'none' });
- if (item.required && !this.data.original[index]) return wx.showToast({ title: `请上传${item.label}`, icon: 'none' });
- if (this.data.original[index]) data.source.push({
- target: item.target,
- src: this.data.original[index],
- });
- if (this.data.thumbnail[index]) data.thumbnail.push({
- target: item.target,
- src: this.data.thumbnail[index],
- });
- }
- this.triggerEvent('stop', data);
- }
- },
- })
|