import { upload } from "../../../../lib/request/upload"; Component({ properties: { visible: { type: Boolean, value: false }, desc: { type: String, value: "" }, images: { type: Array, value: [] }, }, data: { inputDesc: "", inputImages: [] as string[], uploading: false, }, observers: { visible(v: boolean) { if (v) { this.setData({ inputDesc: this.data.desc || "", inputImages: [...(this.data.images as string[])], }); } }, }, methods: { onPopupVisibleChange(e: WechatMiniprogram.CustomEvent<{ visible: boolean }>) { if (!e?.detail?.visible) this.triggerEvent("close"); }, onClose() { this.triggerEvent("close"); }, onDescInput(e: WechatMiniprogram.Input) { const value = (e.detail?.value || "").slice(0, 200); this.setData({ inputDesc: value }); }, onChooseImage() { const current = this.data.inputImages.length; const remain = 9 - current; if (remain <= 0) { wx.showToast({ title: "最多上传9张", icon: "none" }); return; } wx.chooseMedia({ count: remain, mediaType: ["image"], sourceType: ["album", "camera"], camera: "back", success: (res) => { const tempPaths = res.tempFiles.map((f) => f.tempFilePath); this.setData({ uploading: true }); this._uploadSequentially(tempPaths, 0); }, }); }, _uploadSequentially(paths: string[], index: number) { if (index >= paths.length) { this.setData({ uploading: false }); return; } upload({ params: { name: "file", file: paths[index] }, transform({ data }: any): string { return (data as any)?.url || (data as any); }, }).then( (url) => { this.setData({ inputImages: [...this.data.inputImages, url], }); this._uploadSequentially(paths, index + 1); }, (error: any) => { wx.showToast({ title: error?.errMsg || "上传失败", icon: "none", }); this.setData({ uploading: false }); } ); }, onRemoveImage(e: WechatMiniprogram.TouchEvent) { const index = Number((e.currentTarget.dataset as { index?: number })?.index ?? -1); if (index < 0) return; const next = [...this.data.inputImages]; next.splice(index, 1); this.setData({ inputImages: next }); }, onConfirm() { if (this.data.uploading) return; if (!this.data.inputDesc.trim() && this.data.inputImages.length === 0) { wx.showToast({ title: "请填写描述或上传凭证", icon: "none" }); return; } this.triggerEvent("confirm", { desc: this.data.inputDesc, images: this.data.inputImages, }); }, }, });