Files
hms/apps/miniprogram/src/utils/secure-storage.ts
iven fcfc0ba5d9
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled
perf(miniprogram): 全面性能优化 — 分包加载 + 请求缓存 + 渲染优化
分包加载(主包从 517KB 降至 275KB,-47%):
- 将 27 个页面拆入 6 个分包(health/doctor/mall/profile/content/device)
- vendors.js 从 192KB 降至 36KB(-81%)
- echarts 514KB 仅在访问健康趋势页时按需加载

请求层优化:
- GET 请求增加 in-flight 去重 + 60s TTL 响应缓存
- 新建 points store 集中管理积分/签到状态(消除 5 处重复调用)
- health store todaySummary 增加 60s TTL
- mutation 后自动失效缓存(health input/daily-monitoring)
- logout 时清空请求缓存

渲染优化:
- 7 个组件添加 React.memo(EcCanvas/TrendChart/Loading/EmptyState 等)
- 修复 TrendChart setChartReady 导致的双重渲染
- 静态数组(quickServices/quickActions/trendLinks)提取到模块级
- restoreAuth 从页面级提升到 App 级别
- 文章列表图片添加 lazyLoad

构建优化:
- prod 配置添加 terser(drop_console + drop_debugger)
- crypto-js 从全量引入改为按需引入(AES + Utf8)
2026-04-28 11:44:37 +08:00

53 lines
1.5 KiB
TypeScript

import Taro from '@tarojs/taro';
import AES from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';
const ENCRYPTION_KEY = process.env.TARO_APP_ENCRYPTION_KEY || '';
if (!ENCRYPTION_KEY && process.env.NODE_ENV !== 'production') {
console.warn('[secure-storage] TARO_APP_ENCRYPTION_KEY 未设置,敏感数据将以明文存储');
}
function encrypt(plaintext: string): string {
if (!ENCRYPTION_KEY) {
if (process.env.NODE_ENV === 'production') {
throw new Error('[secure-storage] TARO_APP_ENCRYPTION_KEY 未设置,生产环境禁止明文存储');
}
return plaintext;
}
return AES.encrypt(plaintext, ENCRYPTION_KEY).toString();
}
function decrypt(ciphertext: string): string | null {
if (!ENCRYPTION_KEY) {
if (process.env.NODE_ENV === 'production') {
throw new Error('[secure-storage] TARO_APP_ENCRYPTION_KEY 未设置,生产环境禁止明文读取');
}
return ciphertext;
}
try {
const bytes = AES.decrypt(ciphertext, ENCRYPTION_KEY);
const result = bytes.toString(Utf8);
if (!result) return null;
return result;
} catch {
return null;
}
}
export function secureSet(key: string, value: string): void {
const encrypted = encrypt(value);
Taro.setStorageSync(key, encrypted);
}
export function secureGet(key: string): string {
const raw = Taro.getStorageSync(key);
if (!raw || typeof raw !== 'string') return '';
const result = decrypt(raw);
return result ?? '';
}
export function secureRemove(key: string): void {
Taro.removeStorageSync(key);
}