From 09dc74e264b6262cbe41c9c8476e24705871d4a6 Mon Sep 17 00:00:00 2001
From: yxh <172933527@qq.com>
Date: 星期日, 08 十一月 2026 23:31:17 +0800
Subject: [PATCH] yxh

---
 src/permission.ts |   96 ++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 85 insertions(+), 11 deletions(-)

diff --git a/src/permission.ts b/src/permission.ts
index 85c14e2..bca1eba 100644
--- a/src/permission.ts
+++ b/src/permission.ts
@@ -18,6 +18,80 @@
 
 const { loadStart, loadDone } = usePageLoading()
 
+/**
+ * 銆岄�夋嫨宸ヤ綅妯″紡銆嶇殑璺宠浆鍒ゅ畾锛�0/1/2/3锛夈��
+ *
+ * - 0 鎸夐渶瑕佸己鍒堕�夋嫨宸ヤ綅锛氭湭鍏ュ骇 鈫� 寮哄埗鍘婚�夊伐浣嶉〉
+ * - 1 鐧诲綍IP鍖归厤宸ヤ綅锛氬厛鎸夋湰鏈篒P鑷姩鍖归厤骞惰嚜鍔ㄥ叆搴э紱鍖归厤涓嶅埌 鈫� 鎸� 0锛堝幓閫夊伐浣嶉〉锛屽甫鍘熷洜锛�
+ * - 2 鐧诲綍鏃舵墜鍔ㄩ�夋嫨宸ヤ綅锛氬己鍒跺幓閫夊伐浣嶉〉锛屽苟鍦ㄧ‘璁ゆ椂鎶婃湰鏈篒P鍐欏叆鎵�閫夊伐浣嶏紙鍐橧P鍦ㄩ�夊伐浣嶉〉閲屽仛锛�
+ * - 3 鎸夌郴缁熼粯璁ゅ鐞嗭細璇P鏈粦瀹氳繃宸ヤ綅 鈫� 鎸� 2锛堥�変竴娆″苟璁颁綇IP锛夛紱宸茬粦瀹� 鈫� 鎸� 1锛涘尮閰嶄笉鍒� 鈫� 鎸� 0
+ *
+ * @returns true 琛ㄧず宸叉斁琛岋紙鏃犻渶璺宠浆锛夛紝false 琛ㄧず宸茬粡鍙戣捣璺宠浆
+ */
+const checkRoomSelect = async (
+  to: { path: string; fullPath: string },
+  next: (v?: any) => void
+): Promise<boolean> => {
+  const userStore = useUserStoreWithOut()
+  const roomStore = useRoomStoreWithOut()
+
+  // 闈炲尰鐢熻鑹层�佹垨宸插叆搴� 鈫� 涓嶅共棰�
+  const isDoctor =
+    userStore.getRoles.includes('doctor') && !userStore.getRoles.includes('super_admin')
+  if (!isDoctor || roomStore.getIsSetRoom || to.path === '/roomselect') {
+    // 鐜板満鎺掓煡鐢細璇存槑"涓轰粈涔堣繖娆℃病鏈夎嚜鍔ㄥ叆搴�"锛坅dmin 绛夐潪鍖荤敓瑙掕壊鎸夎璁′笉鑷姩鍏ュ骇锛�
+    // 闇�瑕佺偣椤舵爮鐨勩�屾湭鍏ュ骇銆嶆爣绛捐繘閫夊伐浣嶉〉鎵嬪姩閫変竴娆★級
+    if (!isDoctor) {
+      console.info('[room] 褰撳墠瑙掕壊闈炲尰鐢燂紙roles=' + userStore.getRoles.join(',') + '锛夛紝璺宠繃鎸塈P鑷姩鍏ュ骇')
+    }
+    return true
+  }
+
+  const mode = await roomStore.loadRoomSelectMode()
+
+  // 妯″紡 1 / 3锛氬厛灏濊瘯鎸夋湰鏈篒P鑷姩鍖归厤骞惰嚜鍔ㄥ叆搴э紙鎴愬姛灏辩洿鎺ヨ繘锛�
+  if (mode === 1 || mode === 3) {
+    const res = await roomStore.tryAutoSeatByIp()
+    if (res.seated) {
+      return true
+    }
+
+    // 銆愭湰闇�姹傘�戞湰鏈� IP 瀵瑰簲鐨勫伐浣�**鏈変汉鍧愮潃**锛堜笂涓�鐝尰鐢熸病閫�鍑哄伐浣嶏級鈫� 鎻愮ず鏄惁鎺ョ銆�
+    // 鎺ョ鍚庣珛鍗虫垚涓鸿宸ヤ綅涓讳汉锛屽苟鎸夌幇鏈夐�昏緫鑷姩鍔犺浇璇ュ伐浣嶃�屽氨璇婁腑銆嶆偅鑰呫��
+    if (res.reason === 'occupied' && res.roomId != null && res.bedNo) {
+      const bedText = `${res.roomName || '璇婂' + res.roomId} 路 ${res.bedNo}`
+      try {
+        await ElMessageBox.confirm(
+          `鏈満瀵瑰簲鐨勫伐浣嶃��${bedText}銆嶅綋鍓嶇敱銆�${res.occupiedBy || '鍏朵粬鍖荤敓'}銆嶅崰鐢ㄣ�俓n` +
+            `鏄惁鎺ョ璇ュ伐浣嶇户缁伐浣滐紵锛堟帴绠″悗瀵规柟灏嗚寮哄埗绂诲骇锛塦,
+          '宸ヤ綅琚崰鐢�',
+          { confirmButtonText: '鎺ョ宸ヤ綅', cancelButtonText: '鍏堜笉鎺ョ锛堝彧璇诲洿瑙傦級', type: 'warning' }
+        )
+        const ok = await roomStore.takeOverBed(res.roomId, res.bedNo)
+        if (ok) {
+          ElMessage.success(`宸叉帴绠″伐浣嶏細${bedText}`)
+          return true
+        }
+        ElMessage.warning('鎺ョ澶辫触锛屽凡鎸夊彧璇诲洿瑙傝繘鍏ワ紙鍙◢鍚庡湪椤舵爮銆屽伐浣嶃�嶉噷鍐嶈瘯锛�')
+      } catch {
+        // 鐢ㄦ埛閫夋嫨涓嶆帴绠� 鈫� 鎸夊彧璇诲洿瑙傝繘鍏�
+        console.info('[room] 鐢ㄦ埛鏀惧純鎺ョ宸ヤ綅锛屾寜鍙鍥磋杩涘叆')
+      }
+    }
+
+    // 銆愪富/鍥磋銆戞病鑷姩鍏ュ骇涔�**涓嶅啀寮哄埗璺�"閫夊伐浣嶉〉"**锛�
+    // 鍏佽浠�"鏈叆搴� / 鍥磋"韬唤杩涘叆宸ヤ綔鍙帮紙鍙锛夛紝鐢卞伐浣滃彴椤堕儴鐨勩�屽伐浣嶃�嶄笅鎷�
+    // 閫夋嫨瑕佹煡鐪嬶紙鍥磋锛夋垨瑕佸叆搴х殑宸ヤ綅銆傝嫢涓�寰嬪脊鍥為�夊伐浣嶉〉锛屽氨姘歌繙鏃犳硶鍥磋鍒汉銆�
+    console.info('[room] 鏈嚜鍔ㄥ叆搴э紙' + (res.reason || 'nomatch') + '锛夛紝鎸�"鍙洿瑙�"鏀捐')
+    return true
+  }
+
+  // 妯″紡 0 / 2锛氬師鏉ュ己鍒跺幓閫夊伐浣嶉〉锛涙湰娆″悓鏍锋斁琛岋紝鐞嗙敱鍚屼笂锛堢敱椤堕儴涓嬫媺閫夋嫨宸ヤ綅锛夈��
+  // 閫夊伐浣嶉〉浠嶅彲浠庨《鏍忓伐浣嶆爣绛� / 椤堕儴涓嬫媺杩涘叆銆�
+  console.info('[room] 閫夋嫨宸ヤ綅妯″紡=' + mode + '锛氫笉寮哄埗璺抽�夊伐浣嶉〉锛屽厑璁歌繘鍏ュ悗鑷閫夋嫨/鍥磋')
+  return true
+}
+
 const parseURL = (
   url: string | null | undefined
 ): { basePath: string; paramsObject: { [key: string]: string } } => {
@@ -101,7 +175,6 @@
     // 鑾峰彇鎵�鏈夊瓧鍏�
     const dictStore = useDictStoreWithOut()
     const userStore = useUserStoreWithOut()
-    const roomStore = useRoomStoreWithOut()
     const checkTypeStore = useCheckTypeStoreWithOut()
     const permissionStore = usePermissionStoreWithOut()
     if (!dictStore.getIsSetDict) {
@@ -113,11 +186,8 @@
 
     if (userStore.getIsSetUser) {
 
-      // <<<銆愯瘖瀹ら�夋嫨銆�<<<
-      if (to.path !== '/roomselect' &&
-          userStore.getRoles.includes("doctor") && !userStore.getRoles.includes("super_admin")
-          && !roomStore.getIsSetRoom) {
-        next({ path: `/roomselect?redirect=${to.fullPath}` })
+      // <<<銆愯瘖瀹ら�夋嫨銆戞寜銆岄�夋嫨宸ヤ綅妯″紡銆嶅弬鏁帮紙0/1/2/3锛夊喅瀹氭槸鍚﹀己鍒堕�夋嫨宸ヤ綅 >>>
+      if (!(await checkRoomSelect(to, next))) {
         return
       }
 
@@ -135,11 +205,8 @@
       router.addRoute(route as unknown as RouteRecordRaw) // 鍔ㄦ�佹坊鍔犲彲璁块棶璺敱琛�
     })
 
-    // <<<銆愯瘖瀹ら�夋嫨銆�<<<
-    if (to.path !== '/roomselect' &&
-        userStore.getRoles.includes("doctor") && !userStore.getRoles.includes("super_admin")
-        && !roomStore.getIsSetRoom) {
-      next({ path: `/roomselect?redirect=${to.fullPath}` })
+    // <<<銆愯瘖瀹ら�夋嫨銆戞寜銆岄�夋嫨宸ヤ綅妯″紡銆嶅弬鏁帮紙0/1/2/3锛夊喅瀹氭槸鍚﹀己鍒堕�夋嫨宸ヤ綅 >>>
+    if (!(await checkRoomSelect(to, next))) {
       return
     }
 
@@ -178,5 +245,12 @@
   // ==========================================================================
   if (getAccessToken() && to.path !== '/login') {
     setupIdleLogout()
+    // 銆愭湰娆″彉鏇淬��**宸茬Щ闄�** `setupBedReleaseOnUnload()`锛坧agehide + sendBeacon锛夈��
+    //
+    // 鍘熻涓猴細鍏抽棴鏍囩椤� / 鍒锋柊 / 璺宠蛋鏃堕兘浼氶噴鏀惧崰鐢ㄧ殑宸ヤ綅锛屽鑷�
+    //   路 鎸� F5 鍒锋柊涓�娆″氨涓㈠伐浣嶏紱
+    //   路 娴忚鍣ㄥ穿婧冩仮澶嶃�佸垏璧颁竴浼氬効鍥炴潵锛屼篃鍙兘鍥� pagehide 涓㈠伐浣嶃��
+    // 鐜板湪宸ヤ綅鍙湪**鐢ㄦ埛涓诲姩鎿嶄綔**鏃堕噴鏀撅細鎵嬪姩銆岀搴с�嶃�佹墜鍔ㄣ�岄��鍑虹櫥褰曘�嶃��
+    // 鍖荤敓涓嬬彮蹇樼搴х殑鍏滃簳锛氣憼 闂瘖鏃剁粺涓�娓呯┖褰撶彮宸ヤ綅锛涒憽 绠$悊鍛樸�屾竻绌哄伐浣嶃�嶃��
   }
 })

--
Gitblit v1.9.3