eight
2024-08-28 2bc74ebfec4a30beddc66fd55be4947e5f7cf498
src/store/modules/user.ts
@@ -3,6 +3,7 @@
import { getAccessToken, removeToken } from '@/utils/auth'
import { CACHE_KEY, useCache, deleteUserCache } from '@/hooks/web/useCache'
import { getInfo, loginOut } from '@/api/login'
import {RoomVO} from "@/api/ecg/room";
const { wsCache } = useCache()
@@ -19,6 +20,10 @@
  roles: string[]
  isSetUser: boolean
  user: UserVO
  // 医生诊室选择
  isSetRoom: boolean
  room: RoomVO | null
}
export const useUserStore = defineStore('admin-user', {
@@ -31,6 +36,17 @@
      avatar: '',
      nickname: '',
      deptId: 0
    },
    // 医生诊室选择
    isSetRoom: false,
    room: {
      id: 0,
      roomId: 0,
      roomName: "",
      bedNo: "",
      status: null,
      docId: null,
      docName: null
    }
  }),
  getters: {
@@ -45,6 +61,13 @@
    },
    getUser(): UserVO {
      return this.user
    },
    // 医生诊室选择
    getIsSetRoom(): boolean {
      return this.isSetRoom
    },
    getRoom(): RoomVO | null {
      return this.room
    }
  },
  actions: {
@@ -64,6 +87,35 @@
      wsCache.set(CACHE_KEY.USER, userInfo)
      wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
    },
    // 医生入座
    async setRoomInfoAction(room: RoomVO) {
      // 更新 store
      this.room = room
      this.isSetRoom = true
      // 更新 cache
      const userInfo2 = wsCache.get(CACHE_KEY.USER)
      if (userInfo2) {
        userInfo2.room = room
        userInfo2.isSetRoom = true
        wsCache.set(CACHE_KEY.USER, userInfo2)
      }
    },
    // 医生离座
    async clearRoomInfoAction() {
      // 清 store
      this.room = null
      this.isSetRoom = false
      // 更新 cache
      const userInfo2 = wsCache.get(CACHE_KEY.USER)
      if (userInfo2) {
        userInfo2.room = null
        userInfo2.isSetRoom = false
        wsCache.set(CACHE_KEY.USER, userInfo2)
      }
    },
    async setUserAvatarAction(avatar: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
@@ -94,6 +146,17 @@
        nickname: '',
        deptId: 0
      }
      // 医生诊室选择
      this.isSetRoom = false
      this.room = {
        id: 0,
        roomId: 0,
        roomName: "",
        bedNo: "",
        status: null,
        docId: null,
        docName: null
      }
    }
  }
})