2026-09-20 10:52:10 +08:00
|
|
|
|
/** Shared request, configuration and session infrastructure. */
|
2026-09-11 20:23:10 +08:00
|
|
|
|
export const API_BASE_URL = ''
|
|
|
|
|
|
const REQUEST_TIMEOUT = 10000
|
|
|
|
|
|
const SESSION_KEY = 'aiFinanceSession'
|
2026-09-10 10:57:40 +08:00
|
|
|
|
|
2026-09-11 20:23:10 +08:00
|
|
|
|
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)
|
2026-09-15 16:16:55 +08:00
|
|
|
|
if (value == null || typeof value != 'object') return null
|
2026-09-11 20:23:10 +08:00
|
|
|
|
return value as UTSJSONObject
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function isLoggedIn(): boolean {
|
|
|
|
|
|
return getSession() != null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function clearSession(): void {
|
|
|
|
|
|
uni.removeStorageSync(SESSION_KEY)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-15 16:16:55 +08:00
|
|
|
|
const apiMessage = (error: any | null, fallback: string): string => {
|
2026-09-11 20:23:10 +08:00
|
|
|
|
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, '网络请求失败,请稍后重试'))
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-09-10 10:57:40 +08:00
|
|
|
|
|
2026-09-20 10:52:10 +08:00
|
|
|
|
export { apiMessage, requestJson, wait }
|