118 lines
3.7 KiB
TypeScript
118 lines
3.7 KiB
TypeScript
import {
|
|
getApiEnvironment,
|
|
getApiEnvironmentOptions,
|
|
isApiEnvironment,
|
|
} from '../../config/api';
|
|
import { switchApiEnvironment } from '../../services/environment-switch';
|
|
|
|
type DebugEntry = {
|
|
id: string;
|
|
title: string;
|
|
subtitle: string;
|
|
icon: string;
|
|
path: string;
|
|
};
|
|
|
|
const DEBUG_ENTRIES: DebugEntry[] = [
|
|
{
|
|
id: 'unreleased-debug',
|
|
title: '未上线页面 Debug',
|
|
subtitle: '手动配置未发布页面的标题和路由,方便手机调试。',
|
|
icon: '🧪',
|
|
path: '/supportPages/unreleasedDebug/unreleasedDebug',
|
|
},
|
|
{
|
|
id: 'home-content',
|
|
title: '首页内容管理',
|
|
subtitle: '编排首页推荐位、热门推荐和分类分区内容。',
|
|
icon: '🏠',
|
|
path: '/supportPages/homeContentManage/homeContentManage',
|
|
},
|
|
{
|
|
id: 'category-manage',
|
|
title: '分类管理',
|
|
subtitle: '对比本地与云端分类数据,执行新增或更新。',
|
|
icon: '🗂️',
|
|
path: '/supportPages/categoryManage/categoryManage',
|
|
},
|
|
{
|
|
id: 'worksheet-sync',
|
|
title: 'Worksheet 同步',
|
|
subtitle: '按本地配置批量同步年龄/标签,并重建 home+category 配置。',
|
|
icon: '🔄',
|
|
path: '/supportPages/worksheetSync/worksheetSync',
|
|
},
|
|
{
|
|
id: 'category-content',
|
|
title: '分类页内容管理',
|
|
subtitle: '管理各分类下 worksheet 状态,生成分类页数据。',
|
|
icon: '📋',
|
|
path: '/supportPages/categoryContentManage/categoryContentManage',
|
|
},
|
|
];
|
|
|
|
Page({
|
|
data: {
|
|
entries: DEBUG_ENTRIES,
|
|
isDevEnv: false,
|
|
currentEnvironment: '',
|
|
currentBaseUrl: '',
|
|
environments: getApiEnvironmentOptions(),
|
|
isSwitchingEnvironment: false,
|
|
},
|
|
|
|
onShow() {
|
|
const environment = getApiEnvironment();
|
|
const environments = getApiEnvironmentOptions();
|
|
this.setData({
|
|
currentEnvironment: environment,
|
|
currentBaseUrl:
|
|
environments.find((item) => item.environment === environment)
|
|
?.baseUrl || '',
|
|
environments,
|
|
});
|
|
},
|
|
|
|
async onSelectEnvironment(e: WechatMiniprogram.TouchEvent) {
|
|
if (!this.data.isDevEnv || this.data.isSwitchingEnvironment) return;
|
|
const environment: unknown = e.currentTarget.dataset.environment;
|
|
if (!isApiEnvironment(environment)) return;
|
|
this.setData({ isSwitchingEnvironment: true });
|
|
wx.showLoading({ title: '切换环境中', mask: true });
|
|
let errorMessage = '';
|
|
try {
|
|
await switchApiEnvironment(environment);
|
|
} catch (error) {
|
|
errorMessage = error instanceof Error ? error.message : '切换失败';
|
|
} finally {
|
|
wx.hideLoading();
|
|
// 成功时本页已被 reLaunch 销毁,失败时才需要恢复按钮。
|
|
if (getCurrentPages().includes(this)) {
|
|
this.setData({ isSwitchingEnvironment: false });
|
|
this.onShow();
|
|
}
|
|
}
|
|
if (errorMessage) wx.showToast({ title: errorMessage, icon: 'none' });
|
|
},
|
|
|
|
onLoad() {
|
|
const envVersion = wx.getAccountInfoSync().miniProgram.envVersion;
|
|
const isDevEnv = envVersion === 'develop';
|
|
this.setData({ isDevEnv });
|
|
|
|
if (!isDevEnv) {
|
|
wx.showToast({
|
|
title: '仅开发版可用',
|
|
icon: 'none',
|
|
});
|
|
}
|
|
},
|
|
|
|
onTapEntry(e: WechatMiniprogram.TouchEvent) {
|
|
if (!this.data.isDevEnv) return;
|
|
const path = e.currentTarget.dataset.path as string;
|
|
if (!path) return;
|
|
wx.navigateTo({ url: path });
|
|
},
|
|
});
|