// pages/permissions/permissions.js import { bindCar } from '../../api/permission' import util from '../../utils/util' Page({ /** * 页面的初始数据 */ data: { scanFlag: false, scanTimer: null, reloadPage: false }, /** * 生命周期函数--监听页面显示 */ 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' }) } } }) } else { if (this.data.reloadPage) { wx.redirectTo({ url: '/pages/login/login' }) } else { this.setData({ scanFlag: true }) } } } }, bindscancode(e) { const that = this if (that.data.scanFlag) { that.setData({ scanFlag: false }) var response = e.detail.result var scodeType = response.split('|')[0].trim() == 'DEVICE' ? true : false if (!scodeType) { wx.showModal({ content: '请扫描正确的设备二维码', confirmColor: '#333', showCancel: false, success (scanres) { if (scanres.confirm) { var stimer = setTimeout(() => { that.setData({ scanFlag: true }) clearTimeout(stimer) }, 300); } } }) } else { // DEVICE|deviceCode|nonceCode|timeStamp var deviceCode = response.split('|')[1].trim() var nonceCode = response.split('|')[2].trim() var timeStamp = response.split('|')[3].trim() that.bindCarFn(deviceCode, nonceCode, timeStamp) } } }, 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.setStorageSync('device', scanres.data) wx.showModal({ content: '权限登录成功', confirmColor: '#333', showCancel: false, success (pres) { if (pres.confirm) { wx.redirectTo({ url: '/pages/workbench/workbench?form=peimission&deviceName=' + scanres.data.deviceName + '&deviceCode=' + scanres.data.deviceCode, success () { that.setData({ scanFlag: true }) } }) } } }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e.msg, confirmColor: '#333', showCancel: false, confirmText: '重新扫描', success (res) { if (res.confirm) { var timer = setTimeout(() => { that.setData({ scanFlag: true }) clearTimeout(timer) }, 300); } } }) }) }, bindscanerror () { var that = this wx.showModal({ content: '请打开相机权限', confirmColor: '#333', showCancel: false, success (auth) { if (auth.confirm) { wx.openSetting({ success (res) { console.log(res.authSetting) that.setData({ reloadPage: true }) } }) } } }) } })