2026-09-22 17:25:52 +08:00
|
|
|
import { setUserStorage } from '@/services/storage.uts'
|
|
|
|
|
import { API_DEMO_ENABLED, API_BASE_URL, wait, requestJson } from '@/services/api.uts'
|
2026-09-20 10:52:10 +08:00
|
|
|
import { demoProducts, parseProduct, FinanceProduct } from './finance.uts'
|
2026-09-22 17:25:52 +08:00
|
|
|
import { assessmentApiPayload } from '@/services/assessment.uts'
|
2026-09-20 10:52:10 +08:00
|
|
|
export { API_BASE_URL } from '@/services/api.uts'
|
|
|
|
|
|
|
|
|
|
export async function submitAssessment(payload: UTSJSONObject): Promise<string> {
|
2026-09-22 17:25:52 +08:00
|
|
|
const apiPayload = assessmentApiPayload(payload)
|
|
|
|
|
if (API_DEMO_ENABLED) {
|
2026-09-20 10:52:10 +08:00
|
|
|
await wait(650)
|
2026-09-22 17:25:52 +08:00
|
|
|
setUserStorage('latestAssessment', apiPayload)
|
2026-09-20 10:52:10 +08:00
|
|
|
return 'ASSESSMENT-DEMO-001'
|
|
|
|
|
}
|
2026-09-22 17:25:52 +08:00
|
|
|
const result = await requestJson('/assessments', 'POST', apiPayload)
|
2026-09-20 10:52:10 +08:00
|
|
|
const id = result['id'] as string | null
|
|
|
|
|
if (id == null || id.length == 0) throw new Error('评估提交成功但未返回编号')
|
|
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchMatches(): Promise<FinanceProduct[]> {
|
2026-09-22 17:25:52 +08:00
|
|
|
if (API_DEMO_ENABLED) {
|
2026-09-20 10:52:10 +08:00
|
|
|
await wait(320)
|
|
|
|
|
return demoProducts.slice()
|
|
|
|
|
}
|
|
|
|
|
const result = await requestJson('/matches', 'GET', new UTSJSONObject())
|
|
|
|
|
const items = result['items'] as UTSJSONObject[] | null
|
|
|
|
|
return items == null ? [] : items.map((item: UTSJSONObject): FinanceProduct => parseProduct(item))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchProduct(id: string): Promise<FinanceProduct | null> {
|
2026-09-22 17:25:52 +08:00
|
|
|
if (API_DEMO_ENABLED) {
|
2026-09-20 10:52:10 +08:00
|
|
|
await wait(180)
|
|
|
|
|
const products = demoProducts.filter((item: FinanceProduct): boolean => item.id == id)
|
|
|
|
|
return products.length > 0 ? products[0] : null
|
|
|
|
|
}
|
|
|
|
|
const result = await requestJson('/products/' + encodeURIComponent(id), 'GET', new UTSJSONObject())
|
|
|
|
|
return parseProduct(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function sendAssistantMessage(message: string): Promise<string> {
|
2026-09-22 17:25:52 +08:00
|
|
|
if (API_DEMO_ENABLED) {
|
2026-09-20 10:52:10 +08:00
|
|
|
await wait(600)
|
|
|
|
|
return '结合您的评估结果,建议优先选择年化利率较低、可随借随还的产品,并重点确认提前还款规则。'
|
|
|
|
|
}
|
|
|
|
|
const result = await requestJson('/assistant/messages', 'POST', { message })
|
|
|
|
|
const reply = result['reply'] as string | null
|
|
|
|
|
if (reply == null || reply.length == 0) throw new Error('助手暂时没有返回内容')
|
|
|
|
|
return reply
|
|
|
|
|
}
|