修复:解决测试发现的问题
This commit is contained in:
@@ -7,8 +7,8 @@ export const ENV = process.env.NODE_ENV || 'development';
|
||||
*/
|
||||
const BASE_URL_MAP = {
|
||||
development: {
|
||||
MAIN: 'http://192.168.110.100:9300/pb/',
|
||||
// MAIN: 'https://global.nuttyreading.com/',
|
||||
MAIN: 'http://192.168.110.100:9300/pb/', // 张川川
|
||||
// MAIN: 'https://global.nuttyreading.com/', // 线上
|
||||
// PAYMENT: 'https://dev-pay.example.com', // 暂时用不到
|
||||
// CDN: 'https://cdn-dev.example.com', // 暂时用不到
|
||||
},
|
||||
@@ -24,6 +24,6 @@ export const APP_INFO = {
|
||||
VERSION_CODE: '1.0.0', // APP 版本号,可能升级的时候会用,这里需要再确定?
|
||||
}
|
||||
|
||||
export const REQUEST_TIMEOUT = 15000;
|
||||
export const REQUEST_TIMEOUT = 30000;
|
||||
|
||||
export const SERVICE_MAP = (BASE_URL_MAP as any)[ENV] ?? BASE_URL_MAP.development;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// 片段示例 - requestInterceptor 更稳健的写法
|
||||
import type { IRequestOptions } from '../types'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import { APP_INFO } from '@/api/config'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// api/interceptors/response.ts
|
||||
import type { IApiResponse } from '../types';
|
||||
import { t } from '@/utils/i18n'
|
||||
|
||||
/**
|
||||
* 响应拦截器:严格兼容原项目返回约定
|
||||
@@ -12,23 +12,24 @@ import type { IApiResponse } from '../types';
|
||||
*/
|
||||
|
||||
function handleAuthExpired() {
|
||||
// 清空本地登录信息(保持与原项目一致)
|
||||
// 清空本地登录信息
|
||||
try {
|
||||
uni.removeStorageSync('userInfo');
|
||||
} catch (e) {}
|
||||
// 跳转 login,与原项目保持一致的路径
|
||||
// 在小程序/APP/H5 情况下原项目分别做了适配,简单通用处理如下:
|
||||
uni.showToast({ title: '登录失效,请重新登录', icon: 'none' });
|
||||
uni.showToast({ title: t('global.loginExpired'), icon: 'none' });
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({ url: '/pages/login/login' });
|
||||
uni.reLaunch({ url: '/pages/login/login' });
|
||||
}, 600);
|
||||
}
|
||||
|
||||
export function responseInterceptor(res: UniApp.RequestSuccessCallbackResult) {
|
||||
// 先处理非 200 的 http 状态
|
||||
if (res.statusCode && res.statusCode !== 200) {
|
||||
const msg = `网络错误(${res.statusCode})`;
|
||||
uni.showToast({ title: msg, icon: 'none' });
|
||||
const msg = `${t('global.networkConnectionError')} (${res.statusCode})`;
|
||||
setTimeout(() => {
|
||||
uni.showToast({ title: msg, icon: 'none' });
|
||||
}, 10);
|
||||
return Promise.reject({ statusCode: res.statusCode, errMsg: msg, response: res });
|
||||
}
|
||||
|
||||
@@ -59,17 +60,17 @@ export function responseInterceptor(res: UniApp.RequestSuccessCallbackResult) {
|
||||
if (code === '401' || code === 401) {
|
||||
// 触发登出流程
|
||||
handleAuthExpired();
|
||||
return Promise.reject({ statusCode: 0, errMsg: '登录失效', data: httpData });
|
||||
return Promise.reject({ statusCode: 0, errMsg: t('global.loginExpired'), data: httpData });
|
||||
}
|
||||
|
||||
// 原项目还将 1000,1001,1100,402 等视作需要强制登录
|
||||
if (code === '1000' || code === '1001' || code === 1000 || code === 1001 || code === 1100 || code === '402' || code === 402) {
|
||||
handleAuthExpired();
|
||||
return Promise.reject({ statusCode: 0, errMsg: message || '请登录', data: httpData });
|
||||
return Promise.reject({ statusCode: 0, errMsg: message || t('global.loginExpired'), data: httpData });
|
||||
}
|
||||
|
||||
// 其他后端业务错误:toast 并 reject
|
||||
const errMsg = message || '请求异常';
|
||||
const errMsg = message || t('global.requestException');
|
||||
if (errMsg) {
|
||||
uni.showToast({ title: errMsg, icon: 'none' });
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// api/modules/auth.ts
|
||||
import { mainClient } from '@/api/clients/main'
|
||||
import type { IApiResponse } from '@/api/types'
|
||||
import type { IUserInfo, ILoginResponse } from '@/types/user'
|
||||
import type { ILoginResponse } from '@/types/user'
|
||||
|
||||
/**
|
||||
* 验证码登录/注册
|
||||
|
||||
@@ -3,6 +3,7 @@ import { requestInterceptor } from './interceptors/request';
|
||||
import { responseInterceptor } from './interceptors/response';
|
||||
import type { IRequestOptions, ICreateClientConfig } from './types';
|
||||
import { REQUEST_TIMEOUT } from './config';
|
||||
import { t } from '@/utils/i18n'
|
||||
|
||||
export function createRequestClient(cfg: ICreateClientConfig) {
|
||||
const baseURL = cfg.baseURL;
|
||||
@@ -17,14 +18,22 @@ export function createRequestClient(cfg: ICreateClientConfig) {
|
||||
header: options.header || {},
|
||||
};
|
||||
|
||||
// run request interceptor to mutate headers, etc.
|
||||
// 运行请求拦截器,修改 headers 等
|
||||
const intercepted = requestInterceptor(final as IRequestOptions);
|
||||
|
||||
// 全局处理请求 loading
|
||||
const loading = !cfg.loading ? true : cfg.loading // 接口请求参数不传loading,默认显示loading
|
||||
loading && uni.showLoading()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
...intercepted,
|
||||
complete() {
|
||||
// 请求完成关闭 loading
|
||||
loading && uni.hideLoading()
|
||||
},
|
||||
success(res: any) {
|
||||
// delegate to response interceptor
|
||||
// 委托给响应拦截器处理
|
||||
responseInterceptor(res)
|
||||
.then((r) => {
|
||||
resolve(r as any);
|
||||
@@ -34,7 +43,7 @@ export function createRequestClient(cfg: ICreateClientConfig) {
|
||||
});
|
||||
},
|
||||
fail(err: any) {
|
||||
uni.showToast({ title: '网络连接失败', icon: 'none' });
|
||||
uni.showToast({ title: t('global.networkConnectionError'), icon: 'none' });
|
||||
reject(err);
|
||||
},
|
||||
} as any);
|
||||
|
||||
1
api/types.d.ts
vendored
1
api/types.d.ts
vendored
@@ -22,4 +22,5 @@ export interface IRequestOptions extends UniApp.RequestOptions {
|
||||
export interface ICreateClientConfig {
|
||||
baseURL: string;
|
||||
timeout?: number;
|
||||
loading?: boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user