feat: 增加环境切换

This commit is contained in:
R524809
2026-10-19 17:49:19 +08:00
parent 997042789d
commit fe1074e3f0
21 changed files with 968 additions and 123 deletions
+73 -24
View File
@@ -2,11 +2,13 @@ import { fetchPageConfig } from './services/site-api';
import { normalizePreviewImages } from './config/assets';
import { defaultPrintConfig } from './config/config';
import { getUser } from './utils/auth';
import { apiContextKey, getApiConfig, isCurrentApiConfig } from './config/api';
import { generateUUID, getAppUUID, setAppUUID } from './utils/uuid';
let _refreshGeneration = 0;
let _loadGeneration = 0;
const STORAGE_KEY_PAGE_CONFIG = 'pageConfig';
const pageConfigStorageKey = () => `doodle_page_config_v2:${apiContextKey()}`;
// Pending resolvers for getPageConfig() callers waiting on config
let _configResolvers: Array<(config: PageConfigData | null) => void> = [];
@@ -65,6 +67,23 @@ App<IAppOption>({
onHide() {},
/** 切环境不重启 App:作废旧加载、释放等待者并重置共享业务状态。 */
resetApiState() {
_refreshGeneration++;
_loadGeneration++;
this.globalData.user = null;
this.globalData.pageConfig = undefined;
this.globalData.pageConfigReady = false;
this.globalData.pendingCategoryTabId = undefined;
for (const resolve of _configResolvers) resolve(null);
_configResolvers = [];
try {
wx.removeStorageSync('favorites_active_tab');
} catch {
/* 临时页签偏好清理失败不影响环境与用户隔离。 */
}
},
getPrintConfig(): PrintConfig {
return this.globalData.printConfig || defaultPrintConfig;
},
@@ -74,32 +93,54 @@ App<IAppOption>({
wx.setStorageSync('printConfig', config);
},
async loadPageConfig() {
async loadPageConfig(forceRefresh = false) {
let context: ReturnType<typeof getApiConfig>;
try {
context = getApiConfig();
} catch {
// 地址被移除时结束首页等待,开发者仍可进入 Debug 改选可用环境。
this.globalData.pageConfig = undefined;
this.globalData.pageConfigReady = true;
for (const resolve of _configResolvers) resolve(null);
_configResolvers = [];
return false;
}
const generation = ++_loadGeneration;
const current = () =>
generation === _loadGeneration && isCurrentApiConfig(context);
const storageKey = pageConfigStorageKey();
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;
// 仅开发版跳过旧线上预拉取,保证环境切换后的内容来自目标服务。
// 体验/正式版继续沿用原预拉取模式、地址与旧云端任务。
if (context.version !== 'develop')
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 {
// 预拉取失败,继续降级
}
} catch {
// 预拉取失败,继续降级
}
if (!current()) return false;
// L2: storage 缓存
if (!config) {
if (!config && !forceRefresh) {
try {
const cached = wx.getStorageSync(STORAGE_KEY_PAGE_CONFIG);
const cached =
wx.getStorageSync(storageKey) ||
(context.version !== 'develop'
? wx.getStorageSync('pageConfig')
: null);
if (cached && typeof cached === 'object') {
config = cached;
}
@@ -117,6 +158,7 @@ App<IAppOption>({
/* 新后端不可用时继续展示空状态,本地生成仍可使用。 */
}
}
if (!current()) return false;
// 三种数据入口都先转换预览图,旧缓存会随成功加载写回新地址。
config = normalizePreviewImages(config);
@@ -126,7 +168,7 @@ App<IAppOption>({
this.globalData.pageConfig = config;
// 成功获取后写入 storage 缓存
try {
wx.setStorageSync(STORAGE_KEY_PAGE_CONFIG, config);
wx.setStorageSync(storageKey, config);
} catch {
// storage 写入失败不影响主流程
}
@@ -140,16 +182,23 @@ App<IAppOption>({
_configResolvers = [];
// 预拉取/缓存先渲染,再用新后端刷新,避免旧静态配置长期覆盖管理修改。
if (hadCachedConfig) void this.refreshPageConfig();
return !!config;
},
async refreshPageConfig() {
const context = getApiConfig();
const storageKey = pageConfigStorageKey();
const generation = ++_refreshGeneration;
try {
const config = normalizePreviewImages(await fetchPageConfig());
if (generation !== _refreshGeneration) return;
if (
generation !== _refreshGeneration ||
!isCurrentApiConfig(context)
)
return;
this.globalData.pageConfig = config;
try {
wx.setStorageSync(STORAGE_KEY_PAGE_CONFIG, config);
wx.setStorageSync(storageKey, config);
} catch {
/* 缓存失败不影响当前数据。 */
}