const app = getApp() import WxValidate from '../../../utils/wxValidate' Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '课程信息', leftArrow: true, useBar: false }, id: '', school_id: '', form: {}, // 状态列表 statusList: [], // 类型列表 typeList: [], }, initValidate() { const rules = { type: { required: true }, title: { required: true }, brief: { required: true }, limit: { required: true }, start_date: { required: true }, start_time: { required: true }, end_date: { required: true }, end_time: { required: true }, status: { required: true }, refund_hour: { required: true } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { type: { required: '请输入课程类型', }, title: { required: '请输入课程标题', }, brief: { required: '请输入简介', }, limit: { required: '请输入人数上限', }, start_date: { required: '请选择开始日期', }, start_time: { required: '请选择开始时间', }, end_date: { required: '请选择结束日期', }, end_time: { required: '请选择结束时间', }, status: { required: '请选择状态', }, refund_hour: { required: '请输入退款期限', } }; this.WxValidate = new WxValidate(rules, messages) }, // 返回 back: function () { wx.navigateBack({ delta: 1 }) }, // 选择状态 statusChange: function (e) { const that = this; let data = that.data.statusList[e.detail.value]; if (data) { that.setData({ 'form.status': data.value }); that.setData({ 'form.zhstatus': data.label }); } }, // 选择类型 typeChange: function (e) { const that = this; let data = that.data.typeList[e.detail.value]; if (data) { that.setData({ 'form.type': data.value }); that.setData({ 'form.zhtype': data.label }); } }, // 开始上课日期 startdateChange: function (e) { const that = this; that.setData({ 'form.start_date': e.detail.value }) }, // 开始上课时间 starttimeChange: function (e) { const that = this; that.setData({ 'form.start_time': e.detail.value }) }, // 结束上课日期 enddateChange: function (e) { const that = this; that.setData({ 'form.end_date': e.detail.value }) }, // 结束上课时间 endtimeChange: function (e) { const that = this; that.setData({ 'form.end_time': e.detail.value }) }, // 退款期限日期 refunddateChange: function (e) { const that = this; that.setData({ 'form.refund_date': e.detail.value }) }, // 退款期限时间 refundtimeChange: function (e) { const that = this; that.setData({ 'form.refund_time': e.detail.value }) }, // 提交保存 onSubmit: async function (e) { const that = this; const params = e.detail.value; const form = that.data.form; params.school_id = that.data.school_id; params.time_start = form.start_date + ' ' + form.start_time; params.time_end = form.end_date + ' ' + form.end_time; params.refund_hour = form.refund_date + ' ' + form.refund_time; if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { let arr; if (form._id) { arr = await app.$post(`/lesson/${form._id}`, params); } else { arr = await app.$post(`/lesson`, params); } if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); } else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const that = this; that.setData({ id: options.id || '' }) //验证规则函数 that.initValidate(); // 监听用户是否登录 that.watchLogin(); }, // 监听用户是否登录 watchLogin: async function () { const that = this; wx.getStorage({ key: 'user', success: async res => { that.setData({ school_id: res.data.info.id }) const aee = await app.$get(`/dict`, { code: "lesson_status" }); if (aee.errcode == '0' && aee.total > 0) that.setData({ statusList: aee.data[0].list }); const acc = await app.$get(`/dict`, { code: "lesson_type" }); if (acc.errcode == '0' && aee.total > 0) that.setData({ typeList: acc.data[0].list }); if (that.data.id) { const arr = await app.$get(`/lesson/${that.data.id}`); if (arr.errcode == '0') { // 状态 let status = that.data.statusList.find(i => i.value == arr.data.status) if (status) arr.data.zhstatus = status.label; // 类型 let type = that.data.typeList.find(i => i.value == arr.data.type) if (type) arr.data.zhtype = type.label; // 开始时间 arr.data.start_date = arr.data.time_start.slice(0, 10); arr.data.start_time = arr.data.time_start.slice(11, arr.data.time_start.length); // 结束时间 arr.data.end_date = arr.data.time_end.slice(0, 10); arr.data.end_time = arr.data.time_end.slice(11, arr.data.time_end.length); // 退款期限 arr.data.refund_date = arr.data.refund_hour.slice(0, 10); arr.data.refund_time = arr.data.refund_hour.slice(11, arr.data.refund_hour.length); that.setData({ form: arr.data }); } } }, fail: res => { wx.redirectTo({ url: '/pages/index/index', }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })