// pages/login/login.js import UserService from '../../net/api/userApi'; const Cache = require('../../utils/storage'); import { set, get } from '../../utils/storage'; Page({ /** * 页面的初始数据 */ data: { isChecked: false, hintText: "获取电话号码", code: '', msg: '', errno: '', openId: '' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.doLogin(); }, //登录 doLogin() { const _self = this wx.showLoading({ title: '请稍后...', }) wx.login({ success: (res) => { if (res.code) { var data = { 'jsCode': res.code } UserService.doLogin(data) .then(res => { console.log(res) wx.hideLoading() _self.setData({ openId: res.openid }) //1未绑定 0绑定 if (res.isNew == 1) { wx.showToast({ title: '为了给您带来更便捷、个性化的服务,需要您提供手机号,完成注册流程,请点击授权登录', icon: 'none', duration: 2000 }) } else { //保存token Cache.set("token", res.accessToken) //绑定成功 wx.switchTab({ url: '/pages/index/index', }) } }, err => { //TODO 发生未知错误,需要提醒重新进入小程序 wx.hideLoading() _self.doShowExit() }) } else { //TODO 发生未知错误,需要提醒重新进入小程序 _self.doShowExit() } }, }) }, //显示退出弹窗 doShowExit() { wx.showModal({ title: '提示', content: '发生未知错误,请重新打开小程序', showCancel: false, complete: (res) => { if (res.confirm) { wx.exitMiniProgram() } } }) }, //用户服务协议 showServiceDeal() { wx.navigateTo({ url: '/pages/treaty/service/service', }) }, //隐私条款 showPrivacyDeal() { wx.navigateTo({ url: '/pages/treaty/privacy/privacy', }) }, onChange(e) { console.log(e) this.setData({ isChecked: e.detail.value === '1' }); }, //获取手机号 doGetPhoneNumber(e) { const _self = this if (e.detail.errMsg === 'getPhoneNumber:ok') { // 用户同意授权 const { code } = e.detail; console.log(code) //通过后台获取手机号 wx.showLoading({ title: '登录中...', }) var data = { "code": code, "openid": _self.data.openId } console.log(data) UserService.doRegister(data) .then(res => { wx.hideLoading() if (res.accessToken) { Cache.set("token", res.accessToken); wx.switchTab({ url: '/pages/index/index', }) } else { _self.doShowExit() } }, err => { console.log(err) //TODO 绑定失败重新 wx.hideLoading() _self.doShowExit() }) } else { // 用户拒绝授权 console.log('用户拒绝授权'); _self.doShowExit() } }, doGetUserInfo() { wx.getUserProfile({ desc: '获取昵称用于展示', success: (res) => { } }) }, onUnload() { console.log('login卸载') } })