permissions.js 3.4 KB

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