permissions.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. that.setData({
  48. scanFlag: true
  49. })
  50. }
  51. }
  52. })
  53. } else {
  54. // DEVICE|deviceCode|nonceCode|timeStamp
  55. var deviceCode = response.split('|')[1]
  56. var nonceCode = response.split('|')[2]
  57. var timeStamp = response.split('|')[3]
  58. that.bindCarFn(deviceCode, nonceCode, timeStamp)
  59. }
  60. }
  61. },
  62. bindCarFn (deviceCode = '', nonceCode = '', timeStamp = '') {
  63. var that = this
  64. var data = {
  65. deviceCode: deviceCode,
  66. nonceCode: nonceCode,
  67. timeStamp: timeStamp
  68. }
  69. wx.showLoading({
  70. title: '绑定中...',
  71. mask: true
  72. })
  73. bindCar(data).then(scanres => {
  74. that.setData({
  75. scanFlag: true
  76. })
  77. // 绑定成功到扫描档案页面
  78. wx.hideLoading()
  79. wx.showModal({
  80. content: '权限登录成功',
  81. confirmColor: '#333',
  82. showCancel: false,
  83. success (pres) {
  84. if (pres.confirm) {
  85. wx.navigateTo({
  86. url: '/pages/workbench/workbench?form=peimission&deviceName=' + scanres.data.deviceName + '&deviceCode=' + scanres.data.deviceCode
  87. })
  88. }
  89. }
  90. })
  91. }).catch(e => {
  92. that.setData({
  93. scanFlag: false
  94. })
  95. wx.hideLoading()
  96. wx.showModal({
  97. content: e.msg,
  98. confirmColor: '#333',
  99. showCancel: false,
  100. confirmText: '重新扫描',
  101. success (res) {
  102. if (res.confirm) {
  103. setTimeout(() => {
  104. that.setData({
  105. scanFlag: true
  106. })
  107. }, 400);
  108. }
  109. }
  110. })
  111. })
  112. },
  113. bindscanerror () {
  114. wx.showModal({
  115. content: '请打开相机权限',
  116. confirmColor: '#333',
  117. showCancel: false,
  118. success (auth) {
  119. if (auth.confirm) {
  120. wx.openSetting({
  121. success (res) {
  122. console.log(res.authSetting)
  123. }
  124. })
  125. }
  126. }
  127. })
  128. }
  129. })