permissions.js 3.2 KB

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