Files
airz-uniapp/services/api.uts
T
hujun 69130d11e7 refactor: 重构项目分包结构与资源管理
1.  调整页面路由,将业务页面迁移到对应分包,主包仅保留首页
2.  拆分公共服务模块,将账户、融资相关逻辑移入对应分包
3.  统一资源路径,将专属静态资源放入各分包static目录,更新导航栏图标资源
4.  修复启动引导逻辑,实现仅首次冷启动显示引导页
5.  更新README文档,补充分包配置与开发规范说明
2026-09-20 10:52:10 +08:00

57 lines
1.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** Shared request, configuration and session infrastructure. */
export const API_BASE_URL = ''
const REQUEST_TIMEOUT = 10000
const SESSION_KEY = 'aiFinanceSession'
const wait = (duration: number) => new Promise<void>((resolve) => {
setTimeout(() => resolve(), duration)
})
export function saveSession(token: string, phone: string): void {
uni.setStorageSync(SESSION_KEY, { token, phone, loggedInAt: Date.now() })
}
export function getSession(): UTSJSONObject | null {
const value = uni.getStorageSync(SESSION_KEY)
if (value == null || typeof value != 'object') return null
return value as UTSJSONObject
}
export function isLoggedIn(): boolean {
return getSession() != null
}
export function clearSession(): void {
uni.removeStorageSync(SESSION_KEY)
}
const apiMessage = (error: any | null, fallback: string): string => {
if (error == null) return fallback
const text = error.toString()
return text.length > 0 ? text : fallback
}
const requestJson = (path: string, method: RequestMethod, payload: UTSJSONObject): Promise<UTSJSONObject> => {
return new Promise<UTSJSONObject>((resolve, reject) => {
uni.request({
url: API_BASE_URL + path,
method,
data: payload,
timeout: REQUEST_TIMEOUT,
success: (response) => {
if (response.statusCode >= 200 && response.statusCode < 300) {
if (response.data == null) resolve(new UTSJSONObject())
else resolve(response.data as UTSJSONObject)
} else {
reject('请求失败(' + response.statusCode.toString() + ')')
}
},
fail: (error) => {
reject(apiMessage(error, '网络请求失败,请稍后重试'))
}
})
})
}
export { apiMessage, requestJson, wait }