Files
airz-uniapp/packages/account/services/auth.uts
T
hujun dcf12a4471 refactor: 重构存储与接口适配,统一金额单位与会话管理
- 新增配置、存储、金额转换工具服务
- 按userId隔离本地缓存,替换旧设备级存储
- 统一移动端金额单位为万元,新增转换适配逻辑
- 重构认证与请求逻辑,支持真实后端会话
- 调整搜索、帮助、融资模块的模拟开关与数据适配
- 更新文档说明开发状态与配置规则
2026-09-22 17:25:52 +08:00

41 lines
1.8 KiB
Plaintext

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<boolean> {
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<string> {
return acceptSession(await requestJson('/auth/sms-login', 'POST', { phone, code }), phone)
}
export async function loginWithPassword(phone: string, password: string): Promise<string> {
return acceptSession(await requestJson('/auth/password-login', 'POST', { phone, password }), phone)
}
export async function logoutAccount(): Promise<void> {
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()
}