permissions.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // pages/permissions/permissions.js
  2. import { bindCar } from '../../api/permission'
  3. const app = getApp()
  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. 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. const that = this
  45. if (that.data.scanFlag) {
  46. that.setData({
  47. scanFlag: false
  48. })
  49. var response = e.detail.result
  50. var scodeType = response.split('|')[0] == 'DEVICE' ? true : false
  51. if (!scodeType) {
  52. wx.showModal({
  53. content: '请扫描正确的设备二维码',
  54. confirmColor: '#333',
  55. showCancel: false,
  56. success (scanres) {
  57. if (scanres.confirm) {
  58. var stimer = setTimeout(() => {
  59. that.setData({
  60. scanFlag: true
  61. })
  62. clearTimeout(stimer)
  63. }, 500);
  64. }
  65. }
  66. })
  67. } else {
  68. // DEVICE|deviceCode|nonceCode|timeStamp
  69. var deviceCode = response.split('|')[1]
  70. var nonceCode = response.split('|')[2]
  71. var timeStamp = response.split('|')[3]
  72. that.bindCarFn(deviceCode, nonceCode, timeStamp)
  73. }
  74. }
  75. },
  76. bindCarFn (deviceCode = '', nonceCode = '', timeStamp = '') {
  77. var that = this
  78. var data = {
  79. deviceCode: deviceCode,
  80. nonceCode: nonceCode,
  81. timeStamp: timeStamp
  82. }
  83. wx.showLoading({
  84. title: '绑定中...',
  85. mask: true
  86. })
  87. bindCar(data).then(scanres => {
  88. // 绑定成功到扫描档案页面
  89. wx.hideLoading()
  90. wx.showModal({
  91. content: '权限登录成功',
  92. confirmColor: '#333',
  93. showCancel: false,
  94. success (pres) {
  95. if (pres.confirm) {
  96. wx.navigateTo({
  97. url: '/pages/workbench/workbench?form=peimission&deviceName=' + scanres.data.deviceName + '&deviceCode=' + scanres.data.deviceCode,
  98. success () {
  99. that.setData({
  100. scanFlag: true
  101. })
  102. }
  103. })
  104. }
  105. }
  106. })
  107. }).catch(e => {
  108. wx.hideLoading()
  109. wx.showModal({
  110. content: e.msg,
  111. confirmColor: '#333',
  112. showCancel: false,
  113. confirmText: '重新扫描',
  114. success (res) {
  115. if (res.confirm) {
  116. var timer = setTimeout(() => {
  117. that.setData({
  118. scanFlag: true
  119. })
  120. clearTimeout(timer)
  121. }, 500);
  122. }
  123. }
  124. })
  125. })
  126. },
  127. bindscanerror () {
  128. var that = this
  129. wx.showModal({
  130. content: '请打开相机权限',
  131. confirmColor: '#333',
  132. showCancel: false,
  133. success (auth) {
  134. if (auth.confirm) {
  135. wx.openSetting({
  136. success (res) {
  137. console.log(res.authSetting)
  138. that.setData({
  139. reloadPage: true
  140. })
  141. }
  142. })
  143. }
  144. }
  145. })
  146. }
  147. })