Browse Source

fix: bugFixed login accesToken

wangyuan 2 years ago
parent
commit
21d37acc21

+ 1 - 1
api/request.js

@@ -21,7 +21,7 @@ export const wxRequest = (url,data) => {
         console.log('当前请求参数:', data);
         console.log('当前请求返回:', response.data);
         if (response.statusCode !== 200) {
-          reject(response.data.msg || '请求错误')
+          reject(response.data || '请求错误')
         } else {
           if (response.data.code !== '000' && response.data.code !== '102') {
             reject(response.data || '请求错误')

+ 4 - 0
api/workbench.js

@@ -2,14 +2,18 @@
 import { wxRequest } from './request'
 
 // 创建订单、工作人员扫描二维码
+
 const writeOffOrderURL = '/write/off/order'
 
+const writeOffQueryURL = '/write/off/query'
+
 const confirmOrderURL = '/confirm/order'
 
 export const writeOffOrder =  (data) => wxRequest(writeOffOrderURL, data)
 
 export const confirmOrder =  (data) => wxRequest(confirmOrderURL, data)
 
+export const writeOffQuery =  (data) => wxRequest(writeOffQueryURL, data)
 
 
 

+ 1 - 1
app.json

@@ -1,5 +1,5 @@
 {
-  "entryPagePath": "pages/workbench/workbench",
+  "entryPagePath": "pages/login/login",
   "pages": [
     "pages/login/login",
     "pages/workbench/workbench",

+ 85 - 127
pages/login/login.js

@@ -1,6 +1,7 @@
-// pages/login/login.js
 import { pwdLogin, workerLogin, bindWecaht } from '../../api/login'
+import util from '../../utils/util'
 const app = getApp()
+
 Page({
 
   /**
@@ -9,76 +10,52 @@ Page({
   data: {
     username: '',
     userPwd: '',
-    loginCode: '',
     showAccountPwd: false , // 是否使用账号密码登录
   },
 
-  /**
-   * 生命周期函数--监听页面显示
-   */
   onShow() {
-    this.handleWXLogin()
-    // this.loginLogic()
+    this.setTokenExpireTime()
   },
-
-  // 处理登录条件, 如果本地有accwssToken 则直接进行登录
-  loginLogic () {
+  setTokenExpireTime () {
+    const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
+    if (util.getDate() !== tokenExpireTime) {
+      wx.clearStorageSync()
+    }
+    this.handleLogin()
+  },
+  handleLogin () {
     const accessToken = wx.getStorageSync('accessToken')
     if (accessToken) {
-      console.log('本地有token', wx.getStorageSync('device'));
-      this.localTokenLogin()
+      this.handleTokenLogin()
     } else {
       this.handleWXLogin()
     }
   },
-
-  localTokenLogin () {
-    const accessToken = wx.getStorageSync('accessToken')
-    const headImg = wx.getStorageSync('headImg')
-    const userName = wx.getStorageSync('userName')
-    const isBindWechat = wx.getStorageSync('isBindWechat')
-    const workerId = wx.getStorageSync('workerId')
-    const device = wx.getStorageSync('device')
-    const isNeedAccountPwd = wx.getStorageSync('isNeedAccountPwd')
-    app.globalData.accessToken = accessToken
-    app.globalData.userName = userName
-    app.globalData.headImg = headImg
-    app.globalData.isBindWechat = isBindWechat
-    app.globalData.workerId = workerId
-    app.globalData.device = device
-    if (isNeedAccountPwd) { // 需要账号密码登录
-      this.setData({
-        showAccountPwd: true
-      })
-    } else {
-      this.handleDeviceFn(device)
-    }
+  handleTokenLogin () {
+    this.getStorageData()
+    this.handleDeviceFn(wx.getStorageSync('device'))
   },
-
-  // 获取wx.login => code
   handleWXLogin () {
-    var that = this
-    wx.login({
-      success (res) {
-        if (res.code) {
-          //发起网络请求
-          that.setData({
-            loginCode: res.code
-          })
-          that.workerLoginFn(res.code)
-        } else {
-          wx.hideLoading()
-          wx.showModal({
-            content: '登录失败!' + res.errMsg,
-            confirmColor: '#333',
-            showCancel: false
-          })
+    const workerLoginCode = wx.getStorageSync('workerLoginCode')
+    if (workerLoginCode == '403') {
+      this.handleCode403()
+    } else {
+      var that = this
+      wx.login({
+        success (res) {
+          if (res.code) {
+            that.workerLoginFn(res.code)
+          } else {
+            wx.showModal({
+              content: '登录失败!' + res.errMsg,
+              confirmColor: '#333',
+              showCancel: false
+            })
+          }
         }
-      }
-    })
+      })
+    }
   },
-
-  // 使用wx.login > code 登录
   workerLoginFn (code) {
     const that = this
     wx.showLoading({
@@ -86,22 +63,16 @@ Page({
       mask: true
     })
     workerLogin({code: code}).then(res => {
-      // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
       wx.hideLoading()
       wx.vibrateShort()
-      that.setAppGlobalData(res.data)
+      that.setStorageData(res.data)
       that.handleDeviceFn(res.data.device)
-      wx.setStorageSync('isNeedAccountPwd', false)
+      wx.setStorageSync('workerLoginCode', '000')
     }).catch(e => {
       wx.hideLoading()
-      if (e.code == '403') { // 需要用户输入账号密码登录
-        this.setData({
-          showAccountPwd: true
-        })
-        app.globalData.isBindWechat = false
-        app.globalData.isNeedAccountPwd = true
-        wx.setStorageSync('isBindWechat', false)
-        wx.setStorageSync('isNeedAccountPwd', true)
+      if (e.code == '403') {
+        wx.setStorageSync('workerLoginCode', '403')
+        that.handleCode403()
       } else {
         wx.showModal({
           content: e.msg,
@@ -111,60 +82,22 @@ Page({
       }
     })
   },
-
-  // 判断是否绑定了微信
-  handleBindWxFn (flag) {
-    if (!flag) {
-      this.setData({
-        showAccountPwd: true
-      })
-      app.globalData.isBindWechat = false
-      wx.setStorageSync('isBindWechat', false)
-    } else {
-      that.handleDeviceFn(res.data.device)
-    }
-  },
-
-  // 判断是否绑定了设备
-  handleDeviceFn (device) {
-    if (device !== null) {
-      wx.redirectTo({
-        url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
-      })
-    } else {
-      wx.redirectTo({
-        url: '/pages/permissions/permissions',
-      })
-    }
-  },
-
-  bindusername(e) {
-    this.setData({
-      username: e.detail.value
-    })
-  },
-
-  bindUserPwd(e) {
+  handleCode403 () {
     this.setData({
-      userPwd: e.detail.value
+      showAccountPwd: true
     })
   },
-  /**
-   * 账户密码登录
-   */
-  handleLogin() {
-    var that = this
+  pwdLoginFn () {
+    const that = this
     if (this.data.username == '') {
-      wx.showModal({
-        content: '请输入账号',
-        confirmColor: '#333',
-        showCancel: false
+      wx.showToast({
+        title: '请输入账号',
+        icon: 'error'
       })
     } else if (this.data.userPwd == '') {
-      wx.showModal({
-        content: '请输入密码',
-        confirmColor: '#333',
-        showCancel: false
+      wx.showToast({
+        title: '请输入密码',
+        icon: 'error'
       })
     } else {
       var data = {
@@ -180,8 +113,8 @@ Page({
         const response = res.data
         const isBindWechat = response.isBindWechat
         const device = response.device
-        that.setAppGlobalData(response)
-        if (!isBindWechat) { // 界面加一个微信绑定的按钮
+        that.setStorageData(response)
+        if (!isBindWechat) {
           wx.showModal({
             content: '请绑定微信',
             confirmColor: '#333',
@@ -192,11 +125,12 @@ Page({
               }
             }
           })
-        } else { // 已经绑定过微信
+        } else {
           wx.vibrateShort()
           that.handleDeviceFn(device)
         }
       }).catch(e => {
+        console.log(e,'eeeee');
         wx.hideLoading()
         wx.showModal({
           content: e.msg,
@@ -206,29 +140,28 @@ Page({
       })
     }
   },
-  // 绑定微信
   bindWecahtFn (device) {
+    const that = this
     wx.showLoading({
       title: '绑定中...',
       mask: true
     })
     wx.login({
-      success (res) {
-        if (res.code) {
-          bindWecaht({code: res.code}).then(res => {
+      success (codeRes) {
+        if (codeRes.code) {
+          bindWecaht({code: codeRes.code}).then(res => {
             wx.hideLoading()
             wx.vibrateShort()
+            wx.setStorageSync('workerLoginCode', '000')
             wx.showToast({
               title: '绑定成功',
               icon: 'success'
             })
-            app.globalData.isBindWechat = true
-            wx.setStorageSync('isBindWechat', 'true')
             that.handleDeviceFn(device)
           }).catch(e => {
             wx.hideLoading()
             wx.showModal({
-              content: '绑定失败',
+              content: e.msg,
               confirmColor: '#333',
               showCancel: false
             })
@@ -237,8 +170,18 @@ Page({
       }
     })
   },
-  // 设置小程序app数据
-  setAppGlobalData (data) {
+  handleDeviceFn (device) {
+    if (device !== null) {
+      wx.redirectTo({
+        url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
+      })
+    } else {
+      wx.redirectTo({
+        url: '/pages/permissions/permissions',
+      })
+    }
+  },
+  setStorageData (data) {
     app.globalData.accessToken = data.accessToken
     app.globalData.userName = data.userName
     app.globalData.headImg = data.headImg
@@ -246,10 +189,25 @@ Page({
     app.globalData.workerId = data.workerId
     app.globalData.device = data.device
     wx.setStorageSync('accessToken', data.accessToken)
+    wx.setStorageSync('tokenExpireTime', util.getDate())
     wx.setStorageSync('headImg', data.headImg)
     wx.setStorageSync('userName', data.userName)
     wx.setStorageSync('isBindWechat', data.isBindWechat)
     wx.setStorageSync('workerId', data.workerId)
     wx.setStorageSync('device', data.device)
+  },
+  getStorageData () {
+    const accessToken = wx.getStorageSync('accessToken')
+    const headImg = wx.getStorageSync('headImg')
+    const userName = wx.getStorageSync('userName')
+    const isBindWechat = wx.getStorageSync('isBindWechat')
+    const workerId = wx.getStorageSync('workerId')
+    const device = wx.getStorageSync('device')
+    app.globalData.accessToken = accessToken
+    app.globalData.userName = userName
+    app.globalData.headImg = headImg
+    app.globalData.isBindWechat = isBindWechat
+    app.globalData.workerId = workerId
+    app.globalData.device = device
   }
 })

+ 3 - 3
pages/login/login.wxml

@@ -6,12 +6,12 @@
     </view>
     <view class="form">
       <view class="form-item account ittflex">
-        <input bindinput="bindusername" class="input wh" type="text" placeholder="请输入账号" placeholder-class="	placeholder-class"/>
+        <input model:value="{{username}}" class="input wh" type="text" placeholder="请输入账号" placeholder-class="	placeholder-class"/>
       </view>
       <view class="form-item password ittflex">
-        <input bindinput="bindUserPwd" class="input wh" type="text" password placeholder="请输入密码" placeholder-class="	placeholder-class"/>
+        <input model:value="{{userPwd}}" class="input wh" type="text" password placeholder="请输入密码" placeholder-class="	placeholder-class"/>
       </view>
     </view>
-    <view class="submit itt-btn" bindtap="handleLogin">登录</view>
+    <view class="submit itt-btn" bindtap="pwdLoginFn">登录</view>
   </view>
 </view>

+ 28 - 19
pages/permissions/permissions.js

@@ -1,6 +1,6 @@
 // pages/permissions/permissions.js
 import { bindCar } from '../../api/permission'
-const app = getApp()
+import util from '../../utils/util'
 Page({
 
   /**
@@ -17,28 +17,37 @@ Page({
    */
   onShow() {
     wx.hideHomeButton()
-    if (!app.globalData.accessToken) {
-      wx.showModal({
-        content: '请先登录',
-        confirmColor: '#333',
-        showCancel: false,
-        success (scanres) {
-          if (scanres.confirm) {
-            wx.redirectTo({
-              url: '/pages/login/login'
-            })
-          }
-        } 
+    const accessToken = wx.getStorageSync('accessToken')
+    const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
+    if (util.getDate() !== tokenExpireTime) {
+      wx.clearStorageSync()
+      wx.reLaunch({
+        url: '/pages/login/login'
       })
     } else {
-      if (this.data.reloadPage) {
-        wx.redirectTo({
-          url: '/pages/login/login'
+      if (!accessToken) {
+        wx.showModal({
+          content: '请先登录',
+          confirmColor: '#333',
+          showCancel: false,
+          success (scanres) {
+            if (scanres.confirm) {
+              wx.redirectTo({
+                url: '/pages/login/login'
+              })
+            }
+          } 
         })
       } else {
-        this.setData({
-          scanFlag: true
-        })
+        if (this.data.reloadPage) {
+          wx.redirectTo({
+            url: '/pages/login/login'
+          })
+        } else {
+          this.setData({
+            scanFlag: true
+          })
+        }
       }
     }
   },

+ 42 - 31
pages/workbench/workbench.js

@@ -1,6 +1,8 @@
 // pages/workbench/workbench.js
-import { writeOffOrder, confirmOrder } from '../../api/workbench'
+import { writeOffOrder, writeOffQuery } from '../../api/workbench'
 import { bindCar } from '../../api/permission'
+import util from '../../utils/util'
+
 const app = getApp()
 Page({
 
@@ -20,10 +22,11 @@ Page({
     orderId: '',
     orderNo: '',
     age: '',
-    useNum: ''
+    useNum: '',
+    documentId: '',
+    uuid: ''
   },
 
-
   onLoad (options) {
     this.getInitData(options)
   },
@@ -43,19 +46,28 @@ Page({
    */
   onShow() {
     wx.hideHomeButton()
-    if (!app.globalData.accessToken) {
-      wx.showModal({
-        content: '请先登录',
-        confirmColor: '#333',
-        showCancel: false,
-        success (scanres) {
-          if (scanres.confirm) {
-            wx.redirectTo({
-              url: '/pages/login/login'
-            })
-          }
-        } 
+    const accessToken = wx.getStorageSync('accessToken')
+    const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
+    if (util.getDate() !== tokenExpireTime) {
+      wx.clearStorageSync()
+      wx.reLaunch({
+        url: '/pages/login/login'
       })
+    } else {
+      if (!accessToken) {
+        wx.showModal({
+          content: '请先登录',
+          confirmColor: '#333',
+          showCancel: false,
+          success (scanres) {
+            if (scanres.confirm) {
+              wx.redirectTo({
+                url: '/pages/login/login'
+              })
+            }
+          } 
+        })
+      }
     }
   },
 
@@ -84,23 +96,27 @@ Page({
       var successScanTimer = setTimeout(() => {
         var documentId = response.split('|')[1]
         var uuid = response.split('|')[2]
-        this.writeOffOrderFn(documentId, uuid)
+        this.writeOffQueryFn(documentId, uuid)
         clearTimeout(successScanTimer)
       }, 500);
     }
   },
 
-  // 核销用户检测次数
-  writeOffOrderFn (documentId = '', uuid = '') {
+  // 获取扫描用户二维码的信息
+  writeOffQueryFn (documentId,uuid) {
     var that = this
+    wx.showLoading({
+      title: '加载中...',
+    })
     var data = {
       documentId: documentId,
       uuid: uuid
     }
-    wx.showLoading({
-      title: '加载中...',
+    that.setData({
+      documentId: documentId,
+      uuid: uuid
     })
-    writeOffOrder(data).then(res => {
+    writeOffQuery(data).then(res => {
       wx.hideLoading()
       const response = res.data
       that.setData({
@@ -123,6 +139,7 @@ Page({
     })
   },
 
+
   // 取消用户扫描结果
   handlecancel() {
     this.setData({
@@ -134,13 +151,14 @@ Page({
   handlesubmit() {
     var that =this
     var data = {
-      orderId: this.data.orderId
+      documentId:  that.data.documentId,
+      uuid: that.data.uuid
     }
     wx.showLoading({
       title: '加载中...',
       mask: true
     })
-    confirmOrder(data).then(res => {
+    writeOffOrder(data).then(res => {
       wx.hideLoading()
       wx.showToast({
         title: res.msg,
@@ -161,16 +179,9 @@ Page({
 
   // 拉起微信扫一扫,扫车辆 获取新车权限
   handleWXScanDevice () {
-    wx.redirectTo({
+    wx.navigateTo({
       url: '/pages/permissions/permissions'
     })
-    /* var that = this
-    wx.scanCode({
-      onlyFromCamera: true,
-      success (scanRes) {
-        that.handleScanDevice(scanRes.result)
-      }
-    }) */
   },
 
   // 处理扫描的设备二维码

+ 1 - 1
pages/workbench/workbench.wxml

@@ -42,7 +42,7 @@
       <view class="info count">
         <text>剩余次数:</text>
         <text class="bold"></text>
-        <text>{{useNum}}次</text>
+        <text class="use-num">{{useNum}}次</text>
       </view>
       <view class="btn ittflex-jcs">
         <view class="itt-btn cancel" bindtap="handlecancel">取消</view>

+ 4 - 0
pages/workbench/workbench.wxss

@@ -121,6 +121,10 @@
   font-size: 48rpx;
   font-weight: bold;
 }
+.use-num {
+  font-size: 32rpx;
+  font-weight: bold;
+}
 .age {
   margin-bottom: 10rpx;
 }

+ 8 - 0
utils/util.js

@@ -5,5 +5,13 @@ export default {
     return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
       return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
     })
+  },
+  // 2022-10-23
+  getDate () {
+    const date = new Date()
+    const day = date.getDate()
+    const month = date.getMonth() + 1
+    const year = date.getFullYear()
+    return year + '-' + month + '-' + day
   }
 }