visitor_qrcode.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const { visitorQrCode } = require("/utils/api/auth_api")
  2. const app = getApp()
  3. import QRCode from "/utils/util/weapp-qrcode"
  4. Page({
  5. data() {
  6. return {
  7. popupShow: false,
  8. // 0为待获取状态,1为获取二维码显示状态
  9. status: 0,
  10. qrcodeList: [],
  11. // 选中的索引
  12. selectIndex: -1,
  13. popupImagePath: null,
  14. popupTitle: null
  15. }
  16. },
  17. authClick() {
  18. const authPopup = this.$selectComponent('#authPopup')
  19. authPopup.open('芯易行通行管理系统')
  20. },
  21. // 授权获取三要素结果
  22. authResult(e) {
  23. if(e.detail && e.detail.code === 200) {
  24. const params = {
  25. userId: e.detail.data.userId,
  26. username: e.detail.data.username,
  27. idNumber: e.detail.data.idNumber,
  28. phone: e.detail.data.phone,
  29. avatar: e.detail.data.avatar || null,
  30. personPictures: e.detail.data.personPictures || null,
  31. photoBase64: e.detail.data.photoBase64 || null
  32. }
  33. this.getVistorQrcode(e.detail.data.idNumber, e.detail.data.username, e.detail.data.phone)
  34. } else {
  35. my.alert({
  36. title: '提示',
  37. content: '身份授权认证失败!'
  38. })
  39. }
  40. },
  41. // 创建二维码
  42. creatQrCode(id, text) {
  43. return new Promise((resolve, reject) => {
  44. if(!id || !text) {
  45. reject()
  46. return
  47. }
  48. new QRCode(id, {
  49. text: text,
  50. width: 176,
  51. //canvas 画布的宽
  52. height: 176,
  53. //canvas 画布的高
  54. padding: 0,
  55. // 生成二维码四周自动留边宽度,不传入默认为0
  56. correctLevel: QRCode.CorrectLevel.L,
  57. // 二维码可辨识度
  58. callback: res => {
  59. resolve(res.path)
  60. }
  61. })
  62. })
  63. },
  64. async getVistorQrcode(idNumber, name, phone) {
  65. if(!idNumber || !name || !phone) {
  66. my.showToast({
  67. type: 'none',
  68. title: '授权获取信息补全,请重试'
  69. })
  70. return
  71. }
  72. const result = await visitorQrCode({idNumber, name, phone})
  73. this.setData({
  74. status: (result && result.data && result.data.length > 0) ? 1 : 0,
  75. selectIndex: (result && result.data && result.data.length > 0) ? 0 : -1,
  76. qrcodeList: result.data
  77. })
  78. if(!this.data.qrcodeList || this.data.qrcodeList.length <= 0) {
  79. my.showToast({
  80. type: 'none',
  81. content: '暂无访客二维码信息!'
  82. })
  83. return
  84. }
  85. if(this.data.selectIndex >= 0) {
  86. this.updateQrcode(this.data.selectIndex)
  87. }
  88. },
  89. updateQrcode(index) {
  90. const that = this
  91. setTimeout(async () => {
  92. const tempList = JSON.parse(JSON.stringify(this.data.qrcodeList))
  93. my.showLoading({
  94. content: '二维码加载中...'
  95. })
  96. for(let i = 0; i < tempList.length; i++) {
  97. if(index === i) {
  98. if(!tempList[i].ethQrcodePath && tempList[i].ethQrcode) {
  99. tempList[i].ethQrcodePath = await that.creatQrCode('qrcode1', tempList[i].ethQrcode)
  100. }
  101. if(!tempList[i].vguangQrcodePath && tempList[i].vguangQrcode) {
  102. tempList[i].vguangQrcodePath = await that.creatQrCode('qrcode2', tempList[i].vguangQrcode)
  103. }
  104. }
  105. }
  106. my.hideLoading()
  107. that.setData({
  108. qrcodeList: tempList
  109. })
  110. }, 500)
  111. },
  112. qrcodeSelectClick(e) {
  113. const type = Number(e.target.dataset.type)
  114. let popupImagePathTemp = null
  115. let popupTitleTemp = null
  116. if(type === 0) {
  117. popupImagePathTemp = this.data.qrcodeList[this.data.selectIndex].ethQrcodePath
  118. popupTitleTemp = '单机版二维码'
  119. } else if(type === 1) {
  120. popupImagePathTemp = this.data.qrcodeList[this.data.selectIndex].vguangQrcodePath
  121. popupTitleTemp = '微光互联二维码'
  122. }
  123. this.setData({
  124. popupShow: true,
  125. popupImagePath: popupImagePathTemp,
  126. popupTitle: popupTitleTemp
  127. })
  128. },
  129. onPopupClose() {
  130. this.setData({
  131. popupShow: false,
  132. popupImagePath: null,
  133. popupTitle: null
  134. })
  135. },
  136. onQrcodeSelectChange(event) {
  137. this.setData({
  138. selectIndex: event.detail.value
  139. })
  140. this.updateQrcode(this.data.selectIndex)
  141. }
  142. })