import { API_BASE_URL, requestJson, saveSession, clearSession, getSession } from '@/services/api.uts' export { API_BASE_URL } from '@/services/api.uts' import { setUserStorage } from '@/services/storage.uts' export async function requestSmsCode(phone: string): Promise { await requestJson('/auth/sms-code', 'POST', { phone, purpose: 'login' }) return true } function acceptSession(result: UTSJSONObject, phone: string): string { const token = result['token'] as string | null const userId = result['userId'] as string | null const expiresAt = result['expiresAt'] as string | null if (token == null || userId == null || expiresAt == null) throw new Error('登录响应缺少会话信息') saveSession(token, phone, userId, expiresAt) const profile = result['profile'] as UTSJSONObject | null if (profile != null) setUserStorage('finance.account.profile:' + phone, { nickname: profile['nickname'], avatar: profile['avatarUrl'] ?? '' }) return token } export async function loginWithSms(phone: string, code: string): Promise { return acceptSession(await requestJson('/auth/sms-login', 'POST', { phone, code }), phone) } export async function loginWithPassword(phone: string, password: string): Promise { return acceptSession(await requestJson('/auth/password-login', 'POST', { phone, password }), phone) } export async function logoutAccount(): Promise { const session = getSession() if (session == null) { clearSession(); return } try { await requestJson('/auth/logout', 'POST', {}) } catch (error) { // 401已清理会话,视为退出完成;网络失败保留会话以便重试撤销。 if (getSession() != null) throw error } const current = getSession() if (current == null || current['token'] == session['token']) clearSession() }