// 登录 API —— POST /api/auth/login(契约见 P2 认证实测报告) import { http } from './http'; import type { UserProfile } from '../auth/token'; export interface LoginResponse { success: boolean; msg?: string; token: string; expireSeconds: number; userId: string; account: string; name: string; roles: string[]; } export async function login(account: string, password: string): Promise { const { data } = await http.post('/api/auth/login', { account, password }); return data; } /** F2-9:从 JWT payload 解析 org 字段(base64url decode)。解析失败时静默返回 undefined。 */ function parseJwtOrg(token: string): string | undefined { try { const parts = token.split('.'); if (parts.length < 2) return undefined; let b = parts[1].replace(/-/g, '+').replace(/_/g, '/'); b += '='.repeat(-b.length & 3); const payload = JSON.parse(atob(b)) as Record; return typeof payload['org'] === 'string' ? payload['org'] : undefined; } catch { return undefined; } } export function toProfile(resp: LoginResponse): UserProfile { return { userId: resp.userId, account: resp.account, name: resp.name, roles: resp.roles ?? [], org: parseJwtOrg(resp.token), }; }