fix(mini): BASE_URL 按运行环境自动切换
- develop 走本地开发服务,trial/release 走 https://lunar.neatcn.com(原为硬编码 localhost:8080,线上无法连通) - 401 时清除失效 token 交由页面引导重新登录,不再跳转设置页 - 补交 version-badge 组件源码
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* 版本号显示组件
|
||||||
|
* 使用方法:
|
||||||
|
* <version-badge />
|
||||||
|
*/
|
||||||
|
|
||||||
|
const versionInfo = require('../../utils/version.js');
|
||||||
|
|
||||||
|
Component({
|
||||||
|
properties: {
|
||||||
|
// 是否显示详细信息(构建编号、commit SHA)
|
||||||
|
showDetail: {
|
||||||
|
type: Boolean,
|
||||||
|
value: false
|
||||||
|
},
|
||||||
|
// 是否显示构建时间
|
||||||
|
showTime: {
|
||||||
|
type: Boolean,
|
||||||
|
value: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
data: {
|
||||||
|
version: versionInfo.version,
|
||||||
|
buildNumber: versionInfo.buildNumber,
|
||||||
|
buildTime: versionInfo.buildTime,
|
||||||
|
commitSha: versionInfo.commitSha
|
||||||
|
},
|
||||||
|
|
||||||
|
lifetimes: {
|
||||||
|
attached() {
|
||||||
|
// 格式化构建时间
|
||||||
|
if (this.data.buildTime && this.data.buildTime !== 'unknown') {
|
||||||
|
const date = new Date(this.data.buildTime);
|
||||||
|
const formatted = date.toLocaleString('zh-CN', {
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit'
|
||||||
|
});
|
||||||
|
this.setData({ formattedTime: formatted });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"component": true,
|
||||||
|
"usingComponents": {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<view class="version-badge">
|
||||||
|
<text class="version-text">v{{version}}</text>
|
||||||
|
<text class="version-detail" wx:if="{{showDetail && buildNumber !== '0'}}">#{{buildNumber}}</text>
|
||||||
|
<text class="version-detail" wx:if="{{showDetail && commitSha !== 'unknown'}}">({{commitSha}})</text>
|
||||||
|
<text class="version-time" wx:if="{{showTime && formattedTime}}">{{formattedTime}}</text>
|
||||||
|
</view>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
.version-badge {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #9E8E7E;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-text {
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-detail {
|
||||||
|
font-size: 11px;
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-time {
|
||||||
|
font-size: 11px;
|
||||||
|
opacity: 0.6;
|
||||||
|
margin-left: 4px;
|
||||||
|
}
|
||||||
+21
-3
@@ -1,5 +1,23 @@
|
|||||||
// 网络请求封装
|
// 网络请求封装
|
||||||
const BASE_URL = 'http://localhost:8080'; // 开发环境
|
// 按小程序运行环境自动切换后端地址:
|
||||||
|
// - develop(开发者工具):本地开发服务
|
||||||
|
// - trial/release(体验版/正式版):生产域名(需在微信公众平台配置为 request 合法域名)
|
||||||
|
const API_HOSTS = {
|
||||||
|
develop: 'http://localhost:8080',
|
||||||
|
trial: 'https://lunar.neatcn.com',
|
||||||
|
release: 'https://lunar.neatcn.com',
|
||||||
|
};
|
||||||
|
|
||||||
|
function resolveBaseUrl() {
|
||||||
|
try {
|
||||||
|
const envVersion = wx.getAccountInfoSync().miniProgram.envVersion;
|
||||||
|
return API_HOSTS[envVersion] || API_HOSTS.release;
|
||||||
|
} catch (e) {
|
||||||
|
return API_HOSTS.release;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const BASE_URL = resolveBaseUrl();
|
||||||
|
|
||||||
// 请求拦截器
|
// 请求拦截器
|
||||||
function request(options) {
|
function request(options) {
|
||||||
@@ -20,8 +38,8 @@ function request(options) {
|
|||||||
if (res.statusCode === 200) {
|
if (res.statusCode === 200) {
|
||||||
resolve(res.data);
|
resolve(res.data);
|
||||||
} else if (res.statusCode === 401) {
|
} else if (res.statusCode === 401) {
|
||||||
// 未授权,跳转登录
|
// 未授权:清除失效 token,由页面引导重新登录
|
||||||
wx.navigateTo({ url: '/pages/settings/settings' });
|
wx.removeStorageSync('token');
|
||||||
reject(new Error('未授权'));
|
reject(new Error('未授权'));
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(res.data.msg || '请求失败'));
|
reject(new Error(res.data.msg || '请求失败'));
|
||||||
|
|||||||
Reference in New Issue
Block a user