scan.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="scan-page">
  3. <view class="scan-card">
  4. <text class="scan-title">现场签到</text>
  5. <text class="scan-sub">请对准讲师/带教展示的签到二维码</text>
  6. <!-- MP 端:原生扫码 -->
  7. <!-- #ifdef MP-WEIXIN -->
  8. <view class="scan-icon">📷</view>
  9. <button class="big-btn scan-btn" :disabled="busy" @click="openScanner">
  10. {{ busy ? '处理中...' : '扫码签到' }}
  11. </button>
  12. <text class="scan-alt" @click="toggleManual">手动输入签到码</text>
  13. <!-- #endif -->
  14. <!-- 手动输入(H5 降级 + MP 备用) -->
  15. <view v-if="manual" class="manual-box">
  16. <view class="field-row">
  17. <text class="field-label">签到码</text>
  18. <input class="field-input" v-model="inputCode" placeholder="请输入签到码" maxlength="32" />
  19. </view>
  20. <button class="big-btn submit-btn" :disabled="busy" @click="submitManual">
  21. {{ busy ? '处理中...' : '提交签到' }}
  22. </button>
  23. </view>
  24. <text class="scan-tip">· 扫码即完成签到,学时将在 T+3 自动生效</text>
  25. <text class="scan-tip">· 同一场次重复扫码不会重复计学时</text>
  26. </view>
  27. <!-- 结果覆盖层 -->
  28. <view v-if="result" class="result-mask">
  29. <view class="result-card">
  30. <text class="result-icon">{{ resultIcon }}</text>
  31. <text class="result-title">{{ result.title }}</text>
  32. <text class="result-desc">{{ result.desc }}</text>
  33. <button class="big-btn result-btn" @click="closeResult">{{ result.closeText }}</button>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import { attendanceScan } from '@/api/attendance.js'
  40. import { guardPage, guardWorker } from '@/utils/guard.js'
  41. export default {
  42. data() {
  43. return {
  44. authorized: false,
  45. busy: false,
  46. manual: false,
  47. hasSessionId: false,
  48. sessionId: '',
  49. inputSessionId: '',
  50. inputCode: '',
  51. result: null,
  52. lastCode: '',
  53. lastTime: 0
  54. }
  55. },
  56. computed: {
  57. resultIcon() {
  58. if (!this.result) return ''
  59. var map = { success: '✅', dupe: 'ℹ️', invalid: '⚠️', error: '❌' }
  60. return map[this.result.type] || ''
  61. }
  62. },
  63. onLoad(options) {
  64. var self = this
  65. if (!guardPage()) return
  66. // 反扫入口:?sessionId=xxx(场次由讲师/带教页面带出)
  67. if (options && options.sessionId) {
  68. this.sessionId = String(options.sessionId)
  69. this.hasSessionId = true
  70. }
  71. // H5 端 uni.scanCode 不可用:默认展示手动输入表单(降级)
  72. // #ifndef MP-WEIXIN
  73. this.manual = true
  74. // #endif
  75. // 双身份隔离:仅健康服务人员可用签到动作;守卫通过后再唤起扫码
  76. guardWorker().then(function(ok) {
  77. self.authorized = ok
  78. if (!ok) return
  79. // #ifdef MP-WEIXIN
  80. self.openScanner()
  81. // #endif
  82. })
  83. },
  84. onShow() {
  85. // MP 端进入页面自动唤起扫码(仅守卫已放行时,避免与 onLoad 的守卫竞态重复唤起)
  86. // #ifdef MP-WEIXIN
  87. if (this.authorized) {
  88. this.openScanner()
  89. }
  90. // #endif
  91. },
  92. methods: {
  93. openScanner() {
  94. var self = this
  95. uni.scanCode({
  96. scanType: ['qrCode'],
  97. success: function(res) {
  98. var content = (res && res.result) ? String(res.result).trim() : ''
  99. if (!content) return
  100. self.parseAndSubmit(content)
  101. },
  102. fail: function() {
  103. // 用户取消或相机不可用:回落到手动输入
  104. self.manual = true
  105. }
  106. })
  107. },
  108. toggleManual() {
  109. this.manual = !this.manual
  110. },
  111. submitManual() {
  112. var code = (this.inputCode || '').trim()
  113. if (!code) {
  114. uni.showToast({ title: '请输入签到码', icon: 'none' })
  115. return
  116. }
  117. this.submit(code)
  118. },
  119. // 解析扫码内容:后端 scan 入参仅 {code},场次由签到码反查推导。
  120. // 兼容复合内容 sessionId|code(取末段码),纯码直接使用。
  121. parseAndSubmit(content) {
  122. var code = content
  123. if (content.indexOf('|') >= 0) {
  124. var parts = content.split('|')
  125. code = (parts[parts.length - 1] || '').trim()
  126. }
  127. if (!code) return
  128. this.submit(code)
  129. },
  130. submit(code) {
  131. var self = this
  132. // 30s 防抖:同一签到码 30s 内不重复提交(服务端幂等兜底为「已签到」)
  133. var now = Date.now()
  134. if (self.lastCode === code && now - self.lastTime < 30000) {
  135. self.showResult('dupe', '已提交,请勿重复操作', '稍后可在学时账户查看入账情况', '知道了')
  136. return
  137. }
  138. self.busy = true
  139. self.lastCode = code
  140. self.lastTime = now
  141. attendanceScan(code).then(function(res) {
  142. self.busy = false
  143. var data = (res && res.data) ? res.data : {}
  144. // alreadyExists=true / attended=true → 本场次已签到过(后端不重复入账)
  145. if (data.alreadyExists === true || data.attended === true) {
  146. self.showResult('dupe', '已签到', '本场次已签到,无需重复操作', '知道了')
  147. return
  148. }
  149. var time = data.time || data.attendedAt || ''
  150. self.showResult('success', '签到成功', '✓ 学时待 T+3 生效' + (time ? '(' + time + ')' : ''), '完成')
  151. }).catch(function(err) {
  152. self.busy = false
  153. var msg = (err && err.message) || ''
  154. if (msg.indexOf('已签到') >= 0) {
  155. self.showResult('dupe', '已签到', '本场次已签到,无需重复操作', '知道了')
  156. } else if (msg.indexOf('审核') >= 0) {
  157. self.showResult('invalid', '暂无签到资格', '未通过该批次报名审核,无法签到', '知道了')
  158. } else if (msg.indexOf('过期') >= 0 || msg.indexOf('无效') >= 0) {
  159. self.showResult('invalid', '签到码无效或已过期', '请找带教刷新签到码后重试', '重新扫码')
  160. } else {
  161. self.showResult('error', '签到失败', msg || '网络异常,请稍后重试', '重新扫码')
  162. }
  163. })
  164. },
  165. showResult(type, title, desc, closeText) {
  166. this.result = { type: type, title: title, desc: desc, closeText: closeText }
  167. },
  168. closeResult() {
  169. var type = this.result ? this.result.type : ''
  170. this.result = null
  171. if (type === 'invalid' || type === 'error') {
  172. // 失效/失败:清空输入,便于重扫
  173. this.inputCode = ''
  174. // #ifdef MP-WEIXIN
  175. this.openScanner()
  176. // #endif
  177. }
  178. }
  179. }
  180. }
  181. </script>
  182. <style scoped>
  183. .scan-page {
  184. min-height: 100vh;
  185. background: #F5FAFE;
  186. padding: 40rpx 32rpx;
  187. }
  188. .scan-card {
  189. background: #FFFFFF;
  190. border: 1px solid #E3ECF4;
  191. border-radius: 20rpx;
  192. padding: 56rpx 32rpx;
  193. display: flex;
  194. flex-direction: column;
  195. align-items: center;
  196. }
  197. .scan-title { font-size: 36rpx; font-weight: 700; color: #1F4E79; margin-bottom: 12rpx; }
  198. .scan-sub { font-size: 26rpx; color: #3B5A78; margin-bottom: 40rpx; }
  199. .scan-icon { font-size: 96rpx; margin-bottom: 24rpx; }
  200. .big-btn {
  201. width: 100%;
  202. height: 88rpx;
  203. line-height: 88rpx;
  204. background: #2E6FA8;
  205. color: #FFFFFF;
  206. font-size: 32rpx;
  207. font-weight: 600;
  208. border-radius: 44rpx;
  209. border: none;
  210. padding: 0;
  211. text-align: center;
  212. }
  213. .big-btn:active { opacity: 0.9; }
  214. .big-btn[disabled] { background: #94A3B2; }
  215. .scan-btn { margin-bottom: 24rpx; }
  216. .scan-alt { font-size: 26rpx; color: #2E6FA8; }
  217. .manual-box {
  218. width: 100%;
  219. margin-top: 32rpx;
  220. padding-top: 32rpx;
  221. border-top: 1rpx solid #F5FAFE;
  222. }
  223. .field-row {
  224. display: flex;
  225. align-items: center;
  226. margin-bottom: 20rpx;
  227. }
  228. .field-label {
  229. width: 150rpx;
  230. font-size: 26rpx;
  231. color: #3B5A78;
  232. }
  233. .field-input {
  234. flex: 1;
  235. height: 72rpx;
  236. background: #F5FAFE;
  237. border: 1rpx solid #E3ECF4;
  238. border-radius: 12rpx;
  239. padding: 0 20rpx;
  240. font-size: 28rpx;
  241. color: #1F4E79;
  242. }
  243. .submit-btn { margin-top: 12rpx; }
  244. .scan-tip {
  245. display: block;
  246. font-size: 24rpx;
  247. color: #94A3B2;
  248. margin-top: 16rpx;
  249. }
  250. /* 结果覆盖层 */
  251. .result-mask {
  252. position: fixed;
  253. left: 0;
  254. top: 0;
  255. right: 0;
  256. bottom: 0;
  257. background: rgba(18, 57, 95, 0.55);
  258. display: flex;
  259. align-items: center;
  260. justify-content: center;
  261. z-index: 999;
  262. }
  263. .result-card {
  264. width: 78%;
  265. background: #FFFFFF;
  266. border-radius: 20rpx;
  267. padding: 56rpx 40rpx 40rpx;
  268. display: flex;
  269. flex-direction: column;
  270. align-items: center;
  271. }
  272. .result-icon { font-size: 96rpx; margin-bottom: 24rpx; }
  273. .result-title { font-size: 34rpx; font-weight: 700; color: #1F4E79; margin-bottom: 16rpx; }
  274. .result-desc { font-size: 26rpx; color: #3B5A78; text-align: center; line-height: 1.7; margin-bottom: 36rpx; }
  275. .result-btn { width: 100%; }
  276. </style>