Ver código fonte

fix: rebuild login logic

wangyuan 2 anos atrás
pai
commit
42bc12ef19
11 arquivos alterados com 264 adições e 315 exclusões
  1. 6 6
      api/request.js
  2. 10 3
      app.js
  3. 1 1
      app.json
  4. 2 2
      custom-tab-bar/index.js
  5. BIN
      imaes/compant.jpg
  6. BIN
      imaes/share.jpg
  7. 206 40
      pages/index/index.js
  8. 16 1
      pages/index/index.wxml
  9. 17 256
      pages/my/my.js
  10. 5 6
      pages/my/my.wxml
  11. 1 0
      pages/set/set.js

+ 6 - 6
api/request.js

@@ -11,15 +11,12 @@ export const wxRequest = (url,data) => {
       header: {
         systemData:JSON.stringify({
           requestId: itt.getUUID(),
-          loginToken: app.globalData.accessToken,
+          loginToken: wx.getStorageSync('accessToken') || '',
           timestamp: new Date().getTime()
         })
       },
       method: 'POST',
       success (response) {
-        console.log('当前请求地址:', url);
-        console.log('当前请求参数:', data);
-        console.log('当前请求返回:', response.data);
         if (response.statusCode !== 200) {
           reject(response.data.msg || '请求错误')
         } else {
@@ -34,14 +31,17 @@ export const wxRequest = (url,data) => {
               showCancel: false,
               success (res) {
                 if (res.confirm) {
+                  wx.clearStorageSync()
+                  app.globalData.isNeedHeadImg = false
+                  app.globalData.isNeedPhone = false
                   app.globalData.userInfo.login = false
                   app.globalData.accessToken = ''
                   app.globalData.userInfo.headImg= ''
                   app.globalData.userInfo.userName = ''
                   app.globalData.userInfo.phoneNumber = ''
-                  app.globalData.selectedInex = 2
+                  app.globalData.selectedInex = 0
                   wx.reLaunch({
-                    url: '/pages/my/my',
+                    url: '/pages/index/index',
                   })
                 }
               }

+ 10 - 3
app.js

@@ -5,11 +5,18 @@
 App({
   onLaunch() {
     console.log('App onLaunch');
+    const that = this
+    wx.getStorage({
+      key: 'accessToken',
+      success (res) {
+        that.globalData.accessToken = res.data
+      }
+    })
   },
   globalData: {
     accessToken: '',
-    phoneAuth: false, // 是否有号码权限
-    headAuth: false, // 是否有头像权限
+    isNeedPhone: false, // 是否有号码权限
+    isNeedHeadImg: false, // 是否有头像权限
     userInfo: {
       userName: '',
       phoneNumber: '',
@@ -19,7 +26,7 @@ App({
     auth: {
       camera: true
     },
-    selectedInex: 2, // 自定义tabbar选中的index
+    selectedInex: 0, // 自定义tabbar选中的index 默认首页index
     navigateBackParams: {}, // 使用wx.navigateBack 返回上一个页面的参数
   }
 })

+ 1 - 1
app.json

@@ -1,5 +1,5 @@
 {
-  "entryPagePath": "pages/my/my",
+  "entryPagePath": "pages/index/index",
   "pages": [
     "pages/permisission/permission",
     "pages/index/index",

+ 2 - 2
custom-tab-bar/index.js

@@ -1,7 +1,7 @@
 const app = getApp()
 Component({
   data: {
-    selected: 2,
+    selected: 0,
     color: "#7A7E83",
     selectedColor: "#45A6B5",
     list: [{
@@ -32,7 +32,7 @@ Component({
     switchTab(e) {
       const data = e.currentTarget.dataset
       const url = data.path
-      if (data.index == 0 || data.index == 1) {
+      if (data.index == 1 || data.index == 2) {
         if (!app.globalData.userInfo.login) {
           wx.showToast({
             title: '请先授权登录',

BIN
imaes/compant.jpg


BIN
imaes/share.jpg


+ 206 - 40
pages/index/index.js

@@ -1,7 +1,7 @@
 // pages/index/index.js
 import { homePage } from '../../api/index'
 import { createQRcode } from "../../api/document"
-// import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
+import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
 const app = getApp()
 Page({
 
@@ -19,72 +19,241 @@ Page({
     QRCodeBase64: '',
     qrcodeDialog: false,
     qrUserName: '',
-    phoneAuthShow: false
+    phoneAuthShow: false,
+    userAuthShow: false,
+    avatarUrl: '',
+    nickName: ''
   },
   
   onShow () {
     app.globalData.selectedInex = 0
-    if (app.globalData.userInfo.login) {
+  },
+
+  onLoad () {
+    this.init()
+  },
+
+  // 初始化函数,判读需不需要获取wx.login => code
+  init () {
+    console.log(app.globalData.accessToken, 'index onlaod')
+    const accessToken = app.globalData.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()
+    } 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 {
-      wx.reLaunch({
-        url: '/pages/my/my'
-      })
+      if (resIsNeedPhone) { // true 代表需要手机号授权
+        that.setData({
+          phoneAuthShow: true
+        })
+      } else {
+        if (resHeadImg == '') { // 需要授权头像
+          that.setData({
+            userAuthShow: true
+          })
+        } else {
+          app.globalData.userInfo.login = true
+        }
+      }
     }
   },
 
-  onLoad () {
-    //this.ittLoginFn()
+  // 获取本地存储你的头像 昵称 手机号 以及 accessToken 信息
+  appLoginFn () {
+    console.log('使用已存在的accessToekn登录')
+    this.initIndexData()
   },
 
-  ittLoginFn () {
-    // var that = this
+  // 使用wx.login => code => accessToken
+  appCodeLoginFn () {
+    var that = this
     wx.login({
       success (loginCode) {
         if (loginCode.code) {
-          var data = {
-            code: loginCode.code
-          }
-          wx.showLoading({
-            title: '登录中...',
-            mask: true
-          })
-          ittLogin(data).then(res => {
-            wx.hideLoading()
-            app.globalData.accessToken = res.data.accessToken
-            app.globalData.phoneAuth = !res.data.isNeedPhone
-            app.globalData.headAuth = res.data.headImg ? true : false
-            app.globalData.userInfo.headImg= res.data.headImg
-            app.globalData.userInfo.userName = res.data.userName
-            app.globalData.userInfo.phoneNumber = res.data.phoneNumber
-            //const resIsNeedPhone = res.data.isNeedPhone
-            // const resHeadImg = res.data.headImg
-            console.log(app.globalData.phoneAuth, app.globalData.headAuth);
-          }).catch(e => {
-            wx.hideLoading()
-          })
+          that.ittLoginFn(loginCode.code)
         }
       }
     })
   },
 
-  getPhoneNumber (e) {
-    console.log(e.detail.code)
+  // 登录函数
+  ittLoginFn (code) {
+    var that = this
+    var data = {
+      code: code
+    }
+    wx.showLoading({
+      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)
+      wx.hideLoading()
+      that.handleAuth()
+    }).catch(e => {
+      wx.hideLoading()
+    })
+  },
+
+   // 拉起手机号授权弹窗
+   getPhoneNumber (e) {
+    var phonePermission = e.detail.errMsg.split(':')[1]
+    if (phonePermission == 'ok') {
+      var phoneCode = e.detail.code
+      this.bindMobileFn(phoneCode)
+    } else {
+      wx.showToast({
+        title: '授权失败',
+        icon: 'error'
+      })
+    }
+  },
+  // 绑定手机号
+  bindMobileFn (phoneCode) {
+    var that = this
+    var data = {
+      mobileCode: phoneCode
+    }
+    wx.showLoading({
+      title: '加载中...',
+      mask: true
+    })
+    bindMobile(data).then(res => {
+      wx.hideLoading()
+      const resPhoneNumber = res.data
+      app.globalData.userInfo.phoneNumber = resPhoneNumber
+      wx.setStorageSync('phoneNumber', resPhoneNumber)
+      that.setData({
+        phoneAuthShow: false
+      })
+      if (app.globalData.userInfo.headImg == '') {
+        // 需要授权头像
+        that.setData({
+          userAuthShow: true
+        })
+      } else {
+        app.globalData.userInfo.login = true
+      }
+    }).catch(e => {
+      wx.hideLoading()
+      wx.showModal({
+        content: e,
+        confirmColor: '#333',
+        showCancel: false
+      })
+    })
+  },
+
+  // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
+  getUserinfo () {
+    var that = this
+    wx.getUserProfile({
+      desc: '用于获取用户权益'
+    }).then(res => {
+      const _avatarUrl = res.userInfo.avatarUrl
+      const _nickName = res.userInfo.nickName
+      that.setData({
+        avatarUrl: _avatarUrl,
+        nickname: _nickName
+      })
+      that.handleConfirmNickname()
+    }).catch(e => {
+      wx.showToast({
+        title: '授权失败',
+        icon: 'error'
+      })
+    })
+  },
+
+  // 提交用户头像和昵称到后台
+  handleConfirmNickname () {
+    var that = this
+    var data = {
+      avatarUrl: that.data.avatarUrl,
+      nickName: that.data.nickname
+    }
+    wx.showLoading({
+      title: '加载中...',
+      mask: true
+    })
+    bindBaseInfo(data).then(res => {
+      const _headImg = that.data.confirmAvatarUrl
+      const _userName = that.data.confirmNickname
+      app.globalData.userInfo.login = true
+      app.globalData.userInfo.headImg = _headImg
+      app.globalData.userInfo.userName = _userName
+      wx.setStorageSync('headImg', _headImg)
+      wx.setStorageSync('userName', _userName)
+      that.setData({
+        userAuthShow: false
+      })
+      that.initIndexData()
+      wx.hideLoading()
+    }).catch(e => {
+      wx.hideLoading()
+      wx.showModal({
+        content: e,
+        confirmColor: '#333',
+        showCancel: false
+      })
+    })
   },
 
-  onPullDownRefresh () {
+ /*  onPullDownRefresh () {
     wx.vibrateShort({
       type: 'medium'
     })
     this.initIndexData(true)
-  },
+  }, */
+
   onShareAppMessage () {
     return {
       title: 'ITTHealth',
       path: '/pages/index/index'
     }
   },
-  // 获取首页信息
+  // 获取首页信息 档案 剩余次数
   initIndexData (pullDownRefresh = false) {
     var that = this
     wx.showLoading({
@@ -113,10 +282,7 @@ Page({
       })
     })
   },
-  // 打开地图选择位置。
-  openMap () {
-    wx.chooseLocation()
-  },
+
   /**
    * 处理套餐 购买 or 预约  
    * useNumber: 0-购买 1-预约

+ 16 - 1
pages/index/index.wxml

@@ -92,7 +92,22 @@
         <view class="auth-text">
           小程序申请授权你的手机号
         </view>
-        <button class="confirm auth-btn" open-type="getPhoneNumber" bindtap="getPhoneNumber">点击授权</button>
+        <button class="confirm auth-btn" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">点击授权</button>
+      </view>
+    </view>
+  </van-overlay>
+  <van-overlay show="{{ userAuthShow }}">
+    <view class="wrapper ittflex">
+      <view class="userinfo-auth-modal">
+        <view class="modal-title">申请权限</view>
+        <view class="px-border"></view>
+        <view class="app-logo">
+          <image class="logopng" src="../../imaes/logo1@2x.png"></image>
+        </view>
+        <view class="auth-text">
+          小程序申请授权你的公开信息(昵称,头像)
+        </view>
+        <button class="confirm auth-btn" bindtap="getUserinfo">点击授权</button>
       </view>
     </view>
   </van-overlay>

+ 17 - 256
pages/my/my.js

@@ -1,8 +1,5 @@
 // pages/my/my.js
-import itt from '../../utils/util'
-import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
 import { getAccountNmber } from '../../api/appointment'
-import { userUploadHeadImg } from '../../api/upload'
 import { documentList } from "../../api/document"
 const app = getApp()
 Page({
@@ -11,19 +8,8 @@ Page({
    * 页面的初始数据
    */
   data: {
-    overshow: false,
-    pageLogin: false,
-    isRelogin: false, // 是否为设置退出登录重新登录
-    showPhoneAuthBtn: true,
-    showAvaNick: false,
-    isNeedPhoneBtn: false, // 是否需要显示手机号授权
-    isNeedHeadImgBtn: false,// 是否需要显示用户头像 昵称 授权
-    isRegister: false,
-    avatarUrl: '',
-    confirmAvatarUrl: '',
-    confirmNickname: '',
-    nickname: '',
-    phone: '',
+    appHeadImg: '',
+    appUserName: '',
     count: 0,
     isFirstGetCount: true, // 是否为第一次获取检测机会的次数
     gridList: [
@@ -85,10 +71,6 @@ Page({
   },
 
   onShow () {
-    console.log('my onshow ===========', this.data.isFirstGetCount);
-    this.setData({
-      pageLogin: app.globalData.userInfo.login
-    })
     if (!this.data.isFirstGetCount) {
       this.getAccountNmberFn()
     }
@@ -100,7 +82,13 @@ Page({
    */
   onLoad(options) {
     app.globalData.selectedInex = 2
-    this.userLogin()
+    var headImg = wx.getStorageSync('headImg')
+    var userName = wx.getStorageSync('userName')
+    this.setData({
+      appHeadImg: headImg,
+      appUserName: userName
+    })
+    this.getAccountNmberFn()
   },
 
   // 获取用户档案信息
@@ -141,93 +129,6 @@ Page({
     })
   },
 
-  // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
-  getUserinfo () {
-    var that = this
-    wx.getUserProfile({
-      desc: '用于获取用户权益'
-    }).then(res => {
-      const _avatarUrl = res.userInfo.avatarUrl
-      const _nickName = res.userInfo.nickName
-      that.setData({
-        avatarUrl: _avatarUrl,
-        nickname: _nickName
-      })
-      that.handleConfirmNickname()
-    }).catch(e => {
-      wx.showToast({
-        title: '授权失败',
-        icon: 'error'
-      })
-    })
-  },
-
-  // 退出登录后重新登录
-  handleReLogin () {
-    this.userLogin()
-  },
-
-  // 通过 code 获取 token
-  userLogin () {
-    var that = this
-    wx.login({
-      success (loginCode) {
-        if (loginCode.code) {
-          var data = {
-            code: loginCode.code
-          }
-          wx.showLoading({
-            title: '登录中...',
-            mask: true
-          })
-          ittLogin(data).then(res => {
-            wx.hideLoading()
-            app.globalData.accessToken = res.data.accessToken
-            app.globalData.userInfo.headImg= res.data.headImg
-            app.globalData.userInfo.userName = res.data.userName
-            app.globalData.userInfo.phoneNumber = res.data.phoneNumber
-            const resIsNeedPhone = res.data.isNeedPhone
-            const resHeadImg = res.data.headImg
-            that.getAccountNmberFn()
-            if (!resIsNeedPhone && resHeadImg !== '') {
-              app.globalData.userInfo.login = true
-              that.setData({
-                isNeedHeadImgBtn: true,
-                confirmAvatarUrl: app.globalData.userInfo.headImg,
-                confirmNickname: app.globalData.userInfo.userName,
-              })
-            } else {
-              if (resIsNeedPhone) { // true 代表需要手机号授权
-                that.setData({
-                  isNeedPhoneBtn: true
-                })
-              } else {
-                if (resHeadImg == '') {
-                  // 需要授权头像
-                  that.setData({
-                    overshow: true,
-                    phone: res.data,
-                    isNeedPhoneBtn: false,
-                    isNeedHeadImgBtn: true
-                  })
-                } else {
-                  that.setData({
-                    isNeedHeadImgBtn: true,
-                    confirmAvatarUrl: app.globalData.userInfo.headImg,
-                    confirmNickname: app.globalData.userInfo.userName,
-                  })
-                }
-              }
-
-            }
-          }).catch(e => {
-            wx.hideLoading()
-          })
-        }
-      }
-    })
-  },
-
   // 获取用户剩余检测机会
   getAccountNmberFn () {
     var that = this
@@ -241,142 +142,6 @@ Page({
     })
   },
 
-  // 用户本地选择图片头像
-  onChooseAvatar(e) {
-    const { avatarUrl } = e.detail
-    this.uploadImg(avatarUrl) 
-  },
-
-  // 上传用户头像文件到后台
-  uploadImg (avatarUrl) {
-    var that = this
-    var data = {
-      filePath: avatarUrl
-    }
-    wx.showLoading({
-      title: '上传中...',
-      mask: true
-    })
-    userUploadHeadImg(data).then(res => {
-      wx.hideLoading()
-      var response = JSON.parse(res)
-      if (response.code != '000') {
-        wx.showToast({
-          title: '头像上传失败',
-          icon: 'error'
-        })
-      } else {
-        that.setData({
-          avatarUrl: response.data
-        })
-      }
-    }).catch(e => {
-      wx.hideLoading()
-      wx.showModal({
-        content: e,
-        confirmColor: '#333',
-        showCancel: false
-      })
-    })
-  },
-  // 拉起手机号授权弹窗
-  getPhoneNumber (e) {
-    var that = this
-    var phonePermission = e.detail.errMsg.split(':')[1]
-    if (phonePermission == 'ok') {
-      var phoneCode = e.detail.code
-      that.bindMobileFn(phoneCode)
-    } else {
-      wx.showToast({
-        title: '授权失败',
-        icon: 'error'
-      })
-    }
-  },
-  // 绑定手机号
-  bindMobileFn (phoneCode) {
-    var that = this
-    var data = {
-      mobileCode: phoneCode
-    }
-    wx.showLoading({
-      title: '加载中...',
-      mask: true
-    })
-    bindMobile(data).then(res => {
-      wx.hideLoading()
-      app.globalData.userInfo.phoneNumber = res.data
-      if (app.globalData.userInfo.headImg == '') {
-        // 需要授权头像
-        that.setData({
-          overshow: true,
-          phone: res.data,
-          isNeedPhoneBtn: false,
-          isNeedHeadImgBtn: true
-        })
-      } else {
-        that.setData({
-          isNeedHeadImgBtn: true,
-          confirmAvatarUrl: app.globalData.userInfo.headImg,
-          confirmNickname: app.globalData.userInfo.userName,
-        })
-        app.globalData.userInfo.login = true
-      }
-    }).catch(e => {
-      wx.hideLoading()
-      wx.showModal({
-        content: e,
-        confirmColor: '#333',
-        showCancel: false
-      })
-    })
-  },
-  // 提交用户头像和昵称到后台
-  handleConfirmNickname () {
-    var that = this
-    if (that.data.avatarUrl == '') {
-      wx.showToast({
-        title: '请选择头像',
-        icon: 'error'
-      })
-      return
-    }
-    if (that.data.nickname == '') {
-      wx.showToast({
-        title: '请输入昵称',
-        icon: 'error'
-      })
-      return
-    }
-    var data = {
-      avatarUrl: that.data.avatarUrl,
-      nickName: that.data.nickname
-    }
-    wx.showLoading({
-      title: '加载中...',
-      mask: true
-    })
-    bindBaseInfo(data).then(res => {
-      wx.hideLoading()
-      app.globalData.userInfo.login = true
-      that.setData({
-        pageLogin: true,
-        overshow: false,
-        confirmAvatarUrl: that.data.avatarUrl,
-        confirmNickname: that.data.nickname
-      })
-      app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
-      app.globalData.userInfo.userName = that.data.confirmNickname
-    }).catch(e => {
-      wx.hideLoading()
-      wx.showModal({
-        content: e,
-        confirmColor: '#333',
-        showCancel: false
-      })
-    })
-  },
-
   // 路由跳转
   handleNavTo (e) {
     var url = e.currentTarget.dataset.url
@@ -396,19 +161,15 @@ Page({
    * 路由拦截
    * **/
   authNavTo (url) {
-    if (itt.loginAuth()) {
-      if (url.indexOf('/report') !== -1) {
-        app.globalData.selectedInex = 1
-        wx.switchTab({
-          url: url,
-        })
-      } else {
-        wx.navigateTo({
-          url: url,
-        })
-      }
+    if (url.indexOf('/report') !== -1) {
+      app.globalData.selectedInex = 1
+      wx.switchTab({
+        url: url,
+      })
     } else {
-      itt.errorToast('请先授权登录')
+      wx.navigateTo({
+        url: url,
+      })
     }
   }
 })

+ 5 - 6
pages/my/my.wxml

@@ -3,11 +3,10 @@
   <view class="top my-info ittflex">
     <view class="main-info ittflex-jcb">
       <view class="left-con ittflex-jcs">
-        <button wx:if="{{isNeedPhoneBtn}}" size="mini" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">点击授权手机号</button>
-        <view wx:if="{{isNeedHeadImgBtn}}" class="avatar ittflex">
-          <image class="image-ac" src="{{confirmAvatarUrl}}"></image>
+        <view class="avatar ittflex">
+          <image class="image-ac" src="{{appHeadImg}}"></image>
         </view>
-        <text class="username" wx:if="{{isNeedHeadImgBtn}}">{{confirmNickname}}</text>
+        <text class="username">{{appUserName}}</text>
       </view>
       <view class="right-con">
         <view class="check-count">
@@ -16,8 +15,8 @@
           <text class="noraltext">次</text>
         </view>
         <view class="action-btn ittflex-jcs">
-          <view class="appoin-btn ittflex" style="margin-right:10rpx;" bindtap="handleCharge">去充值</view>
-          <view wx:if="{{count !== 0}}" class="appoin-btn ittflex" bindtap="handleAppoint">立即预约</view>
+          <view class="appoin-btn ittflex" bindtap="handleCharge">去充值</view>
+          <view wx:if="{{count !== 0}}" style="margin-left:10rpx;"  class="appoin-btn ittflex" bindtap="handleAppoint">立即预约</view>
         </view>
       </view>
     </view>

+ 1 - 0
pages/set/set.js

@@ -38,6 +38,7 @@ Page({
         if (res.confirm) {
           wx.exitMiniProgram({
             success () {
+              wx.clearStorageSync()
               app.globalData.accessToken = ''
               app.globalData.userInfo.login = false
               app.globalData.userInfo.userName = ''