123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 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/home/home',
- })
- }
- },
- // 获取手机号号
- phoneInput: function (e) {
- this.setData({
- phone: e.detail.value
- })
- },
- // 获取验证码
- codeInput: function (e) {
- this.setData({
- code: e.detail.value
- })
- },
- // 登录
- login: function () {
- console.log('s')
- var self = this
- wx.login({
- success(wxres) {
- requestAPI.userLogin(wxres.code).then(res => {
- if (res.status === 401) {
- self.setData({
- ifUser: false
- })
- } else {
- if (res.data.access_token) {
- wx.setStorageSync('token', res.data.access_token);
- wx.setStorageSync('username', res.data.username);
- wx.switchTab({
- url: '/pages/home/home',
- })
- }
- }
- })
- }
- })
- },
- 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.setStorageSync('username', res.data.username)
- 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 => {
- wx.setStorageSync('userName', res.data.data.sysUser.username);
- })
- }
- })
|