diff --git a/dashboard/src/stores/auth.ts b/dashboard/src/stores/auth.ts index 474ab8986..ee3e49d03 100644 --- a/dashboard/src/stores/auth.ts +++ b/dashboard/src/stores/auth.ts @@ -1,37 +1,36 @@ -import { defineStore } from 'pinia'; -import { router } from '@/router'; -import axios from 'axios'; +import { defineStore } from "pinia"; +import { router } from "@/router"; +import axios from "axios"; -export const useAuthStore = defineStore({ - id: 'auth', +export const useAuthStore = defineStore("auth", { state: () => ({ // @ts-ignore - username: '', - returnUrl: null + username: "", + returnUrl: null, }), actions: { async login(username: string, password: string): Promise { try { - const res = await axios.post('/api/auth/login', { + const res = await axios.post("/api/auth/login", { username: username, - password: password + password: password, }); - - if (res.data.status === 'error') { + + if (res.data.status === "error") { return Promise.reject(res.data.message); } - - this.username = res.data.data.username - localStorage.setItem('user', this.username); - localStorage.setItem('token', res.data.data.token); - localStorage.setItem('change_pwd_hint', res.data.data?.change_pwd_hint); - + + this.username = res.data.data.username; + localStorage.setItem("user", this.username); + localStorage.setItem("token", res.data.data.token); + localStorage.setItem("change_pwd_hint", res.data.data?.change_pwd_hint); + const onboardingCompleted = await this.checkOnboardingCompleted(); this.returnUrl = null; if (onboardingCompleted) { - router.push('/dashboard/default'); + router.push("/dashboard/default"); } else { - router.push('/welcome'); + router.push("/welcome"); } } catch (error) { return Promise.reject(error); @@ -40,40 +39,42 @@ export const useAuthStore = defineStore({ async checkOnboardingCompleted(): Promise { try { // 1. 检查平台配置 - const platformRes = await axios.get('/api/config/get'); - const hasPlatform = (platformRes.data.data.config.platform || []).length > 0; + const platformRes = await axios.get("/api/config/get"); + const hasPlatform = + (platformRes.data.data.config.platform || []).length > 0; if (!hasPlatform) return false; // 2. 检查提供者配置 - const providerRes = await axios.get('/api/config/provider/template'); + const providerRes = await axios.get("/api/config/provider/template"); const providers = providerRes.data.data?.providers || []; const sources = providerRes.data.data?.provider_sources || []; const sourceMap = new Map(); sources.forEach((s: any) => sourceMap.set(s.id, s.provider_type)); - + const hasProvider = providers.some((provider: any) => { - if (provider.provider_type) return provider.provider_type === 'chat_completion'; + if (provider.provider_type) + return provider.provider_type === "chat_completion"; if (provider.provider_source_id) { const type = sourceMap.get(provider.provider_source_id); - if (type === 'chat_completion') return true; + if (type === "chat_completion") return true; } - return String(provider.type || '').includes('chat_completion'); + return String(provider.type || "").includes("chat_completion"); }); return hasProvider; } catch (e) { - console.error('Failed to check onboarding status:', e); + console.error("Failed to check onboarding status:", e); return false; } }, logout() { - this.username = ''; - localStorage.removeItem('user'); - localStorage.removeItem('token'); - router.push('/auth/login'); + this.username = ""; + localStorage.removeItem("user"); + localStorage.removeItem("token"); + router.push("/auth/login"); }, has_token(): boolean { - return !!localStorage.getItem('token'); - } - } + return !!localStorage.getItem("token"); + }, + }, });