feat: 首页、分类页加载数据逻辑重构
This commit is contained in:
+87
-13
@@ -1,12 +1,19 @@
|
||||
import { defaultPrintConfig } from './config/config';
|
||||
import { generateUUID, getAppUUID, setAppUUID } from './utils/uuid';
|
||||
|
||||
const STORAGE_KEY_PAGE_CONFIG = 'pageConfig';
|
||||
|
||||
// Pending resolvers for getPageConfig() callers waiting on config
|
||||
let _configResolvers: Array<(config: PageConfigData | null) => void> = [];
|
||||
|
||||
// app.ts
|
||||
App<IAppOption>({
|
||||
globalData: {
|
||||
env: 'release',
|
||||
uuid: '',
|
||||
printConfig: defaultPrintConfig,
|
||||
pageConfig: undefined,
|
||||
pageConfigReady: false,
|
||||
},
|
||||
onLaunch() {
|
||||
wx.cloud.init({
|
||||
@@ -16,15 +23,8 @@ App<IAppOption>({
|
||||
|
||||
const accountInfo = wx.getAccountInfoSync();
|
||||
const env = accountInfo.miniProgram.envVersion || 'release';
|
||||
/**
|
||||
* 设置环境变量
|
||||
* 开发环境:develop
|
||||
* 体验环境:trial
|
||||
* 正式环境:release
|
||||
*/
|
||||
this.globalData.env = env;
|
||||
|
||||
// 从 localStorage 读取 UUID,如果没有则生成并存储
|
||||
let uuid = getAppUUID();
|
||||
if (!uuid) {
|
||||
uuid = generateUUID();
|
||||
@@ -38,15 +38,13 @@ App<IAppOption>({
|
||||
wx.getStorageSync('printConfig') || defaultPrintConfig;
|
||||
this.globalData.printConfig = printConfig;
|
||||
}
|
||||
|
||||
void this.loadPageConfig();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 小程序显示时的生命周期
|
||||
},
|
||||
onShow() {},
|
||||
|
||||
onHide() {
|
||||
// 小程序隐藏时的生命周期
|
||||
},
|
||||
onHide() {},
|
||||
|
||||
getPrintConfig(): PrintConfig {
|
||||
return this.globalData.printConfig || defaultPrintConfig;
|
||||
@@ -56,4 +54,80 @@ App<IAppOption>({
|
||||
this.globalData.printConfig = config;
|
||||
wx.setStorageSync('printConfig', config);
|
||||
},
|
||||
|
||||
async loadPageConfig() {
|
||||
let config: PageConfigData | null = null;
|
||||
|
||||
// L1: 数据预拉取
|
||||
try {
|
||||
const res = await new Promise<{ fetchedData: string }>(
|
||||
(resolve, reject) => {
|
||||
wx.getBackgroundFetchData({
|
||||
fetchType: 'pre',
|
||||
success: resolve as any,
|
||||
fail: reject,
|
||||
});
|
||||
},
|
||||
);
|
||||
if (res.fetchedData) {
|
||||
const parsed = JSON.parse(res.fetchedData);
|
||||
config = parsed?.data || parsed;
|
||||
}
|
||||
} catch {
|
||||
// 预拉取失败,继续降级
|
||||
}
|
||||
|
||||
// L2: storage 缓存
|
||||
if (!config) {
|
||||
try {
|
||||
const cached = wx.getStorageSync(STORAGE_KEY_PAGE_CONFIG);
|
||||
if (cached && typeof cached === 'object') {
|
||||
config = cached;
|
||||
}
|
||||
} catch {
|
||||
// storage 读取失败,继续降级
|
||||
}
|
||||
}
|
||||
|
||||
// L3: 云函数兜底
|
||||
if (!config) {
|
||||
try {
|
||||
const res = (await wx.cloud.callFunction({
|
||||
name: 'pageConfigFetch',
|
||||
})) as { result?: { success?: boolean; data?: any } };
|
||||
if (res.result?.success && res.result.data) {
|
||||
config = res.result.data;
|
||||
}
|
||||
} catch {
|
||||
// 云函数也失败,config 保持 null
|
||||
}
|
||||
}
|
||||
|
||||
// 写入 globalData
|
||||
if (config) {
|
||||
this.globalData.pageConfig = config;
|
||||
// 成功获取后写入 storage 缓存
|
||||
try {
|
||||
wx.setStorageSync(STORAGE_KEY_PAGE_CONFIG, config);
|
||||
} catch {
|
||||
// storage 写入失败不影响主流程
|
||||
}
|
||||
}
|
||||
|
||||
// 标记就绪,通知等待中的页面
|
||||
this.globalData.pageConfigReady = true;
|
||||
for (const resolve of _configResolvers) {
|
||||
resolve(config);
|
||||
}
|
||||
_configResolvers = [];
|
||||
},
|
||||
|
||||
getPageConfig(): Promise<PageConfigData | null> {
|
||||
if (this.globalData.pageConfigReady) {
|
||||
return Promise.resolve(this.globalData.pageConfig || null);
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
_configResolvers.push(resolve);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user