import * as requestAPI from '../../models/dataModel' import { isPoneAvailable } from '../../utils/util' Page({ data: { phone: '', success: false, code: '', ifUser: true }, onLoad() { if(wx.getStorageSync('token')) { this.getIfExpired() wx.switchTab({ url: '/pages/sample/index', }) } }, // 获取手机号号 phoneInput: function (e) { this.setData({ phone: e.detail.value }) }, // 获取验证码 codeInput: function (e) { this.setData({ code: e.detail.value }) }, // 登录 login: function () { var self = this wx.login({ success(wxres) { requestAPI.userLogin(wxres.code).then(res => { if (res.statusCode === 401) { self.setData({ ifUser: false }) } else { if (res.data.access_token) { wx.setStorageSync('token', res.data.access_token); wx.switchTab({ url: '/pages/sample/index', }) } } }) } }) }, register: function () { let self = this if (this.data.phone.length == 0) { wx.showToast({ title: '手机不能为空', icon: 'none', duration: 3000 }) } else if (!isPoneAvailable(this.data.phone)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 3000 }) } else { requestAPI.getSmsCode(this.data.phone).then(res => { if (isNaN(res.data.msg)) { wx.showToast({ title: res.data.msg, icon: 'none', duration: 3000 }) } else { self.setData({ code: res.data.msg }) requestAPI.getSmsToken(self.data.phone, self.data.code).then(res => { wx.setStorageSync('token', res.data.access_token) wx.login({ success(wxres) { requestAPI.bindOpenId(wxres.code).then(res => { if (res.data.data === true) { self.setData({ ifUser: true }) } }) },fail(res) { wx.showToast({ title: '验证失败', duration: 3000 }) } }) }) } }) } }, getIfExpired() { requestAPI.getUserInfo().then(res => { }) } })