jhwxapp/miniprogram/pageage/project_standard/add/index.js

346 lines
8.7 KiB
JavaScript
Raw Normal View History

2024-05-03 10:37:27 +08:00
import jsonConfig from '../../../utils/json'
2023-11-19 22:40:09 +08:00
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
2024-05-03 10:37:27 +08:00
deptId: "",
projectId: "",
projectName: "",
loginName: "",
imageInfoData: [],
loadShow: false,
standardType: "1",
standardTypeList: [],
info: "",
standardDesc: "",
standardNotes:[],
showPopup:false,
showDetailsName:"",
activeId:null,
mainActiveIndex:0,
2023-11-19 22:40:09 +08:00
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
2024-05-03 10:37:27 +08:00
let {
projectId,
projectName
} = options
2023-11-19 22:40:09 +08:00
//获取缓存数据
wx.getStorage({
2024-05-03 10:37:27 +08:00
key: 'userinfo',
success: res => {
this.setData({
projectId,
projectName,
deptId: res.data.deptId,
loginName: res.data.loginName
})
2023-11-19 22:40:09 +08:00
this.getStandardTypeList();
2024-05-03 10:37:27 +08:00
this.initStandardTypeListNotes(this.data.standardType);
}
})
},
/**
* 初始化标准类型
*/
initStandardTypeListNotes(type) {
let index = type - 1;
this.setData({
activeId: null,
mainActiveIndex: 0,
showDetailsName: "",
standardNotes:jsonConfig.standardTypeListNotes[index]
})
},
//关闭申请明细选择
onShowPopup(e) {
this.setData({
showPopup: true
})
},
//关闭申请明细选择
onClosePopup(e) {
this.setData({
showPopup: false
})
},
/**
* 栏目触发事件
*/
onClickNav(e) {
this.setData({
activeId: null,
mainActiveIndex: e.detail.index
})
2023-11-19 22:40:09 +08:00
},
2024-05-03 10:37:27 +08:00
/**
* 选项触发事件
*/
onClickItem(e) {
this.setData({
activeId:e.detail.id,
showDetailsName: e.detail.text
})
this.onClosePopup();
},
getStandardTypeList() {
2023-11-19 22:40:09 +08:00
let that = this
wx.request({
2024-05-03 10:37:27 +08:00
url: app.globalData.reqUrl + '/wechat/projectStandard/queryStandardType',
method: "get",
data: {},
header: {
'content-type': 'application/x-www-form-urlencoded'
2023-11-19 22:40:09 +08:00
},
2024-05-03 10:37:27 +08:00
success(res) {
2023-11-19 22:40:09 +08:00
res = res.data
2024-05-03 10:37:27 +08:00
if (res.code == 200) {
let list = [];
res.data.forEach(it => {
list.push({
"id": it.dictValue,
"text": it.dictLabel,
"info": it.remark
});
2023-11-19 22:40:09 +08:00
})
that.setData({
2024-05-03 10:37:27 +08:00
standardTypeList: list,
standardType: res.data[0].dictValue,
info: res.data[0].remark
2023-11-19 22:40:09 +08:00
})
}
}
})
},
//图片描述
2024-05-03 10:37:27 +08:00
onInputStandardDesc(e) {
2023-11-19 22:40:09 +08:00
let standardDesc = e.detail.value
this.setData({
standardDesc
})
},
// list 上传图片
2024-05-03 10:37:27 +08:00
onImagesArr(e) {
2023-11-19 22:40:09 +08:00
var data = this.data.imageInfoData
data = e.detail
this.setData({
2024-05-03 10:37:27 +08:00
imageInfoData: data
2023-11-19 22:40:09 +08:00
})
},
//取消页面
2024-05-03 10:37:27 +08:00
cancelSaveView() {
2023-11-19 22:40:09 +08:00
this.returnToPage()
},
//保存
2024-05-03 10:37:27 +08:00
onSave() {
2023-11-19 22:40:09 +08:00
this.setData({
2024-05-03 10:37:27 +08:00
loadShow: true
2023-11-19 22:40:09 +08:00
})
let that = this
2024-05-03 10:37:27 +08:00
let {
projectId,
loginName,
deptId,
standardType,
standardDesc,
imageInfoData,
activeId
} = that.data;
2023-11-19 22:40:09 +08:00
//数据效验
2024-05-03 10:37:27 +08:00
if (projectId == "" || loginName == "" || deptId == "") {
2023-11-19 22:40:09 +08:00
app.toast("数据异常,请刷新页面重试!")
that.setData({
2024-05-03 10:37:27 +08:00
loadShow: false
2023-11-19 22:40:09 +08:00
})
return;
}
2024-05-03 10:37:27 +08:00
if (standardType == "") {
app.toast("请选择标准类型!")
2023-11-19 22:40:09 +08:00
that.setData({
2024-05-03 10:37:27 +08:00
loadShow: false
2023-11-19 22:40:09 +08:00
})
return;
}
2024-05-03 10:37:27 +08:00
if(!activeId){
app.toast("请选择类型明细!")
2023-11-19 22:40:09 +08:00
that.setData({
2024-05-03 10:37:27 +08:00
loadShow: false
2023-11-19 22:40:09 +08:00
})
return;
}
2024-05-03 10:37:27 +08:00
if (imageInfoData.length == 0) {
app.toast("请上传标准图片!")
that.setData({
loadShow: false
})
return;
}
if (standardDesc == "") {
2023-11-19 22:40:09 +08:00
app.toast("请填写图片描述!")
that.setData({
2024-05-03 10:37:27 +08:00
loadShow: false
2023-11-19 22:40:09 +08:00
})
return;
}
let fileUrls = [];
2024-05-03 10:37:27 +08:00
imageInfoData.forEach(async (item) => {
let uploadUrl = app.globalData.uploadUrl + '/common/upload'
let name = "file"
2023-11-19 22:40:09 +08:00
//这里复杂的图片上传,改为同步上传,因为小程序只能上传一张图片
2024-05-03 10:37:27 +08:00
let obj = await that.syncUploadImage(uploadUrl, item, name);
2023-11-19 22:40:09 +08:00
fileUrls.push(obj.data.fileName);
//验证图片上传完毕
2024-05-03 10:37:27 +08:00
if (fileUrls.length == imageInfoData.length) {
2023-11-19 22:40:09 +08:00
let params = {
projectId,
deptId,
2024-05-03 10:37:27 +08:00
imageFile: fileUrls.toString(),
standardType:activeId,
2023-11-19 22:40:09 +08:00
standardDesc,
2024-05-03 10:37:27 +08:00
ord: 1,
createBy: loginName
2023-11-19 22:40:09 +08:00
}
wx.request({
url: app.globalData.reqUrl + '/wechat/projectStandard/add',
2024-05-03 10:37:27 +08:00
method: "POST",
data: params,
2023-11-19 22:40:09 +08:00
header: {
"Username": loginName,
"Content-Type": "application/json"
},
2024-05-03 10:37:27 +08:00
success(res) {
2023-11-19 22:40:09 +08:00
that.setData({
2024-05-03 10:37:27 +08:00
loadShow: false
2023-11-19 22:40:09 +08:00
})
res = res.data
2024-05-03 10:37:27 +08:00
if (res.code == 200) {
2023-11-19 22:40:09 +08:00
app.toast("添加成功!")
2024-05-03 10:37:27 +08:00
setTimeout(() => {
wx.redirectTo({
url: '../list/index',
})
}, 200)
2023-11-19 22:40:09 +08:00
}
}
2024-05-03 10:37:27 +08:00
})
}
})
2023-11-19 22:40:09 +08:00
},
/**
* 这里考虑上传图片异步问题封装为同步
*/
2024-05-03 10:37:27 +08:00
syncUploadImage(url, uploadFile, name) {
2023-11-19 22:40:09 +08:00
return new Promise((resolve, reject) => {
wx.uploadFile({
2024-05-03 10:37:27 +08:00
url, // 上传的服务器接口地址
filePath: uploadFile,
header: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
},
name, //上传的所需字段,后端提供
formData: {
user: 'test'
},
success: (res) => {
// 上传完成操作
const data = JSON.parse(res.data)
resolve({
data: data
})
},
fail: (err) => {
//上传失败修改pedding为reject
console.log("访问接口失败", err);
wx.showToast({
title: "网络出错,上传失败",
icon: 'none',
duration: 1000
});
reject(err)
}
2023-11-19 22:40:09 +08:00
});
})
},
2024-05-03 10:37:27 +08:00
//选择项目标准类型
onSelectStandardType(e) {
2023-11-19 22:40:09 +08:00
this.setData({
2024-05-03 10:37:27 +08:00
standardType: e.detail.id,
info: e.detail.info
2023-11-19 22:40:09 +08:00
})
2024-05-03 10:37:27 +08:00
this.initStandardTypeListNotes(e.detail.id);
2023-11-19 22:40:09 +08:00
},
2024-05-03 10:37:27 +08:00
returnToPage: function () {
/*关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面*/
wx.redirectTo({
url: '../list/index',
})
},
2023-11-19 22:40:09 +08:00
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})