permissions.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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] == '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]
  79. var nonceCode = response.split('|')[2]
  80. var timeStamp = response.split('|')[3]
  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.showModal({
  100. content: '权限登录成功',
  101. confirmColor: '#333',
  102. showCancel: false,
  103. success (pres) {
  104. if (pres.confirm) {
  105. wx.redirectTo({
  106. url: '/pages/workbench/workbench?form=peimission&deviceName=' + scanres.data.deviceName + '&deviceCode=' + scanres.data.deviceCode,
  107. success () {
  108. that.setData({
  109. scanFlag: true
  110. })
  111. }
  112. })
  113. }
  114. }
  115. })
  116. }).catch(e => {
  117. wx.hideLoading()
  118. wx.showModal({
  119. content: e.msg,
  120. confirmColor: '#333',
  121. showCancel: false,
  122. confirmText: '重新扫描',
  123. success (res) {
  124. if (res.confirm) {
  125. var timer = setTimeout(() => {
  126. that.setData({
  127. scanFlag: true
  128. })
  129. clearTimeout(timer)
  130. }, 300);
  131. }
  132. }
  133. })
  134. })
  135. },
  136. bindscanerror () {
  137. var that = this
  138. wx.showModal({
  139. content: '请打开相机权限',
  140. confirmColor: '#333',
  141. showCancel: false,
  142. success (auth) {
  143. if (auth.confirm) {
  144. wx.openSetting({
  145. success (res) {
  146. console.log(res.authSetting)
  147. that.setData({
  148. reloadPage: true
  149. })
  150. }
  151. })
  152. }
  153. }
  154. })
  155. }
  156. })