visitor_qrcode.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // 0为待获取状态,1为获取二维码显示状态
  8. status: 0,
  9. qrcodeList: [],
  10. // 选中的索引
  11. selectIndex: -1,
  12. // 显示的二维码,0表示单机二维码,1表示联机二维码
  13. showQrCodePosition: 0,
  14. // 账号id
  15. adminId: null,
  16. qrcodeTitles: ['单机二维码', '联机二维码']
  17. }
  18. },
  19. onLoad() {
  20. let launchOptions = my.getLaunchOptionsSync()
  21. if(!launchOptions.query) {
  22. launchOptions = my.getEnterOptionsSync()
  23. }
  24. // my.alert({
  25. // title: "launchOptions",
  26. // content: JSON.stringify(launchOptions)
  27. // });
  28. if(launchOptions.query && launchOptions.query.a) {
  29. let id = null
  30. try {
  31. id = Number(launchOptions.query.a)
  32. } catch (err) {
  33. id = null
  34. }
  35. this.setData({
  36. adminId: id
  37. })
  38. }
  39. },
  40. authClick() {
  41. const authPopup = this.$selectComponent('#authPopup')
  42. authPopup.open('芯易行通行管理系统')
  43. },
  44. // 授权获取三要素结果
  45. authResult(e) {
  46. if(e.detail && e.detail.code === 200) {
  47. // const params = {
  48. // userId: e.detail.data.userId,
  49. // username: e.detail.data.username,
  50. // idNumber: e.detail.data.idNumber,
  51. // phone: e.detail.data.phone,
  52. // avatar: e.detail.data.avatar || null,
  53. // personPictures: e.detail.data.personPictures || null,
  54. // photoBase64: e.detail.data.photoBase64 || null
  55. // }
  56. this.getVistorQrcode(e.detail.data.idNumber, e.detail.data.username, e.detail.data.phone)
  57. } else {
  58. my.alert({
  59. title: '提示',
  60. content: '身份授权认证失败!'
  61. })
  62. }
  63. },
  64. // 创建二维码
  65. creatQrCode(id, text) {
  66. return new Promise((resolve, reject) => {
  67. if(!id || !text) {
  68. reject()
  69. return
  70. }
  71. new QRCode(id, {
  72. text: text,
  73. width: 250,
  74. //canvas 画布的宽
  75. height: 250,
  76. //canvas 画布的高
  77. padding: 0,
  78. // 生成二维码四周自动留边宽度,不传入默认为0
  79. correctLevel: QRCode.CorrectLevel.L,
  80. // 二维码可辨识度
  81. callback: res => {
  82. resolve(res.path)
  83. }
  84. })
  85. })
  86. },
  87. async getVistorQrcode(idNumber, name, phone) {
  88. if(!idNumber || !name || !phone) {
  89. my.showToast({
  90. type: 'none',
  91. title: '授权获取信息补全,请重试'
  92. })
  93. return
  94. }
  95. const result = await visitorQrCode({idNumber, name, phone, adminId: this.data.adminId})
  96. this.setData({
  97. status: (result && result.data && result.data.length > 0) ? 1 : 0,
  98. selectIndex: (result && result.data && result.data.length > 0) ? 0 : -1,
  99. qrcodeList: result.data ? result.data.map(item => {
  100. const title = []
  101. const qrcode = []
  102. if(item.ethQrcode) {
  103. title.push(item.zfbMiniEthTitle || this.data.qrcodeTitles[0])
  104. qrcode.push(item.ethQrcode)
  105. }
  106. if(item.vguangQrcode) {
  107. title.push(item.zfbMiniVguangTitle || this.data.qrcodeTitles[1])
  108. qrcode.push(item.vguangQrcode)
  109. }
  110. item.titles = title
  111. item.qrcodes = qrcode
  112. item.qrcodesPath = []
  113. return item
  114. }) : []
  115. })
  116. if(!this.data.qrcodeList || this.data.qrcodeList.length <= 0) {
  117. my.showToast({
  118. type: 'none',
  119. content: '暂无访客二维码信息!'
  120. })
  121. return
  122. }
  123. if(this.data.selectIndex >= 0) {
  124. this.updateQrcode(this.data.selectIndex)
  125. }
  126. },
  127. updateQrcode(index) {
  128. const that = this
  129. setTimeout(async () => {
  130. const tempList = JSON.parse(JSON.stringify(this.data.qrcodeList))
  131. my.showLoading({
  132. content: '二维码加载中...'
  133. })
  134. for(let i = 0; i < tempList.length; i++) {
  135. if(index === i) {
  136. if(!tempList[i].qrcodes || tempList[i].qrcodes.length <= 0) {
  137. break
  138. }
  139. if(tempList[i].qrcodesPath && tempList[i].qrcodesPath.length > 0) {
  140. break
  141. }
  142. for(let j = 0; j < tempList[i].qrcodes.length; j++) {
  143. const qrcodePath = await that.creatQrCode('qrcode', tempList[i].qrcodes[j])
  144. tempList[i].qrcodesPath.push(qrcodePath)
  145. }
  146. }
  147. }
  148. my.hideLoading()
  149. that.setData({
  150. showQrCodePosition: 0,
  151. qrcodeList: tempList
  152. })
  153. }, 500)
  154. },
  155. onQrcodeSelectChange(event) {
  156. this.setData({
  157. selectIndex: event.detail.value
  158. })
  159. this.updateQrcode(this.data.selectIndex)
  160. },
  161. onQrcodeTabsChange(event) {
  162. const type = event.currentTarget.dataset.type
  163. if(this.data.showQrCodePosition === type) {
  164. return
  165. }
  166. this.setData({
  167. showQrCodePosition: type
  168. })
  169. }
  170. })