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

163 lines
4.6 KiB
TypeScript
Raw Normal View History

2025-06-11 08:38:48 +00:00
import { defineStore } from 'pinia'
import { type Ref } from 'vue'
import type { User } from '@/api/type/user'
2025-04-25 08:27:13 +00:00
import UserApi from '@/api/user/user'
2025-06-05 11:47:47 +00:00
import LoginApi from '@/api/user/login'
2025-06-11 08:38:48 +00:00
import { useLocalStorage } from '@vueuse/core'
2025-06-13 03:00:15 +00:00
2025-06-11 08:38:48 +00:00
import { localeConfigKey, getBrowserLang } from '@/locales/index'
2025-05-12 10:19:28 +00:00
import useThemeStore from './theme'
2025-07-03 09:28:35 +00:00
import { defaultPlatformSetting } from '@/utils/theme'
2025-06-13 03:00:15 +00:00
import useLoginStore from './login'
2025-06-05 11:47:47 +00:00
2025-04-16 11:10:00 +00:00
export interface userStateTypes {
userType: number // 1 系统操作者 2 对话用户
2025-04-25 08:27:13 +00:00
userInfo: User | null
2025-04-16 11:10:00 +00:00
version?: string
2025-06-11 08:38:48 +00:00
license_is_valid: boolean
edition: 'CE' | 'PE' | 'EE'
2025-06-16 04:08:09 +00:00
workspace_id: string
2025-06-19 13:43:57 +00:00
workspace_list: Array<any>
2025-04-16 11:10:00 +00:00
}
2025-06-13 03:00:15 +00:00
const useUserStore = defineStore('user', {
2025-04-16 11:10:00 +00:00
state: (): userStateTypes => ({
2025-04-25 08:27:13 +00:00
userType: 1, // 1 系统操作者 2 对话用户
userInfo: null,
2025-04-16 11:10:00 +00:00
version: '',
2025-06-11 08:38:48 +00:00
license_is_valid: false,
edition: 'CE',
2025-06-18 08:57:46 +00:00
workspace_id: '',
2025-06-19 13:43:57 +00:00
workspace_list: [],
2025-04-16 11:10:00 +00:00
}),
actions: {
getLanguage() {
return this.userType === 1
? localStorage.getItem('MaxKB-locale') || getBrowserLang()
: sessionStorage.getItem('language') || getBrowserLang()
},
2025-06-20 19:55:48 +00:00
2025-06-17 10:38:00 +00:00
setWorkspaceId(workspace_id: string) {
this.workspace_id = workspace_id
localStorage.setItem('workspace_id', workspace_id)
2025-04-25 08:27:13 +00:00
},
2025-06-16 04:08:09 +00:00
getWorkspaceId(): string | null {
2025-06-18 08:57:46 +00:00
this.workspace_id = this.workspace_id || localStorage.getItem('workspace_id') || 'default'
return this.workspace_id
2025-06-16 04:08:09 +00:00
},
2025-04-16 11:10:00 +00:00
2025-04-25 08:27:13 +00:00
getPermissions() {
if (this.userInfo) {
2025-06-16 09:02:36 +00:00
if (this.isEE()) {
2025-06-16 10:23:46 +00:00
return [...this.userInfo?.permissions, 'X-PACK-EE']
2025-06-16 09:02:36 +00:00
} else if (this.isPE()) {
2025-06-16 10:23:46 +00:00
return [...this.userInfo?.permissions, 'X-PACK-PE']
2025-06-16 09:02:36 +00:00
}
return this.userInfo?.permissions
2025-06-16 09:02:36 +00:00
} else {
return []
2025-04-25 08:27:13 +00:00
}
},
2025-06-16 10:23:46 +00:00
getEdition() {
if (this.userInfo) {
if (this.isEE()) {
return 'X-PACK-EE'
} else if (this.isPE()) {
return 'X-PACK-PE'
} else {
return 'X-PACK-CE'
}
}
return 'X-PACK-CE'
},
2025-04-25 08:27:13 +00:00
getRole() {
if (this.userInfo) {
return this.userInfo?.role
} else {
2025-06-16 09:02:36 +00:00
return []
2025-04-25 08:27:13 +00:00
}
},
2025-06-17 10:38:00 +00:00
2025-06-05 11:47:47 +00:00
showXpack() {
2025-06-11 08:38:48 +00:00
return this.edition != 'CE'
},
isEnterprise() {
return this.edition != 'CE' && !this.license_is_valid
2025-06-05 11:47:47 +00:00
},
isExpire() {
2025-06-11 08:38:48 +00:00
return this.edition != 'CE' && !this.license_is_valid
2025-06-05 11:47:47 +00:00
},
2025-06-11 08:38:48 +00:00
isCE() {
return this.edition == 'CE' && this.license_is_valid
},
isPE() {
return this.edition == 'PE' && this.license_is_valid
},
isEE() {
2025-06-19 02:21:00 +00:00
return this.edition == 'EE' && this.license_is_valid
2025-06-05 11:47:47 +00:00
},
getEditionName() {
return this.edition
},
2025-06-17 10:38:00 +00:00
async profile(loading?: Ref<boolean>) {
return UserApi.getUserProfile(loading).then((ok) => {
this.userInfo = ok.data
2025-06-19 13:43:57 +00:00
const workspace_list =
ok.data.workspace_list && ok.data.workspace_list.length > 0
? ok.data.workspace_list
: [{ id: 'default', name: 'default' }]
const workspace_id = this.getWorkspaceId()
if (!workspace_id || !workspace_list.some((w) => w.id == workspace_id)) {
this.setWorkspaceId(workspace_list[0].id)
}
this.workspace_list = workspace_list
2025-06-17 10:38:00 +00:00
useLocalStorage<string>(localeConfigKey, 'en-US').value =
ok?.data?.language || this.getLanguage()
const theme = useThemeStore()
theme.setTheme()
return this.asyncGetProfile()
})
},
2025-04-16 11:10:00 +00:00
2025-06-17 10:38:00 +00:00
async asyncGetProfile() {
return new Promise((resolve, reject) => {
UserApi.getProfile()
.then(async (ok) => {
// this.version = ok.data?.version || '-'
this.license_is_valid = ok.data.license_is_valid
this.edition = ok.data.edition
2025-07-02 02:52:39 +00:00
this.version = ok.data.version
2025-06-20 19:55:48 +00:00
const theme = useThemeStore()
2025-06-17 10:38:00 +00:00
if (this.isEE() || this.isPE()) {
2025-06-20 19:55:48 +00:00
await theme.theme()
2025-06-17 10:38:00 +00:00
} else {
2025-07-03 09:28:35 +00:00
theme.setTheme()
2025-06-20 19:55:48 +00:00
theme.themeInfo = {
2025-06-17 10:38:00 +00:00
...defaultPlatformSetting,
}
}
resolve(ok)
})
.catch((error) => {
reject(error)
})
})
},
2025-06-05 11:47:47 +00:00
async postUserLanguage(lang: string, loading?: Ref<boolean>) {
return new Promise((resolve, reject) => {
LoginApi.postLanguage({ language: lang }, loading)
.then(async (ok) => {
useLocalStorage(localeConfigKey, 'en-US').value = lang
window.location.reload()
resolve(ok)
})
.catch((error) => {
reject(error)
})
})
2025-06-11 08:38:48 +00:00
},
2025-04-25 08:27:13 +00:00
},
2025-04-16 11:10:00 +00:00
})
2025-06-13 03:00:15 +00:00
export default useUserStore