const app = getApp() import WxValidate from '../../../utils/wxValidate' Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false }, id: '', form: { yyzz: [], img_url: [] }, }, initValidate() { const rules = { name: { required: true }, brief: { required: true }, phone: { required: true }, address: { required: true }, coach_num: { required: true }, student_num: { required: true }, honor: { required: true }, url: { required: true }, yyzz: { required: true }, img_url: { required: true } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { name: { required: '请输入机构名称', }, brief: { required: '请输入机构简介', }, phone: { required: '请输入联系方式', }, address: { required: '请输入训练地址', }, coach_num: { required: '请输入教练人数', }, student_num: { required: '请输入学员人数', }, honor: { required: '请输入过往荣誉', }, url: { required: '请输入网址', }, yyzz: { required: '请上传营业执照', }, img_url: { required: '请输入机构图片', } }; this.WxValidate = new WxValidate(rules, messages) }, // 返回 back: function () { wx.navigateBack({ delta: 1 }) }, // 营业执照图片上传 yzimgUpl: function (e) { const that = this; let data = that.data.form.yyzz; data.push(e.detail) that.setData({ 'form.yyzz': data }) }, // 营业执照图片删除 yzimgDel: function (e) { const that = this; let list = that.data.form.yyzz; let arr = list.filter((i, index) => index != e.detail.index) that.setData({ 'form.yyzz': arr }) }, // 机构图片上传 imgUpl: function (e) { const that = this; let data = that.data.form.img_url; data.push(e.detail) that.setData({ 'form.img_url': data }) }, // 机构图片删除 imgDel: function (e) { const that = this; let list = that.data.form.img_url; let arr = list.filter((i, index) => index != e.detail.index) that.setData({ 'form.img_url': arr }) }, // 提交保运 onSubmit: async function (e) { const that = this; const params = e.detail.value; const form = that.data.form; params.yyzz = form.yyzz; params.img_url = form.img_url; 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 (that.data.id) arr = await app.$post(`/school/${form._id}`, params); else arr = await app.$post(`/school`, 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 || null }) //验证规则函数 that.initValidate(); // 监听用户是否登录 await that.watchLogin(); }, // 监听用户是否登录 watchLogin: async function () { const that = this; wx.getStorage({ key: 'user', success: async res => { let arr; if (that.data.id) { arr = await app.$get(`/school/${that.data.id}`); if (arr.errcode == '0') { that.setData({ form: arr.data }) } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) } } }, fail: async res => { wx.redirectTo({ url: '/pages/index/index' }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })