YZProjectCloud/yanzhu-ui-app/miniprogram/pageage/project_checked/add/index.js

378 lines
9.2 KiB
JavaScript
Raw Normal View History

2025-07-28 09:52:09 +08:00
// pageage/project_checked/add/index.js
2025-08-03 18:18:48 +08:00
import config from "../../../config";
import fmt from "../../../utils/date.js";
import { getToken, getUserInfo } from "../../../utils/auth";
2025-08-04 17:50:25 +08:00
import { uploadFiles } from "../../../utils/upload.js";
import { tryToJson } from '../../../utils/tools'
2025-08-02 15:44:20 +08:00
import {
2025-08-04 17:50:25 +08:00
getProjectChecked,
2025-08-02 15:44:20 +08:00
findPlanDatas,
2025-08-04 17:50:25 +08:00
listProProjectInfoSubdeptsUsers,
updateProjectChecked,
addProjectChecked,
2025-08-03 18:18:48 +08:00
} from "../../../api/project";
2025-08-02 15:44:20 +08:00
2025-08-03 18:18:48 +08:00
const app = getApp();
2025-07-28 09:52:09 +08:00
Page({
2025-08-02 15:44:20 +08:00
/**
* 页面的初始数据
*/
data: {
maxDate: new Date(2088, 1, 1).getTime(),
2025-08-04 17:50:25 +08:00
minDate: new Date().getTime(),
imageInfoData: [],
2025-08-02 15:44:20 +08:00
form: {
2025-08-04 17:50:25 +08:00
dataId: null,
2025-08-03 18:18:48 +08:00
taskName: "",
2025-08-02 15:44:20 +08:00
task: null,
2025-08-03 18:18:48 +08:00
intro: "", //验收描述
2025-08-02 15:44:20 +08:00
imageUrls: [],
2025-08-03 18:18:48 +08:00
groupDeptUser: "", //班组长
checkingDate: "", //验收时间
2025-08-04 17:50:25 +08:00
technician: '',
2025-08-03 18:18:48 +08:00
technicianUser: "", //技术专员
2025-08-04 17:50:25 +08:00
technicianUserName: "",
supervise: '',
2025-08-03 18:18:48 +08:00
superviseUser: "", //监理专员
2025-08-04 17:50:25 +08:00
superviseUserName: ""
2025-07-28 09:52:09 +08:00
},
2025-08-02 15:44:20 +08:00
projectUserInfo: {},
projectId: "",
projectName: "",
initData: {},
pageNum: 1,
pageSize: 10,
total: 0,
listData: [],
//任务计划
picker: false,
planOptions: [],
2025-08-04 17:50:25 +08:00
groupUserOptions: [],//班组长
technicianUserOptions: [],//技术专员
superviseUserOptions: [],//监理员
2025-08-02 15:44:20 +08:00
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
if (!getToken()) {
wx.redirectTo({
2025-08-03 18:18:48 +08:00
url: "../../../pages/login/login",
});
2025-08-02 15:44:20 +08:00
}
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: {
2025-08-04 17:50:25 +08:00
...this.data.form,
2025-08-03 18:18:48 +08:00
taskName: "",
2025-08-02 15:44:20 +08:00
task: null,
2025-08-03 18:18:48 +08:00
intro: "",
checkingDate: fmt(new Date()).format("YYYY-MM-DD"),
},
2025-08-02 15:44:20 +08:00
});
this.initPlanDatas();
2025-08-04 17:50:25 +08:00
// 获取编辑模式id参数
if (options.id) {
this.setData({
'form.dataId': options.id
});
this.initUserOptions(() => {
// 这里可以添加加载编辑数据的逻辑
this.loadEditData(options.id);
});//获取人员列表
} else {
this.initUserOptions();//获取人员列表
}
2025-08-02 15:44:20 +08:00
},
2025-08-04 17:50:25 +08:00
2025-08-02 15:44:20 +08:00
//项目切换 返回值
onProjectSelect(e) {
let projectId = e.detail.id;
let projectName = e.detail.text;
app.globalData.useProjectId = projectId;
app.globalData.useProjectName = projectName;
this.onLoad();
},
2025-08-04 17:50:25 +08:00
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
2025-08-02 15:44:20 +08:00
2025-08-04 17:50:25 +08:00
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();
});
},
2025-08-02 15:44:20 +08:00
/**
* 初始化计划
*/
initPlanDatas() {
findPlanDatas(app.globalData.useProjectId).then((res) => {
let treeDatas = this.buildTree(res.data, 1, "");
this.setData({
2025-08-03 18:18:48 +08:00
planOptions: treeDatas,
});
2025-08-02 15:44:20 +08:00
});
},
closePicker() {
this.setData({
2025-08-03 18:18:48 +08:00
picker: false,
});
2025-08-02 15:44:20 +08:00
},
/**
2025-08-03 18:18:48 +08:00
* 打开选择窗
*/
2025-08-02 15:44:20 +08:00
openPicker() {
this.setData({
2025-08-03 18:18:48 +08:00
picker: true,
});
2025-08-02 15:44:20 +08:00
},
/**
2025-08-03 18:18:48 +08:00
* 构建树结构
* @param {*} all
* @param {*} id
*/
2025-08-02 15:44:20 +08:00
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);
});
2025-07-28 09:52:09 +08:00
}
2025-08-02 15:44:20 +08:00
return tmps;
},
handleClick: function (e) {
let item = e.detail.item;
if (item) {
this.setData({
form: {
2025-08-04 17:50:25 +08:00
...this.data.form,
2025-08-02 15:44:20 +08:00
task: item,
2025-08-03 18:18:48 +08:00
taskName: item.fullPath,
2025-08-02 15:44:20 +08:00
},
2025-08-03 18:18:48 +08:00
picker: false,
});
2025-08-02 15:44:20 +08:00
}
},
2025-08-04 17:50:25 +08:00
//选择监理员
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
})
},
2025-08-02 15:44:20 +08:00
2025-08-04 17:50:25 +08:00
//验收时间
onInputTime(e) {
this.setData({
"form.checkingDate": e.detail
})
},
2025-08-02 15:44:20 +08:00
/**
2025-08-04 17:50:25 +08:00
* 加载编辑数据
* @param {string} id - 验收记录ID
2025-08-02 15:44:20 +08:00
*/
2025-08-04 17:50:25 +08:00
loadEditData(id) {
2025-08-02 15:44:20 +08:00
2025-08-04 17:50:25 +08:00
// 这里模拟加载编辑数据实际应该调用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;
2025-08-02 15:44:20 +08:00
2025-08-04 17:50:25 +08:00
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;
}
2025-08-02 15:44:20 +08:00
2025-08-04 17:50:25 +08:00
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(",");
2025-08-02 15:44:20 +08:00
2025-08-04 17:50:25 +08:00
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("保存失败,请重试");
}
2025-08-02 15:44:20 +08:00
},
2025-08-04 17:50:25 +08:00
doBack(isRefresh) {
/*返回列表页面并刷新*/
if (isRefresh) {
wx.navigateBack({
delta: 1
2025-08-02 15:44:20 +08:00
});
} else {
2025-08-04 17:50:25 +08:00
wx.redirectTo({
url: "../list/index",
})
2025-08-02 15:44:20 +08:00
}
},
2025-08-04 17:50:25 +08:00
returnToPage: function () {
this.doBack(false);
},
2025-08-03 18:18:48 +08:00
});