feat: 增加环境切换
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
import {
|
||||
getApiEnvironment,
|
||||
getApiEnvironmentOptions,
|
||||
isApiEnvironment,
|
||||
} from '../../config/api';
|
||||
import { switchApiEnvironment } from '../../services/environment-switch';
|
||||
|
||||
type DebugEntry = {
|
||||
id: string;
|
||||
title: string;
|
||||
@@ -47,7 +54,45 @@ const DEBUG_ENTRIES: DebugEntry[] = [
|
||||
Page({
|
||||
data: {
|
||||
entries: DEBUG_ENTRIES,
|
||||
isDevEnv: true,
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user