**功能概述**: 实现简易模式(Simple Mode),为个人用户和小团队提供简化的使用体验,隐藏复杂的分组、订阅、配额等概念。 **后端改动**: 1. 配置系统 - 新增 run_mode 配置项(standard/simple) - 支持环境变量 RUN_MODE - 默认值为 standard 2. 数据库初始化 - 自动创建3个默认分组:anthropic-default、openai-default、gemini-default - 默认分组配置:无并发限制、active状态、非独占 - 幂等性保证:重复启动不会重复创建 3. 账号管理 - 创建账号时自动绑定对应平台的默认分组 - 如果未指定分组,自动查找并绑定默认分组 **前端改动**: 1. 状态管理 - authStore 新增 isSimpleMode 计算属性 - 从后端API获取并同步运行模式 2. UI隐藏 - 侧边栏:隐藏分组管理、订阅管理、兑换码菜单 - 账号管理页面:隐藏分组列 - 创建/编辑账号对话框:隐藏分组选择器 3. 路由守卫 - 限制访问分组、订阅、兑换码相关页面 - 访问受限页面时自动重定向到仪表板 **配置示例**: ```yaml run_mode: simple run_mode: standard ``` **影响范围**: - 后端:配置、数据库迁移、账号服务 - 前端:认证状态、路由、UI组件 - 部署:配置文件示例 **兼容性**: - 简易模式和标准模式可无缝切换 - 不需要数据迁移 - 现有数据不受影响
130 lines
3.0 KiB
TypeScript
130 lines
3.0 KiB
TypeScript
/**
|
|
* Authentication API endpoints
|
|
* Handles user login, registration, and logout operations
|
|
*/
|
|
|
|
import { apiClient } from './client'
|
|
import type {
|
|
LoginRequest,
|
|
RegisterRequest,
|
|
AuthResponse,
|
|
CurrentUserResponse,
|
|
SendVerifyCodeRequest,
|
|
SendVerifyCodeResponse,
|
|
PublicSettings
|
|
} from '@/types'
|
|
|
|
/**
|
|
* Store authentication token in localStorage
|
|
*/
|
|
export function setAuthToken(token: string): void {
|
|
localStorage.setItem('auth_token', token)
|
|
}
|
|
|
|
/**
|
|
* Get authentication token from localStorage
|
|
*/
|
|
export function getAuthToken(): string | null {
|
|
return localStorage.getItem('auth_token')
|
|
}
|
|
|
|
/**
|
|
* Clear authentication token from localStorage
|
|
*/
|
|
export function clearAuthToken(): void {
|
|
localStorage.removeItem('auth_token')
|
|
localStorage.removeItem('auth_user')
|
|
}
|
|
|
|
/**
|
|
* User login
|
|
* @param credentials - Username and password
|
|
* @returns Authentication response with token and user data
|
|
*/
|
|
export async function login(credentials: LoginRequest): Promise<AuthResponse> {
|
|
const { data } = await apiClient.post<AuthResponse>('/auth/login', credentials)
|
|
|
|
// Store token and user data
|
|
setAuthToken(data.access_token)
|
|
localStorage.setItem('auth_user', JSON.stringify(data.user))
|
|
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* User registration
|
|
* @param userData - Registration data (username, email, password)
|
|
* @returns Authentication response with token and user data
|
|
*/
|
|
export async function register(userData: RegisterRequest): Promise<AuthResponse> {
|
|
const { data } = await apiClient.post<AuthResponse>('/auth/register', userData)
|
|
|
|
// Store token and user data
|
|
setAuthToken(data.access_token)
|
|
localStorage.setItem('auth_user', JSON.stringify(data.user))
|
|
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Get current authenticated user
|
|
* @returns User profile data
|
|
*/
|
|
export async function getCurrentUser() {
|
|
return apiClient.get<CurrentUserResponse>('/auth/me')
|
|
}
|
|
|
|
/**
|
|
* User logout
|
|
* Clears authentication token and user data from localStorage
|
|
*/
|
|
export function logout(): void {
|
|
clearAuthToken()
|
|
// Optionally redirect to login page
|
|
// window.location.href = '/login';
|
|
}
|
|
|
|
/**
|
|
* Check if user is authenticated
|
|
* @returns True if user has valid token
|
|
*/
|
|
export function isAuthenticated(): boolean {
|
|
return getAuthToken() !== null
|
|
}
|
|
|
|
/**
|
|
* Get public settings (no auth required)
|
|
* @returns Public settings including registration and Turnstile config
|
|
*/
|
|
export async function getPublicSettings(): Promise<PublicSettings> {
|
|
const { data } = await apiClient.get<PublicSettings>('/settings/public')
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Send verification code to email
|
|
* @param request - Email and optional Turnstile token
|
|
* @returns Response with countdown seconds
|
|
*/
|
|
export async function sendVerifyCode(
|
|
request: SendVerifyCodeRequest
|
|
): Promise<SendVerifyCodeResponse> {
|
|
const { data } = await apiClient.post<SendVerifyCodeResponse>('/auth/send-verify-code', request)
|
|
return data
|
|
}
|
|
|
|
export const authAPI = {
|
|
login,
|
|
register,
|
|
getCurrentUser,
|
|
logout,
|
|
isAuthenticated,
|
|
setAuthToken,
|
|
getAuthToken,
|
|
clearAuthToken,
|
|
getPublicSettings,
|
|
sendVerifyCode
|
|
}
|
|
|
|
export default authAPI
|