Jelajahi Sumber

fix: fix auth again

wangyuan 2 tahun lalu
induk
melakukan
5064ec6de9
3 mengubah file dengan 84 tambahan dan 121 penghapusan
  1. 2 2
      api/my.js
  2. 81 117
      pages/index/index.js
  3. 1 2
      pages/index/index.json

+ 2 - 2
api/my.js

@@ -3,7 +3,7 @@
 import { wxRequest } from './request'
 
 const bindMobileURL = '/user/bind/mobile' // 获取用户手机号
-const loginURL = '/user/login' // 登录
+const userLoginURL = '/user/login' // 登录
 const bindBaseInfoURL = '/user/bind/base/info' // 绑定用户头像昵称
 const myReportListURL =  '/get/my/report/list' // 我的报告
 const getReportURL =  '/get/report' // 查看我的报告
@@ -11,7 +11,7 @@ const getReportUserInfoURL = '/get/report/user/info'
 const chargeListURL = '/get/charge/list' // 我的充值记录
 const getExaminationOrderListURL = '/get/examination/order/list' // 我的核销记录
 
-export const ittLogin =  (data) => wxRequest(loginURL, data)
+export const userLogin =  (data) => wxRequest(userLoginURL, data)
 
 export const bindMobile =  (data) => wxRequest(bindMobileURL, data)
 

+ 81 - 117
pages/index/index.js

@@ -1,7 +1,6 @@
-// pages/index/index.js
 import { homePage, appGetConfigImg } from '../../api/index'
 import { createQRcode } from "../../api/document"
