2025-07-22 11:21:01 +08:00
|
|
|
|
import HttpRequest from './http';
|
|
|
|
|
|
import { merge } from 'lodash-es';
|
|
|
|
|
|
import { getToken } from '../auth';
|
2025-08-12 13:43:21 +08:00
|
|
|
|
import { CLIENT_ID } from '@/enums/cacheEnums';
|
2025-07-22 11:21:01 +08:00
|
|
|
|
import { RequestCodeEnum, RequestMethodsEnum } from '@/enums/requestEnums';
|
|
|
|
|
|
import { useUserStore } from '@/stores/user'
|
|
|
|
|
|
import { useMessage } from '../message';
|
|
|
|
|
|
|
|
|
|
|
|
const message = useMessage();
|
|
|
|
|
|
|
|
|
|
|
|
const requestHooks = {
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
requestInterceptorsHook(options, config) {
|
2025-08-12 16:19:19 +08:00
|
|
|
|
console.log("request=>",options);
|
|
|
|
|
|
// console.log("request=>config=>",config)
|
2025-07-22 11:21:01 +08:00
|
|
|
|
const { urlPrefix, baseUrl, withToken, isAuth } = config;
|
|
|
|
|
|
options.header = options.header ?? {};
|
|
|
|
|
|
if (urlPrefix) {
|
|
|
|
|
|
options.url = `${urlPrefix}${options.url}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (baseUrl) {
|
|
|
|
|
|
options.url = `${baseUrl}${options.url}`;
|
|
|
|
|
|
}
|
2025-08-13 09:22:58 +08:00
|
|
|
|
const token = getToken(); //console.log("token=>",token)
|
2025-07-22 11:21:01 +08:00
|
|
|
|
if (withToken && !options.header.token) {
|
|
|
|
|
|
options.header.token = token;
|
|
|
|
|
|
}
|
|
|
|
|
|
return options;
|
|
|
|
|
|
},
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
responseInterceptorsHook(response, config) {
|
2025-08-12 16:19:19 +08:00
|
|
|
|
console.log("response=>",response)
|
|
|
|
|
|
// console.log("response=>config=>",config)
|
2025-07-22 11:21:01 +08:00
|
|
|
|
const { isTransformResponse, isReturnDefaultResponse, isAuth } = config;
|
|
|
|
|
|
|
|
|
|
|
|
if (isReturnDefaultResponse) {
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!isTransformResponse) {
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
const { logout, isLogin } = useUserStore();
|
|
|
|
|
|
const { code, data, msg, show } = response.data;
|
|
|
|
|
|
// console.log(code,data,msg,show)
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
|
case RequestCodeEnum.SUCCESS:
|
|
|
|
|
|
msg && show && message.toast(msg);
|
|
|
|
|
|
return data;
|
|
|
|
|
|
case RequestCodeEnum.FAILED:
|
|
|
|
|
|
message.toast(msg);
|
|
|
|
|
|
return Promise.reject(msg);
|
|
|
|
|
|
case RequestCodeEnum.TOKEN_INVALID:
|
|
|
|
|
|
if (isAuth && isLogin) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
2025-08-12 13:43:21 +08:00
|
|
|
|
message.alert(msg)
|
2025-07-22 11:21:01 +08:00
|
|
|
|
// return data;
|
|
|
|
|
|
return Promise.reject(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
// 响应异常拦截器
|
|
|
|
|
|
responseInterceptorsCatchHook(options, err) {
|
|
|
|
|
|
if (options.method.toUpperCase() == RequestMethodsEnum.POST) {
|
|
|
|
|
|
console.log('请求失败:', err, options);
|
|
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 默认配置
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
|
|
requestOptions: {// 请求配置
|
|
|
|
|
|
timeout: 10 * 1000,
|
2025-08-12 13:43:21 +08:00
|
|
|
|
header: {
|
|
|
|
|
|
version: '1.0.0',
|
|
|
|
|
|
clientId:`${CLIENT_ID || 2}`, // clientId传2
|
|
|
|
|
|
}
|
2025-07-22 11:21:01 +08:00
|
|
|
|
},
|
|
|
|
|
|
baseUrl: `${import.meta.env.VITE_APP_BASE_URL || ''}`,// 基础 URL
|
|
|
|
|
|
isReturnDefaultResponse: false,// 是否返回默认响应
|
|
|
|
|
|
isTransformResponse: true, // 是否转换响应
|
|
|
|
|
|
urlPrefix: '',// url 前缀
|
|
|
|
|
|
ignoreCancel: true,// 忽略重复请求取消
|
|
|
|
|
|
withToken: true,// 携带 Token
|
|
|
|
|
|
isAuth: false,// 接口是否鉴权
|
|
|
|
|
|
retryCount: 2,// 重试次数
|
|
|
|
|
|
retryTimeout: 300, // 重试超时
|
|
|
|
|
|
requestHooks: requestHooks,// 请求 Hook
|
|
|
|
|
|
sslVerify: false // 设置为false,不验证 ssl 证书
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function createRequest(opt) {
|
|
|
|
|
|
return new HttpRequest(
|
|
|
|
|
|
merge(defaultOptions, opt || {})
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
const request = createRequest();
|
|
|
|
|
|
export default request;
|