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
+62 -22
View File
@@ -12,28 +12,6 @@ page {
box-sizing: border-box;
}
.debug-page__hero {
padding: 48rpx 40rpx;
background: @bg-white;
border-radius: @radius-xl;
box-shadow: @shadow;
}
.debug-page__hero-title {
display: block;
font-size: 40rpx;
font-weight: 800;
color: @text-title;
}
.debug-page__hero-desc {
display: block;
margin-top: 16rpx;
font-size: 26rpx;
line-height: 1.6;
color: @text-secondary;
}
.debug-page__section {
margin-top: 32rpx;
}
@@ -125,3 +103,65 @@ page {
font-size: 24rpx;
line-height: 1.6;
}
.debug-page__environment {
padding: 32rpx;
background: @bg-white;
border-radius: @radius-xl;
box-shadow: @shadow;
}
.debug-page__environment-url {
display: block;
word-break: break-all;
color: @text-secondary;
font-size: 24rpx;
}
.debug-page__environment-options {
display: flex;
flex-direction: column;
margin-top: 24rpx;
}
/* 明确按钮高度及整行点击区域,避免原生 button 被全局样式压缩。 */
.debug-page__environment-options .debug-environment-option {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 96rpx;
flex-shrink: 0;
box-sizing: border-box;
margin: 0;
padding: 0 24rpx;
border: 0;
font-size: 28rpx;
line-height: 1.4;
color: @text-title;
background: @bg-header;
border-radius: 20rpx;
}
.debug-page__environment-options .debug-environment-option::after {
border: 0;
}
.debug-page__environment-options
.debug-environment-option
+ .debug-environment-option {
margin-top: 16rpx;
}
.debug-page__environment-options .debug-environment-option--selected {
background: @brand;
font-weight: 700;
}
.debug-page__environment-note {
display: block;
margin-top: 20rpx;
color: @text-secondary;
font-size: 22rpx;
line-height: 1.6;
}
+46 -1
View File
@@ -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() {
+20 -5
View File
@@ -1,9 +1,24 @@
<view class="debug-page">
<view class="debug-page__hero">
<text class="debug-page__hero-title">Debug 工具页</text>
<text class="debug-page__hero-desc">
仅开发环境可见,用于发布前检查和同步调试数据。
</text>
<view wx:if="{{isDevEnv}}" class="debug-page__environment">
<text class="debug-page__section-title"
>接口环境 · {{currentEnvironment}}</text
>
<text class="debug-page__environment-url">{{currentBaseUrl}}</text>
<view class="debug-page__environment-options">
<button
wx:for="{{environments}}"
wx:key="environment"
class="debug-environment-option {{currentEnvironment === item.environment ? 'debug-environment-option--selected' : ''}}"
data-environment="{{item.environment}}"
disabled="{{!item.available || isSwitchingEnvironment}}"
bindtap="onSelectEnvironment">
{{item.environment}}
<text wx:if="{{!item.available}}">(未配置)</text>
</button>
</view>
<text class="debug-page__environment-note"
>切换后自动返回首页;下次打开开发版继续使用所选环境。</text
>
</view>
<view class="debug-page__section">