586 lines
16 KiB
JavaScript
586 lines
16 KiB
JavaScript
import config from '../../../config'
|
||
import {
|
||
getToken,
|
||
getUserInfo
|
||
} from '../../../utils/auth'
|
||
import {
|
||
findProRoles,
|
||
findDictCache,
|
||
findCardOcrFront
|
||
} from '../../../api/publics'
|
||
import {
|
||
addUser,
|
||
getUserRoles
|
||
} from '../../../api/login'
|
||
import {
|
||
findProSubUsersInfoById
|
||
} from '../../../api/project'
|
||
const app = getApp()
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
limit: 1,
|
||
maxDate: new Date(2088, 1, 1).getTime(),
|
||
form: {},
|
||
active: 0,
|
||
flowNodes: [{
|
||
text: '信息登记'
|
||
}, {
|
||
text: '信息审核'
|
||
}, {
|
||
text: '人员入场'
|
||
}],
|
||
deptsList: [],
|
||
postsList: [],
|
||
allPostList:[],
|
||
proRoleList: [],
|
||
loadShow: false,
|
||
imgBase: config.baseImgUrl,
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad(options) {
|
||
if (!getToken()) {
|
||
wx.redirectTo({
|
||
url: '../../../login/login',
|
||
})
|
||
}
|
||
if (options && options.id) {
|
||
//查询数据回填...
|
||
this.initData(options.id);
|
||
}
|
||
const proUserInfo = getUserInfo();
|
||
this.setData({
|
||
"form.comId": proUserInfo.projectUserInfo.comId,
|
||
"form.deptId": app.globalData.useProjectId,
|
||
"form.projectId": app.globalData.useProjectId,
|
||
"form.projectName": app.globalData.useProjectName
|
||
});
|
||
this.getDictCache();
|
||
this.getAllProDeptRoles();
|
||
},
|
||
|
||
/**
|
||
* 获取单位
|
||
*/
|
||
getAllProDeptRoles() {
|
||
findProRoles(app.globalData.useProjectId).then(res => {
|
||
let list = [];
|
||
res.data.forEach(item => {
|
||
list.push({
|
||
"roleId": item.roleId,
|
||
"roleName": item.roleName,
|
||
"roleKey": item.roleKey,
|
||
"state": false
|
||
});
|
||
});
|
||
this.setData({
|
||
proRoleList: list
|
||
});
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 获取字典缓存数据
|
||
*/
|
||
getDictCache() {
|
||
// 初始化岗位
|
||
findDictCache("user_work_type").then(res => {
|
||
if (res.code == 200) {
|
||
let depts = [];
|
||
res.data.forEach(item => {
|
||
if(item.remark){
|
||
const exists = depts.some(ops => ops.id === item.remark);
|
||
if (!exists) {
|
||
depts.push({
|
||
'id': item.remark,
|
||
'text': item.remark,
|
||
'post': []
|
||
});
|
||
}
|
||
}
|
||
});
|
||
depts.forEach(des => {
|
||
res.data.forEach(item => {
|
||
if (des.id == item.remark) {
|
||
des.post.push({
|
||
'id': item.dictValue,
|
||
'text': item.dictLabel
|
||
});
|
||
}
|
||
});
|
||
})
|
||
this.setData({
|
||
deptsList: depts,
|
||
allPostList: res.data
|
||
});
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 初始化数据
|
||
* @param {*} id
|
||
*/
|
||
initData(id) {
|
||
findProSubUsersInfoById(id).then(userRes => {
|
||
if (userRes.code == 200 && userRes.data != null) {
|
||
userRes.data.deptId = userRes.data.projectId;
|
||
userRes.data.nickName = userRes.data.userName;
|
||
userRes.data.phonenumber = userRes.data.userPhone;
|
||
if (userRes.data.userInfos) {
|
||
let userInfosJSON = JSON.parse(userRes.data.userInfos);
|
||
userRes.data.nativePlace = userInfosJSON.nativePlace;
|
||
userRes.data.nation = userInfosJSON.nation;
|
||
userRes.data.address = userInfosJSON.address;
|
||
userRes.data.cardImgPos = userInfosJSON.cardImgPos;
|
||
userRes.data.cardImgInv = userInfosJSON.cardImgInv;
|
||
}
|
||
if (userRes.data.cardImgPos) {
|
||
userRes.data.cardImgPos = (this.data.imgBase + userRes.data.cardImgPos).split(',');
|
||
}
|
||
if (userRes.data.cardImgInv) {
|
||
userRes.data.cardImgInv = (this.data.imgBase + userRes.data.cardImgInv).split(',');
|
||
}
|
||
if (userRes.data.userPicture) {
|
||
userRes.data.avatar = (this.data.imgBase + userRes.data.userPicture).split(',');
|
||
}
|
||
this.setData({
|
||
active: 100,
|
||
form: userRes.data
|
||
});
|
||
this.initUserRole(userRes.data.userId);
|
||
}
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 初始化用户角色
|
||
* @param {*} userId
|
||
*/
|
||
initUserRole(userId){
|
||
getUserRoles(userId).then(res => {
|
||
if(res.code==200){
|
||
let _roleIds = [];
|
||
let _roleNames = "";
|
||
res.data.roles.forEach(item =>{
|
||
_roleIds.push(item.roleId);
|
||
_roleNames += "," + item.roleName;
|
||
});
|
||
this.setData({
|
||
"form.deptId":res.data.deptId,
|
||
"form.roleIds": _roleIds,
|
||
"form.roleNames":_roleNames.substring(1)
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
//选择角色
|
||
onAddRoles(e) {
|
||
if (e.detail.length > 0) {
|
||
let _roleIds = "";
|
||
let _roleNames = "";
|
||
e.detail.forEach(item => {
|
||
_roleIds += "," + item.roleId;
|
||
_roleNames += "," + item.roleName;
|
||
});
|
||
this.setData({
|
||
"form.roleIds": _roleIds.substring(1).split(','),
|
||
"form.roleNames":_roleNames
|
||
})
|
||
console.log("dddddddd",this.data.form);
|
||
} else {
|
||
this.setData({
|
||
"form.roleIds": []
|
||
})
|
||
}
|
||
},
|
||
|
||
//取消页面
|
||
cancelSaveView() {
|
||
this.returnToPage()
|
||
},
|
||
|
||
/**
|
||
* 委托代理提交参建单位信息
|
||
*/
|
||
submitSubUsers() {
|
||
let {
|
||
form
|
||
} = this.data;
|
||
//数据效验
|
||
if (!form.comId || !form.deptId || !form.projectId) {
|
||
app.toast("数据异常,请刷新页面重试!")
|
||
return false;
|
||
}
|
||
if (!form.cardImgPos || form.cardImgPos.length == 0) {
|
||
app.toast("请上传身份证正面照!");
|
||
return false;
|
||
}
|
||
if (!form.cardImgInv || form.cardImgInv.length == 0) {
|
||
app.toast("请上传身份证反面照!");
|
||
return false;
|
||
}
|
||
if (!form.avatar || form.avatar.length == 0) {
|
||
app.toast("请上传进场半身近照!");
|
||
return false;
|
||
}
|
||
if (!form.nickName) {
|
||
app.toast("请填写人员姓名!");
|
||
return false;
|
||
}
|
||
if (!form.cardCode) {
|
||
app.toast("请填写身份证号!");
|
||
return false;
|
||
} else {
|
||
const cardCodePattern = /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/;
|
||
if (!cardCodePattern.test(form.cardCode)) {
|
||
app.toast("身份证号码不正确!");
|
||
return false;
|
||
}
|
||
}
|
||
if (!form.phonenumber) {
|
||
app.toast("请填写联系电话!");
|
||
return false;
|
||
} else {
|
||
const phonePattern = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
|
||
if (!phonePattern.test(form.phonenumber)) {
|
||
app.toast("人员联系电话不正确!");
|
||
return false;
|
||
}
|
||
}
|
||
if(!form.id){
|
||
if (!form.password) {
|
||
app.toast("请输入登录密码!");
|
||
return false;
|
||
}else if(form.password.length<6){
|
||
app.toast("登录密码最少6位字符!");
|
||
return false;
|
||
}
|
||
if (!form.depts) {
|
||
app.toast("请选择岗位部门!");
|
||
return false;
|
||
}
|
||
}
|
||
if (!form.workType) {
|
||
app.toast("请选择岗位级别!");
|
||
return false;
|
||
}
|
||
if (!form.roleIds || form.roleIds.length==0) {
|
||
app.toast("请选择用户角色!");
|
||
return false;
|
||
}
|
||
let that = this;
|
||
//弹出确认
|
||
wx.showModal({
|
||
title: '提示',
|
||
content: '是否确认保存总包人员信息?',
|
||
success: function (sm) {
|
||
if (sm.confirm) {
|
||
that.submitSubUserForm();
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 委托代理提交参建单位表单
|
||
*/
|
||
submitSubUserForm() {
|
||
let _form = {
|
||
...this.data.form
|
||
};
|
||
this.setData({
|
||
loadShow: true
|
||
})
|
||
let uploadFiles = [];
|
||
if (_form.cardImgPos && _form.cardImgPos.length > 0) {
|
||
uploadFiles.push({
|
||
type: 'cardImgPos',
|
||
path: _form.cardImgPos[0]
|
||
});
|
||
}
|
||
if (_form.cardImgInv && _form.cardImgInv.length > 0) {
|
||
uploadFiles.push({
|
||
type: 'cardImgInv',
|
||
path: _form.cardImgInv[0]
|
||
});
|
||
}
|
||
if (_form.avatar && _form.avatar.length > 0) {
|
||
uploadFiles.push({
|
||
type: 'userPicture',
|
||
path: _form.avatar[0]
|
||
});
|
||
}
|
||
let that = this;
|
||
let uploads = [];
|
||
uploadFiles.forEach(async (item, idx) => {
|
||
let obj;
|
||
if (item.path.indexOf(this.data.imgBase) > -1) {
|
||
obj = {
|
||
data: {
|
||
data: {
|
||
url: item.path.replace(this.data.imgBase, "")
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
//这里复杂的图片上传,改为同步上传,因为小程序只能上传一张图片
|
||
obj = await that.syncUploadImage(item.path);
|
||
}
|
||
if (item.type == "cardImgPos") {
|
||
_form.cardImgPos = obj.data.data.url;
|
||
}
|
||
if (item.type == "cardImgInv") {
|
||
_form.cardImgInv = obj.data.data.url;
|
||
}
|
||
if (item.type == "userPicture") {
|
||
_form.avatar = obj.data.data.url;
|
||
}
|
||
uploads.push(obj.data.data.url);
|
||
//验证图片上传完毕
|
||
if (uploads.length == uploadFiles.length) {
|
||
let userInfos = {};
|
||
userInfos.nation = _form.nation;
|
||
userInfos.nativePlace = _form.nativePlace;
|
||
userInfos.address = _form.address;
|
||
userInfos.cardImgPos = _form.cardImgPos;
|
||
userInfos.cardImgInv = _form.cardImgInv;
|
||
_form.userInfos = JSON.stringify(userInfos);
|
||
addUser(_form).then(res => {
|
||
this.setData({
|
||
loadShow: false
|
||
});
|
||
if (res.code == 200) {
|
||
app.toast("保存数据成功!")
|
||
setTimeout(() => {
|
||
wx.redirectTo({
|
||
url: `../list/index`,
|
||
})
|
||
}, 200)
|
||
}
|
||
});
|
||
}
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 个人身份证正面照
|
||
* @param {*} options
|
||
*/
|
||
fileUploadCardImgPos(options) {
|
||
let file = options.detail;
|
||
this.setData({
|
||
"form.cardImgPos": file
|
||
});
|
||
file.forEach(async (item, idx) => {
|
||
let obj = await this.syncUploadImage(item);
|
||
findCardOcrFront(obj.data.data.url).then(res => {
|
||
if (res.code == 200) {
|
||
if (res.data.nation.indexOf("族") < 0) {
|
||
res.data.nation = res.data.nation + "族";
|
||
}
|
||
this.setData({
|
||
"form.nickName": res.data.name,
|
||
"form.cardCode": res.data.cardId,
|
||
"form.nation": res.data.nation,
|
||
"form.nativePlace": res.data.native,
|
||
"form.address": res.data.address
|
||
})
|
||
if (!res.data.name || !res.data.cardId) {
|
||
this.setData({
|
||
"form.cardImgPos": []
|
||
});
|
||
app.toast("身份证正面照识别失败!请重新上传");
|
||
}
|
||
}
|
||
});
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 个人身份证反面照
|
||
* @param {*} options
|
||
*/
|
||
fileUploadCardImgInv(options) {
|
||
let file = options.detail;
|
||
this.setData({
|
||
"form.cardImgInv": file
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 个人半身近照
|
||
* @param {*} options
|
||
*/
|
||
fileUploadUserPicture(options) {
|
||
let file = options.detail;
|
||
this.setData({
|
||
"form.avatar": file
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 输入个人姓名
|
||
* @param {*} e
|
||
*/
|
||
inputUserName(e) {
|
||
this.setData({
|
||
"form.nickName": e.detail.value
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 输入个人身份证号
|
||
* @param {*} e
|
||
*/
|
||
inputUserCode(e) {
|
||
this.setData({
|
||
"form.cardCode": e.detail.value
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 输入联系电话
|
||
* @param {*} e
|
||
*/
|
||
inputUserPhone(e) {
|
||
this.setData({
|
||
"form.phonenumber": e.detail.value
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 登陆密码
|
||
* @param {*} e
|
||
*/
|
||
inputPassword(e) {
|
||
this.setData({
|
||
"form.password": e.detail.value
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 选择部门
|
||
* @param {*} e
|
||
*/
|
||
onDepts(e) {
|
||
let _list = this.data.deptsList;
|
||
_list.forEach(item => {
|
||
if (item.id == e.detail.id) {
|
||
this.setData({
|
||
"form.depts": e.detail.id,
|
||
"form.workType": null,
|
||
postsList: item.post
|
||
})
|
||
}
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 选择岗位
|
||
* @param {*} e
|
||
*/
|
||
onPosts(e) {
|
||
this.setData({
|
||
"form.workType": e.detail.id
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 这里考虑上传图片异步问题,封装为同步
|
||
*/
|
||
syncUploadImage(file) {
|
||
let _baseUrl = config.baseUrl;
|
||
return new Promise((resolve, reject) => {
|
||
wx.uploadFile({
|
||
url: _baseUrl + "/file/upload", // 上传的服务器接口地址
|
||
filePath: file,
|
||
header: {
|
||
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
||
'Authorization': 'Bearer ' + getToken()
|
||
},
|
||
name: "file", //上传的所需字段,后端提供
|
||
formData: {},
|
||
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)
|
||
}
|
||
});
|
||
})
|
||
},
|
||
|
||
returnToPage: function () {
|
||
wx.redirectTo({
|
||
url: `../list/index`
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面初次渲染完成
|
||
*/
|
||
onReady() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面隐藏
|
||
*/
|
||
onHide() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面卸载
|
||
*/
|
||
onUnload() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面相关事件处理函数--监听用户下拉动作
|
||
*/
|
||
onPullDownRefresh() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面上拉触底事件的处理函数
|
||
*/
|
||
onReachBottom() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 用户点击右上角分享
|
||
*/
|
||
onShareAppMessage() {
|
||
|
||
}
|
||
}) |