import { models, db, _ } from '../../utils/cloudbase.js' Page({ data: { item: {}, imgList: [], // 本地或云端可预览的图片 URL fileIDs: [], // 云端 fileID description: '', // 新增描述字段 }, onLoad(options) { const itemData = decodeURIComponent(options.data); const item = JSON.parse(itemData); this.setData({ item }); }, // 上传图片 chooseImage() { if (this.data.imgList.length >= 6) { wx.showToast({ title: '最多上传6张图片', icon: 'none' }); return; } wx.chooseImage({ count: 6 - this.data.imgList.length, // 剩余可上传数 sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: (res) => { res.tempFilePaths.forEach(filePath => { const cloudPath = 'aftersales/' + Date.now() + '-' + Math.floor(Math.random() * 1000) + filePath.match(/\.[^.]+?$/)[0]; wx.cloud.uploadFile({ cloudPath, filePath, success: uploadRes => { // 上传成功,更新列表 this.setData({ imgList: [...this.data.imgList, uploadRes.fileID], fileIDs: [...this.data.fileIDs, uploadRes.fileID] }); }, fail: () => { wx.showToast({ title: '上传失败', icon: 'none' }); } }); }); } }); }, // 删除图片 deleteImage(e) { const index = e.currentTarget.dataset.index; const fileID = this.data.imgList[index]; // 直接取字符串 wx.cloud.deleteFile({ fileList: [fileID] }).then(res => { console.log('删除结果', res); // 前端同步移除 const imgList = [...this.data.imgList]; imgList.splice(index, 1); this.setData({ imgList }); // 如果你还要同步更新 fileIDs,也要处理 const fileIDs = [...this.data.fileIDs]; fileIDs.splice(index, 1); this.setData({ fileIDs }); }).catch(err => { console.error('删除文件失败', err); }); }, // 预览图片 previewImage(e) { const currentUrl = e.currentTarget.dataset.url; wx.previewImage({ current: currentUrl, urls: this.data.imgList }); }, // 问题描述 onDescriptionInput(e) { this.setData({ description: e.detail.value }); }, // 提交表单 async submitForm() { const { description, fileIDs } = this.data; if (!description.trim()) { wx.showToast({ title: '请输入问题描述', icon: 'none' }); return; } if (fileIDs.length === 0) { wx.showToast({ title: '请先上传图片', icon: 'none' }); return; } wx.showLoading({ title: '提交中...' }); try { // 这里替换成你真实的参数 const goods_id = this.data.item.merchandise_id || ""; const school_id = this.data.item.schools_id || ""; const user_id = this.data.item.user_id || ""; const order_id = this.data.item.orders_id || ""; console.log(this.data.item, 'goods_id',); const { data } = await models.wx_afterservice.create({ data: { goods_id, url: fileIDs, des: description, school_id, user_id, state: 0, order_id, }, envType: "prod", }); console.log('创建成功,id:', data.id); wx.hideLoading(); wx.showToast({ title: '提交成功', icon: 'success' }); this.setData({ imgList: [], // 本地或云端可预览的图片 URL fileIDs: [], // 云端 fileID description: '', // 新增描述字段 }) setTimeout(() => { wx.navigateBack(); }, 1000); // 提交成功后可以清空表单或者跳转页面 } catch (error) { wx.hideLoading(); wx.showToast({ title: '提交失败,请重试', icon: 'none' }); console.error('提交接口失败', error); } } });