Files
code-bang/frontend/miniapp/pages/demand/publish/index.js
T
2026-09-23 17:06:46 +08:00

307 lines
7.4 KiB
JavaScript

// pages/demand/publish/index.js
import { fetchConfig } from '../../../services/home/home';
Page({
/**
* 页面的初始数据
*/
data: {
contact: '',
content: '',
budgetAmount: 0,
limitTime: 0,
tabList: [],
developType: -1,
originFiles: [],
attachFiles: [],
gridConfig: {
column: 4,
width: 160,
height: 160,
},
config: {
count: 5,
},
sileLimt: {
size: 5, unit: 'MB', message: '图片大小不超过 {sizeLimit} MB'
}
},
/**
* 自定义函数
*/
handleSuccess(e) {
const { files } = e.detail;
console.log(files);
this.setData({
originFiles: files,
});
this.uploadFiles(files, 'origin');
},
handleRemove(e) {
const { index } = e.detail;
const { originFiles } = this.data;
originFiles.splice(index, 1);
this.setData({
originFiles,
});
},
handleClick(e) {
console.log(e.detail.file);
},
handleSuccessAttach(e) {
const { files } = e.detail;
console.log(files);
this.setData({
attachFiles: [...this.data.attachFiles, ...files],
});
this.uploadFiles(files, 'attach');
},
handleRemoveAttach(e) {
console.log(e);
const { index } = e.currentTarget.dataset;
const { attachFiles } = this.data;
attachFiles.splice(index, 1);
this.setData({
attachFiles,
});
},
onInputChange(e) {
console.log(e)
const value = e.detail.value;
const { field } = e.currentTarget.dataset; // 获取 data-field 的值
console.log(value)
console.log(field)
this.setData({
[field]: value // 使用动态属性名设置值
});
},
onDevelopTypeTap(e) {
const { key } = e.currentTarget.dataset;
if (key === undefined || key === null || key === '') return;
if (this.data.developType == key) return;
this.setData({
developType: key
});
},
loadConfig() {
fetchConfig().then(
(res) => {
console.log(res);
if (res.code == 1) {
let resData = res.data;
this.setData({
tabList: resData.developTypes
});
}
}
);
},
onSubmit() {
const { developType, attachFiles, originFiles, content, budgetAmount, limitTime, contact } = this.data;
const validations = [
{
condition: developType == -1,
message: '请选择开发类型'
},
{
condition: !content.trim(),
message: '需求描述不能为空'
},
{
condition: !attachFiles.length && !originFiles.length,
message: '图片|附件必传'
},
{
condition: !contact.trim(),
message: '请填写联系方式'
}
];
for (const validation of validations) {
if (validation.condition) {
wx.showToast({
title: validation.message,
icon: 'error',
duration: 2000
});
return;
}
}
let imageFiles = originFiles.map(file => file.fullUrl);
let uploadAttachFiles = attachFiles.map(file => ({ url: file.fullUrl, name: file.name }));
wx.$util.request(wx.$api.DemandSubmit, {
develop_type: developType,
desc: content,
images: imageFiles,
files: uploadAttachFiles,
budget_amount: budgetAmount,
limit_time: limitTime,
contact: contact,
}, 'POST').then(res => {
if (res.code === 1) {
wx.requestSubscribeMessage({
tmplIds: ['dhLlEFPxhLkhrqKcDzvMA3tngRiyzDBy9nwxvqIAASg', '1Yz9jAXFaySOQyeUfM1V2utomImvVKBRx7m-9FvQ3xo'],
complete(re) {
wx.showModal({
title: '提示',
showCancel: false,
content: '发布成功,匹配到工程师会有消息通知',
success(res) {
if (res.confirm) {
wx.navigateBack();
console.log('用户点击确定')
}
}
});
}
});
} else {
wx.showToast({
title: res.msg || '发布失败',
icon: 'error',
duration: 2000
});
}
});
},
uploadFiles(files, type) {
// 逐个上传文件
files.forEach((file, index) => {
if (file.status == 'done') {
console.log(file.fullUrl);
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) => {
console.log(res);
if (res.statusCode == 413) {
wx.showToast({
title: '上传文件过大',
icon: 'error'
});
type === 'attach' && this.setData({
attachFiles: this.data.attachFiles.filter(item => item.url !== file.url)
});
return;
}
const response = JSON.parse(res.data);
console.log("response:", response);
if (response.code === 1 && response.data && response.data.length > 0) {
const uploadedFile = response.data[0];
const newFile = {
...file,
fullUrl: uploadedFile.url,
status: 'done'
};
// 根据type追加到对应的数组
if (type === 'origin') {
const idx = this.data.originFiles.findIndex(item => item.url === newFile.url);
this.setData({
[`originFiles[${idx}]`]: newFile
});
} else if (type === 'attach') {
const idx = this.data.attachFiles.findIndex(item => item.url === newFile.url);
this.setData({
[`attachFiles[${idx}]`]: newFile
});
}
// 如果是最后一个文件,显示上传成功提示
if (index === files.length - 1) {
wx.showToast({
title: '上传成功',
icon: 'success'
});
}
} else {
wx.showToast({
title: response.msg || '上传失败',
icon: 'error'
});
type === 'attach' && this.setData({
attachFiles: this.data.attachFiles.filter(item => item.url !== file.url)
});
}
},
fail: (err) => {
console.log('Upload failed:', err);
wx.showToast({
title: '上传失败2',
icon: 'error'
});
type === 'attach' && this.setData({
attachFiles: this.data.attachFiles.filter(item => item.url !== file.url)
});
}
});
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.loadConfig();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})