// pages/login/login.js import WxValidate from '../../utils/wxValidate'; import { format, match_status } from '../../utils/dict'; const app = getApp() Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '比赛信息管理', leftArrow: true, useBar: false }, // 主体高度 infoHeight: '', form: {}, // 赛制 formatList: format, // 状态 statusList: match_status, // 赛制 format: [], id: '' }, initValidate() { const rules = { name: { required: true }, start_time: { required: true }, end_time: { required: true }, address: { required: true } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { name: { required: '请输入比赛名称' }, start_time: { required: '请选择开始时间' }, end_time: { required: '请选择结束时间' }, address: { required: '请输入比赛地点' } }; this.WxValidate = new WxValidate(rules, messages) }, back: function () { wx.navigateBack({ delta: 1 }) }, // 选择比赛开始时间 dateChange: function (e) { const that = this; const { type } = e.currentTarget.dataset; const { value } = e.detail; if (type == 'start') that.setData({ 'form.start_time': value }) else if (type == 'end') that.setData({ 'form.end_time': value }) }, // 选择赛制 formatChange: function (e) { const that = this; const format = that.data.format; const { value } = e.detail; let list = [...format]; let data = that.data.formatList[value]; if (data) { if (format && format.length > 0) { let is_data = format.find((i) => i.label == data.label); if (!is_data) list.push(data) } else list.push(data) that.setData({ format: list }) } }, // 删除赛制 toDel: function (e) { const that = this; let list = that.data.format; const { index } = e.currentTarget.dataset; list.splice(index, 1); that.setData({ format: list }); }, // 选择状态 statusChange: function (e) { const that = this; const { value } = e.detail; that.setData({ 'form.status': value }) }, // 提交比赛 onSubmit: async function (e) { const that = this; const params = e.detail.value; params.match_time = params.start_time + '-' + params.end_time; const format = that.data.format; params.format = format; const id = that.data.id; if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { if (params.format && params.format.length > 0) { let arr; if (id) arr = await app.$post(`/courtAdmin/api/match/${id}`, params); else arr = await app.$post(`/courtAdmin/api/match`, params); if (arr.errcode == '0') { wx.showToast({ title: `维护比赛完成`, icon: 'success', duration: 2000 }); that.back(); } else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) } else wx.showToast({ title: `请选择赛制`, icon: 'error', duration: 2000 }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const that = this; if (options && options.id) that.setData({ id: options.id }) //验证规则函数 that.initValidate(); // 计算高度 that.searchHeight(); // 监听用户是否登录 that.watchLogin(); }, // 监听用户是否登录 watchLogin: async function () { const that = this; let id = that.data.id; wx.getStorage({ key: 'token', success: async res => { if (id) { const arr = await app.$get(`/courtAdmin/api/match/${id}`); if (arr.errcode == '0') { // 处理时间 let data = arr.data; data.start_time = data.match_time.substring(0, 10); data.end_time = data.match_time.substring(11, data.match_time.length); that.setData({ format: data.format }); that.setData({ form: data }); } } }, fail: res => { wx.redirectTo({ url: '/pages/index/index', }) } }) }, // 计算高度 searchHeight: function () { let frameStyle = this.data.frameStyle; let client = app.globalData.client; let infoHeight = client.windowHeight; // 是否去掉状态栏 if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2); // 是否减去底部菜单 if (frameStyle.useBar) infoHeight = infoHeight - 50; if (infoHeight) this.setData({ infoHeight: infoHeight }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })