feat: 重构评估流程为内存状态管理并接入真实提交与报告接口

本次重构将评估草稿改为内存状态管理,移除本地存储草稿,新增评估流程内存状态模块;接入真实的账户资料获取、头像上传接口,新增账户缓存清理逻辑;重构评估提交逻辑,接入真实提交接口并返回评估ID;新增分析页和报告页的真实状态查询、报告获取功能;更新README文档,记录开发进度和接口状态。
This commit is contained in:
hujun
2026-09-24 11:50:07 +08:00
parent dcf12a4471
commit dbfd637388
22 changed files with 1468 additions and 434 deletions
+43 -3
View File
@@ -1,5 +1,7 @@
/** Shared request, configuration and session infrastructure. */
import { API_BASE_URL, APP_ENVIRONMENT } from './config.uts'
import { clearAccountCache } from './cache-lifecycle.uts'
import { clearAssessmentFlow } from './assessment-flow.uts'
export { API_BASE_URL, API_DEMO_ENABLED } from './config.uts'
const REQUEST_TIMEOUT = 10000
const SESSION_KEY = 'aiFinanceSession'
@@ -10,6 +12,8 @@ export function saveSession(token: string, phone: string, userId: string, expire
if (token.length == 0 || userId.length == 0 || expiresAt.length == 0) throw new Error('登录响应不完整')
const expiry = Date.parse(expiresAt)
if (isNaN(expiry) || expiry <= Date.now()) throw new Error('登录凭证已过期')
const previous = getSession()
if (previous != null && previous['userId'] != userId) clearAccountCache(previous['userId'] as string)
uni.setStorageSync(SESSION_KEY, { token, phone, userId, expiresAt, loggedInAt: Date.now() })
}
@@ -21,16 +25,24 @@ export function getSession(): UTSJSONObject | null {
const userId = session['userId'] as string | null
const expiresAt = session['expiresAt'] as string | null
if (token == null || userId == null || expiresAt == null || token.length == 0 || userId.length == 0 || token.startsWith('demo-')) {
uni.removeStorageSync(SESSION_KEY)
clearSession()
return null
}
const expiry = Date.parse(expiresAt)
if (isNaN(expiry) || expiry <= Date.now()) { uni.removeStorageSync(SESSION_KEY); return null }
if (isNaN(expiry) || expiry <= Date.now()) { clearSession(); return null }
return session
}
export function isLoggedIn(): boolean { return getSession() != null }
export function clearSession(): void { uni.removeStorageSync(SESSION_KEY) }
export function clearSession(): void {
clearAssessmentFlow()
const value = uni.getStorageSync(SESSION_KEY)
if (value != null && typeof value == 'object') {
const userId = (value as UTSJSONObject)['userId'] as string | null
if (userId != null) clearAccountCache(userId)
}
uni.removeStorageSync(SESSION_KEY)
}
const currentToken = (): string => { const session = getSession(); return session == null ? '' : session['token'] as string }
const apiMessage = (error: any | null, fallback: string): string => {
@@ -75,3 +87,31 @@ const requestJson = (path: string, method: RequestMethod, payload: UTSJSONObject
}
export { apiMessage, requestJson, wait }
export function uploadJson(path: string, filePath: string): Promise<UTSJSONObject> {
return new Promise<UTSJSONObject>((resolve, reject) => {
const sentToken = currentToken()
if (sentToken.length == 0) { reject(new Error('请先登录')); return }
if (API_BASE_URL.length == 0 || (APP_ENVIRONMENT == 'production' && !API_BASE_URL.startsWith('https://'))) {
reject(new Error('服务地址尚未配置')); return
}
uni.uploadFile({ url: API_BASE_URL + path, filePath, name: 'file', timeout: REQUEST_TIMEOUT,
header: { Authorization: 'Bearer ' + sentToken, 'X-Requested-With': 'XMLHttpRequest' },
success: (response) => {
if (currentToken() != sentToken) { reject(new Error('登录状态已变化,请重新操作')); return }
if (response.statusCode == 401) clearSession()
try {
const body = JSON.parse<UTSJSONObject>(response.data)
if (body == null) throw new Error('上传响应格式错误')
if (response.statusCode < 200 || response.statusCode >= 300 || body['code'] != 1) {
if (body['code'] == 401) clearSession()
throw new Error((body['msg'] as string | null) ?? '头像上传失败')
}
const data = body['data'] as UTSJSONObject | null
if (data == null) throw new Error('上传响应不完整')
resolve(data)
} catch (error) { reject(error) }
}, fail: () => { reject(new Error('头像上传失败,请检查网络后重试')) }
})
})
}