const app = getApp() import WxValidate from '../../../utils/wxValidate'; Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '学员信息维护', leftArrow: true, useBar: false }, id: '', form: {}, user: {}, // 教练学校 coachSchool: {}, // 学校学员 studentList: [], // 折扣设置 discount_typeList: [] }, initValidate() { const rules = { student_id: { required: true } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { student_id: { required: '请选择学员' } }; this.WxValidate = new WxValidate(rules, messages) }, // 返回 back: function () { wx.navigateBack({ delta: 1 }) }, // 选择学生 stuChange: function (e) { const that = this; let data = that.data.studentList[e.detail.value]; if (data) { console.log(data); that.setData({ 'form.student_id': data.student_id }) that.setData({ 'form.student_id_name': data.student_id_name }) } }, // 选择折扣类型 typeChange: function (e) { const that = this; let data = that.data.discount_typeList[e.detail.value]; if (data) { that.setData({ 'form.config.discount_type': data.value }); that.setData({ 'form.config.number': null }) } }, // 折扣判断 moneyInput: function (e) { const that = this; const form = that.data.form; if (form.config && form.config.discount_type && form.config.discount_type == 'discount') { var regex = /^(10|\d)(\.\d{1,1})?$/; if (!regex.test(e.detail.value)) { wx.showToast({ title: `数据不允许`, icon: 'error', duration: 1000 }); that.setData({ 'form.config.number': null }) } } }, onSubmit: async function (e) { const that = this; const params = e.detail.value; if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { params.config = { discount_type: params['config.discount_type'], number: params['config.number'] } let arr; if (that.data.id) arr = await app.$post(`/rsc/${that.data.id}`, params); else arr = await app.$post(`/rsc`, params); if (arr.errcode == '0') { wx.showToast({ title: `信息维护成功`, icon: 'success', duration: 2000 }); that.back() } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) } } }, /** * 生命周期函数--监听页面加载 */ onLoad: async function (options) { const that = this; that.setData({ id: options.id || '' }); //验证规则函数 that.initValidate(); // 查询其他信息 await that.searchOther(); // 监听用户是否登录 await that.watchLogin(); }, searchOther: async function () { const that = this; let arr; // 折扣设置 arr = await app.$get(`/dict`, { code: 'rsc_type' }); if (arr.errcode == '0' && arr.total > 0) { that.setData({ discount_typeList: arr.data[0].list }) } }, // 监听用户是否登录 watchLogin: async function () { const that = this; wx.getStorage({ key: 'user', success: async res => { that.setData({ user: res.data }); // 查询教练学校 let arr; arr = await app.$get(`/rcs`, { coach_id: res.data.info.id }); if (arr.errcode == '0' && arr.total > 0) that.setData({ coachSchool: arr.data[0] }); // 查询学校学员 arr = await app.$get(`/rss`, { school_id: that.data.coachSchool.school_id }); if (arr.errcode == '0') that.setData({ studentList: arr.data }); let form = { school_id: that.data.coachSchool.school_id, coach_id: that.data.user.info.id } if (that.data.id) { arr = await app.$get(`/rsc/${that.data.id}`); if (arr.errcode == '0') { let student = that.data.studentList.find(i => i.student_id == arr.data.student_id); if (student) arr.data.student_id_name = student.student_id_name; form = arr.data; } } that.setData({ form }) }, fail: async res => { wx.redirectTo({ url: '/pages/index/index' }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })