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/store/modules/room.ts |  551 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 536 insertions(+), 15 deletions(-)

diff --git a/src/store/modules/room.ts b/src/store/modules/room.ts
index f3f771b..d751770 100644
--- a/src/store/modules/room.ts
+++ b/src/store/modules/room.ts
@@ -1,15 +1,76 @@
 import { store } from '@/store'
 import { defineStore } from 'pinia'
 import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
-import {RoomVO} from "@/api/ecg/room";
+import { RoomApi, RoomVO, RoomSeatResp } from "@/api/ecg/room";
+import { DoctorApi } from "@/api/ecg/doctor";
+import { getConfigKey } from '@/api/infra/config'
+import { useUserStore } from '@/store/modules/user'
 
 const { wsCache } = useCache("sessionStorage")
 
+/** 鍙傛暟 key锛氶�夋嫨宸ヤ綅妯″紡锛堜笌鍚庣 Constants.ECG_ROOM_SELECT_MODE_KEY 涓�鑷达級 */
+const ROOM_SELECT_MODE_KEY = 'ecg.room.select.mode'
+
+/**
+ * 閫夋嫨宸ヤ綅妯″紡锛�
+ *  0 = 鎸夐渶瑕佸己鍒堕�夋嫨宸ヤ綅锛�=鍘熻涓猴級
+ *  1 = 鐧诲綍IP鍖归厤宸ヤ綅锛堝尮閰嶄笉鍒版寜 0 鎵ц锛�
+ *  2 = 鐧诲綍鏃舵墜鍔ㄩ�夋嫨宸ヤ綅锛屽苟鍐欏叆鏈満IP
+ *  3 = 鎸夌郴缁熼粯璁ゅ鐞嗭紙璇P鏈粦瀹氳繃鈫掓寜2閫変竴娆″苟璁颁綇IP锛涘凡缁戝畾鈫掓寜1鑷姩鍖归厤锛涘尮閰嶄笉鍒扳啋鎸�0锛�
+ */
+export type RoomSelectMode = 0 | 1 | 2 | 3
+
+/** 鑷姩鍖归厤宸ヤ綅鐨勭粨鏋滐紝渚涜矾鐢卞畧鍗垎鏀敤 */
+export interface AutoSeatResult {
+  /** 鏄惁宸叉垚鍔熷叆搴� */
+  seated: boolean
+  /** 鍚庣璇嗗埆鍒扮殑鏈満IP */
+  clientIp: string | null
+  /**
+   * 鏈叆搴у師鍥狅紙seated=true 鏃舵棤鎰忎箟锛夛細
+   * - nomatch锛氳IP娌″尮閰嶅埌宸ヤ綅
+   * - occupied锛氬尮閰嶅埌浣嗗叏琚粬浜哄崰鐢�
+   * - multi锛�**璇P瀵瑰簲澶氫釜鍙�夊伐浣�**锛屾棤娉曞彧鍑璉P纭畾锛岄渶瑕佷汉宸ョ偣閫�
+   * - ip_invalid锛氭湰鏈篒P涓嶅彲鐢紙鍥炵幆 / IPv6 / 鏈寚瀹� / 闈炴硶锛夛紝涓嶈兘浣滀负宸ヤ綅鏍囪瘑
+   * - ip_is_server锛氳瘑鍒埌鐨勬槸**鏈嶅姟鍣ㄨ嚜韬湴鍧�**锛堝湪鏈嶅姟鍣ㄤ笂/杩滅▼妗岄潰閲岃闂紝鎴栦唬鐞嗚鐩栦簡瀹㈡埛绔湴鍧�锛�
+   * - error锛氭帴鍙e紓甯告垨鍏ュ骇鍐欏簱鏈敓鏁�
+   */
+  reason?: 'nomatch' | 'occupied' | 'multi' | 'ip_invalid' | 'ip_is_server' | 'error'
+  /** 琚崰鐢ㄦ椂鐨勫崰鐢ㄤ汉锛屼究浜庢彁绀� */
+  occupiedBy?: string | null
+  /** 銆愭湰闇�姹傘�戣鍗犵敤鏃跺懡涓殑鍊欓�夊伐浣嶏紙渚�"鏄惁鎺ョ"纭鍚庣洿鎺ヨ皟鐢� takeOverBed锛� */
+  roomId?: number | null
+  bedNo?: string | null
+  roomName?: string | null
+}
+
 interface RoomInfoVO {
-  // 鍖荤敓璇婂閫夋嫨
+  // 鍖荤敓璇婂閫夋嫨锛�= 褰撳墠**鏌ョ湅**鐨勫伐浣嶏細鐪嬭嚜宸卞氨鏄嚜宸卞叆搴х殑锛岀湅鍒汉灏辨槸鍥磋瀵硅薄锛�
   isSetRoom: boolean
   room: RoomVO
+  // 閫夋嫨宸ヤ綅妯″紡锛�-1 琛ㄧず杩樻病鍙栧埌锛�
+  roomSelectMode: number
+  /**
+   * 褰撳墠鐧诲綍浜�**宸插叆搴�**鐨勫伐浣嶏紙绯荤粺椤舵爮鏄剧ず鐢紱鏈叆搴т负 null锛夈��
+   *
+   * 涓� `room` 鐨勫尯鍒細`room` 鏄�"鎴戞鍦ㄧ湅鍝釜宸ヤ綅"锛堝洿瑙傛椂鏄埆浜虹殑宸ヤ綅锛夛紝
+   * 鑰� `currentSeat` 鏄�"鎴戝叆搴у湪鍝釜宸ヤ綅"锛堟湇鍔$ clinic_room.doc_id = 鎴戯級銆�
+   * 涓よ�呯浉鍚� 鈫� 鍙搷浣滐紱涓嶅悓 鈫� 鍙鍥磋銆�
+   */
+  currentSeat: RoomSeatResp | null
+  /**
+   * 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戝綋鍓嶇櫥褰曚汉鍗犳嵁鐨�**鍏ㄩ儴**宸ヤ綅锛堣妭鍋囨棩鍊肩彮澶氬矖浣嶏級銆�
+   *
+   * 涓� `currentSeat` 鐨勫尯鍒細`currentSeat` 鍙槸"榛樿/棣栭�夊彇"鐨勯偅涓�涓紙椤舵爮鏂囨鐢級锛�
+   * `mySeats` 鎵嶆槸瀹屾暣闆嗗悎锛沗getCanOperate` 鐢ㄥ畠鍒ゆ柇"褰撳墠鏌ョ湅鐨勫伐浣嶆槸鍚﹀睘浜庢垜"銆�
+   */
+  mySeats: RoomSeatResp[]
+  /** 褰撳墠鏌ョ湅宸ヤ綅鐨勪富浜虹殑濮撳悕锛堝洿瑙傛椂鐢ㄤ簬鎻愮ず"姝e湪鍥磋 X锛堜富浜� 寮犱笁锛�"锛� */
+  viewOwnerName: string | null
+  /** 褰撳墠鏌ョ湅鐨勫伐浣嶇幇鍦ㄦ槸鍚�**鏃犱汉鎿嶄綔**锛堜富浜哄凡绂诲骇锛涘彧鎻愮ず锛屼笉鑷姩鎺ョ锛� */
+  viewTargetVacant: boolean
 }
+
 
 export const useRoomStore = defineStore('current-room', {
   state: (): RoomInfoVO => ({
@@ -26,7 +87,17 @@
       docName: null,
       checkTypes: null,
       opType: null
-    }
+    },
+    // 閫夋嫨宸ヤ綅妯″紡锛�-1 = 灏氭湭浠庡悗绔彇鍒�
+    roomSelectMode: -1,
+    // 褰撳墠鍏ュ骇鐨勫伐浣嶏紙椤舵爮鏄剧ず锛涙湭鍏ュ骇涓� null锛�
+    currentSeat: null,
+    // 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戞垜鍗犳嵁鐨勫叏閮ㄥ伐浣嶏紙鏈叆搴т负绌烘暟缁勶級
+    mySeats: [],
+    // 姝e湪鏌ョ湅鐨勫伐浣嶇殑涓讳汉濮撳悕锛堝洿瑙傛彁绀虹敤锛�
+    viewOwnerName: null,
+    // 姝e湪鏌ョ湅鐨勫伐浣嶇幇鍦ㄦ槸鍚︽棤浜烘搷浣滐紙涓讳汉宸茬搴� 鈫� 鍙彁绀猴紝涓嶈嚜鍔ㄦ帴绠★級
+    viewTargetVacant: false
   }),
   getters: {
     // 鍖荤敓璇婂閫夋嫨
@@ -35,6 +106,64 @@
     },
     getRoom(): RoomVO | null {
       return this.room
+    },
+    /** 褰撳墠鍏ュ骇鐨勫伐浣嶏紙鍙兘涓� null = 鏈叆搴э級 */
+    getCurrentSeat(): RoomSeatResp | null {
+      return this.currentSeat
+    },
+    /** 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戞垜鍗犳嵁鐨勫叏閮ㄥ伐浣� */
+    getMySeats(): RoomSeatResp[] {
+      return this.mySeats || []
+    },
+    /** 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戞垜鍗犳嵁鐨勫伐浣嶆暟閲忥紙椤舵爮鎻愮ず"鎴戠殑宸ヤ綅 N 涓�"锛� */
+    getMySeatCount(): number {
+      return (this.mySeats || []).length
+    },
+    /**
+     * 銆愪富/鍥磋銆戝綋鍓嶆槸鍚�**鍙互鎿嶄綔涓氬姟**銆�
+     *
+     * 鍒ゅ畾锛堟湰闇�姹傛敼涓�"鍖呭惈"锛夛細
+     *   褰撳墠鏌ョ湅鐨勫伐浣� 鈭� 鎴戠殑宸ヤ綅鍒楄〃 鈬� 鍙搷浣滐紙鏈嶅姟绔彟鏈変竴閬撶‖鏍¢獙锛氳宸ヤ綅 doc_id 蹇呴』鏄垜锛夈��
+     * 涓�閿宸ヤ綅鍚庯紝鎴戜笉鍙兘鍚屾椂鍙湪"涓�涓�"宸ヤ綅涓婏紝鎵�浠ヤ笉鑳藉啀鐢�"鐩哥瓑"鍒ゆ柇銆�
+     * 鍏煎锛歚mySeats` 杩樻病鍙栧埌锛堟棫鎺ュ彛/鏃х紦瀛橈級鏃跺洖閫�鍒� `currentSeat` 鐩哥瓑鍒ゆ柇銆�
+     */
+    getCanOperate(): boolean {
+      const view = this.room
+      if (!view || view.roomId == null || !view.bedNo) {
+        return false
+      }
+      const seats = this.mySeats || []
+      if (seats.length > 0) {
+        return seats.some(
+          (s) => String(s.roomId) === String(view.roomId) && String(s.bedNo) === String(view.bedNo)
+        )
+      }
+      const seat = this.currentSeat
+      if (!seat || !seat.seated) {
+        return false
+      }
+      return String(seat.roomId) === String(view.roomId) && String(seat.bedNo) === String(view.bedNo)
+    },
+    /** 褰撳墠鏄惁鍦�**鍥磋鍒汉鐨勫伐浣�**锛堟湁搴т絾鐪嬬殑涓嶆槸鑷繁閭e骇锛屾垨娌″骇鍙湅鍒汉锛� */
+    getIsViewingOther(): boolean {
+      return this.isSetRoom && !this.getCanOperate
+    },
+    /**
+     * 椤舵爮灞曠ず鏂囨锛歚1鍙烽棬璇� 路 A1`锛涙湭鍏ュ骇杩斿洖绌轰覆锛堢敱缁勪欢鏄剧ず"鏈叆搴�"锛夈��
+     * 璇婂鍚嶄负绌烘椂閫�鍖栦负鍙樉绀哄伐浣嶅彿锛岄伩鍏嶅嚭鐜� `undefined 路 A1`銆�
+     */
+    getSeatText(): string {
+      const seat = this.currentSeat
+      if (!seat || !seat.seated) return ''
+      const name = seat.roomName || `璇婂${seat.roomId ?? ''}`
+      return seat.bedNo ? `${name} 路 ${seat.bedNo}` : name
+    },
+    /** 椤舵爮"褰撳墠鏌ョ湅鐨勫伐浣�"鏂囨锛堝洿瑙傛椂鐢ㄥ畠锛岃�屼笉鏄垜鑷繁鍏ュ骇鐨勫伐浣嶏級 */
+    getViewText(): string {
+      const view = this.room
+      if (!view || view.roomId == null || !view.bedNo) return ''
+      const name = view.roomName || `璇婂${view.roomId}`
+      return `${name} 路 ${view.bedNo}`
     }
   },
   actions: {
@@ -51,14 +180,114 @@
       this.room!.checkTypes = room.checkTypes
       this.room!.opType = room.opType
       this.isSetRoom = true
+      // 銆愪富/鍥磋銆戣繖鏉¤矾寰勯兘鏄�"鎴戝叆搴х殑宸ヤ綅"锛堝叆搴� / 鎭㈠鎴戠殑宸ヤ綅 / 鑷姩鍏ュ骇 / 閫夊伐浣嶉〉鍥炲~锛夛紝
+      // 鍥犳鎶� currentSeat 涓�璧疯涓� 鈥斺�� 鍚﹀垯 getCanOperate 浼氳鍒や负"鏃犲骇鍙"銆�
+      this.currentSeat = {
+        seated: true,
+        id: room.id ?? null,
+        roomId: room.roomId ?? null,
+        roomName: room.roomName ?? null,
+        bedNo: room.bedNo ?? null,
+        ip: room.ip ?? null,
+        status: room.status ?? null,
+        docId: room.docId ?? null,
+        docName: room.docName ?? null,
+        checkTypes: room.checkTypes ?? null,
+        opType: room.opType ?? null
+      } as RoomSeatResp
+      this.viewOwnerName = room.docName ?? null
+      this.viewTargetVacant = false
 
-      // 鏇存柊 cache
-      const bedInfo = wsCache.get(CACHE_KEY.ROOM)
-      if (bedInfo) {
-        bedInfo.room = room
-        bedInfo.isSetRoom = true
-        wsCache.set(CACHE_KEY.ROOM, bedInfo)
+      // 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戞妸璇ュ伐浣嶅苟鍏ャ�屾垜鐨勫伐浣嶃�嶏紙宸插瓨鍦ㄥ垯鏇存柊锛屼笉瀛樺湪鍒欐柊澧烇級
+      this.upsertMySeat(this.currentSeat)
+
+      // 銆愪慨澶嶃�戝啓鍏ョ紦瀛橈紙sessionStorage锛夈��
+      //
+      // 鍘熷疄鐜版槸 `const bedInfo = wsCache.get(...); if (bedInfo) { ...; wsCache.set(...) }`
+      // 鈥斺�� **鍙湪缂撳瓨宸插瓨鍦ㄦ椂鎵嶆洿鏂帮紝鑰屽叏椤圭洰娌℃湁浠讳綍鍦版柟棣栨鍐欏叆**锛�
+      // 浜庢槸 ROOM 缂撳瓨浠庢湭琚垱寤猴紝`room` 鍙椿鍦ㄥ唴瀛橀噷锛�
+      //   路 鍒锋柊椤甸潰 / 鏂板紑鏍囩椤� / 浠庝細璇濇仮澶嶅悗锛宍isSetRoom` 鍙堟槸鍒濆鐨� false锛�
+      //   路 鑰屽悇鍖荤敓椤甸潰锛圧outine/index/DevInstall锛夊彧鍦� `isSetRoom === true` 鏃�
+      //     鎵嶅惎鍔ㄥ彇鏁板畾鏃跺櫒 鈬� **椤甸潰涓�鐗囩┖鐧斤紝鐪嬭捣鏉ュ儚"娌″姞杞芥帓闃熺梾浜�"**锛�
+      //   路 瑙掕壊涓� doctor 鐨勭敤鎴疯繕浼氳璺敱瀹堝崼寮瑰洖 /roomselect 閲嶆柊閫夎瘖瀹ゃ��
+      // 杩欓噷鏀逛负**鏃犳潯浠跺啓鍏�**锛涙仮澶嶈鏂囦欢鏈熬鐨� setupRoomStorePersistence()銆�
+      wsCache.set(CACHE_KEY.ROOM, {
+        isSetRoom: true,
+        room: this.room,
+        currentSeat: this.currentSeat,
+        mySeats: this.mySeats
+      })
+    },
+    /** 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戞妸涓�鏉″伐浣嶅苟鍏� mySeats锛堟寜 roomId+bedNo 鍘婚噸锛� */
+    upsertMySeat(seat: RoomSeatResp | null) {
+      if (!seat || seat.roomId == null || !seat.bedNo) return
+      const list = (this.mySeats || []).filter(
+        (s) => !(String(s.roomId) === String(seat.roomId) && String(s.bedNo) === String(seat.bedNo))
+      )
+      list.push(seat)
+      this.mySeats = list
+    },
+    /** 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戠敤鍚庣杩斿洖鐨勫畬鏁村垪琛ㄦ浛鎹� mySeats锛屽苟鍚屾 currentSeat */
+    applyMySeats(seats: RoomSeatResp[]) {
+      this.mySeats = Array.isArray(seats) ? seats : []
+      if (this.mySeats.length === 0) {
+        this.currentSeat = null
+        return
       }
+      // currentSeat锛氫紭鍏堝彇"褰撳墠鏌ョ湅鐨勫伐浣�"瀵瑰簲鐨勯偅鏉★紝鍚﹀垯绗竴鏉�
+      const view = this.room
+      const matched = this.mySeats.find(
+        (s) =>
+          null != view &&
+          String(s.roomId) === String(view.roomId) &&
+          String(s.bedNo) === String(view.bedNo)
+      )
+      this.currentSeat = matched ?? this.mySeats[0]
+    },
+    /**
+     * 銆愪富/鍥磋銆戞妸"鎴戞鍦ㄧ湅鐨勫伐浣�"鍒囧埌鍒汉锛堟垨鍒囧洖鑷繁锛夆�斺��**涓嶆敼鎴戠殑鍏ュ骇宸ヤ綅**銆�
+     *
+     * 鍥磋灏辨槸闈犺繖涓姩浣滃疄鐜扮殑锛歚room`锛堢湅璋侊級涓� `currentSeat`锛堟垜鍧愬摢锛夊垎绂伙紝
+     * 涓よ�呬笉鍚屾椂 {@link getCanOperate} 涓� false 鈫� 鐣岄潰鍙锛屾湇鍔$鍙︽湁涓�閬撶‖鏍¢獙銆�
+     *
+     * @param bed       瑕佹煡鐪嬬殑宸ヤ綅锛堟潵鑷伐浣嶅垪琛級
+     * @param ownerName 璇ュ伐浣嶅綋鍓嶄富浜虹殑濮撳悕锛堟棤涓绘椂浼� null锛�
+     */
+    async setViewTargetAction(bed: RoomVO, ownerName?: string | null) {
+      this.room!.id = bed.id ?? null
+      this.room!.roomId = bed.roomId ?? null
+      this.room!.roomName = bed.roomName ?? null
+      this.room!.bedNo = bed.bedNo ?? null
+      this.room!.ip = bed.ip ?? null
+      this.room!.status = bed.status ?? null
+      this.room!.docId = bed.docId ?? null
+      this.room!.docName = bed.docName ?? null
+      this.room!.checkTypes = bed.checkTypes ?? null
+      this.room!.opType = bed.opType ?? null
+      this.isSetRoom = true
+      this.viewOwnerName = ownerName ?? bed.docName ?? null
+      this.viewTargetVacant = bed.docId == null
+      wsCache.set(CACHE_KEY.ROOM, {
+        isSetRoom: true,
+        room: this.room,
+        currentSeat: this.currentSeat,
+        mySeats: this.mySeats
+      })
+    },
+    /**
+     * 銆愪富/鍥磋銆戝埛鏂�"鎴戞鍦ㄧ湅鐨勫伐浣�"鐨勬渶鏂扮姸鎬侊紙鐢ㄤ簬鍙戠幇涓讳汉宸茬搴э級銆�
+     *
+     * 鍙洿鏂� docId/docName 涓� vacant 鏍囪锛屼笉鏀瑰彉"鎴戝湪鐪嬪摢涓伐浣�"銆�
+     * 鎸夌‘璁ゅ彛寰勶細涓讳汉绂诲骇鍚�**鍙彁绀恒�佷笉鑷姩鎺ョ**銆�
+     */
+    refreshViewTargetState(bed: RoomVO) {
+      if (!this.room || String(this.room.roomId) !== String(bed.roomId) || this.room.bedNo !== bed.bedNo) {
+        return
+      }
+      this.room.docId = bed.docId ?? null
+      this.room.docName = bed.docName ?? null
+      this.viewOwnerName = bed.docName ?? null
+      this.viewTargetVacant = bed.docId == null
     },
     // 鍖荤敓绂诲骇
     async clearRoomInfoAction() {
@@ -72,18 +301,310 @@
       this.room.docName = null
 
       this.isSetRoom = false
+      // 椤舵爮鏍囩鍚屾娓呯┖锛堢搴у悗鏄剧ず"鏈叆搴�"锛�
+      this.currentSeat = null
+      // 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戠搴у悗 mySeats 涓�骞舵竻绌�
+      this.mySeats = []
+      this.viewOwnerName = null
+      this.viewTargetVacant = false
 
-      // 鏇存柊 cache
-      const roomInfo = wsCache.get(CACHE_KEY.ROOM)
-      if (roomInfo) {
-        roomInfo.room = null
-        roomInfo.isSetRoom = false
-        wsCache.set(CACHE_KEY.ROOM, roomInfo)
+      // 娓呯紦瀛橈紙涓庝笂闈㈢殑鍐欏叆瀵瑰簲锛岄伩鍏嶄笅娆″惎鍔ㄦ妸宸茬搴х殑璇婂鎭㈠鍑烘潵锛�
+      wsCache.delete(CACHE_KEY.ROOM)
+    },
+    /**
+     * 銆愪慨澶嶃�戝簲鐢ㄥ惎鍔ㄦ椂浠� sessionStorage 鎭㈠銆屽尰鐢熷凡閫夎瘖瀹ゃ�嶃��
+     *
+     * 涓轰粈涔堥渶瑕侊細`isSetRoom` 鏄唴瀛樻�侊紝鍒锋柊鍗充涪锛涜�屽悇鍖荤敓椤甸潰閮介潬瀹冨喅瀹氭槸鍚﹀彇鏁般��
+     * 鎭㈠鏃跺悓鏃舵牎楠岃瘖瀹や俊鎭畬鏁达紙roomId + bedNo锛夛紝閬垮厤鑴忕紦瀛樻寚鍚戜笉瀛樺湪鐨勫伐浣嶃��
+     */
+    restoreRoomInfo() {
+      if (this.isSetRoom && this.room?.roomId != null) {
+        return // 鍐呭瓨閲屽凡鏈夛紝鏃犻渶鎭㈠
+      }
+      const cached = wsCache.get(CACHE_KEY.ROOM)
+      if (cached?.isSetRoom && cached?.room?.roomId != null && cached?.room?.bedNo != null) {
+        this.room = { ...this.room, ...cached.room }
+        this.isSetRoom = true
+        // 銆愪富/鍥磋銆戣繛"鎴戝叆搴х殑宸ヤ綅"涓�璧锋仮澶嶏細鍚﹀垯鍒锋柊鍚� getCanOperate 浼氳鍒や负鍙
+        // 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戜紭鍏堟仮澶嶅畬鏁村垪琛紱鏃х紦瀛樻病鏈� mySeats 鏃剁敤 currentSeat 鍏滃簳
+        if (Array.isArray(cached?.mySeats) && cached.mySeats.length > 0) {
+          this.mySeats = cached.mySeats
+        } else if (cached?.currentSeat) {
+          this.mySeats = [cached.currentSeat]
+        }
+        if (cached?.currentSeat) {
+          this.currentSeat = cached.currentSeat
+          this.viewOwnerName = cached.currentSeat.docName ?? null
+          // 鈿狅笍銆愭湰娆′慨澶嶃��**涓嶆妸"鍥磋瀵硅薄"甯﹀埌涓嬩竴娆℃墦寮�**锛�
+          // 缂撳瓨閲岀殑 room 鑻ヤ笌鎴戝叆搴х殑宸ヤ綅涓嶆槸鍚屼竴涓紝璇存槑涓婃鍦ㄥ洿瑙傚埆浜恒��
+          // 鑻ョ収鍘熸牱鎭㈠锛屽埛鏂板悗宸ヤ綔鍙颁細涓�鐩存樉绀�**鍒汉宸ヤ綅鐨勯槦鍒�**锛堢湅鐫�鍍�"鎮h�呭垪琛ㄧ┖浜�"锛夛紝
+          // 鑰屽尰鐢熶互涓哄湪鐪嬭嚜宸辩殑宸ヤ綅 鈥斺�� 鐜板満鍙嶉鐨�"鍒楄〃鏁翠釜鏄┖鐨�"寰堝彲鑳藉氨鏄繖涓��
+          // 鍥磋鏄�"褰撳墠浼氳瘽閲岀殑涓存椂閫夋嫨"锛屽埛鏂�/閲嶅紑涓�寰嬪洖鍒版垜鑷繁鐨勫伐浣嶃��
+          const seat = cached.currentSeat
+          const cachedIsMine =
+            String(cached.room.roomId) === String(seat.roomId) && cached.room.bedNo === seat.bedNo
+          if (!cachedIsMine) {
+            this.room = {
+              ...this.room,
+              id: seat.id ?? null,
+              roomId: seat.roomId ?? null,
+              roomName: seat.roomName ?? null,
+              bedNo: seat.bedNo ?? null,
+              ip: seat.ip ?? null,
+              status: seat.status ?? null,
+              docId: seat.docId ?? null,
+              docName: seat.docName ?? null,
+              checkTypes: seat.checkTypes ?? null,
+              opType: seat.opType ?? null
+            }
+            this.viewOwnerName = seat.docName ?? null
+            this.viewTargetVacant = false
+            console.info('[room] 涓婃鍦ㄥ洿瑙傚叾浠栧伐浣嶏紝鏈鎵撳紑鍥炲埌鎴戣嚜宸辩殑宸ヤ綅', this.room)
+          }
+        }
       }
     },
+
+    /* ==================== 閫夋嫨宸ヤ綅妯″紡锛�0/1/2/3锛� ==================== */
+
+    /** 璇诲彇銆岄�夋嫨宸ヤ綅妯″紡銆嶅弬鏁帮細浼氳瘽鍐呭彧鍙栦竴娆★紱鍙栦笉鍒版垨闈炴硶涓�寰嬫寜 0锛堝師琛屼负锛夛紝閬垮厤褰卞搷瀛橀噺閮ㄧ讲 */
+    async loadRoomSelectMode(force = false): Promise<number> {
+      if (!force && this.roomSelectMode >= 0) {
+        return this.roomSelectMode
+      }
+      let mode = 0
+      try {
+        const v = await getConfigKey(ROOM_SELECT_MODE_KEY)
+        const n = Number(v)
+        if ([0, 1, 2, 3].includes(n)) {
+          mode = n
+        }
+      } catch (e) {
+        mode = 0
+      }
+      this.roomSelectMode = mode
+      return mode
+    },
+
+    /**
+     * 妯″紡 1/3锛氭寜鏈満IP鑷姩鍖归厤骞惰嚜鍔ㄥ叆搴с��
+     *
+     * 銆愭湰娆′慨澶嶃�戞敼涓�**璋冪敤鍚庣涓�涓帴鍙�**锛坄POST /clinic/room/auto-seat`锛夊畬鎴�
+     * 銆屽尮閰� 鈫� 鍒ゅ畾 鈫� 钀藉骇銆嶏紝涓嶅啀鐢卞墠绔嫾 `getIpBed 鈫� bindIpToBed 鈫� bedDoctorOn` 涓夋锛�
+     * 鍘熸潵浠讳竴姝ュけ璐ラ兘鍙兘鍥炶惤"鎵嬮�夊伐浣�"锛岀幇鍦鸿〃鐜板氨鏄�"鐧诲綍鍚庢病鏈夎嚜鍔ㄥ叆搴�"涓旂湅涓嶅嚭鍘熷洜銆�
+     * 鐜板湪鍚庣鐩存帴杩斿洖 `reason`锛坕p_invalid / nomatch / multi / occupied / error锛変笌璇嗗埆鍒扮殑 IP銆�
+     *
+     * 鍖归厤绛栫暐锛堝悗绔疄鐜帮紝涓庡師鍓嶇閫昏緫涓�鑷达級锛�
+     * 路 宸茬粡鏄垜鐨勫伐浣�      鈫� 骞傜瓑鎭㈠锛堝埛鏂�/閲嶈繘/涓婃娌℃甯哥搴ч兘璧拌繖鏉★級锛�
+     * 路 绌洪棽鍊欓��**鎭板ソ 1 涓�** 鈫� 鍏ュ骇锛堟湇鍔$鍚屾椂淇濊瘉"涓�涓敤鎴峰彧鍗犱竴涓伐浣�"锛夛紱
+     * 路 绌洪棽鍊欓��**澶氫釜**     鈫� 涓嶆搮鑷叆搴э紙reason='multi'锛夛紝浜や汉宸ョ偣閫夛紱
+     * 路 鍏ㄨ浠栦汉鍗犵敤        鈫� reason='occupied' + 鍗犵敤浜哄鍚嶏紙**涓嶆尋鎺�**宸插湪鎿嶄綔鐨勪汉锛夈��
+     */
+    async tryAutoSeatByIp(): Promise<AutoSeatResult> {
+      try {
+        const resp: any = (await RoomApi.autoSeat()) || {}
+        const clientIp: string | null = resp.clientIp ?? null
+        if (resp.seated) {
+          await this.setRoomInfoAction({
+            id: resp.id ?? null,
+            roomId: resp.roomId ?? null,
+            roomName: resp.roomName ?? null,
+            bedNo: resp.bedNo ?? null,
+            ip: resp.ip ?? null,
+            status: resp.status ?? null,
+            docId: resp.docId ?? null,
+            docName: resp.docName ?? null,
+            checkTypes: resp.checkTypes ?? null,
+            opType: resp.opType ?? null
+          } as RoomVO)
+          // 椤舵爮"褰撳墠宸ヤ綅"鏍囩鐢�
+          this.currentSeat = resp as RoomSeatResp
+          // 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戞妸璇ュ伐浣嶅苟鍏ユ垜鐨勫伐浣嶅垪琛�
+          this.upsertMySeat(this.currentSeat)
+          console.info('[room] 鎸塈P鑷姩鍏ュ骇鎴愬姛', resp)
+          return { seated: true, clientIp }
+        }
+        console.info('[room] 鎸塈P鑷姩鍏ュ骇鏈垚鍔�', resp)
+        return {
+          seated: false,
+          clientIp,
+          reason: (resp.reason || 'nomatch') as AutoSeatResult['reason'],
+          occupiedBy: resp.ownerName ?? resp.docName ?? null,
+          // 銆愭湰闇�姹傘�戣鍗犵敤鏃跺悗绔細甯﹀洖鍊欓�夊伐浣嶄俊鎭紝渚�"鏄惁鎺ョ"纭鍚庣洿鎺ヨ皟鐢�
+          roomId: resp.roomId ?? null,
+          bedNo: resp.bedNo ?? null,
+          roomName: resp.roomName ?? null
+        }
+      } catch (e) {
+        console.error('[tryAutoSeatByIp] 鑷姩鍏ュ骇鎺ュ彛寮傚父', e)
+        return { seated: false, clientIp: null, reason: 'error' }
+      }
+    },
+
+    /**
+     * 鏌ヨ骞惰褰�"褰撳墠鐧诲綍浜哄凡鍏ュ骇鐨勫伐浣�"锛堢郴缁熼《鏍忔樉绀虹敤锛夈��
+     *
+     * 鍦烘櫙锛氬埛鏂伴〉闈� / 鏂板紑鏍囩椤� / 鐩存帴杩涚郴缁燂紝鍐呭瓨閲岀殑 `currentSeat` 鍙兘鏄┖鐨勶紝
+     * 浣嗗悗绔煡閬撴垜鏄摢涓伐浣嶇殑涓讳汉 鈥斺�� 鏌ヤ竴娆″氨鑳借椤舵爮涓庡悇椤甸潰鍙e緞涓�鑷淬��
+     *
+     * 銆愪富/鍥磋銆戞敞鎰忥細**鍙湁鍦ㄦ垜杩樻病閫�"瑕佺湅鐨勫伐浣�"銆佹垨鎴戠湅鐨勫氨鏄嚜宸卞伐浣嶆椂**锛�
+     * 鎵嶆妸鏌ョ湅鐩爣涔熷榻愬埌鎴戠殑宸ヤ綅锛涘惁鍒欎細鎶婃鍦ㄥ洿瑙傜殑瀵硅薄瑕嗙洊鎺夛紙鍥磋灏辨病娉曠敤浜嗭級銆�
+     */
+    async fetchMySeat(): Promise<RoomSeatResp | null> {
+      try {
+        // 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戜紭鍏堝彇"鎴戠殑鍏ㄩ儴宸ヤ綅"锛涘悗绔湭鍗囩骇锛�404锛夋椂鍥為��鍗曞�兼帴鍙�
+        let seats: RoomSeatResp[] = []
+        let fallbackSingle: any = null
+        try {
+          const list: any = await RoomApi.getMySeats()
+          seats = Array.isArray(list) ? (list as RoomSeatResp[]) : []
+        } catch (e) {
+          console.warn('[fetchMySeat] /my-seats 涓嶅彲鐢紝鍥為�� /my-seat', e)
+          const resp: any = (await RoomApi.getMySeat()) || {}
+          if (resp.seated) {
+            fallbackSingle = resp
+            seats = [resp as RoomSeatResp]
+          }
+        }
+
+        if (seats.length === 0 && !fallbackSingle) {
+          this.mySeats = []
+          this.currentSeat = null
+          return null
+        }
+
+        this.applyMySeats(seats)
+
+        const noViewTarget = !this.isSetRoom || this.room.roomId == null || !this.room.bedNo
+        const viewingMine = !noViewTarget && this.getCanOperate
+        if (!noViewTarget && !viewingMine) {
+          // 姝e湪鍥磋鍒汉 鈫� 鍙洿鏂�"鎴戠殑宸ヤ綅鍒楄〃"锛屼笉鍔ㄦ煡鐪嬬洰鏍�
+          console.info('[room] 宸插埛鏂版垜鐨勫伐浣嶅垪琛紙褰撳墠姝e湪鍥磋浠栦汉锛屼笉鍒囨崲鏌ョ湅鐩爣锛�', seats)
+          return this.currentSeat
+        }
+
+        // 鏈�夋煡鐪嬬洰鏍� / 鐪嬬殑灏辨槸鑷繁鐨勬煇涓伐浣� 鈫� 瀵归綈鍒拌宸ヤ綅锛堜紭鍏堝綋鍓嶆煡鐪嬬殑閭d釜锛�
+        const target = this.currentSeat ?? seats[0]
+        if (target) {
+          await this.setRoomInfoAction({
+            id: target.id ?? null,
+            roomId: target.roomId ?? null,
+            roomName: target.roomName ?? null,
+            bedNo: target.bedNo ?? null,
+            ip: target.ip ?? null,
+            status: target.status ?? null,
+            docId: target.docId ?? null,
+            docName: target.docName ?? null,
+            checkTypes: target.checkTypes ?? null,
+            opType: target.opType ?? null
+          } as RoomVO)
+        }
+        console.info('[room] 鏌ヨ鍒版垜鐨勫伐浣嶅垪琛�', seats)
+        return this.currentSeat
+      } catch (e) {
+        console.error('[fetchMySeat] 鏌ヨ褰撳墠宸ヤ綅澶辫触', e)
+        return null
+      }
+    },
+
+    /**
+     * 銆愭湰闇�姹偮锋帴鐝嚜鍔╂帴绠°�戞妸鎸囧畾宸ヤ綅浠�"鍒汉鍗犵敤"鏀瑰埌鎴戝悕涓嬶紝骞跺垏鎹㈠埌璇ュ伐浣嶃��
+     *
+     * @returns 鏄惁鎺ョ鎴愬姛
+     */
+    async takeOverBed(roomId: number | null, bedNo: string | null): Promise<boolean> {
+      if (roomId == null || !bedNo) return false
+      try {
+        await RoomApi.takeOverBed({ roomId, bedNo })
+        // 鍒锋柊"鎴戠殑宸ヤ綅"锛堟鏃跺悗绔凡鎶婅宸ヤ綅 doc_id 璁句负鎴戯級
+        await this.fetchMySeat()
+        // 鍒囨崲鍒拌宸ヤ綅锛堣嫢鏄垰鎺ョ銆佷笖涓嶅湪鍒楄〃閲岋紝鍒欐墜鍔ㄨˉ涓�鏉″苟鍒囪繃鍘伙級
+        let seat = (this.mySeats || []).find(
+          (s) => String(s.roomId) === String(roomId) && String(s.bedNo) === String(bedNo)
+        )
+        if (!seat) {
+          const userStore = useUserStore()
+          const curUser: any = userStore.getUser || {}
+          seat = {
+            seated: true,
+            roomId,
+            bedNo,
+            roomName: this.room?.roomName ?? null,
+            docId: curUser.id ?? null,
+            docName: curUser.nickname ?? null
+          } as RoomSeatResp
+          this.mySeats = [...(this.mySeats || []), seat]
+        }
+        await this.setRoomInfoAction({
+          id: seat.id ?? null,
+          roomId: seat.roomId ?? null,
+          roomName: seat.roomName ?? null,
+          bedNo: seat.bedNo ?? null,
+          ip: seat.ip ?? null,
+          status: seat.status ?? null,
+          docId: seat.docId ?? null,
+          docName: seat.docName ?? null,
+          checkTypes: seat.checkTypes ?? null,
+          opType: seat.opType ?? null
+        } as RoomVO)
+        console.info('[room] 鎺ョ宸ヤ綅鎴愬姛', roomId, bedNo)
+        return true
+      } catch (e) {
+        console.error('[takeOverBed] 鎺ョ宸ヤ綅澶辫触', e)
+        return false
+      }
+    },
+
+    /**
+     * 鍏ュ骇锛坆ed-doctor-on锛�+ 鍐欏叆浼氳瘽銆傛娊鍑烘潵缁欍�屾墜鍔ㄩ�夋嫨銆嶅拰銆屾寜IP鑷姩鍖归厤銆嶅叡鐢ㄣ��
+     * @returns 鏄惁鍏ュ骇鎴愬姛
+     */
+    async seatRoom(bed: Partial<RoomVO>): Promise<boolean> {
+      const userStore = useUserStore()
+      const curUser: any = userStore.getUser || {}
+      const tempRoomVO: RoomVO = {
+        id: bed.id ?? null,
+        roomId: bed.roomId ?? null,
+        roomName: bed.roomName ?? null,
+        bedNo: bed.bedNo ?? null,
+        ip: bed.ip ?? null,
+        status: bed.status ?? null,
+        docId: curUser.id ?? null,
+        docName: curUser.nickname ?? null,
+        checkTypes: bed.checkTypes ?? null,
+        opType: bed.opType ?? null
+      }
+      const data = await DoctorApi.bedDoctorOn(tempRoomVO)
+      if (data !== 0) {
+        console.warn('[seatRoom] bedDoctorOn 杩斿洖闈� 0锛�', data)
+        return false
+      }
+      await this.setRoomInfoAction(tempRoomVO)
+      // 銆愭湰闇�姹偮蜂竴浜哄宸ヤ綅銆戝叆搴у悗鍒锋柊"鎴戠殑鍏ㄩ儴宸ヤ綅"锛堝彲鑳藉凡鏈夊涓級
+      await this.fetchMySeat()
+      return true
+    },
+
+    /**
+     * 妯″紡 2/3锛氭妸鏈満IP鍐欏埌鎸囧畾宸ヤ綅銆�
+     * @returns { code, conflict, msg } 鈥斺�� code=409 琛ㄧず璇P宸茬粦鍦ㄥ埆鐨勫伐浣嶏紙conflict=true锛夛紝闇�纭鍚庡甫 force 閲嶈皟
+     */
+    async bindCurrentIp(bedId: number, force = false): Promise<any> {
+      const resp: any = await RoomApi.bindIpToBed(bedId, force)
+      return resp || {}
+    }
   }
 })
 
 export const useRoomStoreWithOut = () => {
   return useRoomStore(store)
 }
+
+/**
+ * 鍦ㄥ簲鐢ㄥ惎鍔ㄩ樁娈佃皟鐢ㄤ竴娆★紙瑙� src/main.ts锛夛紝鎶娿�屽凡閫夎瘖瀹ゃ�嶄粠 sessionStorage 鎭㈠鍒� store銆�
+ *
+ * 蹇呴』鏃╀簬璺敱瀹堝崼涓庡悇鍖荤敓椤甸潰鐨� onMounted锛屽惁鍒欓〉闈細鍥犱负 isSetRoom=false 鑰屽畬鍏ㄤ笉鍙栨暟銆�
+ */
+export const setupRoomStorePersistence = () => {
+  useRoomStoreWithOut().restoreRoomInfo()
+}

--
Gitblit v1.9.3