// dispatcher/pages/dirver/add/add.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { id: '', genderArray: [ { id: '1', name: '男' }, { id: '2', name: '女' } ], genderIndex: 0, showDeleteDialog: false, driverData: { driver_id: '', driver_name: '', driver_gender: '1', driver_phone: '', account: '', password: '' } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const id = options.id || '' if (id) { this.getDetail(id) this.setData({ ['driverData.driver_id']: id }) wx.setNavigationBarTitle({ title: '编辑司机', }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, // 监听输入框值 bindChangeValue: function (e) { const key = 'driverData.'+e.currentTarget.dataset.key this.setData({ [key]: e.detail.value }) console.log(this.data.driverData) }, // 选择性别 selectGender: function (e) { const genderIndex = e.detail.value let gender = this.data.genderArray[genderIndex] this.setData({ genderIndex, ['driverData.driver_gender']: gender.id }) }, // 获取详情 getDetail: function (id) { const url = '/mobile/detail_driver' app.AppService.appRequest(url, 'POST', { driver_id: id }, (res) => { // 选中性别 let genderIndex = 0 if (res.data.driver_gender == 2) { genderIndex = 1 } this.setData({ driverData: res.data, genderIndex }) }) }, // 修改信息 editDriverData: function () { const driverData = this.data.driverData const url = '/mobile/save_driver' const reg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/ const regCar = /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/ if (!driverData.driver_name) { wx.showToast({ title: '请输入司机姓名', icon: 'none' }) return } else if (!driverData.driver_phone) { wx.showToast({ title: '请输入联系电话', icon: 'none' }) return } else if (!driverData.account) { wx.showToast({ title: '请输入车牌号码', icon: 'none' }) return } else if (!driverData.password) { wx.showToast({ title: '请输入密码', icon: 'none' }) return } else if (!reg.test(driverData.driver_phone)) { wx.showToast({ title: '请输入正确的联系电话', icon: 'none' }) return } else if (!regCar.test(driverData.account)) { wx.showToast({ title: '请输入正确的车牌号码', icon: 'none' }) return } else if (driverData.password.length < 4) { wx.showToast({ title: '密码不能少于4位', icon: 'none' }) return } app.AppService.appRequest(url, 'POST', driverData, (res) => { wx.showToast({ title: '保存成功', }) var timer = setTimeout(() => { wx.navigateBack() clearTimeout(timer) }, 2000) }) }, // 打开删除弹窗 openDeleteDialog: function () { this.setData({ showDeleteDialog: true }) }, // 关闭删除弹窗 closeDeleteDialog: function () { this.setData({ showDeleteDialog: false }) }, // 删除司机 deleteDriver: function () { const url = '/mobile/delete_driver' app.AppService.appRequest(url, 'POST', { driver_id: this.data.driverData.driver_id }, (res) => { wx.showToast({ title: '删除成功', }) wx.navigateBack() }) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, })