permissions.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // pages/permissions/permissions.js
  2. import { bindCar } from '../../api/permission'
  3. import util from '../../utils/util'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. scanFlag: false,
  10. scanTimer: null,
  11. reloadPage: false
  12. },
  13. /**
  14. * 生命周期函数--监听页面显示
  15. */
  16. onShow() {
  17. wx.hideHomeButton()
  18. const accessToken = wx.getStorageSync('accessToken')
  19. const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
  20. if (util.getDate() !== tokenExpireTime) {
  21. wx.clearStorageSync()
  22. wx.reLaunch({
  23. url: '/pages/login/login'
  24. })
  25. } else {
  26. if (!accessToken) {
  27. wx.showModal({
  28. content: '请先登录',
  29. confirmColor: '#333',
  30. showCancel: false,
  31. success (scanres) {
  32. if (scanres.confirm) {
  33. wx.redirectTo({
  34. url: '/pages/login/login'
  35. })
  36. }
  37. }
  38. })
  39. } else {
  40. if (this.data.reloadPage) {
  41. wx.redirectTo({
  42. url: '/pages/login/login'
  43. })
  44. } else {
  45. this.setData({
  46. scanFlag: true
  47. })
  48. }
  49. }
  50. }
  51. },
  52. bindscancode(e) {
  53. const that = this
  54. if (that.data.scanFlag) {
  55. that.setData({
  56. scanFlag: false
  57. })
  58. var response = e.detail.result
  59. var scodeType = response.split('|')[0].trim() == 'DEVICE' ? true : false
  60. if (!scodeType) {
  61. wx.showModal({
  62. content: '请扫描正确的设备二维码',
  63. confirmColor: '#333',
  64. showCancel: false,
  65. success (scanres) {
  66. if (scanres.confirm) {
  67. var stimer = setTimeout(() => {
  68. that.setData({
  69. scanFlag: true
  70. })
  71. clearTimeout(stimer)
  72. }, 300);
  73. }
  74. }
  75. })
  76. } else {
  77. // DEVICE|deviceCode|nonceCode|timeStamp
  78. var deviceCode = response.split('|')[1].trim()
  79. var nonceCode = response.split('|')[2].trim()
  80. var timeStamp = response.split('|')[3].trim()
  81. that.bindCarFn(deviceCode, nonceCode, timeStamp)
  82. }
  83. }
  84. },
  85. bindCarFn (deviceCode = '', nonceCode = '', timeStamp = '') {
  86. var that = this
  87. var data = {
  88. deviceCode: deviceCode,
  89. nonceCode: nonceCode,
  90. timeStamp: timeStamp
  91. }
  92. wx.showLoading({
  93. title: '绑定中...',
  94. mask: true
  95. })
  96. bindCar(data).then(scanres => {
  97. // 绑定成功到扫描档案页面
  98. wx.hideLoading()
  99. wx.setStorageSync('device', scanres.data)
  100. wx.showModal({
  101. content: '权限登录成功',
  102. confirmColor: '#333',
  103. showCancel: false,
  104. success (pres) {
  105. if (pres.confirm) {
  106. wx.redirectTo({
  107. url: '/pages/workbench/workbench?form=peimission&deviceName=' + scanres.data.deviceName + '&deviceCode=' + scanres.data.deviceCode,
  108. success () {
  109. that.setData({
  110. scanFlag: true
  111. })
  112. }
  113. })
  114. }
  115. }
  116. })
  117. }).catch(e => {
  118. wx.hideLoading()
  119. wx.showModal({
  120. content: e.msg,
  121. confirmColor: '#333',
  122. showCancel: false,
  123. confirmText: '重新扫描',
  124. success (res) {
  125. if (res.confirm) {
  126. var timer = setTimeout(() => {
  127. that.setData({
  128. scanFlag: true
  129. })
  130. clearTimeout(timer)
  131. }, 300);
  132. }
  133. }
  134. })
  135. })
  136. },
  137. bindscanerror () {
  138. var that = this
  139. wx.showModal({
  140. content: '请打开相机权限',
  141. confirmColor: '#333',
  142. showCancel: false,
  143. success (auth) {
  144. if (auth.confirm) {
  145. wx.openSetting({
  146. success (res) {
  147. console.log(res.authSetting)
  148. that.setData({
  149. reloadPage: true
  150. })
  151. }
  152. })
  153. }
  154. }
  155. })
  156. }
  157. })