You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
5.8 KiB
JavaScript

2 years ago
// pages/user-edit/user-edit.js
import {
getTeamAll,
saveUser,
getUserById
} from '../../api/api'
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
roleText: '',
roleValue: [],
roles: [{
label: '班长',
value: 'leader'
},
{
label: '班员',
value: 'user'
},
],
name: '',
phone: '',
password: '',
groupText: '',
groupValue: [],
phoneError: false,
groups: [],
id: '',
role: ''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log(options.id)
this.setData({
role: app.globalData.currentUser.jtRoleCode
})
if (options.id) {
this.data.id = options.id
getUserById({id: options.id}).then(res => {
this.setData({
id: options.id,
name: res.data.jtName,
phone: res.data.jtMobile,
password: res.data.password,
groupText: res.data.jtTeamName,
groupValue: res.data.jtTeamId ? [res.data.jtTeamId] : [],
roleText: res.data.jtRoleName,
roleValue: res.data.jtRoleCode ? [res.data.jtRoleCode] : [],
})
})
} else if (app.globalData.currentUser.jtRoleCode == 'leader') {
this.setData({
groupText: app.globalData.currentUser.jtTeamName,
groupValue: app.globalData.currentUser.jtTeamId ? [app.globalData.currentUser.jtTeamId] : [],
roleText: app.globalData.currentUser.jtRoleName,
roleValue: ['user'],
})
}
getTeamAll().then(res => {
if (res.data && res.data.length > 0) {
const arr = res.data.map(e => {
return {
label: e.jtTeamsName,
value: e.id
}
})
this.setData({
groups: arr
})
}
})
},
remove() {
const param = {
id: this.data.id,
jtTeamId: this.data.groupValue.length > 0 ? this.data.groupValue[0] : '',
roleCode: this.data.roleValue.length > 0 ? this.data.roleValue[0] : '',
state: 0
}
saveUser(param).then(res => {
wx.showToast({
icon: 'none',
title: '删除成功',
}).then(res => {
wx.navigateBack()
})
})
},
onPickerChange(e) {
console.log(e)
const {
key
} = e.currentTarget.dataset;
const {
value,
label
} = e.detail;
this.setData({
[`${key}Visible`]: false,
[`${key}Value`]: value,
[`${key}Text`]: label.join(' '),
});
},
onPickerCancel(e) {
const {
key
} = e.currentTarget.dataset;
this.setData({
[`${key}Visible`]: false,
});
},
onRolePicker() {
this.setData({
roleVisible: true
});
},
onGroupPicker() {
this.setData({
groupVisible: true
});
},
formSubmit(e) {
console.log('form发生了submit事件携带数据为', e.detail.value)
const values = {...e.detail.value}
for (const key in values) {
if (Object.hasOwnProperty.call(values, key)) {
const element = values[key];
if (!element) {
wx.showToast({
icon: 'none',
title: '姓名/账号/密码不能为空',
})
return
}
}
}
if (!this.data.groupText) {
wx.showToast({
icon: 'none',
title: '请选择班组',
})
return
}
if (!this.data.roleText) {
wx.showToast({
icon: 'none',
title: '请选择角色',
})
return
}
const param = {
id: this.data.id,
jtName: values.name,
password: values.password,
jtMobile: values.phone,
jtTeamId: this.data.groupValue.length > 0 ? this.data.groupValue[0] : '',
jtTeamName: this.data.groupText,
roleCode: this.data.roleValue.length > 0 ? this.data.roleValue[0] : ''
}
saveUser(param).then(res => {
wx.showToast({
icon: 'none',
title: '保存成功',
}).then(res => {
wx.navigateBack()
})
})
},
onPhoneInput(e) {
const {
phoneError
} = this.data;
const isPhoneNumber = /^[1][3,4,5,7,8,9][0-9]{9}$/.test(e.detail.value);
if (phoneError === isPhoneNumber) {
this.setData({
phoneError: !isPhoneNumber,
});
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})