Browse Source

pref: optimize the code scanning logic

wangyuan 2 năm trước cách đây
mục cha
commit
5207880ba7

BIN
images/saoma@2x.png


+ 37 - 15
pages/login/login.js

@@ -34,12 +34,19 @@ Page({
             loginCode: res.code
           })
           workerLogin({code: res.code}).then(res => {
-            // 登录成功 进入首页 todo
+            // 登录成功 进入首页 如果绑定过设备 直接进入到用户档案扫码
             wx.hideLoading()
             that.setAppGlobalData(res.data)
-            wx.redirectTo({
-              url: '/pages/permissions/permissions?form=login',
-            })
+            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()
@@ -88,7 +95,7 @@ Page({
     app.globalData.workerId = data.workerId
   },
   /**
-   * 生命周期函数--监听页面隐藏
+   * 账户密码登录
    */
   handleLogin() {
     var that = this
@@ -115,22 +122,31 @@ Page({
       })
       pwdLogin(data).then(res => {
         wx.hideLoading()
-        that.setAppGlobalData(res.data)
-        if (!res.data.isBindWechat) { // 界面加一个微信绑定的按钮
+        const response = res.data
+        const isBindWechat = response.isBindWechat
+        const device = response.device
+        that.setAppGlobalData(response)
+        if (!isBindWechat) { // 界面加一个微信绑定的按钮
           wx.showModal({
             content: '请绑定微信',
             confirmColor: '#333',
             showCancel: false,
             success (res) {
               if (res.confirm) {
-                that.bindWecahtFn()
+                that.bindWecahtFn(device)
               }
             }
           })
         } else { // 已经绑定过微信
-          wx.redirectTo({
-            url: '/pages/permissions/permissions',
-          })
+          if (device !== null) {
+            wx.redirectTo({
+              url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
+            })
+          } else {
+            wx.redirectTo({
+              url: '/pages/permissions/permissions',
+            })
+          }
         }
       }).catch(e => {
         wx.hideLoading()
@@ -143,7 +159,7 @@ Page({
     }
   },
   // 绑定微信
-  bindWecahtFn () {
+  bindWecahtFn (device) {
     wx.login({
       success (res) {
         if (res.code) {
@@ -159,9 +175,15 @@ Page({
             })
             app.globalData.isBindWechat = true
             // 进入首页 todo
-            wx.redirectTo({
-              url: '/pages/permissions/permissions',
-            })
+            if (device !== null) {
+              wx.redirectTo({
+                url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
+              })
+            } else {
+              wx.redirectTo({
+                url: '/pages/permissions/permissions',
+              })
+            }
           }).catch(e => {
             wx.hideLoading()
             wx.showModal({

+ 15 - 5
pages/permissions/permissions.js

@@ -7,7 +7,8 @@ Page({
    * 页面的初始数据
    */
   data: {
-    scanFlag: true
+    scanFlag: true,
+    scanTimer: null
   },
 
   /**
@@ -28,6 +29,10 @@ Page({
           }
         } 
       })
+    } else {
+      this.setData({
+        scanFlag: true
+      })
     }
   },
 
@@ -52,7 +57,7 @@ Page({
                   scanFlag: true
                 })
                 clearTimeout(stimer)
-              }, 400);
+              }, 600);
             }
           } 
         })
@@ -79,7 +84,7 @@ Page({
     })
     bindCar(data).then(scanres => {
       that.setData({
-        scanFlag: true
+        scanFlag: false
       })
       // 绑定成功到扫描档案页面
       wx.hideLoading()
@@ -90,7 +95,12 @@ Page({
         success (pres) {
           if (pres.confirm) {
             wx.navigateTo({
-              url: '/pages/workbench/workbench?form=peimission&deviceName=' + scanres.data.deviceName + '&deviceCode=' + scanres.data.deviceCode
+              url: '/pages/workbench/workbench?form=peimission&deviceName=' + scanres.data.deviceName + '&deviceCode=' + scanres.data.deviceCode,
+              success () {
+                that.setData({
+                  scanFlag: true
+                })
+              }
             })
           }
         }
@@ -112,7 +122,7 @@ Page({
                 scanFlag: true
               })
               clearTimeout(timer)
-            }, 400);
+            }, 600);
           }
         }
       })

+ 152 - 92
pages/workbench/workbench.js

@@ -1,5 +1,6 @@
 // pages/workbench/workbench.js
 import { writeOffOrder, confirmOrder } from '../../api/workbench'
+import { bindCar } from '../../api/permission'
 const app = getApp()
 Page({
 
@@ -9,7 +10,7 @@ Page({
   data: {
     scanFlag: true,
     overlayShow: false,
-    deviceName: '',
+    deviceName: 'XX',
     userName: '',
     headImg: '',
     workerId: '',
@@ -17,7 +18,9 @@ Page({
     personSex: true,
     birthday: '',
     orderId: '',
-    orderNo: ''
+    orderNo: '',
+    age: '',
+    useNum: ''
   },
 
 
@@ -34,13 +37,109 @@ Page({
     })
   },
 
+
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow() {
+    if (!app.globalData.accessToken) {
+      wx.showModal({
+        content: '请先登录',
+        confirmColor: '#333',
+        showCancel: false,
+        success (scanres) {
+          if (scanres.confirm) {
+            wx.redirectTo({
+              url: '/pages/login/login'
+            })
+          }
+        } 
+      })
+    }
+  },
+
+  // 拉起微信扫一扫,扫描用户档案二维码
+  handleWxScanUser () {
+    var that = this
+    wx.scanCode({
+      onlyFromCamera: true,
+      success (scanRes) {
+        that.handleScanUser(scanRes.result)
+      }
+    })
+  },
+
+  // 处理扫描用户二维码
+  handleScanUser (response) {
+    var scodeType = response.split('|')[0] == 'DOCUMENT' ? true : false
+    if (!scodeType) {
+      wx.showModal({
+        content: '请扫描正确的二维码',
+        confirmColor: '#333',
+        showCancel: false
+      })
+    } else {
+      // DOCUMENT|documentId|uuid
+      var successScanTimer = setTimeout(() => {
+        var documentId = response.split('|')[1]
+        var uuid = response.split('|')[2]
+        this.writeOffOrderFn(documentId, uuid)
+        clearTimeout(successScanTimer)
+      }, 500);
+    }
+  },
+
+  // 核销用户检测次数
+  writeOffOrderFn (documentId = '', uuid = '') {
+    var that = this
+    var data = {
+      documentId: documentId,
+      uuid: uuid
+    }
+    wx.showLoading({
+      title: '加载中...',
+    })
+    writeOffOrder(data).then(res => {
+      wx.hideLoading()
+      const response = res.data
+      if (response.useNum == 0) {
+        wx.showModal({
+          content: '该用户检测次数为0,提示充值',
+          confirmColor: '#333',
+          showCancel: false
+        })
+      } else {
+        that.setData({
+          orderNo: response.orderNo,
+          orderId: response.orderId,
+          personName: response.personName,
+          personSex: response.personSex,
+          birthday: response.birthday,
+          age: response.age,
+          useNum: response.useNum,
+          overlayShow: true
+        })
+      }
+    }).catch(e => {
+      wx.hideLoading()
+      wx.showModal({
+        content: e.msg,
+        confirmColor: '#333',
+        showCancel: false
+      })
+    })
+  },
+
+  // 取消用户扫描结果
   handlecancel() {
     this.setData({
       overlayShow: false
     })
   },
 
+  // 提交用户扫描结果
   handlesubmit() {
+    var that =this
     var data = {
       orderId: this.data.orderId
     }
@@ -54,6 +153,9 @@ Page({
         title: res.msg,
         icon: 'success'
       })
+      that.setData({
+        overlayShow: false
+      })
     }).catch(e => {
       wx.hideLoading()
       wx.showModal({
@@ -64,116 +166,74 @@ Page({
     })
   },
 
-  /**
-   * 生命周期函数--监听页面显示
-   */
-  onShow() {
-    if (!app.globalData.accessToken) {
-      wx.showModal({
-        content: '请先登录',
-        confirmColor: '#333',
-        showCancel: false,
-        success (scanres) {
-          if (scanres.confirm) {
-            wx.redirectTo({
-              url: '/pages/login/login'
-            })
-          }
-        } 
-      })
-    }
+  // 拉起微信扫一扫,扫车辆 获取新车权限
+  handleWXScanDevice () {
+    var that = this
+    wx.scanCode({
+      onlyFromCamera: true,
+      success (scanRes) {
+        that.handleScanDevice(scanRes.result)
+      }
+    })
   },
 
-  bindscancode (e) {
+  // 处理扫描的设备二维码
+  handleScanDevice (response) {
     var that = this
-    if (that.data.scanFlag) {
-      var response = e.detail.result
-      console.log(response, 'response');
-      var scodeType = response.split('|')[0] == 'DOCUMENT' ? true : false
-      if (!scodeType) {
-        that.setData({
-          scanFlag: false
-        })
-        wx.showModal({
-          content: '请扫描正确的二维码',
-          confirmColor: '#333',
-          showCancel: false,
-          success (scanres) {
-            if (scanres.confirm) {
-              that.setData({
-                scanFlag: true
-              })
-            }
-          } 
-        })
-      } else {
-        // DOCUMENT|documentId|uuid
-        var documentId = response.split('|')[1]
-        var uuid = response.split('|')[2]
-        that.writeOffOrderFn(documentId, uuid)
-      }
+    var scodeType = response.split('|')[0] == 'DEVICE' ? true : false
+    if (!scodeType) {
+      wx.showModal({
+        content: '请扫描正确的设备二维码',
+        confirmColor: '#333',
+        showCancel: false
+      })
+    } else {
+      // DEVICE|deviceCode|nonceCode|timeStamp
+      var deviceTimer = setTimeout(() => {
+        var deviceCode = response.split('|')[1]
+        var nonceCode = response.split('|')[2]
+        var timeStamp = response.split('|')[3]
+        that.bindCarFn(deviceCode, nonceCode, timeStamp)
+        clearTimeout(deviceTimer)
+      }, 500);
     }
   },
 
-  writeOffOrderFn (documentId = '', uuid = '') {
+  // 绑定设备
+  bindCarFn (deviceCode = '', nonceCode = '', timeStamp = '') {
     var that = this
     var data = {
-      documentId: documentId,
-      uuid: uuid
+      deviceCode: deviceCode,
+      nonceCode: nonceCode,
+      timeStamp: timeStamp
     }
     wx.showLoading({
-      title: '加载中...',
+      title: '绑定中...',
       mask: true
     })
-    writeOffOrder(data).then(res => {
-      console.log(res, 'writeOffOrder');
-      that.setData({
-        scanFlag: true,
-        orderNo: res.data.orderNo,
-        orderId: res.data.orderId,
-        personName: res.data.personName,
-        personSex: res.data.personSex,
-        birthday: res.data.birthday,
-        overlayShow: true
-      })
-    }).catch(e => {
-      that.setData({
-        scanFlag: false
-      })
+    bindCar(data).then(scanres => {
+      // 绑定成功到扫描档案页面
       wx.hideLoading()
       wx.showModal({
-        content: e.msg,
+        content: '权限登录成功',
         confirmColor: '#333',
         showCancel: false,
-        confirmText: '重新扫描',
-        success (res) {
-          if (res.confirm) {
-            var timer = setTimeout(() => {
-              that.setData({
-                scanFlag: true
-              })
-              clearTimeout(timer)
-            }, 400);
+        success (pres) {
+          if (pres.confirm) {
+            that.setData({
+              deviceName: scanres.data.deviceName
+            })
           }
         }
       })
+    }).catch(e => {
+      wx.hideLoading()
+      wx.showModal({
+        content: e.msg,
+        confirmColor: '#333',
+        showCancel: false
+      })
     })
   },
 
-  bindscanerror () {
-    wx.showModal({
-      content: '请打开相机权限',
-      confirmColor: '#333',
-      showCancel: false,
-      success (auth) {
-        if (auth.confirm) {
-          wx.openSetting({
-            success (res) {
-              console.log(res.authSetting)
-            }
-          })
-        }
-      }
-    })
-  }
 })

+ 9 - 12
pages/workbench/workbench.wxml

@@ -1,5 +1,6 @@
 <!--pages/workbench/workbench.wxml-->
 <view class="work-content wh">
+  <view class="notice">当前已获得{{deviceName}}检测车权限</view>
   <view class="account-info  ittflex-jcs">
     <view class="left half ittflex-jcs">
       <view class="head-img">
@@ -11,18 +12,14 @@
       </view>
     </view>
     <view class="right half">
-      <view class="text">
-        <view class="basetext">当前已获得</view>
-        <view class="basetext">{{deviceName}}检测车权限</view>
-      </view>
-      <view class="arror">
-        <image class="wh" src="../../images/you.png"></image>
-      </view>
+      <view class="itt-btn reauth-btn" data-type="1" bindtap="handleWXScanDevice">获取新车权限</view>
     </view>
   </view>
-  <view class="tips">核销用户检测次数</view>
-  <view class="camera">
-    <camera device-position="back" flash="off" mode="scanCode" bindscancode="bindscancode" binderror="bindscanerror" style="width: 100%; height: 100%;"></camera>
+  <view class="main-scan">
+    <view class="scanicon" data-type="0" bindtap="handleWxScanUser">
+      <image class="wh" src="../../images/saoma@2x.png"></image>
+    </view>
+    <view class="tips">核销用户检测次数</view>
   </view>
   <view class="overlay wh" wx:if="{{overlayShow}}">
     <view class="modal">
@@ -41,12 +38,12 @@
       </view>
       <view class="info age">
         <text>年龄:</text>
-        <text>岁</text>
+        <text>{{age}}岁</text>
       </view>
       <view class="info count">
         <text>剩余次数:</text>
         <text class="bold"></text>
-        <text>次</text>
+        <text>{{useNum}}次</text>
       </view>
       <view class="btn ittflex-jcs">
         <view class="itt-btn cancel" bindtap="handlecancel">取消</view>

+ 25 - 0
pages/workbench/workbench.wxss

@@ -4,6 +4,15 @@
   padding-top: 1rpx;
   position: relative;
 }
+.notice {
+  width: 100%;
+  height: 68rpx;
+  font-size: 26rpx;
+  color: #333333;
+  text-align: center;
+  line-height: 68rpx;
+  background-color:rgba(69, 166, 181, 0.2);
+}
 .account-info {
   width: 690rpx;
   height: 160rpx;
@@ -47,12 +56,28 @@
   justify-content: flex-end;
   align-items: center;
 }
+.reauth-btn {
+  width: 196rpx;
+  height: 60rpx;
+  background: #45A6B5;
+  border-radius: 10rpx;
+  margin-right: 20rpx;
+}
 .tips {
   width: 690rpx;
   margin: 40rpx auto 20rpx;
   font-size: 30rpx;
   font-weight: bold;
   color: #333333;
+  text-align: center;
+}
+.main-scan {
+  margin-top: 312rpx;
+}
+.scanicon {
+  width: 320rpx;
+  height: 320rpx;
+  margin: 0 auto;
 }
 .camera {
   width: 100%;