// pages/test/index.js const app = getApp() import WxValidate from '../../utils/wxValidate' Page({ /** * 页面的初始数据 */ data: { frameStyle: { useTop: true, name: '测试页面', leftArrow: true, useBar: true }, // 主体高度 infoHeight: '', // 图片 img_url: [], // dialog弹框 dialog: { title: '弹框标题', show: false, type: '1' }, form: {} }, initValidate() { const rules = { start_time: { required: true } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { start_time: { required: '开始时间' } }; this.WxValidate = new WxValidate(rules, messages) }, // 跳转菜单 tabPath(e) { let { route } = e.detail.detail; if (route) wx.redirectTo({ url: `/${route}` }) }, // 返回上一页 back: function () { wx.navigateBack({ delta: 1 }) }, // 上传图片 imgUpl: function (e) { const that = this; let data = that.data.img_url; data.push(e.detail) that.setData({ img_url: data }) }, // 删除图片 imgDel: function (e) { const that = this; let list = that.data.img_url; let arr = list.filter((i, index) => index != e.detail.index) that.setData({ img_url: arr }) }, // 打开弹框 toDialog: function () { const that = this; that.setData({ dialog: { title: '弹框标题', show: true, type: '1' } }) }, // 关闭弹框 toClose: function () { const that = this; that.setData({ dialog: { title: '弹框标题', show: false, type: '1' } }) }, // 确认选择 datetimeChange: function (e) { const that = this; that.setData({ [`form.${e.detail.name}`]: e.detail.datetime }); }, // 提交保存 toSubmit: function (e) { const that = this; const params = e.detail.value; console.log(params); if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 }) return false } else { console.log(params); } }, /** * 生命周期函数--监听页面加载 */ onLoad: async function (options) { const that = this; //验证规则函数 that.initValidate(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })