Files
Migu2.0/frontends/apps/simple-platform-vue/src/api/auth.ts
T
zhaowei.huangandCursor 42978930ca feat: 迁入 MiGu.Server、平台前端与车辆列表 reflection 回退
从 Simple-FR 拆出 Platform.Server 并重命名为 MiGu.Server;frontends 源码与构建脚本迁入本仓库。地图监控在 projection/cars 失败或为空时回退 reflection 车辆列表;Simple 仓库已移除旧 Platform.Server。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 18:16:34 +08:00

51 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import http from './http'
import type { LoginRequest, LoginResponse, MeResponse, Scope } from '@/types/auth'
import { mockLogin } from '@/mock/server'
// 是否走前端 Mock。由 VITE_USE_MOCK 控制:
// - .env.development 默认 'true' → 本地无后端即可联调
// - .env.production 默认 'false' → 生产强制走 Platform.Server 真实 API
// 任何不等于字符串 'true' 的取值都视为 false,避免 build 时静态替换出错。
const MOCK = import.meta.env.VITE_USE_MOCK === 'true'
export async function login(req: LoginRequest): Promise<LoginResponse> {
if (MOCK) return mockLogin(req)
const { data } = await http.post<LoginResponse>('/auth/login', req)
return data
}
export async function logout(): Promise<void> {
if (MOCK) return
await http.post('/auth/logout')
}
/**
* AR-6 (会话 45):服务端切换 scope 并重发 token + perms。
* 替代过去 stores/auth.ts switchScope 客户端硬编码 allowedOps 的伪权限做法。
* Mock 模式下退化为本地翻转 scopemockLogin 已经依据 scope 返回不同 perms)。
*/
export async function switchScope(scope: Scope): Promise<LoginResponse> {
if (MOCK) {
// mock 直接复用 mockLogin 的 perm 推算逻辑,伪造一次「免密码登录」。
return mockLogin({ username: 'mock-user', password: 'mock', scope })
}
const { data } = await http.post<LoginResponse>('/auth/switch-scope', { scope })
return data
}
/**
* 拿当前 token / Cookie 让后端实校一次身份。成功表示 token 仍被服务端接受,
* 失败(401)由 http 拦截器统一清 state + 跳 /login。
*
* 解决「Platform.Server 随机 secret 重启 → 老 token 失效 → 前端 isAuthed 仍 true 误放行」的窗口。
*/
export async function getMe(): Promise<MeResponse> {
if (MOCK) {
// mock 模式下没有 secret 概念,本地 store 里有什么就是什么;直接 throw 让守卫
// 走「认可本地状态」分支,避免 mock 联调误判为「身份失效」。
throw new Error('mock-mode-skip-me-validation')
}
const { data } = await http.get<MeResponse>('/auth/me')
return data
}