Browse Source

fix: use localstroage

wangyuan 2 years ago
parent
commit
d18226257d

+ 24 - 3
api/request.js

@@ -11,7 +11,7 @@ export const wxRequest = (url,data) => {
       header: {
         systemData:JSON.stringify({
           requestId: itt.getUUID(),
-          loginToken: app.globalData.accessToken,
+          loginToken: wx.getStorageSync('accessToken') || '',
           timestamp: new Date().getTime()
         })
       },
@@ -23,8 +23,29 @@ export const wxRequest = (url,data) => {
         if (response.statusCode !== 200) {
           reject(response.data.msg || '请求错误')
         } else {
-          if (response.data.code !== '000') {
-            reject(response.data)
+          if (response.data.code !== '000' && response.data.code !== '102') {
+            reject(response.data || '请求错误')
+          }  else if (response.data.code == '102') {
+            wx.hideLoading()
+            // 重新登录
+            wx.showModal({
+              content: response.data.msg,
+              confirmColor: '#333',
+              showCancel: false,
+              success (res) {
+                if (res.confirm) {
+                  wx.clearStorageSync()
+                  app.globalData.accessToken = ''
+                  app.globalData.userName = ''
+                  app.globalData.headImg = ''
+                  app.globalData.workerId = ''
+                  app.globalData.device = null
+                  wx.reLaunch({
+                    url: '/pages/login/login'
+                  })
+                }
+              }
+            })
           } else {
             resolve(response.data)
           }

+ 10 - 1
app.js

@@ -2,12 +2,21 @@
 App({
   onLaunch() {
     console.log('APP onLaunch');
+    const that = this
+    wx.getStorage({
+      key: 'accessToken',
+      success (res) {
+        that.globalData.accessToken = res.data
+      }
+    })
   },
   globalData: {
     isBindWechat: false,
     accessToken: '',
     workerId: '',
     userName: '',
-    headImg: ''
+    headImg: '',
+    device: null,
+    isNeedAccountPwd: false
   }
 })

+ 2 - 0
note.txt

@@ -0,0 +1,2 @@
+2022-10-20
+  1,camera组件扫码拦截,不要连续扫码

+ 122 - 69
pages/login/login.js

@@ -18,52 +18,54 @@ Page({
    */
   onShow() {
     this.handleWXLogin()
+    // this.loginLogic()
   },
-  // 触达微信登录
+
+  // 处理登录条件, 如果本地有accwssToken 则直接进行登录
+  loginLogic () {
+    const accessToken = wx.getStorageSync('accessToken')
+    if (accessToken) {
+      console.log('本地有token', wx.getStorageSync('device'));
+      this.localTokenLogin()
+    } 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)
+    }
+  },
+
+  // 获取wx.login => code
   handleWXLogin () {
     var that = this
     wx.login({
       success (res) {
         if (res.code) {
           //发起网络请求
-          wx.showLoading({
-            title: '加载中...',
-            mask: true
-          })
           that.setData({
             loginCode: res.code
           })
-          workerLogin({code: res.code}).then(res => {
-            // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
-            wx.hideLoading()
-            wx.vibrateShort()
-            that.setAppGlobalData(res.data)
-            const device = res.data.device
-            if (device !== null) {
-              wx.redirectTo({
-                url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
-              })
-            } else {
-              wx.redirectTo({
-                url: '/pages/permissions/permissions',
-              })
-            }
-          }).catch(e => {
-            // e.code == 403 未绑定微信 进入账户密码登录页面
-            wx.hideLoading()
-            console.log(e,'errot');
-            if (e.code == '403') {
-              that.setData({
-                showAccountPwd: true
-              })
-            } else {
-              wx.showModal({
-                content: e.msg,
-                confirmColor: '#333',
-                showCancel: false
-              })
-            }
-          })
+          that.workerLoginFn(res.code)
         } else {
           wx.hideLoading()
           wx.showModal({
@@ -75,6 +77,67 @@ Page({
       }
     })
   },
+
+  // 使用wx.login > code 登录
+  workerLoginFn (code) {
+    const that = this
+    wx.showLoading({
+      title: '加载中...',
+      mask: true
+    })
+    workerLogin({code: code}).then(res => {
+      // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
+      wx.hideLoading()
+      wx.vibrateShort()
+      that.setAppGlobalData(res.data)
+      that.handleDeviceFn(res.data.device)
+      wx.setStorageSync('isNeedAccountPwd', false)
+    }).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)
+      } else {
+        wx.showModal({
+          content: e.msg,
+          confirmColor: '#333',
+          showCancel: false
+        })
+      }
+    })
+  },
+
+  // 判断是否绑定了微信
+  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
@@ -86,15 +149,6 @@ Page({
       userPwd: e.detail.value
     })
   },
-
-  // 设置小程序app数据
-  setAppGlobalData (data) {
-    app.globalData.accessToken = data.accessToken
-    app.globalData.userName = data.userName
-    app.globalData.headImg = data.headImg
-    app.globalData.isBindWechat = data.isBindWechat
-    app.globalData.workerId = data.workerId
-  },
   /**
    * 账户密码登录
    */
@@ -140,15 +194,7 @@ Page({
           })
         } else { // 已经绑定过微信
           wx.vibrateShort()
-          if (device !== null) {
-            wx.redirectTo({
-              url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
-            })
-          } else {
-            wx.redirectTo({
-              url: '/pages/permissions/permissions',
-            })
-          }
+          that.handleDeviceFn(device)
         }
       }).catch(e => {
         wx.hideLoading()
@@ -162,13 +208,13 @@ Page({
   },
   // 绑定微信
   bindWecahtFn (device) {
+    wx.showLoading({
+      title: '绑定中...',
+      mask: true
+    })
     wx.login({
       success (res) {
         if (res.code) {
-          wx.showLoading({
-            title: '绑定中...',
-            mask: true
-          })
           bindWecaht({code: res.code}).then(res => {
             wx.hideLoading()
             wx.vibrateShort()
@@ -177,20 +223,12 @@ Page({
               icon: 'success'
             })
             app.globalData.isBindWechat = true
-            // 进入首页 todo
-            if (device !== null) {
-              wx.redirectTo({
-                url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
-              })
-            } else {
-              wx.redirectTo({
-                url: '/pages/permissions/permissions',
-              })
-            }
+            wx.setStorageSync('isBindWechat', 'true')
+            that.handleDeviceFn(device)
           }).catch(e => {
             wx.hideLoading()
             wx.showModal({
-              content: e.msg,
+              content: '绑定失败',
               confirmColor: '#333',
               showCancel: false
             })
@@ -198,5 +236,20 @@ Page({
         }
       }
     })
+  },
+  // 设置小程序app数据
+  setAppGlobalData (data) {
+    app.globalData.accessToken = data.accessToken
+    app.globalData.userName = data.userName
+    app.globalData.headImg = data.headImg
+    app.globalData.isBindWechat = data.isBindWechat
+    app.globalData.workerId = data.workerId
+    app.globalData.device = data.device
+    wx.setStorageSync('accessToken', data.accessToken)
+    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)
   }
 })

+ 1 - 1
pages/login/login.wxss

@@ -29,7 +29,7 @@
 .input {
   font-size: 30rpx;
   font-weight: 400;
-  color: #999999;
+  color: #333;
 }
 .submit {
   width: 590rpx;

+ 8 - 15
pages/permissions/permissions.js

@@ -7,7 +7,7 @@ Page({
    * 页面的初始数据
    */
   data: {
-    scanFlag: true,
+    scanFlag: false,
     scanTimer: null,
     reloadPage: false
   },
@@ -16,7 +16,7 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow() {
-    console.log('permission show');
+    wx.hideHomeButton()
     if (!app.globalData.accessToken) {
       wx.showModal({
         content: '请先登录',
@@ -44,15 +44,14 @@ Page({
   },
 
   bindscancode(e) {
-    var that = this
+    const that = this
     if (that.data.scanFlag) {
+      that.setData({
+        scanFlag: false
+      })
       var response = e.detail.result
-      console.log(response, 'response');
       var scodeType = response.split('|')[0] == 'DEVICE' ? true : false
       if (!scodeType) {
-        that.setData({
-          scanFlag: false
-        })
         wx.showModal({
           content: '请扫描正确的设备二维码',
           confirmColor: '#333',
@@ -64,7 +63,7 @@ Page({
                   scanFlag: true
                 })
                 clearTimeout(stimer)
-              }, 600);
+              }, 500);
             }
           } 
         })
@@ -90,9 +89,6 @@ Page({
       mask: true
     })
     bindCar(data).then(scanres => {
-      that.setData({
-        scanFlag: false
-      })
       // 绑定成功到扫描档案页面
       wx.hideLoading()
       wx.showModal({
@@ -113,9 +109,6 @@ Page({
         }
       })
     }).catch(e => {
-      that.setData({
-        scanFlag: false
-      })
       wx.hideLoading()
       wx.showModal({
         content: e.msg,
@@ -129,7 +122,7 @@ Page({
                 scanFlag: true
               })
               clearTimeout(timer)
-            }, 600);
+            }, 500);
           }
         }
       })

+ 6 - 5
pages/permissions/permissions.wxml

@@ -1,11 +1,12 @@
 <!--pages/permissions/permissions.wxml-->
 <view class="permission wh">
   <view class="canmer-box">
-    <camera device-position="back" flash="off" mode="scanCode" bindscancode="bindscancode" binderror="bindscanerror" style="width: 100%; height: 100%;"></camera>
-    <!-- <view class="top-modal"></view>
-    <view class="bottom-modal"></view>
-    <view class="left-modal"></view>
-    <view class="right-modal"></view> -->
+    <block wx:if="{{scanFlag}}">
+      <camera device-position="back" flash="off" mode="scanCode" bindscancode="bindscancode" binderror="bindscanerror" style="width: 100%; height: 100%;"></camera>
+      <view class="scanmodal">
+        <view class="line"></view>
+      </view>
+    </block>
   </view>
   <view class="actions">
     <view class="tips">教程</view>

+ 27 - 0
pages/permissions/permissions.wxss

@@ -4,6 +4,24 @@
   height: 900rpx;
   position: relative;
 }
+.scanmodal {
+  width: 100%;
+  height: 900rpx;
+  background: transparent;
+  opacity: 0.5;
+  position: absolute;
+  top: 0;
+}
+.line {
+  height: calc(100% - 2px);
+  width: 100%;
+  background: linear-gradient(180deg, rgba(0, 255, 51, 0) 43%, #45A6B5 211%);
+  border-bottom: 3px solid #45A6B5;
+  transform: translateY(-100%);
+  animation: radar-beam 2s infinite;
+  animation-timing-function: cubic-bezier(0.53, 0, 0.43, 0.99);
+  animation-delay: 1.4s;
+}
 .top-modal {
   width: 100%;
   height: 150rpx;
@@ -44,4 +62,13 @@
   color: #333333;
   text-indent: 30rpx;
   margin-top: 30rpx;
+}
+@keyframes radar-beam {
+  0% {
+      transform: translateY(-100%);
+  }
+
+  100% {
+      transform: translateY(0);
+  }
 }

+ 6 - 2
pages/workbench/workbench.js

@@ -42,6 +42,7 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow() {
+    wx.hideHomeButton()
     if (!app.globalData.accessToken) {
       wx.showModal({
         content: '请先登录',
@@ -168,13 +169,16 @@ Page({
 
   // 拉起微信扫一扫,扫车辆 获取新车权限
   handleWXScanDevice () {
-    var that = this
+    wx.redirectTo({
+      url: '/pages/permissions/permissions'
+    })
+    /* var that = this
     wx.scanCode({
       onlyFromCamera: true,
       success (scanRes) {
         that.handleScanDevice(scanRes.result)
       }
-    })
+    }) */
   },
 
   // 处理扫描的设备二维码