// pages/demand/deliver/index.js import { demandOperate } from '../../../services/demand/fetchDemand'; Page({ /** * 页面的初始数据 */ data: { demandId: 0, remark: '', // 开发完成凭证截图(软件成品展示图等) imageFiles: [], imageMax: 9, imageSizeLimit: { size: 20, unit: 'MB', message: '图片大小不超过 {sizeLimit} MB' }, // 交付附件(源码或软件成品文件) attachFiles: [], attachMax: 5, attachConfig: { count: 5, }, attachSizeLimit: { size: 20, unit: 'MB', message: '附件大小不超过 {sizeLimit} MB' }, gridConfig: { column: 4, width: 160, height: 160, }, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const demandId = options?.demandId; this.setData({ demandId }); }, // 备注变更 onRemarkChange(e) { this.setData({ remark: e.detail.value }); }, // 凭证截图选择成功 handleSuccessImage(e) { const files = e.detail?.files || []; const { imageFiles, imageMax } = this.data; const remain = imageMax - imageFiles.length; const accepted = files.slice(0, remain); if (files.length > remain) { wx.showToast({ title: `最多上传${imageMax}张截图`, icon: 'none' }); } if (!accepted.length) { return; } this.setData({ imageFiles: [...imageFiles, ...accepted] }); this.uploadFiles(accepted, 'image'); }, // 移除凭证截图 handleRemoveImage(e) { const { index } = e.detail; const { imageFiles } = this.data; imageFiles.splice(index, 1); this.setData({ imageFiles }); }, // 交付附件选择成功 handleSuccessAttach(e) { const files = e.detail?.files || []; const { attachFiles, attachMax } = this.data; const remain = attachMax - attachFiles.length; const accepted = files.slice(0, remain); if (files.length > remain) { wx.showToast({ title: `最多上传${attachMax}个附件`, icon: 'none' }); } if (!accepted.length) { return; } const list = [...attachFiles, ...accepted]; this.setData({ attachFiles: list, attachConfig: { count: attachMax - list.length } }); this.uploadFiles(accepted, 'attach'); }, // 移除交付附件 handleRemoveAttach(e) { const { index } = e.currentTarget.dataset; const { attachFiles, attachMax } = this.data; attachFiles.splice(index, 1); this.setData({ attachFiles, attachConfig: { count: attachMax - attachFiles.length } }); }, // 上传文件到服务器 uploadFiles(files, type) { files.forEach((file, index) => { if (file.status === 'done') { return; } wx.uploadFile({ url: wx.$api.UploadFile, filePath: file.url, name: 'file', formData: { 'name': file.name, 'type': file.type }, header: { 'Accept': 'application/json', 'token': wx.getStorageSync('token') }, success: (res) => { if (res.statusCode == 413) { wx.showToast({ title: '文件大小超出限制', icon: 'none' }); this.removeFileByUrl(file.url, type); return; } let response = {}; try { response = JSON.parse(res.data); } catch (err) { response = {}; } if (response.code === 1 && response.data && response.data.length > 0) { this.updateFile(type, { ...file, fullUrl: response.data[0].url, status: 'done' }); if (index === files.length - 1) { wx.showToast({ title: '上传成功', icon: 'success' }); } } else { wx.showToast({ title: response.msg || '上传失败', icon: 'none' }); this.removeFileByUrl(file.url, type); } }, fail: () => { wx.showToast({ title: '上传失败', icon: 'none' }); this.removeFileByUrl(file.url, type); } }); }); }, // 上传完成后回写 updateFile(type, newFile) { if (type === 'image') { const { imageFiles } = this.data; const idx = imageFiles.findIndex(item => item.url === newFile.url); if (idx === -1) { return; } const list = [...imageFiles]; list[idx] = newFile; this.setData({ imageFiles: list }); } else { const { attachFiles, attachMax } = this.data; const idx = attachFiles.findIndex(item => item.url === newFile.url); if (idx === -1) { return; } const list = [...attachFiles]; list[idx] = newFile; this.setData({ attachFiles: list, attachConfig: { count: attachMax - list.length } }); } }, // 上传失败时移除 removeFileByUrl(url, type) { if (type === 'image') { this.setData({ imageFiles: this.data.imageFiles.filter(item => item.url !== url) }); } else { const { attachFiles, attachMax } = this.data; const list = attachFiles.filter(item => item.url !== url); this.setData({ attachFiles: list, attachConfig: { count: attachMax - list.length } }); } }, // 表单验证 validateForm() { const { imageFiles, attachFiles } = this.data; if (!imageFiles.length) { wx.showToast({ title: '请上传开发完成的凭证截图', icon: 'none' }); return false; } if (!attachFiles.length) { wx.showToast({ title: '请上传源码或软件附件', icon: 'none' }); return false; } if (imageFiles.some(item => item.status !== 'done') || attachFiles.some(item => item.status !== 'done')) { wx.showToast({ title: '文件上传中,请稍候', icon: 'none' }); return false; } return true; }, // 提交发货 onSubmit() { if (!this.validateForm()) { return; } wx.showLoading({ title: '提交中...' }); const { demandId, imageFiles, attachFiles, remark } = this.data; // 只提交服务端地址与文件名,避免提交本地临时路径 const params = { demandId, opType: 'deliverGoods', images: imageFiles.map(file => ({ url: file.fullUrl || file.url, name: file.name || '' })), files: attachFiles.map(file => ({ url: file.fullUrl || file.url, name: file.name })), remark }; demandOperate(params).then( (res) => { console.log(res); wx.hideLoading(); if (res.code == 1) { wx.showModal({ title: '提示', showCancel: false, content: '发货成功,等待客户确认收货', success: (res2) => { if (res2.confirm) { //返回上一页(需求详情)并刷新 wx.$util.backAndRefresh(); console.log('用户点击确定'); } } }); } else { wx.showToast({ title: res.msg || '发货失败', icon: 'none' }); } }, () => { //接口异常处理 wx.hideLoading(); wx.showToast({ title: '操作失败', icon: 'none' }); }, ); } })