feat: 优化字帖功能
This commit is contained in:
@@ -8,7 +8,8 @@ const CACHE_CONFIG = {
|
||||
expireDays: 7, // 缓存过期天数
|
||||
// url: 'https://cdn.joeyone.cn/doodle/words-svg-all.json' // 远程数据地址
|
||||
// url: 'https://cdn.joeyone.cn/doodle/words-svg-3000.json' // 远程数据地址
|
||||
url: 'https://cdn.joeyone.cn/doodle/char_svg_3500.json' // 远程数据地址
|
||||
// url: 'https://cdn.joeyone.cn/doodle/char_svg_3500.json' // 远程数据地址
|
||||
url: 'https://cdn.joeyone.cn/doodle/char_common_stroke.json', // 远程数据地址
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -28,7 +29,7 @@ interface CacheData {
|
||||
function isCacheExpired(timestamp: number): boolean {
|
||||
const now = Date.now();
|
||||
const expireTime = CACHE_CONFIG.expireDays * 24 * 60 * 60 * 1000; // 7天的毫秒数
|
||||
return (now - timestamp) > expireTime;
|
||||
return now - timestamp > expireTime;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +53,10 @@ function getCacheData(): CacheData | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log('使用本地缓存的SVG汉字数据,缓存时间:', new Date(cacheData.timestamp).toLocaleString());
|
||||
console.log(
|
||||
'使用本地缓存的SVG汉字数据,缓存时间:',
|
||||
new Date(cacheData.timestamp).toLocaleString(),
|
||||
);
|
||||
return cacheData;
|
||||
} catch (error) {
|
||||
console.error('读取SVG汉字数据缓存失败:', error);
|
||||
@@ -69,7 +73,7 @@ function saveCacheData(data: Record<string, string[]>): void {
|
||||
const cacheData: CacheData = {
|
||||
data,
|
||||
timestamp: Date.now(),
|
||||
version: '1.0' // 可以用于后续版本控制
|
||||
version: '1.0', // 可以用于后续版本控制
|
||||
};
|
||||
|
||||
wx.setStorageSync(CACHE_CONFIG.key, JSON.stringify(cacheData));
|
||||
@@ -91,13 +95,17 @@ async function fetchRemoteData(): Promise<Record<string, string[]>> {
|
||||
{
|
||||
timeout: 15000, // 15秒超时
|
||||
header: {
|
||||
'Accept': 'application/json',
|
||||
'Cache-Control': 'no-cache'
|
||||
}
|
||||
}
|
||||
Accept: 'application/json',
|
||||
'Cache-Control': 'no-cache',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
console.log('远程SVG汉字数据获取成功,共', Object.keys(svgWordsData).length, '个汉字');
|
||||
console.log(
|
||||
'远程SVG汉字数据获取成功,共',
|
||||
Object.keys(svgWordsData).length,
|
||||
'个汉字',
|
||||
);
|
||||
return svgWordsData;
|
||||
}
|
||||
|
||||
@@ -143,7 +151,12 @@ export function clearWordsSvgCache(): void {
|
||||
* 获取缓存信息
|
||||
* @returns 缓存信息对象
|
||||
*/
|
||||
export function getCacheInfo(): { exists: boolean; timestamp?: number; isExpired?: boolean; daysLeft?: number } {
|
||||
export function getCacheInfo(): {
|
||||
exists: boolean;
|
||||
timestamp?: number;
|
||||
isExpired?: boolean;
|
||||
daysLeft?: number;
|
||||
} {
|
||||
try {
|
||||
const cacheStr = wx.getStorageSync(CACHE_CONFIG.key);
|
||||
if (!cacheStr) {
|
||||
@@ -152,13 +165,19 @@ export function getCacheInfo(): { exists: boolean; timestamp?: number; isExpired
|
||||
|
||||
const cacheData: CacheData = JSON.parse(cacheStr);
|
||||
const isExpired = isCacheExpired(cacheData.timestamp);
|
||||
const daysLeft = isExpired ? 0 : Math.ceil((CACHE_CONFIG.expireDays * 24 * 60 * 60 * 1000 - (Date.now() - cacheData.timestamp)) / (24 * 60 * 60 * 1000));
|
||||
const daysLeft = isExpired
|
||||
? 0
|
||||
: Math.ceil(
|
||||
(CACHE_CONFIG.expireDays * 24 * 60 * 60 * 1000 -
|
||||
(Date.now() - cacheData.timestamp)) /
|
||||
(24 * 60 * 60 * 1000),
|
||||
);
|
||||
|
||||
return {
|
||||
exists: true,
|
||||
timestamp: cacheData.timestamp,
|
||||
isExpired,
|
||||
daysLeft
|
||||
daysLeft,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取缓存信息失败:', error);
|
||||
|
||||
Reference in New Issue
Block a user