const REG = /^((13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8})|(8){11}$/ const api = require('../../config/api') const util = require('../../utils/util.js'); const uutil = require('../../utils/index') // pages/login/login.js Page({ /** * 页面的初始数据 */ data: { phone: '', psw: '', showPsw: false, tg: false }, updatePhone(e) { this.setData({phone: e.detail.value}) this.check(false) }, updatePsw(e) { this.setData({psw: e.detail.value}) this.check(false) }, check(tip) { if (!this.data.phone) { tip && wx.showToast({title: '请输入手机号', icon: 'none'}) } else if (!REG.test(this.data.phone)) { tip && wx.showToast({title: '手机号格式不正确!', icon: 'none'}) } else if (!this.data.psw) { tip && wx.showToast({title: '请输入密码', icon: 'none'}) } else { this.setData({ tg: true }) return true } }, showPsw() { this.setData({showPsw: true}) }, hidePsw() { this.setData({showPsw: false}) }, async login() { if (!this.check(true)) return; //登录远程服务器 util.request(api.userLogin, { userName: this.data.phone, password: uutil.encryption(this.data.psw) }, 'POST').then(res => { if (res.code === 0) { //存储用户信息 wx.setStorageSync('userInfo', res.data.user); wx.setStorageSync('token', res.data.token); wx.setStorageSync('userId', res.data.user.userId); wx.setStorageSync('department', res.data.department); wx.setStorageSync('isLogin', true); getApp().setLoginProps(true) wx.redirectTo({ url: '/pages/index/index', }) } else { wx.showToast({title: res.msg, icon: 'none'}) } }); }, onLoad() { if (wx.getStorageSync('token')) { } } })