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

162 lines
4.7 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-13 03:00:15 +00:00
import { cloneDeep } from 'lodash'
2025-06-11 09:41:53 +00:00
import ThemeApi from '@/api/system-settings/theme'
2025-06-11 08:38:48 +00:00
import { useLocalStorage } from '@vueuse/core'
2025-06-13 03:00:15 +00:00
// import { defaultPlatformSetting } from '@/utils/theme'
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-06-11 08:38:48 +00:00
import { useElementPlusTheme } from 'use-element-plus-theme'
import { defaultPlatformSetting } from '@/utils/theme.ts'
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-05 11:47:47 +00:00
themeInfo: any
2025-06-16 04:08:09 +00:00
workspace_id: string
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-05 11:47:47 +00:00
themeInfo: null,
2025-06-16 04:08:09 +00:00
workspace_id: 'default',
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-05 11:47:47 +00:00
isDefaultTheme() {
return !this.themeInfo?.theme || this.themeInfo?.theme === '#3370FF'
},
setTheme(data: any) {
2025-06-11 08:38:48 +00:00
const { changeTheme } = useElementPlusTheme(this.themeInfo?.theme)
2025-06-05 11:47:47 +00:00
changeTheme(data?.['theme'])
this.themeInfo = cloneDeep(data)
},
2025-04-28 07:23:25 +00:00
async profile(loading?: Ref<boolean>) {
2025-04-28 10:14:16 +00:00
return UserApi.getUserProfile(loading).then((ok) => {
2025-05-06 02:13:52 +00:00
this.userInfo = ok.data
useLocalStorage<string>(localeConfigKey, 'en-US').value =
ok?.data?.language || this.getLanguage()
2025-05-12 10:19:28 +00:00
const theme = useThemeStore()
theme.setTheme()
2025-06-05 11:47:47 +00:00
return this.asyncGetProfile()
2025-04-25 08:27:13 +00:00
})
},
2025-06-16 04:08:09 +00:00
getWorkspaceId(): string | null {
if (this.workspace_id) {
return this.workspace_id
}
const workspace_id = localStorage.getItem('workspace_id')
if (workspace_id) {
this.workspace_id = workspace_id
}
return workspace_id
},
2025-06-05 11:47:47 +00:00
async asyncGetProfile() {
return new Promise((resolve, reject) => {
UserApi.getProfile()
.then(async (ok) => {
// this.version = ok.data?.version || '-'
2025-06-11 08:38:48 +00:00
console.log(ok)
this.license_is_valid = ok.data.license_is_valid
this.edition = ok.data.edition
2025-04-16 11:10:00 +00:00
2025-06-11 08:38:48 +00:00
if (this.isEE() || this.isPE()) {
// await this.theme()
2025-06-05 11:47:47 +00:00
} else {
this.themeInfo = {
2025-06-11 08:38:48 +00:00
...defaultPlatformSetting,
2025-06-05 11:47:47 +00:00
}
}
resolve(ok)
})
.catch((error) => {
reject(error)
})
})
},
2025-04-16 11:10:00 +00:00
2025-04-25 08:27:13 +00:00
getPermissions() {
if (this.userInfo) {
return this.isXPack && this.XPACK_LICENSE_IS_VALID
? [...this.userInfo?.permissions, 'x-pack']
: this.userInfo?.permissions
} else {
return this.userInfo?.permissions
2025-04-25 08:27:13 +00:00
}
},
getRole() {
if (this.userInfo) {
return this.userInfo?.role
} else {
return ''
}
},
2025-06-05 11:47:47 +00:00
async theme(loading?: Ref<boolean>) {
return await ThemeApi.getThemeInfo(loading).then((ok) => {
this.setTheme(ok.data)
// 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'
// }
})
},
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() {
return this.edition == 'EE' && this.license_is_valid
2025-06-05 11:47:47 +00:00
},
2025-06-13 03:00:15 +00:00
changeUserType(num: number, token?: string) {
this.userType = num
const login = useLoginStore()
login.userAccessToken = token || ''
2025-06-05 11:47:47 +00:00
},
2025-04-16 11:10:00 +00:00
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