UnisKB/ui/src/stores/modules/user.ts

143 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-10-19 10:18:45 +00:00
import { defineStore } from 'pinia'
2024-07-23 10:21:46 +00:00
import { type Ref } from 'vue'
2023-10-20 03:26:14 +00:00
import type { User } from '@/api/type/user'
2024-09-02 11:05:36 +00:00
import { cloneDeep } from 'lodash'
2023-10-19 10:18:45 +00:00
import UserApi from '@/api/user'
2024-07-15 10:53:10 +00:00
import ThemeApi from '@/api/theme'
2024-07-16 09:32:27 +00:00
import { useElementPlusTheme } from 'use-element-plus-theme'
2023-10-19 10:18:45 +00:00
2023-11-02 01:56:14 +00:00
export interface userStateTypes {
2023-11-30 10:50:42 +00:00
userType: number // 1 系统操作者 2 对话用户
2023-10-19 10:18:45 +00:00
userInfo: User | null
token: any
2024-03-21 08:26:03 +00:00
version?: string
accessToken?: string
2024-07-10 04:22:41 +00:00
XPACK_LICENSE_IS_VALID: false
isXPack: false
2024-07-15 10:53:10 +00:00
themeInfo: any
2023-10-19 10:18:45 +00:00
}
const useUserStore = defineStore({
id: 'user',
2023-11-02 01:56:14 +00:00
state: (): userStateTypes => ({
2023-11-30 10:50:42 +00:00
userType: 1,
2023-10-19 10:18:45 +00:00
userInfo: null,
2024-03-21 08:26:03 +00:00
token: '',
version: '',
2024-07-10 04:22:41 +00:00
XPACK_LICENSE_IS_VALID: false,
2024-07-15 10:53:10 +00:00
isXPack: false,
themeInfo: null
2023-10-19 10:18:45 +00:00
}),
actions: {
2024-07-22 10:52:56 +00:00
showXpack() {
return this.isXPack
},
2024-07-16 09:32:27 +00:00
isDefaultTheme() {
return !this.themeInfo?.theme || this.themeInfo?.theme === '#3370FF'
},
setTheme(data: any) {
2024-07-23 10:21:46 +00:00
const { changeTheme } = useElementPlusTheme(this.themeInfo?.theme)
2024-07-16 09:32:27 +00:00
changeTheme(data?.['theme'])
2024-09-02 11:05:36 +00:00
this.themeInfo = cloneDeep(data)
2024-07-16 09:32:27 +00:00
},
2024-07-10 10:04:38 +00:00
isExpire() {
return this.isXPack && !this.XPACK_LICENSE_IS_VALID
},
isEnterprise() {
2024-07-10 04:22:41 +00:00
return this.isXPack && this.XPACK_LICENSE_IS_VALID
},
2023-10-19 10:18:45 +00:00
getToken(): String | null {
if (this.token) {
return this.token
}
return this.userType === 1 ? localStorage.getItem('token') : this.getAccessToken()
},
getAccessToken() {
const accessToken = sessionStorage.getItem('accessToken')
if (accessToken) {
return accessToken
}
return localStorage.getItem('accessToken')
2023-10-19 10:18:45 +00:00
},
getPermissions() {
if (this.userInfo) {
2024-07-10 04:22:41 +00:00
return this.isXPack && this.XPACK_LICENSE_IS_VALID
? [...this.userInfo?.permissions, 'x-pack']
: this.userInfo?.permissions
2023-10-19 10:18:45 +00:00
} else {
return []
}
},
getRole() {
if (this.userInfo) {
return this.userInfo?.role
} else {
return ''
}
},
2023-11-30 10:50:42 +00:00
changeUserType(num: number) {
this.userType = num
},
2024-03-21 08:26:03 +00:00
2024-07-11 10:28:01 +00:00
async asyncGetProfile() {
return new Promise((resolve, reject) => {
UserApi.getProfile()
2024-07-18 08:17:55 +00:00
.then(async (ok) => {
2024-07-11 10:28:01 +00:00
this.version = ok.data?.version || '-'
this.isXPack = ok.data?.IS_XPACK
this.XPACK_LICENSE_IS_VALID = ok.data?.XPACK_LICENSE_IS_VALID
2024-07-18 10:20:54 +00:00
2024-07-18 08:17:55 +00:00
if (this.isEnterprise()) {
await this.theme()
}
2024-07-11 10:28:01 +00:00
resolve(ok)
})
.catch((error) => {
reject(error)
})
2024-03-21 08:26:03 +00:00
})
},
2024-07-23 10:21:46 +00:00
async theme(loading?: Ref<boolean>) {
return await ThemeApi.getThemeInfo(loading).then((ok) => {
2024-07-16 10:50:44 +00:00
this.setTheme(ok.data)
2024-08-22 07:01:35 +00:00
// window.document.title = this.themeInfo['title'] || 'MaxKB'
// const link = document.querySelector('link[rel="icon"]') as any
// if (link) {
// link['href'] = this.themeInfo['icon'] || '/favicon.ico'
// }
2024-07-15 10:53:10 +00:00
})
},
2023-10-19 10:18:45 +00:00
async profile() {
2024-07-16 10:50:44 +00:00
return UserApi.profile().then(async (ok) => {
2023-10-19 10:18:45 +00:00
this.userInfo = ok.data
2024-07-16 10:15:10 +00:00
return this.asyncGetProfile()
2023-10-19 10:18:45 +00:00
})
},
2024-07-11 11:51:28 +00:00
async login(auth_type: string, username: string, password: string) {
return UserApi.login(auth_type, { username, password }).then((ok) => {
2023-10-19 10:18:45 +00:00
this.token = ok.data
localStorage.setItem('token', ok.data)
return this.profile()
})
},
async logout() {
return UserApi.logout().then(() => {
localStorage.removeItem('token')
return true
})
2024-07-11 11:51:28 +00:00
},
async getAuthType() {
return UserApi.getAuthType().then((ok) => {
return ok.data
})
2023-10-19 10:18:45 +00:00
}
}
})
export default useUserStore