import WxValidate from '../../utils/wxValidate' const app = getApp() Page({ data: { form: {}, current: 1, // 用户类别 typeList: [], }, // 登陆注册监听 click(e) { const that = this; let index = e.currentTarget.dataset.code; that.setData({ current: index }) }, // 选择用户类别 typeChange: function (e) { const that = this; let index = e.detail.value; let data = that.data.typeList[index]; if (data) that.setData({ 'form.type': data.value }); that.setData({ 'form.type_name': data.label }); }, // 提交登录 onSubmit: async function (e) { 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 { const res = await app.$api('user/login', 'POST', params); if (res.errcode === 0) { app.globalData.userInfo = res.data;//存用户信息到app.js wx.setStorage({ key: "user", data: res.data })// 存用户信息到storage,以便之后判断用户是否登录 wx.showToast({ title: `账号登录成功`, icon: 'success', duration: 2000 }) //登录成功提示 wx.navigateBack({ delta: 1 }); } else wx.showToast({ title: res.errmsg, icon: 'none', duration: 2000 }) } }, // 提交注册 reSubmit: 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 { if (params.password !== params.is_password) { wx.showToast({ title: '密码输入不一致', duration: 2000, icon: 'error', }) } else { params.openid = that.data.form.openid delete params.is_password const res = await app.$api('user', 'POST', params); if (res.errcode === 0) { wx.showToast({ title: `账号注册成功`, icon: 'success', duration: 2000 }) //登录成功提示 that.setData({ current: 1, form: { openid: that.data.form.openid } }) } else wx.showToast({ title: res.errmsg, icon: 'none', duration: 2000 }) } } }, // 忘记密码 forgot() { wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 }) }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { const that = this; wx.showLoading({ title: '加载中', mask: true }) //验证规则函数 await that.initValidate() await that.searchOther() await that.search() wx.hideLoading() }, // 验证表单 initValidate() { const rules = { account: { required: true }, password: { required: true, } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { account: { required: '请输入账号', }, password: { required: '请输入密码', } }; this.WxValidate = new WxValidate(rules, messages) }, async searchOther() { const that = this; let res; // 类别 res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' }) if (res.errcode == '0') that.setData({ typeList: res.data }) }, // 查询通知 async search() { const that = this; wx.getStorage({ key: 'openid', async success(res) { that.setData({ 'form.openid': res.data }) }, fail(err) { // console.log(err); } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })