// pageage/project_checked/add/index.js import config from "../../../config"; import fmt from "../../../utils/date.js"; import { getToken, getUserInfo } from "../../../utils/auth"; import { uploadFiles } from "../../../utils/upload.js"; import { tryToJson } from '../../../utils/tools' import { getProjectChecked, findPlanDatas, listProProjectInfoSubdeptsUsers, updateProjectChecked, addProjectChecked, } from "../../../api/project"; const app = getApp(); Page({ /** * 页面的初始数据 */ data: { maxDate: new Date(2088, 1, 1).getTime(), minDate: new Date().getTime(), imageInfoData: [], form: { dataId: null, taskName: "", task: null, intro: "", //验收描述 imageUrls: [], groupDeptUser: "", //班组长 checkingDate: "", //验收时间 technician: '', technicianUser: "", //技术专员 technicianUserName: "", supervise: '', superviseUser: "", //监理专员 superviseUserName: "" }, projectUserInfo: {}, projectId: "", projectName: "", initData: {}, pageNum: 1, pageSize: 10, total: 0, listData: [], //任务计划 picker: false, planOptions: [], groupUserOptions: [],//班组长 technicianUserOptions: [],//技术专员 superviseUserOptions: [],//监理员 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { if (!getToken()) { wx.redirectTo({ url: "../../../pages/login/login", }); } const proUserInfo = getUserInfo(); this.setData({ projectUserInfo: proUserInfo.projectUserInfo, projectId: app.globalData.useProjectId, projectName: app.globalData.useProjectName, initData: { id: app.globalData.useProjectId, text: app.globalData.useProjectName, }, pageNum: 1, pageSize: 10, listData: [], total: 0, form: { ...this.data.form, taskName: "", task: null, intro: "", checkingDate: fmt(new Date()).format("YYYY-MM-DD"), }, }); this.initPlanDatas(); // 获取编辑模式id参数 if (options.id) { this.setData({ 'form.dataId': options.id }); this.initUserOptions(() => { // 这里可以添加加载编辑数据的逻辑 this.loadEditData(options.id); });//获取人员列表 } else { this.initUserOptions();//获取人员列表 } }, //项目切换 返回值 onProjectSelect(e) { let projectId = e.detail.id; let projectName = e.detail.text; app.globalData.useProjectId = projectId; app.globalData.useProjectName = projectName; this.onLoad(); }, checkSysRole(user, role) { let tmp = user.sysRoles; if (!tmp) { return false; } let tmps = tmp.split(","); return tmps.find(it => it.indexOf(role) == 0); }, /** * 获取用户列表 */ initUserOptions(cb) { listProProjectInfoSubdeptsUsers({ projectId: app.globalData.useProjectId, pageNum: 1, pageSize: 1000 }).then((res) => { let users = (res.rows || []).map(it => { it.text = `${it.userName}(${it.userPhone})` it.id = it.userId return it; }); let groupUserOptions = []; let technicianUserOptions = []; let superviseUserOptions = []; users.forEach(item => { if (item.userPost == '3') { groupUserOptions.push(item); } else if (this.checkSysRole(item, 'zbjsy_')) { technicianUserOptions.push(item); } else if (item.userPost == '71') { superviseUserOptions.push(item); } }); this.setData({ groupUserOptions, technicianUserOptions, superviseUserOptions, }); cb && cb(); }); }, /** * 初始化计划 */ initPlanDatas() { findPlanDatas(app.globalData.useProjectId).then((res) => { let treeDatas = this.buildTree(res.data, 1, ""); this.setData({ planOptions: treeDatas, }); }); }, closePicker() { this.setData({ picker: false, }); }, /** * 打开选择窗 */ openPicker() { this.setData({ picker: true, }); }, /** * 构建树结构 * @param {*} all * @param {*} id */ buildTree(all, id, path) { let tmps = all.filter((d) => d.parentId == id); if (tmps.length > 0) { tmps.forEach((it) => { it.fullPath = path ? path + "/" + it.taskName : it.taskName; it.children = this.buildTree(all, it.taskId, it.fullPath); }); } return tmps; }, handleClick: function (e) { let item = e.detail.item; if (item) { this.setData({ form: { ...this.data.form, task: item, taskName: item.fullPath, }, picker: false, }); } }, //选择监理员 onSuperviseUserSelect(e) { let item = e.detail; if (item) { this.setData({ form: { ...this.data.form, supervise: item.userId, superviseUser: item.userPhone, superviseUserName: item.userName, } }) } }, //选择技术专员 onTechnicianUserSelect(e) { let item = e.detail; if (item) { this.setData({ form: { ...this.data.form, technician: item.userId, technicianUser: item.userPhone, technicianUserName: item.userName, } }) } }, onIntroInput(e) { this.setData({ form: { ...this.data.form, intro: e.detail.value, } }) }, // 上传图片 onImagesArr(e) { this.setData({ imageInfoData: e.detail }) }, //验收时间 onInputTime(e) { this.setData({ "form.checkingDate": e.detail }) }, /** * 加载编辑数据 * @param {string} id - 验收记录ID */ loadEditData(id) { // 这里模拟加载编辑数据,实际应该调用API获取详情 // 例如: const res = await getProjectCheckedDetail(id) wx.showLoading({ title: '加载中...', }) getProjectChecked(id).then(res => { let data = res.data || {}; let form = this.data.form; let task = tryToJson(data.workingPosition, {}); form.task = task; form.taskName = task.full; form.taskId = task.id; let imgs = data.imageUrls ? data.imageUrls.split(",") : []; let user = this.data.technicianUserOptions.find(it => it.userPhone == data.technicianUser); form.technician = user?.userId || ''; user = this.data.superviseUserOptions.find(it => it.userPhone == data.superviseUser); form.supervise = user?.userId || ''; form.intro = data.intro; form.checkingDate = data.checkingDate; form.dataId = data.id; form.groupDeptUser = data.groupDeptUser; form.groupDeptUserName = data.groupDeptUserName; form.technicianUser = data.technicianUser; form.technicianUserName = data.technicianUserName; form.superviseUser = data.superviseUser; form.superviseUserName = data.superviseUserName; this.setData({ imageInfoData: imgs, form: { ...form } }) wx.hideLoading() }) }, //提交保存 async submitSave() { let form = this.data.form; if (!form.taskName) { app.toast("请选择任务计划!"); return false; } if (!form.technician) { app.toast("请选择技术专员!"); return false; } if (!form.supervise) { app.toast("请选择监理员!"); return false; } if (!form.intro) { app.toast("请填写结果描述!"); return false; } if (!form.checkingDate) { app.toast("请选择完成时间!"); return false; } if (this.data.imageInfoData.length == 0) { app.toast("请上传图片!"); return; } let postData = { workingPosition: form.task.taskName, intro: form.intro, checkingDate: form.checkingDate, imageUrls: "", groupDeptUser: this.data.projectUserInfo.userPhone, groupDeptUserName: this.data.projectUserInfo.userName, technicianUser: form.technicianUser, technicianUserName: form.technicianUserName, superviseUser: form.superviseUser, superviseUserName: form.superviseUserName, comId: this.data.projectUserInfo.comId, projectId: app.globalData.useProjectId, approveStatus: 1, } let taskInfo = { id: form.task.id, name: form.task.taskName, full: form.task.fullPath } postData.workingPosition = JSON.stringify(taskInfo); let fileUrls = await uploadFiles(this.data.imageInfoData); postData.imageUrls = fileUrls.join(","); try { if (form.dataId) { postData.id = form.dataId; await updateProjectChecked(postData); } else { await addProjectChecked(postData); } app.toast("保存成功!"); this.doBack(true); } catch (error) { console.error('保存失败:', error); app.toast("保存失败,请重试"); } }, doBack(isRefresh) { /*返回列表页面并刷新*/ if (isRefresh) { wx.navigateBack({ delta: 1 }); } else { wx.redirectTo({ url: "../list/index", }) } }, returnToPage: function () { this.doBack(false); }, });