fix(mini): BASE_URL 按运行环境自动切换
- develop 走本地开发服务,trial/release 走 https://lunar.neatcn.com(原为硬编码 localhost:8080,线上无法连通) - 401 时清除失效 token 交由页面引导重新登录,不再跳转设置页 - 补交 version-badge 组件源码
This commit is contained in:
+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) {
|
||||
@@ -20,8 +38,8 @@ function request(options) {
|
||||
if (res.statusCode === 200) {
|
||||
resolve(res.data);
|
||||
} else if (res.statusCode === 401) {
|
||||
// 未授权,跳转登录
|
||||
wx.navigateTo({ url: '/pages/settings/settings' });
|
||||
// 未授权:清除失效 token,由页面引导重新登录
|
||||
wx.removeStorageSync('token');
|
||||
reject(new Error('未授权'));
|
||||
} else {
|
||||
reject(new Error(res.data.msg || '请求失败'));
|
||||
|
||||
Reference in New Issue
Block a user