Browse Source

feat(mp): 健康服务域身份守卫guardWorker与命名对齐

- 新增 utils/guard.js guardWorker():双身份隔离 Must 红线
  - 判据对齐 IdentityGateCard 契约(roles/hasProfile)
  - 快路径:role∈{housekeeper,worker} 或已建档标记 → 放行
  - 慢路径兜底:POST /api/practitioner/mine,按是否 onboarded
    (status=approved / serviceStatus=on / lifecycleStage=service / authLevel!=none)判定
  - 网络异常不误判:仅明确无档案/无身份才拦截,加载失败放行
  - 无身份:toast + switchTab 到 /pages/mine/index
- 接入 11 个健康服务域页面 onLoad/onShow/onReady(guardPage 之后)
  practitioner/index+detail, cohort/index+detail, attendance/scan+qr,
  credit/index, eval/index+submit, promotion/index, evidence/index
- 命名对齐:IdentityGateCard.vue → identity-gate-card.vue(kebab-case 文件名,
  模板 PascalCase 绑定不变,与 empty-state 等惯例一致)
- 字段核对:work/index 用 item.source(= TrainOrderDispatch.source),
  未虚构 dispatchSource(后端 /api/order/mine VO 未面出 source,属后端待补)
liaoxg 17 hours ago
parent
commit
a6a39c40fa

+ 0 - 0
train-frontend/components/IdentityGateCard.vue → train-frontend/components/identity-gate-card.vue


+ 8 - 3
train-frontend/pages/attendance/qr.vue

@@ -46,6 +46,7 @@
 import UQRCode from '@/common/uqrcode.js'
 import { issueAttendanceCode, rotateAttendanceCode } from '@/api/attendance.js'
 import { formatDateTime } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import ErrorState from '@/components/error-state.vue'
 import EmptyState from '@/components/empty-state.vue'
 
