index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="page">
  3. <view v-if="loading && !qrToken" class="state">加载中…</view>
  4. <view v-else-if="loadError" class="state error">{{ loadError }}</view>
  5. <template v-else>
  6. <view class="canvas-wrap">
  7. <canvas
  8. canvas-id="identityQr"
  9. class="qr-canvas"
  10. :style="canvasStyle"
  11. :width="canvasPx"
  12. :height="canvasPx"
  13. />
  14. </view>
  15. <text class="expire-line">{{ expireHint }}</text>
  16. <text class="tip">请让核验端扫描此码。码内为加密身份令牌,请勿自行解析。</text>
  17. <view class="btn-refresh" @click="refreshQr">刷新二维码</view>
  18. </template>
  19. </view>
  20. </template>
  21. <script>
  22. import UQRCode from 'uqrcodejs'
  23. import { getToken, getIdentityQrPayload } from '../../utils/api'
  24. const CANVAS_PX = 300
  25. export default {
  26. data() {
  27. return {
  28. loading: false,
  29. loadError: '',
  30. qrToken: '',
  31. expireAtMs: 0,
  32. nowMs: Date.now(),
  33. canvasPx: CANVAS_PX,
  34. canvasReady: false,
  35. countdownTimer: null,
  36. refreshTimer: null
  37. }
  38. },
  39. computed: {
  40. canvasStyle() {
  41. const n = this.canvasPx
  42. return `width:${n}px;height:${n}px;`
  43. },
  44. expireHint() {
  45. if (!this.expireAtMs) return ''
  46. const left = Math.max(0, Math.floor((this.expireAtMs - this.nowMs) / 1000))
  47. if (left <= 0) return '已过期,请刷新'
  48. return `剩余有效时间约 ${left} 秒`
  49. }
  50. },
  51. onReady() {
  52. this.canvasReady = true
  53. this.tryDrawQr()
  54. },
  55. onShow() {
  56. this.fetchQr()
  57. },
  58. onUnload() {
  59. this.clearTimers()
  60. },
  61. methods: {
  62. clearTimers() {
  63. if (this.countdownTimer) {
  64. clearInterval(this.countdownTimer)
  65. this.countdownTimer = null
  66. }
  67. if (this.refreshTimer) {
  68. clearTimeout(this.refreshTimer)
  69. this.refreshTimer = null
  70. }
  71. },
  72. scheduleRefreshBeforeExpire() {
  73. if (this.refreshTimer) {
  74. clearTimeout(this.refreshTimer)
  75. this.refreshTimer = null
  76. }
  77. if (!this.expireAtMs) return
  78. const ms = this.expireAtMs - Date.now() - 5000
  79. if (ms < 200) return
  80. this.refreshTimer = setTimeout(() => {
  81. this.refreshTimer = null
  82. if (getToken()) this.fetchQr(true)
  83. }, ms)
  84. },
  85. startCountdown() {
  86. if (this.countdownTimer) clearInterval(this.countdownTimer)
  87. this.nowMs = Date.now()
  88. this.countdownTimer = setInterval(() => {
  89. this.nowMs = Date.now()
  90. }, 1000)
  91. },
  92. normalizePayload(raw) {
  93. if (!raw || typeof raw !== 'object') return null
  94. const o =
  95. raw.data != null && typeof raw.data === 'object' && !raw.token
  96. ? raw.data
  97. : raw
  98. const token = o.token
  99. const expiresAt = o.expires_at != null ? o.expires_at : o.expiresAt
  100. if (!token || typeof token !== 'string') return null
  101. return { token, expiresAt }
  102. },
  103. async fetchQr(isRefresh) {
  104. const token = getToken()
  105. if (!token) {
  106. this.loadError = '请先登录'
  107. return
  108. }
  109. if (!isRefresh) this.loadError = ''
  110. this.loading = true
  111. try {
  112. const raw = await getIdentityQrPayload(token)
  113. const parsed = this.normalizePayload(raw)
  114. if (!parsed) {
  115. throw new Error('接口返回无效')
  116. }
  117. this.qrToken = parsed.token
  118. this.expireAtMs = parsed.expiresAt ? new Date(parsed.expiresAt).getTime() : 0
  119. this.clearTimers()
  120. this.startCountdown()
  121. this.scheduleRefreshBeforeExpire()
  122. this.$nextTick(() => this.tryDrawQr())
  123. } catch (e) {
  124. this.qrToken = ''
  125. this.expireAtMs = 0
  126. this.loadError = (e && e.message) || '获取失败'
  127. } finally {
  128. this.loading = false
  129. }
  130. },
  131. refreshQr() {
  132. this.fetchQr(true)
  133. },
  134. tryDrawQr() {
  135. if (!this.canvasReady || !this.qrToken) return
  136. this.drawQr()
  137. },
  138. drawQr() {
  139. const text = this.qrToken
  140. const size = this.canvasPx
  141. try {
  142. const qr = new UQRCode()
  143. qr.data = text
  144. qr.size = size
  145. qr.errorCorrectLevel = UQRCode.errorCorrectLevel.L
  146. qr.margin = 4
  147. qr.make()
  148. const ctx = uni.createCanvasContext('identityQr', this)
  149. qr.canvasContext = ctx
  150. qr.drawCanvas().catch(() => {
  151. uni.showToast({ title: '二维码绘制失败', icon: 'none' })
  152. })
  153. } catch (e) {
  154. uni.showToast({ title: '二维码生成失败', icon: 'none' })
  155. }
  156. }
  157. }
  158. }
  159. </script>
  160. <style scoped>
  161. .page {
  162. min-height: 100vh;
  163. background: #f5f5f5;
  164. padding: 48rpx 32rpx 80rpx;
  165. box-sizing: border-box;
  166. display: flex;
  167. flex-direction: column;
  168. align-items: center;
  169. }
  170. .state {
  171. font-size: 28rpx;
  172. color: #666;
  173. margin-top: 120rpx;
  174. }
  175. .state.error {
  176. color: #e54d42;
  177. padding: 0 32rpx;
  178. text-align: center;
  179. line-height: 1.6;
  180. }
  181. .canvas-wrap {
  182. background: #fff;
  183. padding: 24rpx;
  184. border-radius: 16rpx;
  185. box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.06);
  186. margin-bottom: 32rpx;
  187. }
  188. .qr-canvas {
  189. display: block;
  190. }
  191. .expire-line {
  192. font-size: 28rpx;
  193. color: #259653;
  194. margin-bottom: 24rpx;
  195. }
  196. .tip {
  197. display: block;
  198. font-size: 24rpx;
  199. color: #999;
  200. line-height: 1.6;
  201. text-align: center;
  202. padding: 0 16rpx;
  203. margin-bottom: 48rpx;
  204. }
  205. .btn-refresh {
  206. background: #fff;
  207. color: #259653;
  208. font-size: 30rpx;
  209. padding: 24rpx 56rpx;
  210. border-radius: 12rpx;
  211. border: 1rpx solid #259653;
  212. }
  213. </style>