审计后续 H1: 补齐小程序端透析功能,对接后端 12 个 API 路由。 新增内容: - 患者端: 透析记录列表/详情 + 透析处方列表/详情(只读,4 页面) - 医护端: 透析记录列表/详情/创建 + 处方列表/详情/创建(6 页面) - Service 层: dialysis.ts(患者端只读)+ doctor/dialysis.ts(医护端 CRUD) - 集成入口: 医生工作台快捷操作 + 患者"我的"菜单 + 路由注册 - 基础设施: api.delete 扩展支持 data 参数(后端 delete 需要 version)
146 lines
4.7 KiB
TypeScript
146 lines
4.7 KiB
TypeScript
import Taro from '@tarojs/taro';
|
|
import { secureGet, secureSet, secureRemove } from '@/utils/secure-storage';
|
|
|
|
const BASE_URL = process.env.TARO_APP_API_URL || 'http://localhost:3000/api/v1';
|
|
|
|
interface ApiResponse<T> {
|
|
success: boolean;
|
|
data?: T;
|
|
message?: string;
|
|
}
|
|
|
|
async function getHeaders(): Promise<Record<string, string>> {
|
|
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
const token = secureGet('access_token');
|
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
const patientId = Taro.getStorageSync('current_patient_id');
|
|
if (patientId) headers['X-Patient-Id'] = patientId;
|
|
const tenantId = secureGet('tenant_id');
|
|
if (tenantId) headers['X-Tenant-Id'] = tenantId;
|
|
return headers;
|
|
}
|
|
|
|
// --- Token refresh deduplication ---
|
|
let refreshPromise: Promise<boolean> | null = null;
|
|
|
|
async function tryRefreshToken(): Promise<boolean> {
|
|
if (refreshPromise) return refreshPromise;
|
|
refreshPromise = doRefresh();
|
|
refreshPromise.finally(() => { refreshPromise = null; });
|
|
return refreshPromise;
|
|
}
|
|
|
|
async function doRefresh(): Promise<boolean> {
|
|
const refreshToken = secureGet('refresh_token');
|
|
if (!refreshToken) return false;
|
|
try {
|
|
const res = await Taro.request({
|
|
url: `${BASE_URL}/auth/refresh`,
|
|
method: 'POST',
|
|
data: { refresh_token: refreshToken },
|
|
});
|
|
if (res.statusCode === 200 && res.data?.success) {
|
|
secureSet('access_token', res.data.data.access_token);
|
|
secureSet('refresh_token', res.data.data.refresh_token);
|
|
return true;
|
|
}
|
|
} catch {
|
|
// token 刷新失败
|
|
}
|
|
secureRemove('access_token');
|
|
secureRemove('refresh_token');
|
|
secureRemove('user_data');
|
|
secureRemove('user_roles');
|
|
secureRemove('tenant_id');
|
|
secureRemove('wechat_openid');
|
|
return false;
|
|
}
|
|
|
|
// --- Core request ---
|
|
async function request<T>(method: string, path: string, data?: unknown): Promise<T> {
|
|
const headers = await getHeaders();
|
|
const url = `${BASE_URL}${path}`;
|
|
const res = await Taro.request({ url, method: method as any, data, header: headers, timeout: 15000 });
|
|
|
|
if (res.statusCode === 401) {
|
|
const refreshed = await tryRefreshToken();
|
|
if (refreshed) return request<T>(method, path, data);
|
|
const pages = Taro.getCurrentPages();
|
|
const currentPath = pages[pages.length - 1]?.path || '';
|
|
if (!currentPath.includes('pages/login')) {
|
|
Taro.reLaunch({ url: '/pages/login/index' });
|
|
}
|
|
throw new Error('登录已过期');
|
|
}
|
|
|
|
const body = res.data as ApiResponse<T>;
|
|
if (!body.success) throw new Error(body.message || '请求失败');
|
|
return body.data as T;
|
|
}
|
|
|
|
function buildQuery(params?: Record<string, string | number | undefined>): string {
|
|
if (!params) return '';
|
|
const entries = Object.entries(params).filter(([, v]) => v !== undefined && v !== '');
|
|
return entries.length > 0
|
|
? '?' +
|
|
entries
|
|
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
|
|
.join('&')
|
|
: '';
|
|
}
|
|
|
|
// --- GET request cache + deduplication ---
|
|
interface CacheEntry { data: unknown; expiry: number }
|
|
const responseCache = new Map<string, CacheEntry>();
|
|
const inflightRequests = new Map<string, Promise<unknown>>();
|
|
const DEFAULT_CACHE_TTL = 60_000;
|
|
|
|
function getCacheKey(url: string): string {
|
|
const patientId = Taro.getStorageSync('current_patient_id') || '';
|
|
return `${url}#${patientId}`;
|
|
}
|
|
|
|
export function clearRequestCache(prefix?: string): void {
|
|
if (prefix) {
|
|
for (const key of responseCache.keys()) {
|
|
if (key.includes(prefix)) responseCache.delete(key);
|
|
}
|
|
} else {
|
|
responseCache.clear();
|
|
}
|
|
}
|
|
|
|
export const api = {
|
|
get: <T>(path: string, params?: Record<string, string | number | undefined>, cacheTtl?: number): Promise<T> => {
|
|
const url = `${path}${buildQuery(params)}`;
|
|
const cacheKey = getCacheKey(url);
|
|
|
|
const cached = responseCache.get(cacheKey);
|
|
if (cached && Date.now() < cached.expiry) {
|
|
return Promise.resolve(cached.data as T);
|
|
}
|
|
|
|
const inflight = inflightRequests.get(cacheKey);
|
|
if (inflight) return inflight as Promise<T>;
|
|
|
|
const promise = request<T>('GET', url).then((data) => {
|
|
inflightRequests.delete(cacheKey);
|
|
const ttl = cacheTtl ?? DEFAULT_CACHE_TTL;
|
|
if (ttl > 0) {
|
|
responseCache.set(cacheKey, { data, expiry: Date.now() + ttl });
|
|
}
|
|
return data;
|
|
}).catch((err) => {
|
|
inflightRequests.delete(cacheKey);
|
|
throw err;
|
|
});
|
|
|
|
inflightRequests.set(cacheKey, promise);
|
|
return promise;
|
|
},
|
|
|
|
post: <T>(path: string, data?: unknown) => request<T>('POST', path, data),
|
|
put: <T>(path: string, data?: unknown) => request<T>('PUT', path, data),
|
|
delete: <T>(path: string, data?: unknown) => request<T>('DELETE', path, data),
|
|
};
|