// pages/workbench/workbench.js import { writeOffOrder, writeOffQuery } from '../../api/workbench' import { bindCar } from '../../api/permission' import util from '../../utils/util' const app = getApp() Page({ /** * 页面的初始数据 */ data: { scanFlag: true, overlayShow: false, deviceName: 'XX', userName: '', headImg: '', workerId: '', personName: '', personSex: true, birthday: '', orderId: '', orderNo: '', age: '', useNum: '', documentId: '', uuid: '' }, onLoad (options) { this.getInitData(options) }, getInitData (data) { this.setData({ deviceName: data.deviceName ? data.deviceName : '', userName: app.globalData.userName, headImg: app.globalData.headImg, workerId: app.globalData.workerId }) }, /** * 生命周期函数--监听页面显示 */ onShow() { wx.hideHomeButton() 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' }) } } }) } } }, // 拉起微信扫一扫,扫描用户档案二维码 handleWxScanUser () { var that = this wx.scanCode({ onlyFromCamera: true, success (scanRes) { that.handleScanUser(scanRes.result) } }) }, // 处理扫描用户二维码 handleScanUser (response) { var scodeType = response.split('|')[0].trim() == 'DOCUMENT' ? true : false if (!scodeType) { wx.showModal({ content: '请扫描正确的二维码', confirmColor: '#333', showCancel: false }) } else { // DOCUMENT|documentId|uuid var successScanTimer = setTimeout(() => { var documentId = response.split('|')[1].trim() var uuid = response.split('|')[2].trim() this.writeOffQueryFn(documentId, uuid) clearTimeout(successScanTimer) }, 300); } }, // 获取扫描用户二维码的信息 writeOffQueryFn (documentId,uuid) { var that = this wx.showLoading({ title: '加载中...', }) var data = { documentId: documentId, uuid: uuid } that.setData({ documentId: documentId, uuid: uuid }) writeOffQuery(data).then(res => { wx.hideLoading() const response = res.data that.setData({ orderNo: response.orderNo, orderId: response.orderId, personName: response.personName, personSex: response.personSex, birthday: response.birthday.split(' ')[0], 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 = { documentId: that.data.documentId, uuid: that.data.uuid } wx.showLoading({ title: '加载中...', mask: true }) writeOffOrder(data).then(res => { wx.hideLoading() wx.showToast({ title: res.msg, icon: 'success' }) that.setData({ overlayShow: false }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e.msg, confirmColor: '#333', showCancel: false }) }) }, // 拉起微信扫一扫,扫车辆 获取新车权限 handleWXScanDevice () { wx.navigateTo({ url: '/pages/permissions/permissions' }) }, // 处理扫描的设备二维码 handleScanDevice (response) { var that = this 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) }, 300); } }, // 绑定设备 bindCarFn (deviceCode = '', nonceCode = '', timeStamp = '') { var that = this var data = { deviceCode: deviceCode, nonceCode: nonceCode, timeStamp: timeStamp } wx.showLoading({ title: '绑定中...', mask: true }) bindCar(data).then(scanres => { // 绑定成功到扫描档案页面 wx.hideLoading() wx.showModal({ content: '权限登录成功', confirmColor: '#333', showCancel: false, success (pres) { if (pres.confirm) { that.setData({ deviceName: scanres.data.deviceName }) } } }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e.msg, confirmColor: '#333', showCancel: false }) }) }, })