优化:请求接口全局处理loading和错误提示

This commit is contained in:
2025-11-26 16:37:47 +08:00
24 changed files with 568 additions and 771 deletions

View File

@@ -34,7 +34,7 @@ export function responseInterceptor(res: UniApp.RequestSuccessCallbackResult) {
}
// 可能为字符串,尝试解析(原项目也做了类似处理)
let httpData: IApiResponse | string = res.data as any;
let httpData: IApiResponse | string = res.data;
if (typeof httpData === 'string') {
try {
httpData = JSON.parse(httpData);
@@ -44,19 +44,20 @@ export function responseInterceptor(res: UniApp.RequestSuccessCallbackResult) {
}
// 规范化 message 字段
const message = (httpData as any).msg || (httpData as any).message || (httpData as any).errMsg || '';
const message = (httpData as IApiResponse).msg || (httpData as IApiResponse).message || (httpData as IApiResponse).errMsg || '';
// 成功判断:与原项目一致的条件
const successFlag = (httpData as any).success === true || (httpData as any).code === 0;
const code = (httpData as IApiResponse).code;
// 成功判断
const successFlag = (httpData as IApiResponse).success === true || code === 0;
if (successFlag) {
// 返回原始 httpData(与原项目 dataFactory 返回 Promise.resolve(httpData) 保持一致)
// 但大多数调用者更关心 data 字段,这里返回整个 httpData调用者可取 .data
// 返回原始 httpData
// 实际数据每个接口不同,调用者需根据实际情况取 .data 字段
return Promise.resolve(httpData);
}
// 登录失效或需要强制登录的一些 code(与原项目一致)
const code = (httpData as any).code;
// 登录失效或需要强制登录的一些 code
if (code === '401' || code === 401) {
// 触发登出流程
handleAuthExpired();
@@ -64,7 +65,7 @@ export function responseInterceptor(res: UniApp.RequestSuccessCallbackResult) {
}
// 原项目还将 1000,1001,1100,402 等视作需要强制登录
if (code === '1000' || code === '1001' || code === 1000 || code === 1001 || code === 1100 || code === '402' || code === 402) {
if (code == 1000 || code == 1001 || code === 1100 || code === 402) {
handleAuthExpired();
return Promise.reject({ statusCode: 0, errMsg: message || t('global.loginExpired'), data: httpData });
}

View File

@@ -253,4 +253,16 @@ export async function getActivityDescription() {
method: 'POST'
})
return res
}
/**
* 获取充值列表
*/
export async function getTransactionDetailsList(current: number, limit: number, userId: string,) {
const res = await mainClient.request<IApiResponse>({
url: 'common/transactionDetails/getTransactionDetailsList',
method: 'POST',
data: { current, limit, userId, }
})
return res
}

View File

@@ -8,6 +8,7 @@ import { t } from '@/utils/i18n'
export function createRequestClient(cfg: ICreateClientConfig) {
const baseURL = cfg.baseURL;
const timeout = cfg.timeout ?? REQUEST_TIMEOUT;
let reqCount= 0
async function request<T = any>(options: IRequestOptions): Promise<T> {
// 组装 final options
@@ -23,14 +24,18 @@ export function createRequestClient(cfg: ICreateClientConfig) {
// 全局处理请求 loading
const loading = !cfg.loading ? true : cfg.loading // 接口请求参数不传loading默认显示loading
loading && uni.showLoading()
if (loading) {
uni.showLoading({ mask: true })
reqCount++
}
return new Promise((resolve, reject) => {
uni.request({
...intercepted,
complete() {
// 请求完成关闭 loading
loading && uni.hideLoading()
loading && reqCount--
reqCount <= 0 && uni.hideLoading()
},
success(res: any) {
// 委托给响应拦截器处理