-import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
+import { userLogin, bindMobile, bindBaseInfo } from '../../api/my'
 import { appointmentList } from '../../api/appointment'
 const app = getApp()
 Page({
@@ -10,7 +9,6 @@ Page({
    * 页面的初始数据
    */
   data: {
-    locationStr: '定位地址…',
     useNumber: 0, // 剩余预约次数
     documentVos: [], // 档案信息
     cardNo: '',
@@ -29,86 +27,36 @@ Page({
     topBarObj: null,
     homepageImg: ''
   },
-  
-  onShow () {
-    app.globalData.selectedInex = 0
-    if (this.data.isRefreshPage) {
-      this.initIndexData()
-    }
-  },
 
-  onLoad () {
-    this.init()
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow() {
+    this.handleToken()
   },
-
-  // 初始化函数,判读需不需要获取wx.login => code
-  init () {
-    console.log(app.globalData.accessToken, 'index onlaod')
-    const accessToken = app.globalData.accessToken
+  handleToken () {
+    const accessToken = wx.getStorageSync('accessToken')
+    console.log('accessToken',accessToken);
     if (accessToken) {
-      var isNeedPhone = wx.getStorageSync('isNeedPhone')
-      var isNeedHeadImg = wx.getStorageSync('isNeedHeadImg')
-      var headImg = wx.getStorageSync('headImg')
-      var userName = wx.getStorageSync('userName')
-      var phoneNumber = wx.getStorageSync('phoneNumber')
-      app.globalData.isNeedPhone = isNeedPhone
-      app.globalData.isNeedHeadImg = isNeedHeadImg
-      app.globalData.userInfo.headImg= headImg
-      app.globalData.userInfo.userName = userName
-      app.globalData.userInfo.phoneNumber = phoneNumber
-      app.globalData.userInfo.login = true
-      this.handleAuth()
+      this.storageTokenLogin()
     } else {
-      this.appCodeLoginFn()
-    }
-  },
-
-  // 是否需要权限判断 手机号 头像 昵称
-  handleAuth () {
-    // 不需要手机号授权 有头像和昵称
-    const resIsNeedPhone = app.globalData.isNeedPhone
-    const resHeadImg = app.globalData.userInfo.headImg
-    const that = this
-    if (!resIsNeedPhone && resHeadImg !== '') {
-      app.globalData.userInfo.login = true
-      this.initIndexData()
-    } else {
-      if (resIsNeedPhone) { // true 代表需要手机号授权
-        that.setData({
-          phoneAuthShow: true
-        })
-      } else {
-        if (resHeadImg == '') { // 需要授权头像
-          that.setData({
-            userAuthShow: true
-          })
-        } else {
-          app.globalData.userInfo.login = true
-        }
-      }
+      this.handleWXCodeLogin()
     }
   },
-
-  // 获取本地存储你的头像 昵称 手机号 以及 accessToken 信息
-  appLoginFn () {
-    console.log('使用已存在的accessToekn登录')
-    this.initIndexData()
+  storageTokenLogin () {
+    this.handleUserLoginRes()
   },
-
-  // 使用wx.login => code => accessToken
-  appCodeLoginFn () {
+  handleWXCodeLogin () {
     var that = this
     wx.login({
       success (loginCode) {
         if (loginCode.code) {
-          that.ittLoginFn(loginCode.code)
+          that.userLoginFn(loginCode.code)
         }
       }
     })
   },
-
-  // 登录函数
-  ittLoginFn (code) {
+  userLoginFn (code) {
     var that = this
     var data = {
       code: code
@@ -117,40 +65,45 @@ Page({
       title: '登录中...',
       mask: true
     })
-    ittLogin(data).then(res => {
-      const response = res.data
-      const accessToken = response.accessToken
-      const isNeedPhone = response.isNeedPhone
-      const isNeedHeadImg = response.headImg ? true : false
-      const headImg = response.headImg
-      const userName = response.userName
-      const phoneNumber = response.phoneNumber
-      app.globalData.accessToken = accessToken
-      app.globalData.isNeedPhone = isNeedPhone
-      app.globalData.isNeedHeadImg = isNeedHeadImg
-      app.globalData.userInfo.headImg= headImg
-      app.globalData.userInfo.userName = userName
-      app.globalData.userInfo.phoneNumber = phoneNumber
-      wx.setStorageSync('accessToken', accessToken)
-      wx.setStorageSync('isNeedPhone', isNeedPhone)
-      wx.setStorageSync('isNeedHeadImg', isNeedHeadImg)
-      wx.setStorageSync('headImg', headImg)
-      wx.setStorageSync('userName', userName)
-      wx.setStorageSync('phoneNumber', phoneNumber)
+    userLogin(data).then(res => {
       wx.hideLoading()
-      that.handleAuth()
+      that.setStorage(res.data)
     }).catch(e => {
       wx.hideLoading()
     })
   },
-
-   // 拉起手机号授权弹窗
-   getPhoneNumber (e) {
+  handleUserLoginRes () {
+    const that = this
+    const _isNeedPhone = wx.getStorageSync('isNeedPhone')
+    const _headImg = wx.getStorageSync('headImg')
+    console.log(_isNeedPhone, _headImg, '79');
+    if (!_isNeedPhone && _headImg !== '') {
+      app.globalData.userInfo.login = true
+      this.initIndexData()
+    } else {
+      if (_isNeedPhone) {
+        that.setData({
+          phoneAuthShow: true
+        })
+      } else {
+        if (_headImg == '') { 
+          that.setData({
+            userAuthShow: true
+          })
+        } else {
+          app.globalData.userInfo.login = true
+        }
+      }
+    }
+  },
+  // 拉起手机号授权弹窗
+  getPhoneNumber (e) {
     var phonePermission = e.detail.errMsg.split(':')[1]
     if (phonePermission == 'ok') {
       var phoneCode = e.detail.code
       this.bindMobileFn(phoneCode)
     } else {
+      wx.setStorageSync('isNeedPhone', true)
       wx.showToast({
         title: '授权失败',
         icon: 'error'
@@ -163,6 +116,7 @@ Page({
     var data = {
       mobileCode: phoneCode
     }
+    const _headImg = wx.getStorageSync('headImg')
     wx.showLoading({
       title: '加载中...',
       mask: true
@@ -171,11 +125,13 @@ Page({
       wx.hideLoading()
       const resPhoneNumber = res.data
       app.globalData.userInfo.phoneNumber = resPhoneNumber
+      app.globalData.isNeedPhone = false
+      wx.setStorageSync('isNeedPhone', false)
       wx.setStorageSync('phoneNumber', resPhoneNumber)
       that.setData({
         phoneAuthShow: false
       })
-      if (app.globalData.userInfo.headImg == '') {
+      if (!_headImg) {
         // 需要授权头像
         that.setData({
           userAuthShow: true
@@ -192,7 +148,6 @@ Page({
       })
     })
   },
-
   // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
   getUserinfo () {
     var that = this
@@ -213,9 +168,8 @@ Page({
       })
     })
   },
-
-  // 提交用户头像和昵称到后台
-  handleConfirmNickname () {
+   // 提交用户头像和昵称到后台
+   handleConfirmNickname () {
     var that = this
     var data = {
       avatarUrl: that.data.avatarUrl,
@@ -231,8 +185,10 @@ Page({
       app.globalData.userInfo.login = true
       app.globalData.userInfo.headImg = _headImg
       app.globalData.userInfo.userName = _userName
+      app.globalData.isNeedHeadImg = false
       wx.setStorageSync('headImg', _headImg)
       wx.setStorageSync('userName', _userName)
+      wx.setStorageSync('isNeedHeadImg', false)
       that.setData({
         userAuthShow: false
       })
@@ -247,25 +203,6 @@ Page({
       })
     })
   },
-
- /*  onPullDownRefresh () {
-    wx.vibrateShort({
-      type: 'medium'
-    })
-    this.initIndexData(true)
-  }, */
-
-  onShareAppMessage () {
-    return {
-      title: '3分钟500+项健康指标',
-      path: '/pages/index/index',
-      imageUrl: '../../imaes/share.jpg'
-    }
-  },
-  // 获取首页信息 档案 剩余次数, 
-  /*  定位地址这边换成
-  1. 如果没有预约信息,就这条直接隐藏掉
-  2. 如果有预约信息,则显示,您预约的2022-10-25 8:00-12:00的健康筛查   待体验 */
   initIndexData (pullDownRefresh = false) {
     var that = this
     wx.showLoading({
@@ -332,7 +269,6 @@ Page({
       wx.hideNavigationBarLoading()
     })
   },
-
   /**
    * 处理套餐 购买 or 预约  
    * useNumber: 0-购买 1-预约
@@ -473,5 +409,33 @@ Page({
       qrcodeDialog: false,
       QRCodeBase64: ''
     })
+  },
+  onShareAppMessage () {
+    return {
+      title: '3分钟500+项健康指标',
+      path: '/pages/index/index',
+      imageUrl: '../../imaes/share.jpg'
+    }
+  },
+  setStorage (response) {
+    const accessToken = response.accessToken
+    const isNeedPhone = response.isNeedPhone
+    const isNeedHeadImg = response.headImg ? false : true
+    const headImg = response.headImg
+    const userName = response.userName
+    const phoneNumber = response.phoneNumber
+    app.globalData.accessToken = accessToken
+    app.globalData.isNeedPhone = isNeedPhone
+    app.globalData.isNeedHeadImg = isNeedHeadImg
+    app.globalData.userInfo.headImg= headImg
+    app.globalData.userInfo.userName = userName
+    app.globalData.userInfo.phoneNumber = phoneNumber
+    wx.setStorageSync('accessToken', accessToken)
+    wx.setStorageSync('isNeedPhone', isNeedPhone)
+    wx.setStorageSync('isNeedHeadImg', isNeedHeadImg)
+    wx.setStorageSync('headImg', headImg)
+    wx.setStorageSync('userName', userName)
+    wx.setStorageSync('phoneNumber', phoneNumber)
+    this.handleUserLoginRes()
   }
 })

+ 1 - 2
pages/index/index.json

@@ -1,4 +1,3 @@
 {
-  "navigationBarTitleText": "首页",
-  "enablePullDownRefresh": false
+  "navigationBarTitleText": "首页"
 }