|
|
@ -1,190 +1,202 @@
|
|
|
|
import axios from 'axios'
|
|
|
|
// 引入必要的库和模块
|
|
|
|
import qs from 'qs'
|
|
|
|
import axios from 'axios'; // 用于发起HTTP请求
|
|
|
|
import cookie from 'vue-cookies'
|
|
|
|
import qs from 'qs'; // 用于序列化查询参数
|
|
|
|
import router from '@/router'
|
|
|
|
import cookie from 'vue-cookies'; // 用于管理cookie
|
|
|
|
import merge from 'lodash/merge'
|
|
|
|
import router from '@/router'; // 项目路由实例
|
|
|
|
import { clearLoginInfo } from '@/utils'
|
|
|
|
import merge from 'lodash/merge'; // 用于合并对象
|
|
|
|
import { ElMessage } from 'element-plus'
|
|
|
|
import { clearLoginInfo } from '@/utils'; // 清除登录信息的工具函数
|
|
|
|
|
|
|
|
import { ElMessage } from 'element-plus'; // Element Plus的消息提示组件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个自定义的axios实例
|
|
|
|
const http = axios.create({
|
|
|
|
const http = axios.create({
|
|
|
|
timeout: 1000 * 30,
|
|
|
|
timeout: 1000 * 30, // 设置请求超时时间为30秒
|
|
|
|
withCredentials: true,
|
|
|
|
withCredentials: true, // 允许跨域请求时携带凭证(如Cookie)
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
'Content-Type': 'application/json; charset=utf-8' // 默认设置请求头为JSON格式
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 请求拦截
|
|
|
|
* 请求拦截器
|
|
|
|
|
|
|
|
* 在每个请求发送之前执行,可以在此处添加通用的请求头或处理请求参数等。
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
http.interceptors.request.use(
|
|
|
|
http.interceptors.request.use(
|
|
|
|
config => {
|
|
|
|
config => {
|
|
|
|
config.headers.Authorization = cookie.get('Authorization') // 请求头带上token
|
|
|
|
// 如果存在Authorization token,则添加到请求头中
|
|
|
|
// 只针对get方式进行序列化
|
|
|
|
config.headers.Authorization = cookie.get('Authorization');
|
|
|
|
if (config.method === 'get' || config.method === 'GET') {
|
|
|
|
|
|
|
|
config.paramsSerializer = function (params) {
|
|
|
|
// 对GET请求进行参数序列化,支持数组格式重复参数
|
|
|
|
return qs.stringify(params, { arrayFormat: 'repeat' })
|
|
|
|
if (config.method.toLowerCase() === 'get') {
|
|
|
|
}
|
|
|
|
config.paramsSerializer = params => qs.stringify(params, { arrayFormat: 'repeat' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
error => Promise.reject(error) // 捕获请求错误并返回拒绝的Promise
|
|
|
|
return Promise.reject(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 响应拦截
|
|
|
|
* 响应拦截器
|
|
|
|
|
|
|
|
* 在接收到响应数据后执行,可以根据响应状态码或自定义业务逻辑来处理响应。
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
http.interceptors.response.use(
|
|
|
|
http.interceptors.response.use(
|
|
|
|
response => {
|
|
|
|
response => {
|
|
|
|
// blob 格式处理
|
|
|
|
// 直接返回blob类型的响应数据
|
|
|
|
if (response.request.responseType === 'blob') {
|
|
|
|
if (response.request.responseType === 'blob') {
|
|
|
|
return response
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = response.data
|
|
|
|
|
|
|
|
// 00000 请求成功
|
|
|
|
|
|
|
|
if (res.code === '00000' || res.code === 'A00002') {
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// A00001 用于直接显示提示用户的错误,内容由输入决定
|
|
|
|
|
|
|
|
if (res.code === 'A00001') {
|
|
|
|
const res = response.data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据不同的响应码执行相应的操作
|
|
|
|
|
|
|
|
switch (res.code) {
|
|
|
|
|
|
|
|
case '00000': // 成功响应
|
|
|
|
|
|
|
|
case 'A00002': // 特定成功的响应码
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'A00001': // 用户级错误消息
|
|
|
|
ElMessage({
|
|
|
|
ElMessage({
|
|
|
|
message: res.msg || res.data || 'Error',
|
|
|
|
message: res.msg || res.data || 'Error',
|
|
|
|
type: 'error',
|
|
|
|
type: 'error',
|
|
|
|
duration: 1.5 * 1000
|
|
|
|
duration: 1500
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return Promise.reject(res)
|
|
|
|
return Promise.reject(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A00004 未授权
|
|
|
|
case 'A00004': // 未授权
|
|
|
|
if (res.code === 'A00004') {
|
|
|
|
clearLoginInfo();
|
|
|
|
clearLoginInfo()
|
|
|
|
router.push({ name: 'login' });
|
|
|
|
router.push({ name: 'login' })
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A00005 服务器异常
|
|
|
|
case 'A00005': // 服务器异常
|
|
|
|
if (res.code === 'A00005') {
|
|
|
|
console.error(`接口地址: ${response.config.url.replace(import.meta.env.VITE_APP_BASE_API, '')}`, '\n', `异常信息: ${res}`);
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
|
|
|
|
console.error('============== 请求异常 ==============', '\n', `接口地址: ${response.config.url.replace(import.meta.env.VITE_APP_BASE_API, '')}`, '\n', `异常信息: ${res}`, '\n', '============== 请求异常 end ==========')
|
|
|
|
|
|
|
|
ElMessage({
|
|
|
|
ElMessage({
|
|
|
|
message: '服务器出了点小差,请稍后再试',
|
|
|
|
message: '服务器出了点小差,请稍后再试',
|
|
|
|
type: 'error',
|
|
|
|
type: 'error',
|
|
|
|
duration: 1.5 * 1000,
|
|
|
|
duration: 1500,
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return Promise.reject(res)
|
|
|
|
return Promise.reject(res);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
error => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
// 处理不同HTTP状态码的错误响应
|
|
|
|
console.log('========请求失败========', '\n', error.response, '\n', '========请求失败 end========')
|
|
|
|
console.log('请求失败:', error.response);
|
|
|
|
switch (error.response.status) {
|
|
|
|
const status = error.response?.status;
|
|
|
|
case 400:
|
|
|
|
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
|
|
case 400: // 客户端错误
|
|
|
|
ElMessage({
|
|
|
|
ElMessage({
|
|
|
|
message: error.response.data,
|
|
|
|
message: error.response.data,
|
|
|
|
type: 'error',
|
|
|
|
type: 'error',
|
|
|
|
duration: 1500,
|
|
|
|
duration: 1500,
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
|
|
|
|
clearLoginInfo()
|
|
|
|
case 401: // 未授权
|
|
|
|
router.push({ name: 'login' })
|
|
|
|
clearLoginInfo();
|
|
|
|
break
|
|
|
|
router.push({ name: 'login' });
|
|
|
|
case 405:
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 405: // 方法不允许
|
|
|
|
ElMessage({
|
|
|
|
ElMessage({
|
|
|
|
message: 'http请求方式有误',
|
|
|
|
message: 'http请求方式有误',
|
|
|
|
type: 'error',
|
|
|
|
type: 'error',
|
|
|
|
duration: 1500,
|
|
|
|
duration: 1500,
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
case 500:
|
|
|
|
|
|
|
|
|
|
|
|
case 500: // 服务器内部错误
|
|
|
|
ElMessage({
|
|
|
|
ElMessage({
|
|
|
|
message: '服务器出了点小差,请稍后再试',
|
|
|
|
message: '服务器出了点小差,请稍后再试',
|
|
|
|
type: 'error',
|
|
|
|
type: 'error',
|
|
|
|
duration: 1500,
|
|
|
|
duration: 1500,
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
case 501:
|
|
|
|
|
|
|
|
|
|
|
|
case 501: // 服务器不支持当前请求功能
|
|
|
|
ElMessage({
|
|
|
|
ElMessage({
|
|
|
|
message: '服务器不支持当前请求所需要的某个功能',
|
|
|
|
message: '服务器不支持当前请求所需要的某个功能',
|
|
|
|
type: 'error',
|
|
|
|
type: 'error',
|
|
|
|
duration: 1500,
|
|
|
|
duration: 1500,
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
customClass: 'element-error-message-zindex'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 请求地址处理
|
|
|
|
* 请求地址处理
|
|
|
|
* @param {*} actionName action方法名称
|
|
|
|
* @param {string} actionName - API方法名称
|
|
|
|
|
|
|
|
* @returns {string} - 完整的API请求URL
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
http.adornUrl = actionName => {
|
|
|
|
http.adornUrl = actionName => import.meta.env.VITE_APP_BASE_API + actionName;
|
|
|
|
return import.meta.env.VITE_APP_BASE_API + actionName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* im请求地址处理
|
|
|
|
* IM请求地址处理
|
|
|
|
* @param {*} actionName action方法名称
|
|
|
|
* @param {string} actionName - IM API方法名称
|
|
|
|
|
|
|
|
* @returns {string} - 完整的IM API请求URL
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
http.adornImUrl = actionName => {
|
|
|
|
http.adornImUrl = actionName => import.meta.env.VITE_APP_IM_API + actionName;
|
|
|
|
return import.meta.env.VITE_APP_IM_API + actionName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* im ws 请求地址处理
|
|
|
|
* IM WebSocket请求地址处理
|
|
|
|
* @param {*} actionName action方法名称
|
|
|
|
* @param {string} actionName - WebSocket IM API方法名称
|
|
|
|
|
|
|
|
* @returns {string} - 完整的WebSocket IM API请求URL
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
http.adornWsImUrl = actionName => {
|
|
|
|
http.adornWsImUrl = actionName => import.meta.env.VITE_APP_WS_IM_API + actionName;
|
|
|
|
return import.meta.env.VITE_APP_WS_IM_API + actionName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* get请求参数处理
|
|
|
|
* GET请求参数处理
|
|
|
|
* @param {*} params 参数对象
|
|
|
|
* @param {Object} [params={}] - 参数对象
|
|
|
|
* @param {*} openDefultParams 是否开启默认参数?
|
|
|
|
* @param {boolean} [openDefultParams=true] - 是否开启默认参数
|
|
|
|
|
|
|
|
* @returns {Object} - 处理后的参数对象
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
http.adornParams = (params = {}, openDefultParams = true) => {
|
|
|
|
http.adornParams = (params = {}, openDefultParams = true) => {
|
|
|
|
const defaults = {
|
|
|
|
const defaults = { t: Date.now() }; // 添加时间戳以防止缓存
|
|
|
|
t: Date.now()
|
|
|
|
return openDefultParams ? merge(defaults, params) : params;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return openDefultParams ? merge(defaults, params) : params
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* post请求数据处理
|
|
|
|
* POST请求数据处理
|
|
|
|
* @param {*} data 数据对象
|
|
|
|
* @param {Object} [data={}] - 数据对象
|
|
|
|
* @param {*} openDefultdata 是否开启默认数据?
|
|
|
|
* @param {boolean} [openDefultdata=true] - 是否开启默认数据
|
|
|
|
* @param {*} contentType 数据格式
|
|
|
|
* @param {string} [contentType='json'] - 数据格式:'json' 或 'form'
|
|
|
|
* json: 'application/json; charset=utf-8'
|
|
|
|
* @returns {string|FormData} - 处理后的数据
|
|
|
|
* form: 'application/x-www-form-urlencoded; charset=utf-8'
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
|
|
|
|
http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
|
|
|
|
const defaults = {
|
|
|
|
const defaults = { t: Date.now() };
|
|
|
|
t: Date.now()
|
|
|
|
data = openDefultdata ? merge(defaults, data) : data;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = openDefultdata ? merge(defaults, data) : data
|
|
|
|
// 根据contentType决定如何序列化数据
|
|
|
|
return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
|
|
|
|
return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 文件上传函数
|
|
|
|
|
|
|
|
* @param {string} url - 上传文件的目标URL
|
|
|
|
|
|
|
|
* @param {File} file - 要上传的文件
|
|
|
|
|
|
|
|
* @returns {Promise} - 返回上传结果的Promise
|
|
|
|
|
|
|
|
*/
|
|
|
|
const uploadFile = function (url, file) {
|
|
|
|
const uploadFile = function (url, file) {
|
|
|
|
const config = {
|
|
|
|
const config = {
|
|
|
|
// 添加请求头
|
|
|
|
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
Authorization: cookie.get('Authorization') // 请求头带上token
|
|
|
|
Authorization: cookie.get('Authorization') // 添加token到请求头
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const param = new FormData()
|
|
|
|
|
|
|
|
// 通过append向form对象添加数据
|
|
|
|
|
|
|
|
param.append('file', file)
|
|
|
|
|
|
|
|
return axios.post(url, param, config)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const param = new FormData();
|
|
|
|
|
|
|
|
param.append('file', file); // 向FormData对象添加文件
|
|
|
|
|
|
|
|
return axios.post(url, param, config);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default http
|
|
|
|
export default http;
|
|
|
|
export { uploadFile }
|
|
|
|
export { uploadFile };
|
|
|
|