workbench.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // pages/workbench/workbench.js
  2. import { writeOffOrder, writeOffQuery } from '../../api/workbench'
  3. import { bindCar } from '../../api/permission'
  4. import util from '../../utils/util'
  5. const app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. scanFlag: true,
  12. overlayShow: false,
  13. deviceName: 'XX',
  14. userName: '',
  15. headImg: '',
  16. workerId: '',
  17. personName: '',
  18. personSex: true,
  19. birthday: '',
  20. orderId: '',
  21. orderNo: '',
  22. age: '',
  23. useNum: '',
  24. documentId: '',
  25. uuid: ''
  26. },
  27. onLoad (options) {
  28. this.getInitData(options)
  29. },
  30. getInitData (data) {
  31. this.setData({
  32. deviceName: data.deviceName ? data.deviceName : '',
  33. userName: app.globalData.userName,
  34. headImg: app.globalData.headImg,
  35. workerId: app.globalData.workerId
  36. })
  37. },
  38. /**
  39. * 生命周期函数--监听页面显示
  40. */
  41. onShow() {
  42. wx.hideHomeButton()
  43. const accessToken = wx.getStorageSync('accessToken')
  44. const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
  45. if (util.getDate() !== tokenExpireTime) {
  46. wx.clearStorageSync()
  47. wx.reLaunch({
  48. url: '/pages/login/login'
  49. })
  50. } else {
  51. if (!accessToken) {
  52. wx.showModal({
  53. content: '请先登录',
  54. confirmColor: '#333',
  55. showCancel: false,
  56. success (scanres) {
  57. if (scanres.confirm) {
  58. wx.redirectTo({
  59. url: '/pages/login/login'
  60. })
  61. }
  62. }
  63. })
  64. }
  65. }
  66. },
  67. // 拉起微信扫一扫,扫描用户档案二维码
  68. handleWxScanUser () {
  69. var that = this
  70. wx.scanCode({
  71. onlyFromCamera: true,
  72. success (scanRes) {
  73. that.handleScanUser(scanRes.result)
  74. }
  75. })
  76. },
  77. // 处理扫描用户二维码
  78. handleScanUser (response) {
  79. var scodeType = response.split('|')[0].trim() == 'DOCUMENT' ? true : false
  80. if (!scodeType) {
  81. wx.showModal({
  82. content: '请扫描正确的二维码',
  83. confirmColor: '#333',
  84. showCancel: false
  85. })
  86. } else {
  87. // DOCUMENT|documentId|uuid
  88. var successScanTimer = setTimeout(() => {
  89. var documentId = response.split('|')[1].trim()
  90. var uuid = response.split('|')[2].trim()
  91. this.writeOffQueryFn(documentId, uuid)
  92. clearTimeout(successScanTimer)
  93. }, 300);
  94. }
  95. },
  96. // 获取扫描用户二维码的信息
  97. writeOffQueryFn (documentId,uuid) {
  98. var that = this
  99. wx.showLoading({
  100. title: '加载中...',
  101. })
  102. var data = {
  103. documentId: documentId,
  104. uuid: uuid
  105. }
  106. that.setData({
  107. documentId: documentId,
  108. uuid: uuid
  109. })
  110. writeOffQuery(data).then(res => {
  111. wx.hideLoading()
  112. const response = res.data
  113. that.setData({
  114. orderNo: response.orderNo,
  115. orderId: response.orderId,
  116. personName: response.personName,
  117. personSex: response.personSex,
  118. birthday: response.birthday.split(' ')[0],
  119. age: response.age,
  120. useNum: response.useNum,
  121. overlayShow: true
  122. })
  123. }).catch(e => {
  124. wx.hideLoading()
  125. wx.showModal({
  126. content: e.msg,
  127. confirmColor: '#333',
  128. showCancel: false
  129. })
  130. })
  131. },
  132. // 取消用户扫描结果
  133. handlecancel() {
  134. this.setData({
  135. overlayShow: false
  136. })
  137. },
  138. // 提交用户扫描结果
  139. handlesubmit() {
  140. var that =this
  141. var data = {
  142. documentId: that.data.documentId,
  143. uuid: that.data.uuid
  144. }
  145. wx.showLoading({
  146. title: '加载中...',
  147. mask: true
  148. })
  149. writeOffOrder(data).then(res => {
  150. wx.hideLoading()
  151. wx.showToast({
  152. title: res.msg,
  153. icon: 'success'
  154. })
  155. that.setData({
  156. overlayShow: false
  157. })
  158. }).catch(e => {
  159. wx.hideLoading()
  160. wx.showModal({
  161. content: e.msg,
  162. confirmColor: '#333',
  163. showCancel: false
  164. })
  165. })
  166. },
  167. // 拉起微信扫一扫,扫车辆 获取新车权限
  168. handleWXScanDevice () {
  169. wx.navigateTo({
  170. url: '/pages/permissions/permissions'
  171. })
  172. },
  173. // 处理扫描的设备二维码
  174. handleScanDevice (response) {
  175. var that = this
  176. var scodeType = response.split('|')[0] == 'DEVICE' ? true : false
  177. if (!scodeType) {
  178. wx.showModal({
  179. content: '请扫描正确的设备二维码',
  180. confirmColor: '#333',
  181. showCancel: false
  182. })
  183. } else {
  184. // DEVICE|deviceCode|nonceCode|timeStamp
  185. var deviceTimer = setTimeout(() => {
  186. var deviceCode = response.split('|')[1]
  187. var nonceCode = response.split('|')[2]
  188. var timeStamp = response.split('|')[3]
  189. that.bindCarFn(deviceCode, nonceCode, timeStamp)
  190. clearTimeout(deviceTimer)
  191. }, 300);
  192. }
  193. },
  194. // 绑定设备
  195. bindCarFn (deviceCode = '', nonceCode = '', timeStamp = '') {
  196. var that = this
  197. var data = {
  198. deviceCode: deviceCode,
  199. nonceCode: nonceCode,
  200. timeStamp: timeStamp
  201. }
  202. wx.showLoading({
  203. title: '绑定中...',
  204. mask: true
  205. })
  206. bindCar(data).then(scanres => {
  207. // 绑定成功到扫描档案页面
  208. wx.hideLoading()
  209. wx.showModal({
  210. content: '权限登录成功',
  211. confirmColor: '#333',
  212. showCancel: false,
  213. success (pres) {
  214. if (pres.confirm) {
  215. that.setData({
  216. deviceName: scanres.data.deviceName
  217. })
  218. }
  219. }
  220. })
  221. }).catch(e => {
  222. wx.hideLoading()
  223. wx.showModal({
  224. content: e.msg,
  225. confirmColor: '#333',
  226. showCancel: false
  227. })
  228. })
  229. },
  230. })