parse.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import Base64 from "./base64.js"
  2. import store from "../store/index.js"
  3. export default class Parse {
  4. // private property
  5. static getInstance() {
  6. if (!this.instance) {
  7. this.instance = new Parse();
  8. }
  9. return this.instance
  10. }
  11. info = {}
  12. // public method for decoding
  13. _processor(res) {
  14. const base64 = Base64.getInstance();
  15. console.log(res)
  16. let data = res.replace("https://goto.huijy.net/", "")
  17. let stationId
  18. let employeeId
  19. let labelId
  20. let info = {
  21. stationId: undefined,
  22. labelId: undefined,
  23. employeeId: undefined
  24. }
  25. const idArr = data.match(/^\d{1,}/g)
  26. if (idArr === null) {
  27. throw new Error("二维码是个啥玩意?!")
  28. }
  29. // 处理后的data
  30. data = data.replace(/^\d{1,}\//g, '')
  31. // 解压后的data
  32. data = base64.decode(data)
  33. // 员工id
  34. if (data.includes('e')) {
  35. const employeeIdStrArr = data.match(/e\d{1,}/g)
  36. if (employeeIdStrArr === null) {
  37. return
  38. }
  39. const employeeIdArr = employeeIdStrArr[0].match(/\d{1,}/g)
  40. if (employeeIdArr === null) {
  41. return
  42. }
  43. employeeId = employeeIdArr[0]
  44. this.info.employeeId = employeeId
  45. store.commit('updateEmployeeId', employeeId);
  46. }
  47. // 标签id
  48. if (data.includes('l')) {
  49. const labelIdStrArr = data.match(/l\d{1,}/g)
  50. if (labelIdStrArr === null) {
  51. return
  52. }
  53. const labelIdArr = labelIdStrArr[0].match(/\d{1,}/g)
  54. if (labelIdArr === null) {
  55. return
  56. }
  57. labelId = labelIdArr[0]
  58. this.info.labelId = labelId
  59. store.commit("updateLabelId", labelId)
  60. }
  61. stationId = idArr[0]
  62. this.info.stationId = stationId
  63. store.commit("updateStationId", stationId)
  64. return;
  65. }
  66. async parseCode(res) {
  67. try {
  68. this._processor(res)
  69. return Promise.resolve(this.info)
  70. } catch (e) {
  71. return Promise.reject("亲,请扫油站提供的码呦~")
  72. }
  73. }
  74. async _scanCode() {
  75. try {
  76. const scanCodePro = await uni.scanCode({
  77. onlyFromCamera: true
  78. })
  79. if (scanCodePro[0] != null && scanCodePro[0].errMsg == "scanCode:fail") {
  80. throw new Error(1)
  81. }
  82. if (scanCodePro[0] != null) {
  83. throw new Error(2)
  84. }
  85. return this.parseCode(scanCodePro[1].result)
  86. // return Promise.resolve(scanCodePro[1].result)
  87. } catch (e) {
  88. if(e.message == 1){
  89. return Promise.reject("亲,请扫油站提供的码呦~")
  90. }else{
  91. return Promise.reject("亲,请扫二维码呀~")
  92. }
  93. // const showModalPro = await uni.showModal({
  94. // title: '( •̀ ω •́ )✧',
  95. // showCancel: false,
  96. // content: '请扫描二维码呦',
  97. // confirmText: '重新扫码',
  98. // })
  99. // return this.scanCode();
  100. }
  101. }
  102. async scanCode(showModalFlag = true, moduleConfig = {}) { //msg 消息
  103. let showModalPro = [
  104. null,
  105. {
  106. cancel: false,
  107. confirm: true
  108. }
  109. ]
  110. if (showModalFlag) {
  111. showModalPro = await uni.showModal({
  112. title: moduleConfig.title || '(~ ̄▽ ̄)~',
  113. showCancel: moduleConfig.showCancel,
  114. content: moduleConfig.content || '亲,请扫码使用~',
  115. cancelText: moduleConfig.cancelText || '返回',
  116. confirmText: moduleConfig.confirmText || '立刻扫码',
  117. })
  118. }
  119. if (showModalPro[1].cancel) {
  120. uni.navigateBack({
  121. delta: 1
  122. })
  123. return Promise.reject("返回")
  124. } else if (showModalPro[1].confirm) {
  125. return this._scanCode().catch((errMsg) => {
  126. console.log(errMsg)
  127. return this.scanCode(true, {
  128. content: errMsg
  129. })
  130. });
  131. }
  132. }
  133. }