feat: 支持饮水记录卡片

This commit is contained in:
richarjiang
2025-09-02 15:50:35 +08:00
parent ed694f6142
commit 85a3c742df
16 changed files with 2066 additions and 56 deletions

View File

@@ -9,8 +9,8 @@ export async function setAuthToken(token: string | null): Promise<void> {
inMemoryToken = token;
}
export function getAuthToken(): string | null {
return inMemoryToken;
export function getAuthToken(): Promise<string | null> {
return AsyncStorage.getItem(STORAGE_KEYS.authToken);
}
export type ApiRequestOptions = {
@@ -31,7 +31,7 @@ async function doFetch<T>(path: string, options: ApiRequestOptions = {}): Promis
...(options.headers || {}),
};
const token = getAuthToken();
const token = await getAuthToken();
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
@@ -77,14 +77,6 @@ export const STORAGE_KEYS = {
privacyAgreed: '@privacy_agreed',
} as const;
export async function loadPersistedToken(): Promise<string | null> {
try {
const t = await AsyncStorage.getItem(STORAGE_KEYS.authToken);
return t || null;
} catch {
return null;
}
}
// 流式文本 POST基于 XMLHttpRequest支持增量 onChunk 回调与取消
export type TextStreamCallbacks = {
@@ -99,9 +91,9 @@ export type TextStreamOptions = {
signal?: AbortSignal;
};
export function postTextStream(path: string, body: any, callbacks: TextStreamCallbacks, options: TextStreamOptions = {}) {
export async function postTextStream(path: string, body: any, callbacks: TextStreamCallbacks, options: TextStreamOptions = {}) {
const url = buildApiUrl(path);
const token = getAuthToken();
const token = await getAuthToken();
// 生成请求ID用于追踪和取消
const requestId = `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
@@ -128,11 +120,6 @@ export function postTextStream(path: string, body: any, callbacks: TextStreamCal
resolved = true;
};
// 日志:请求开始
try {
console.log('[AI_CHAT][stream] start', { url, hasToken: !!token, body });
} catch { }
xhr.open('POST', url, true);
// 设置超时(可选)
if (typeof options.timeoutMs === 'number') {