| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <template>
- <view class="scan-page">
- <view class="scan-card">
- <text class="scan-title">现场签到</text>
- <text class="scan-sub">请对准讲师/带教展示的签到二维码</text>
- <!-- MP 端:原生扫码 -->
- <!-- #ifdef MP-WEIXIN -->
- <view class="scan-icon">📷</view>
- <button class="big-btn scan-btn" :disabled="busy" @click="openScanner">
- {{ busy ? '处理中...' : '扫码签到' }}
- </button>
- <text class="scan-alt" @click="toggleManual">手动输入签到码</text>
- <!-- #endif -->
- <!-- 手动输入(H5 降级 + MP 备用) -->
- <view v-if="manual" class="manual-box">
- <view class="field-row">
- <text class="field-label">签到码</text>
- <input class="field-input" v-model="inputCode" placeholder="请输入签到码" maxlength="32" />
- </view>
- <button class="big-btn submit-btn" :disabled="busy" @click="submitManual">
- {{ busy ? '处理中...' : '提交签到' }}
- </button>
- </view>
- <text class="scan-tip">· 扫码即完成签到,学时将在 T+3 自动生效</text>
- <text class="scan-tip">· 同一场次重复扫码不会重复计学时</text>
- </view>
- <!-- 结果覆盖层 -->
- <view v-if="result" class="result-mask">
- <view class="result-card">
- <text class="result-icon">{{ resultIcon }}</text>
- <text class="result-title">{{ result.title }}</text>
- <text class="result-desc">{{ result.desc }}</text>
- <button class="big-btn result-btn" @click="closeResult">{{ result.closeText }}</button>
- </view>
- </view>
- </view>
- </template>
- <script>
- import { attendanceScan } from '@/api/attendance.js'
- import { guardPage, guardWorker } from '@/utils/guard.js'
- export default {
- data() {
- return {
- authorized: false,
- busy: false,
- manual: false,
- hasSessionId: false,
- sessionId: '',
- inputSessionId: '',
- inputCode: '',
- result: null,
- lastCode: '',
- lastTime: 0
- }
- },
- computed: {
- resultIcon() {
- if (!this.result) return ''
- var map = { success: '✅', dupe: 'ℹ️', invalid: '⚠️', error: '❌' }
- return map[this.result.type] || ''
- }
- },
- onLoad(options) {
- var self = this
- if (!guardPage()) return
- // 反扫入口:?sessionId=xxx(场次由讲师/带教页面带出)
- if (options && options.sessionId) {
- this.sessionId = String(options.sessionId)
- this.hasSessionId = true
- }
- // H5 端 uni.scanCode 不可用:默认展示手动输入表单(降级)
- // #ifndef MP-WEIXIN
- this.manual = true
- // #endif
- // 双身份隔离:仅健康服务人员可用签到动作;守卫通过后再唤起扫码
- guardWorker().then(function(ok) {
- self.authorized = ok
- if (!ok) return
- // #ifdef MP-WEIXIN
- self.openScanner()
- // #endif
- })
- },
- onShow() {
- // MP 端进入页面自动唤起扫码(仅守卫已放行时,避免与 onLoad 的守卫竞态重复唤起)
- // #ifdef MP-WEIXIN
- if (this.authorized) {
- this.openScanner()
- }
- // #endif
- },
- methods: {
- openScanner() {
- var self = this
- uni.scanCode({
- scanType: ['qrCode'],
- success: function(res) {
- var content = (res && res.result) ? String(res.result).trim() : ''
- if (!content) return
- self.parseAndSubmit(content)
- },
- fail: function() {
- // 用户取消或相机不可用:回落到手动输入
- self.manual = true
- }
- })
- },
- toggleManual() {
- this.manual = !this.manual
- },
- submitManual() {
- var code = (this.inputCode || '').trim()
- if (!code) {
- uni.showToast({ title: '请输入签到码', icon: 'none' })
- return
- }
- this.submit(code)
- },
- // 解析扫码内容:后端 scan 入参仅 {code},场次由签到码反查推导。
- // 兼容复合内容 sessionId|code(取末段码),纯码直接使用。
- parseAndSubmit(content) {
- var code = content
- if (content.indexOf('|') >= 0) {
- var parts = content.split('|')
- code = (parts[parts.length - 1] || '').trim()
- }
- if (!code) return
- this.submit(code)
- },
- submit(code) {
- var self = this
- // 30s 防抖:同一签到码 30s 内不重复提交(服务端幂等兜底为「已签到」)
- var now = Date.now()
- if (self.lastCode === code && now - self.lastTime < 30000) {
- self.showResult('dupe', '已提交,请勿重复操作', '稍后可在学时账户查看入账情况', '知道了')
- return
- }
- self.busy = true
- self.lastCode = code
- self.lastTime = now
- attendanceScan(code).then(function(res) {
- self.busy = false
- var data = (res && res.data) ? res.data : {}
- // alreadyExists=true / attended=true → 本场次已签到过(后端不重复入账)
- if (data.alreadyExists === true || data.attended === true) {
- self.showResult('dupe', '已签到', '本场次已签到,无需重复操作', '知道了')
- return
- }
- var time = data.time || data.attendedAt || ''
- self.showResult('success', '签到成功', '✓ 学时待 T+3 生效' + (time ? '(' + time + ')' : ''), '完成')
- }).catch(function(err) {
- self.busy = false
- var msg = (err && err.message) || ''
- if (msg.indexOf('已签到') >= 0) {
- self.showResult('dupe', '已签到', '本场次已签到,无需重复操作', '知道了')
- } else if (msg.indexOf('审核') >= 0) {
- self.showResult('invalid', '暂无签到资格', '未通过该批次报名审核,无法签到', '知道了')
- } else if (msg.indexOf('过期') >= 0 || msg.indexOf('无效') >= 0) {
- self.showResult('invalid', '签到码无效或已过期', '请找带教刷新签到码后重试', '重新扫码')
- } else {
- self.showResult('error', '签到失败', msg || '网络异常,请稍后重试', '重新扫码')
- }
- })
- },
- showResult(type, title, desc, closeText) {
- this.result = { type: type, title: title, desc: desc, closeText: closeText }
- },
- closeResult() {
- var type = this.result ? this.result.type : ''
- this.result = null
- if (type === 'invalid' || type === 'error') {
- // 失效/失败:清空输入,便于重扫
- this.inputCode = ''
- // #ifdef MP-WEIXIN
- this.openScanner()
- // #endif
- }
- }
- }
- }
- </script>
- <style scoped>
- .scan-page {
- min-height: 100vh;
- background: #F5FAFE;
- padding: 40rpx 32rpx;
- }
- .scan-card {
- background: #FFFFFF;
- border: 1px solid #E3ECF4;
- border-radius: 20rpx;
- padding: 56rpx 32rpx;
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- .scan-title { font-size: 36rpx; font-weight: 700; color: #1F4E79; margin-bottom: 12rpx; }
- .scan-sub { font-size: 26rpx; color: #3B5A78; margin-bottom: 40rpx; }
- .scan-icon { font-size: 96rpx; margin-bottom: 24rpx; }
- .big-btn {
- width: 100%;
- height: 88rpx;
- line-height: 88rpx;
- background: #2E6FA8;
- color: #FFFFFF;
- font-size: 32rpx;
- font-weight: 600;
- border-radius: 44rpx;
- border: none;
- padding: 0;
- text-align: center;
- }
- .big-btn:active { opacity: 0.9; }
- .big-btn[disabled] { background: #94A3B2; }
- .scan-btn { margin-bottom: 24rpx; }
- .scan-alt { font-size: 26rpx; color: #2E6FA8; }
- .manual-box {
- width: 100%;
- margin-top: 32rpx;
- padding-top: 32rpx;
- border-top: 1rpx solid #F5FAFE;
- }
- .field-row {
- display: flex;
- align-items: center;
- margin-bottom: 20rpx;
- }
- .field-label {
- width: 150rpx;
- font-size: 26rpx;
- color: #3B5A78;
- }
- .field-input {
- flex: 1;
- height: 72rpx;
- background: #F5FAFE;
- border: 1rpx solid #E3ECF4;
- border-radius: 12rpx;
- padding: 0 20rpx;
- font-size: 28rpx;
- color: #1F4E79;
- }
- .submit-btn { margin-top: 12rpx; }
- .scan-tip {
- display: block;
- font-size: 24rpx;
- color: #94A3B2;
- margin-top: 16rpx;
- }
- /* 结果覆盖层 */
- .result-mask {
- position: fixed;
- left: 0;
- top: 0;
- right: 0;
- bottom: 0;
- background: rgba(18, 57, 95, 0.55);
- display: flex;
- align-items: center;
- justify-content: center;
- z-index: 999;
- }
- .result-card {
- width: 78%;
- background: #FFFFFF;
- border-radius: 20rpx;
- padding: 56rpx 40rpx 40rpx;
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- .result-icon { font-size: 96rpx; margin-bottom: 24rpx; }
- .result-title { font-size: 34rpx; font-weight: 700; color: #1F4E79; margin-bottom: 16rpx; }
- .result-desc { font-size: 26rpx; color: #3B5A78; text-align: center; line-height: 1.7; margin-bottom: 36rpx; }
- .result-btn { width: 100%; }
- </style>
|