feat: 完成全部接口替换为新后端接口
This commit is contained in:
+54
-22
@@ -1,7 +1,11 @@
|
||||
import { fetchPageConfig } from './services/site-api';
|
||||
import { normalizePreviewImages } from './config/assets';
|
||||
import { defaultPrintConfig } from './config/config';
|
||||
import { getUser } from './utils/auth';
|
||||
import { generateUUID, getAppUUID, setAppUUID } from './utils/uuid';
|
||||
|
||||
let _refreshGeneration = 0;
|
||||
|
||||
const STORAGE_KEY_PAGE_CONFIG = 'pageConfig';
|
||||
|
||||
// Pending resolvers for getPageConfig() callers waiting on config
|
||||
@@ -18,11 +22,12 @@ App<IAppOption>({
|
||||
pageConfigReady: false,
|
||||
},
|
||||
onLaunch() {
|
||||
wx.cloud.init({
|
||||
env: 'cloud1-9gifs7a2756e2c87',
|
||||
traceUser: true,
|
||||
});
|
||||
|
||||
try {
|
||||
wx.removeStorageSync('user_info');
|
||||
wx.removeStorageSync('user_info_expire');
|
||||
} catch {
|
||||
/* 旧云登录缓存清理失败不影响新凭证命名空间。 */
|
||||
}
|
||||
const accountInfo = wx.getAccountInfoSync();
|
||||
const env = accountInfo.miniProgram.envVersion || 'release';
|
||||
this.globalData.env = env;
|
||||
@@ -41,19 +46,22 @@ App<IAppOption>({
|
||||
this.globalData.printConfig = printConfig;
|
||||
}
|
||||
|
||||
// 静默登录(不阻塞页面渲染)
|
||||
getUser()
|
||||
.then((user) => {
|
||||
this.globalData.user = user;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('静默登录失败', err);
|
||||
});
|
||||
// 启动与回到前台共用登录 Promise,不阻塞公开页面渲染。
|
||||
this.warmAuthentication();
|
||||
|
||||
void this.loadPageConfig();
|
||||
},
|
||||
|
||||
onShow() {},
|
||||
onShow() {
|
||||
this.warmAuthentication();
|
||||
if (this.globalData.pageConfigReady) void this.refreshPageConfig();
|
||||
},
|
||||
|
||||
warmAuthentication() {
|
||||
void getUser().catch(() => {
|
||||
console.warn('静默登录暂未完成,公开浏览仍可使用');
|
||||
});
|
||||
},
|
||||
|
||||
onHide() {},
|
||||
|
||||
@@ -100,20 +108,19 @@ App<IAppOption>({
|
||||
}
|
||||
}
|
||||
|
||||
// L3: 云函数兜底
|
||||
// L3: 新后端动态兜底;微信预拉取机制和地址保持不变。
|
||||
const hadCachedConfig = !!config;
|
||||
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;
|
||||
}
|
||||
config = await fetchPageConfig();
|
||||
} catch {
|
||||
// 云函数也失败,config 保持 null
|
||||
/* 新后端不可用时继续展示空状态,本地生成仍可使用。 */
|
||||
}
|
||||
}
|
||||
|
||||
// 三种数据入口都先转换预览图,旧缓存会随成功加载写回新地址。
|
||||
config = normalizePreviewImages(config);
|
||||
|
||||
// 写入 globalData
|
||||
if (config) {
|
||||
this.globalData.pageConfig = config;
|
||||
@@ -131,6 +138,31 @@ App<IAppOption>({
|
||||
resolve(config);
|
||||
}
|
||||
_configResolvers = [];
|
||||
// 预拉取/缓存先渲染,再用新后端刷新,避免旧静态配置长期覆盖管理修改。
|
||||
if (hadCachedConfig) void this.refreshPageConfig();
|
||||
},
|
||||
|
||||
async refreshPageConfig() {
|
||||
const generation = ++_refreshGeneration;
|
||||
try {
|
||||
const config = normalizePreviewImages(await fetchPageConfig());
|
||||
if (generation !== _refreshGeneration) return;
|
||||
this.globalData.pageConfig = config;
|
||||
try {
|
||||
wx.setStorageSync(STORAGE_KEY_PAGE_CONFIG, config);
|
||||
} catch {
|
||||
/* 缓存失败不影响当前数据。 */
|
||||
}
|
||||
for (const page of getCurrentPages()) {
|
||||
if (
|
||||
page.route === 'pages/home/home' &&
|
||||
typeof page.initData === 'function'
|
||||
)
|
||||
void page.initData(config);
|
||||
}
|
||||
} catch {
|
||||
/* 保留已显示的数据,网络错误不清空预拉取首屏。 */
|
||||
}
|
||||
},
|
||||
|
||||
getPageConfig(): Promise<PageConfigData | null> {
|
||||
|
||||
Reference in New Issue
Block a user