permissions.js 3.7 KB

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