Files
code-bang/frontend/miniapp/pages/demand/deliver/index.js
T

315 lines
8.4 KiB
JavaScript
Raw Normal View History

2026-09-30 14:56:56 +08:00
// pages/demand/deliver/index.js
import { demandOperate } from '../../../services/demand/fetchDemand';
Page({
/**
* 页面的初始数据
*/
data: {
demandId: 0,
remark: '',
2026-10-08 20:34:08 +08:00
// 交付链接(Git仓库/网盘等持久获取方式),选填
deliverLinks: '',
2026-09-30 14:56:56 +08:00
// 开发完成凭证截图(软件成品展示图等)
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 });
},
2026-10-08 20:34:08 +08:00
// 交付链接变更
onDeliverLinksChange(e) {
this.setData({ deliverLinks: e.detail.value });
},
// 交付链接(单个):只取第一行,避免粘贴时带入多余内容
getDeliverLinks() {
const link = String(this.data.deliverLinks || '')
.split(/[\r\n]+/)[0]
.trim();
return link ? [link] : [];
},
2026-09-30 14:56:56 +08:00
// 凭证截图选择成功
2026-09-30 15:59:39 +08:00
// 注意:t-upload 绑定了 files,success 事件返回的是完整文件列表(含已选的旧文件),
// 必须整体覆盖,不能往 imageFiles 里追加,否则已选图片会重复
2026-09-30 14:56:56 +08:00
handleSuccessImage(e) {
const files = e.detail?.files || [];
2026-09-30 15:59:39 +08:00
const { imageMax } = this.data;
if (files.length > imageMax) {
2026-09-30 14:56:56 +08:00
wx.showToast({
title: `最多上传${imageMax}张截图`,
icon: 'none'
});
}
2026-09-30 15:59:39 +08:00
const list = files.slice(0, imageMax);
this.setData({ imageFiles: list });
// 只上传未完成的(已上传过的 status 为 done)
this.uploadFiles(list.filter(file => file.status !== 'done'), 'image');
2026-09-30 14:56:56 +08:00
},
// 移除凭证截图
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 }
});
}
},
2026-10-08 20:34:08 +08:00
// 表单验证:源码一般在沟通群交付,平台仅凭证截图必填,附件与链接选填
2026-09-30 14:56:56 +08:00
validateForm() {
const { imageFiles, attachFiles } = this.data;
if (!imageFiles.length) {
wx.showToast({
title: '请上传开发完成的凭证截图',
icon: 'none'
});
return false;
}
2026-10-08 20:34:08 +08:00
const links = this.getDeliverLinks();
if (links.length > 1) {
wx.showToast({
title: '只能填写1个交付链接',
icon: 'none'
});
return false;
}
if (links.some(item => !/^https?:\/\//i.test(item))) {
2026-09-30 14:56:56 +08:00
wx.showToast({
2026-10-08 20:34:08 +08:00
title: '交付链接需以 http:// 或 https:// 开头',
2026-09-30 14:56:56 +08:00
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;
2026-09-30 16:57:26 +08:00
// 只提交服务端地址,避免提交本地临时路径(后端按英文逗号分隔存储)
2026-09-30 14:56:56 +08:00
const params = {
demandId,
opType: 'deliverGoods',
2026-09-30 16:57:26 +08:00
images: imageFiles.map(file => file.fullUrl || file.url),
files: attachFiles.map(file => file.fullUrl || file.url),
2026-10-08 20:34:08 +08:00
links: this.getDeliverLinks(),
2026-09-30 14:56:56 +08:00
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'
});
},
);
}
})