import { defineStore } from 'pinia' import ChatAPI from '@/api/chat/chat' import type { ChatProfile, ChatUserProfile } from '@/api/type/chat' import type { LoginRequest } from '@/api/type/user' import type { Ref } from 'vue' import { useLocalStorage } from '@vueuse/core' import { localeConfigKey } from '@/locales/index' import useUserStore from './user' interface ChatUser { // 用户id id: string } interface Chat { chat_profile?: ChatProfile application?: any chatUserProfile?: ChatUserProfile token?: string accessToken?: string } const useChatUserStore = defineStore('chat-user', { state: (): Chat => ({ chat_profile: undefined, application: undefined, accessToken: undefined, }), actions: { setAccessToken(accessToken: string) { this.accessToken = accessToken }, getChatProfile() { return ChatAPI.chatProfile(this.accessToken as string).then((ok) => { this.chat_profile = ok.data return this.chat_profile }) }, async getChatUserProfile() { const res = await ChatAPI.getChatUserProfile() this.chatUserProfile = res.data }, applicationProfile() { return ChatAPI.applicationProfile().then((ok) => { this.application = ok.data const user = useUserStore() useLocalStorage(localeConfigKey, 'en-US').value = ok?.data?.language || user.getLanguage() if (this.application.custom_theme) { this.application['custom_theme']['theme_color'] = ok.data?.custom_theme?.theme_color || '#3370FF' } else { this.application.custom_theme = { theme_color: ok.data?.custom_theme?.theme_color || '#3370FF', } } }) }, isAuthentication() { if (this.chat_profile) { return Promise.resolve(this.chat_profile.authentication) } else { return this.getChatProfile().then((ok) => { return ok.authentication }) } }, getToken() { if (this.token) { return this.token } const token = sessionStorage.getItem(`${this.accessToken}-accessToken`) if (token) { this.token = token return token } const local_token = localStorage.getItem(`${this.accessToken}-accessToken`) if (local_token) { this.token = local_token return local_token } return localStorage.getItem(`accessToken`) }, setToken(token: string) { this.token = token sessionStorage.setItem(`${this.accessToken}-accessToken`, token) localStorage.setItem(`${this.accessToken}-accessToken`, token) }, /** *匿名认证 */ anonymousAuthentication() { return ChatAPI.anonymousAuthentication(this.accessToken as string).then((ok) => { this.setToken(ok.data) return this.token }) }, passwordAuthentication(password: string) { return ChatAPI.passwordAuthentication(this.accessToken as string, password).then((ok) => { this.setToken(ok.data) return this.token }) }, login(request: LoginRequest, loading?: Ref) { return ChatAPI.login(this.accessToken as string, request, loading).then((ok) => { this.setToken(ok.data.token) return this.token }) }, ldapLogin(request: LoginRequest, loading?: Ref) { return ChatAPI.ldapLogin(this.accessToken as string, request, loading).then((ok) => { this.setToken(ok.data.token) return this.token }) }, logout() { return ChatAPI.logout().then(() => { sessionStorage.removeItem(`${this.accessToken}-accessToken`) localStorage.removeItem(`${this.accessToken}-accessToken`) this.token = undefined return true }) }, }, }) export default useChatUserStore