@@ -83,9 +84,13 @@ export default {
     this.canvasSize = winW - 80 > 260 ? 260 : (winW - 80 < 200 ? 200 : winW - 80)
   },
   onReady() {
-    if (this.sessionId) {
-      this.loadCode()
-    }
+    var self = this
+    if (!this.sessionId) return
+    if (!guardPage()) return
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.loadCode()
+    })
   },
   onUnload() {
     this.clearTimer()

+ 15 - 3
train-frontend/pages/attendance/scan.vue

@@ -42,11 +42,12 @@
 
 <script>
 import { attendanceScan } from '@/api/attendance.js'
-import { guardPage } from '@/utils/guard.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 
 export default {
   data() {
     return {
+      authorized: false,
       busy: false,
       manual: false,
       hasSessionId: false,
@@ -66,6 +67,7 @@ export default {
     }
   },
   onLoad(options) {
+    var self = this
     if (!guardPage()) return
     // 反扫入口:?sessionId=xxx(场次由讲师/带教页面带出)
     if (options && options.sessionId) {
@@ -76,11 +78,21 @@ export default {
     // #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 端进入页面自动唤起扫码
+    // MP 端进入页面自动唤起扫码(仅守卫已放行时,避免与 onLoad 的守卫竞态重复唤起)
     // #ifdef MP-WEIXIN
-    this.openScanner()
+    if (this.authorized) {
+      this.openScanner()
+    }
     // #endif
   },
   methods: {

+ 9 - 2
train-frontend/pages/cohort/detail.vue

@@ -84,6 +84,7 @@
 <script>
 import { getCohortDetail, cohortSignup } from '@/api/cohort.js'
 import { creditHours, formatDateTime, parseDate } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import StatusTag from '@/components/status-tag.vue'
 import EmptyState from '@/components/empty-state.vue'
 import ErrorState from '@/components/error-state.vue'
@@ -168,8 +169,14 @@ export default {
     }
   },
   onLoad(options) {
-    this.cohortId = options && options.cohortId ? options.cohortId : null
-    this.loadDetail()
+    var self = this
+    if (!guardPage()) return
+    var cohortId = options && options.cohortId ? options.cohortId : null
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.cohortId = cohortId
+      self.loadDetail()
+    })
   },
   methods: {
     loadDetail() {

+ 8 - 2
train-frontend/pages/cohort/index.vue

@@ -65,6 +65,7 @@
 import { getCohortList } from '@/api/cohort.js'
 import { getCourseList } from '@/utils/api.js'
 import { creditHours, formatDateTime, parseDate } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import StatusTag from '@/components/status-tag.vue'
 import EmptyState from '@/components/empty-state.vue'
 import ErrorState from '@/components/error-state.vue'
@@ -122,8 +123,13 @@ export default {
     }
   },
   onLoad() {
-    this.loadCourses()
-    this.loadCohorts()
+    var self = this
+    if (!guardPage()) return
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.loadCourses()
+      self.loadCohorts()
+    })
   },
   methods: {
     loadCourses() {

+ 7 - 1
train-frontend/pages/credit/index.vue

@@ -101,6 +101,7 @@
 <script>
 import { getCreditMine, getCreditPending, creditObject } from '@/api/credit.js'
 import { creditHours, formatDateTime } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import StatusTag from '@/components/status-tag.vue'
 import EmptyState from '@/components/empty-state.vue'
 import ErrorState from '@/components/error-state.vue'
@@ -190,7 +191,12 @@ export default {
     }
   },
   onShow() {
-    this.loadAll()
+    var self = this
+    if (!guardPage()) return
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.loadAll()
+    })
   },
   methods: {
     loadAll() {

+ 7 - 1
train-frontend/pages/eval/index.vue

@@ -55,6 +55,7 @@
 <script>
 import { getEvalMine } from '@/api/eval.js'
 import { creditHours } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import StarRating from '@/components/star-rating.vue'
 import EmptyState from '@/components/empty-state.vue'
 import ErrorState from '@/components/error-state.vue'
@@ -106,7 +107,12 @@ export default {
     }
   },
   onLoad() {
-    this.loadData()
+    var self = this
+    if (!guardPage()) return
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.loadData()
+    })
   },
   methods: {
     num(v) {

+ 13 - 6
train-frontend/pages/eval/submit.vue

@@ -56,6 +56,7 @@
 <script>
 import { submitClientEval } from '@/api/eval.js'
 import { getServiceOrderDetail } from '@/utils/api.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import StarRating from '@/components/star-rating.vue'
 import ErrorState from '@/components/error-state.vue'
 
@@ -87,12 +88,18 @@ export default {
     }
   },
   onLoad(options) {
-    this.orderId = options && options.orderId ? options.orderId : null
-    if (!this.orderId) {
-      this.error = true
-      return
-    }
-    this.loadOrder()
+    var self = this
+    if (!guardPage()) return
+    var orderId = options && options.orderId ? options.orderId : null
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.orderId = orderId
+      if (!self.orderId) {
+        self.error = true
+        return
+      }
+      self.loadOrder()
+    })
   },
   methods: {
     onScore(val) {

+ 7 - 1
train-frontend/pages/evidence/index.vue

@@ -77,6 +77,7 @@
 <script>
 import { exportEvidence, getEvidenceStatus } from '@/api/evidence.js'
 import { formatDate } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import StatusTag from '@/components/status-tag.vue'
 import EmptyState from '@/components/empty-state.vue'
 import LoadingSkeleton from '@/components/loading-skeleton.vue'
@@ -95,7 +96,12 @@ export default {
     }
   },
   onShow() {
-    this.refreshAll()
+    var self = this
+    if (!guardPage()) return
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.refreshAll()
+    })
   },
   onUnload() {
     this.stopAllTimers()

+ 1 - 1
train-frontend/pages/mine/index.vue

@@ -128,7 +128,7 @@
 <script>
 import { getMyCert, getMyCoupons, redeemCoupon, verifyCert, getMyBadges } from '@/utils/api.js'
 import { jumpToCfc } from '@/utils/sso.js'
-import IdentityGateCard from '@/components/IdentityGateCard.vue'
+import IdentityGateCard from '@/components/identity-gate-card.vue'
 import LoadingSkeleton from '@/components/loading-skeleton.vue'
 import ErrorState from '@/components/error-state.vue'
 import EmptyState from '@/components/empty-state.vue'

+ 9 - 2
train-frontend/pages/practitioner/detail.vue

@@ -89,6 +89,7 @@
 import { getMyPractitioner, getPractitionerDetail } from '@/api/practitioner.js'
 import { maskName, maskPhone, maskAddress } from '@/utils/mask.js'
 import { creditHours } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 
 export default {
   data() {
@@ -157,9 +158,15 @@ export default {
     }
   },
   onLoad(options) {
+    var self = this
+    if (!guardPage()) return
     // 带 userId → 他人档案(admin/org 预览);否则 → 本人档案
-    this.targetUserId = (options && options.userId) ? options.userId : null
-    this.loadDetail()
+    var targetUserId = (options && options.userId) ? options.userId : null
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.targetUserId = targetUserId
+      self.loadDetail()
+    })
   },
   methods: {
     loadDetail() {

+ 8 - 2
train-frontend/pages/practitioner/index.vue

@@ -143,6 +143,7 @@ import { getMyPractitioner } from '@/api/practitioner.js'
 import { getMyServiceOrderCounts } from '@/utils/api.js'
 import { maskName } from '@/utils/mask.js'
 import { creditHours } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 
 export default {
   data() {
@@ -222,8 +223,13 @@ export default {
     }
   },
   onLoad() {
-    this.loadProfile()
-    this.loadOrderBadge()
+    var self = this
+    if (!guardPage()) return
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.loadProfile()
+      self.loadOrderBadge()
+    })
   },
   methods: {
     // P1 工作台角标:只取 /api/order/mine 的 pending 组 count

+ 7 - 1
train-frontend/pages/promotion/index.vue

@@ -113,6 +113,7 @@
 import { getMyPractitioner } from '@/api/practitioner.js'
 import { applyPromotion, getPromotionRecords } from '@/api/promotion.js'
 import { creditHours, formatDateTime } from '@/utils/format.js'
+import { guardPage, guardWorker } from '@/utils/guard.js'
 import PromoTrack from '@/components/promo-track.vue'
 import RingProgress from '@/components/ring-progress.vue'
 import StatusTag from '@/components/status-tag.vue'
@@ -198,7 +199,12 @@ export default {
     }
   },
   onLoad() {
-    this.loadAll()
+    var self = this
+    if (!guardPage()) return
+    guardWorker().then(function(ok) {
+      if (!ok) return
+      self.loadAll()
+    })
   },
   methods: {
     loadAll() {

+ 78 - 0
train-frontend/utils/guard.js

@@ -4,6 +4,8 @@
  * 需求:登录前可浏览课程信息,但班级/报名/投票/打卡/支付等实际操作必须登录。
  * 未登录触发 requireLogin() 时:toast 提示 + 跳回首页 tab + 触发登录浮层(showLoginSheet)。
  */
+import { getMyPractitioner } from '@/api/practitioner.js'
+
 export function requireLogin() {
   var loggedIn = false
   try {
@@ -32,4 +34,80 @@ export function requireLogin() {
 /** 页面级守卫:在 onShow/onLoad 首行调用,未登录返回 false 阻止后续加载 */
 export function guardPage() {
   return requireLogin()
+}
+
+/**
+ * 健康服务身份同步判据(快路径,无网络请求)
+ * 与 IdentityGateCard.visible(设计文档 §3.2 契约 roles/hasProfile)同口径:
+ * - role ∈ {housekeeper, worker}(登录时写入 store/storage)→ 命中
+ * - 已建档标记 healthServiceProfile === true(由 practitioner/index 建档成功后写入)→ 命中
+ * 命中任一即具备健康服务人员身份。
+ */
+function hasHealthIdentitySync() {
+  var role = 'student'
+  try {
+    var app = typeof getApp === 'function' ? getApp() : null
+    if (app && app.$store && app.$store.state && app.$store.state.role) {
+      role = app.$store.state.role
+    }
+  } catch (e) {}
+  if (role !== 'housekeeper' && role !== 'worker') {
+    role = uni.getStorageSync('role') || role
+  }
+  if (role === 'housekeeper' || role === 'worker') return true
+  var hsp = uni.getStorageSync('healthServiceProfile')
+  return hsp === true || hsp === 'true'
+}
+
+/** 无健康服务身份:提示 + 跳回「我的」(tabBar 页用 switchTab),让用户看到身份状态 */
+function blockWorker() {
+  uni.showToast({ title: '暂未开通健康服务身份', icon: 'none' })
+  setTimeout(function() {
+    uni.switchTab({ url: '/pages/mine/index' })
+  }, 600)
+}
+
+/**
+ * 健康服务域页面守卫(双身份隔离 Must 红线)
+ *
+ * 训练营身份用户不得进入健康服务域页面(practitioner/cohort/attendance/credit/
+ * eval/promotion/evidence),本守卫在 guardPage() 之后调用。
+ *
+ * 判据(对齐 IdentityGateCard 契约,避免误判):
+ * 1. 快路径(无网络):role ∈ {housekeeper, worker} 或已建档标记 → 放行;
+ * 2. 慢路径(无缓存兜底):POST /api/practitioner/mine。因后端 mine 会为任意
+ *    登录用户懒建档(status=pending / lifecycleStage=training),「档案存在」不等于
+ *    「已开通身份」,故按是否已 onboarded 判定:status==='approved' / serviceStatus==='on'
+ *    / lifecycleStage==='service' / authLevel!=='none' 视为已开通;
+ * 3. 网络异常不误判:仅当接口明确返回「无档案/无身份」才拦截,加载失败放行(避免把
+ *    健康服务用户挡在门外)。
+ *
+ * @returns {Promise<boolean>} resolve(true)=放行;resolve(false)=已拦截(内部已 toast+跳转)
+ */
+export function guardWorker() {
+  if (hasHealthIdentitySync()) {
+    return Promise.resolve(true)
+  }
+  return getMyPractitioner().then(function(res) {
+    var p = (res && res.data) ? res.data : {}
+    var onboarded = p.status === 'approved'
+      || p.serviceStatus === 'on'
+      || p.lifecycleStage === 'service'
+      || (p.authLevel && p.authLevel !== 'none')
+    if (onboarded) {
+      uni.setStorageSync('healthServiceProfile', true)
+      return true
+    }
+    blockWorker()
+    return false
+  }).catch(function(err) {
+    var code = err && err.code
+    var msg = (err && err.message) || ''
+    // 明确「无档案/无身份」才拦截;网络抖动/5xx 放行
+    if (code === 404 || msg.indexOf('档案') >= 0 || msg.indexOf('身份') >= 0) {
+      blockWorker()
+      return false
+    }
+    return true
+  })
 }