eight
2024-08-28 2bc74ebfec4a30beddc66fd55be4947e5f7cf498
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { store } from '@/store'
import { defineStore } from 'pinia'
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()
 
interface UserVO {
  id: number
  avatar: string
  nickname: string
  deptId: number
}
 
interface UserInfoVO {
  // USER 缓存
  permissions: string[]
  roles: string[]
  isSetUser: boolean
  user: UserVO
 
  // 医生诊室选择
  isSetRoom: boolean
  room: RoomVO | null
}
 
export const useUserStore = defineStore('admin-user', {
  state: (): UserInfoVO => ({
    permissions: [],
    roles: [],
    isSetUser: false,
    user: {
      id: 0,
      avatar: '',
      nickname: '',
      deptId: 0
    },
    // 医生诊室选择
    isSetRoom: false,
    room: {
      id: 0,
      roomId: 0,
      roomName: "",
      bedNo: "",
      status: null,
      docId: null,
      docName: null
    }
  }),
  getters: {
    getPermissions(): string[] {
      return this.permissions
    },
    getRoles(): string[] {
      return this.roles
    },
    getIsSetUser(): boolean {
      return this.isSetUser
    },
    getUser(): UserVO {
      return this.user
    },
    // 医生诊室选择
    getIsSetRoom(): boolean {
      return this.isSetRoom
    },
    getRoom(): RoomVO | null {
      return this.room
    }
  },
  actions: {
    async setUserInfoAction() {
      if (!getAccessToken()) {
        this.resetState()
        return null
      }
      let userInfo = wsCache.get(CACHE_KEY.USER)
      if (!userInfo) {
        userInfo = await getInfo()
      }
      this.permissions = userInfo.permissions
      this.roles = userInfo.roles
      this.user = userInfo.user
      this.isSetUser = true
      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`
      this.user.avatar = avatar
      userInfo.user.avatar = avatar
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async setUserNicknameAction(nickname: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
      this.user.nickname = nickname
      userInfo.user.nickname = nickname
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async loginOut() {
      await loginOut()
      removeToken()
      deleteUserCache() // 删除用户缓存
      this.resetState()
    },
    resetState() {
      this.permissions = []
      this.roles = []
      this.isSetUser = false
      this.user = {
        id: 0,
        avatar: '',
        nickname: '',
        deptId: 0
      }
      // 医生诊室选择
      this.isSetRoom = false
      this.room = {
        id: 0,
        roomId: 0,
        roomName: "",
        bedNo: "",
        status: null,
        docId: null,
        docName: null
      }
    }
  }
})
 
export const useUserStoreWithOut = () => {
  return useUserStore(store)
}