123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // pages/login/login.js
- import { loginByWeChat, updateUserInfo } from '../../api/user'
- import { navigateTo, showModal, showLoading, hideLoading, setStorageSync,getStorageSync, navigateBack, reLaunch, showToast } from '../../utils/util'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- canIUse: wx.canIUse('button.open-type.getPhoneNumber'),
- showAuth: false
- },
- pageData: {
- wxCode: ''
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- this.getWXCode()
- },
- // 获取登录code
- getWXCode: function () {
- wx.login({
- success: (res) => {
- this.pageData.wxCode = res.code
- }
- })
- },
-
- // 手机号登录
- tapPhoneLoginAction: function() {
- navigateTo('/pages/loginMobeil/loginMobeil')
- },
- // 授权手机号
- getPhoneNumber: function(e) {
- const { encryptedData, iv } = e.detail
- if (!encryptedData || !iv) {
- this.tapPhoneLoginAction()
- return false
- }
- showLoading({
- title: '登录中...'
- })
- const { wxCode } = this.pageData
- this.wxLogin(wxCode,encryptedData, iv)
-
- },
- // 授权用户信息 --> 登录
- onGotUserInfo: function(e) {
- let { encryptedData, iv, rawData, signature } = e.detail
- if (iv && iv.length && signature && signature.length && encryptedData && encryptedData.length) {
- try {
- rawData = JSON.parse(rawData)
- } catch (error) {
- rawData = rawData
- }
- const params = {
- signature,
- encryptedData,
- iv,
- userName:rawData.nickName,
- avatar:rawData.avatarUrl
- }
- updateUserInfo(params).then((result)=>{
- if (result && result.code == 0) {
- let userInfo = getStorageSync('USERINFO')
- userInfo = Object.assign({},userInfo,result.data)
- setStorageSync('USERINFO', userInfo)
- // 获取用户信心成功返回
- this.redirect()
- } else {
- this.tapPhoneLoginAction()
- }
- })
- } else {
- this.tapPhoneLoginAction()
- }
- this.setData({
- showAuth: false
- })
- },
- // 登录
- wxLogin: function(code, encryptedData, iv) {
- const params = {
- code,
- encryptedData,
- iv
- }
- loginByWeChat(params).then((result) => {
- hideLoading()
- const data = result.data
- if (data && data.userInfo && data.userInfo.token) {
- setStorageSync('USERINFO', data.userInfo)
- if(parseInt(data.newUser) == 1) { // 1为新用户、0为旧用户,去拿用户信息
- this.setData({
- showAuth: true
- })
- } else {
- // 登录成功返回
- this.redirect()
- }
-
- } else {
- this.tapPhoneLoginAction()
- }
- }).catch((err) => {
- hideLoading()
- showModal({
- title: '提示',
- content: err.msg,
- showCancel: false
- })
- this.tapPhoneLoginAction()
- })
- },
- // 登录成功返回
- redirect: function() {
- // 在内存中的所有页面栈对象
- const pages = getCurrentPages()
- if (pages.length > 1) {
- // 上个页面
- const prePage = pages[pages.length - 2]
- // 页面的登录回调
- if (prePage && prePage.loginCallback) {
- prePage.loginCallback()
- }
- navigateBack()
- } else {
- reLaunch('/pages/index/index')
- }
- },
- // 取消登录
- handleNotLoginClick: function() {
- const pages = getCurrentPages() /*在内存中的所有页面栈对象*/
- if (pages.length > 2) {
- navigateBack()
- } else {
- reLaunch('/pages/index/index')
- }
- }
- })
|