2023-07-06 20:59:22 +08:00
|
|
|
|
import router from '../router';
|
2021-06-07 17:22:07 +08:00
|
|
|
|
import Axios from 'axios';
|
|
|
|
|
|
import config from './config';
|
2023-10-18 15:24:29 +08:00
|
|
|
|
import { getClientUuid, getToken, joinClientParams } from './utils/storage';
|
2021-06-07 17:22:07 +08:00
|
|
|
|
import { templateResolve } from './utils/string';
|
|
|
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
|
|
|
|
|
|
|
export interface Result {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 响应码
|
|
|
|
|
|
*/
|
|
|
|
|
|
code: number;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 响应消息
|
|
|
|
|
|
*/
|
|
|
|
|
|
msg: string;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
data?: any;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-16 17:37:33 +08:00
|
|
|
|
enum ResultEnum {
|
|
|
|
|
|
SUCCESS = 200,
|
|
|
|
|
|
ERROR = 400,
|
|
|
|
|
|
PARAM_ERROR = 405,
|
|
|
|
|
|
SERVER_ERROR = 500,
|
|
|
|
|
|
NO_PERMISSION = 501,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 20:59:22 +08:00
|
|
|
|
const baseUrl: string = config.baseApiUrl;
|
2023-10-10 17:39:46 +08:00
|
|
|
|
// const baseWsUrl: string = config.baseWsUrl;
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通知错误消息
|
|
|
|
|
|
* @param msg 错误消息
|
|
|
|
|
|
*/
|
|
|
|
|
|
function notifyErrorMsg(msg: string) {
|
|
|
|
|
|
// 危险通知
|
|
|
|
|
|
ElMessage.error(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create an axios instance
|
|
|
|
|
|
const service = Axios.create({
|
|
|
|
|
|
baseURL: baseUrl, // url = base url + request url
|
2023-10-10 17:39:46 +08:00
|
|
|
|
timeout: 60000, // request timeout
|
2023-07-06 20:59:22 +08:00
|
|
|
|
});
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
// request interceptor
|
|
|
|
|
|
service.interceptors.request.use(
|
2022-04-12 17:14:44 +08:00
|
|
|
|
(config: any) => {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// do something before request is sent
|
2023-09-16 17:07:48 +08:00
|
|
|
|
const token = getToken();
|
2021-06-07 17:22:07 +08:00
|
|
|
|
if (token) {
|
|
|
|
|
|
// 设置token
|
2023-07-06 20:59:22 +08:00
|
|
|
|
config.headers['Authorization'] = token;
|
2023-10-18 15:24:29 +08:00
|
|
|
|
config.headers['Client-Uuid'] = getClientUuid();
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-07-06 20:59:22 +08:00
|
|
|
|
return config;
|
2021-06-07 17:22:07 +08:00
|
|
|
|
},
|
2023-07-06 20:59:22 +08:00
|
|
|
|
(error) => {
|
|
|
|
|
|
return Promise.reject(error);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-07-06 20:59:22 +08:00
|
|
|
|
);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
// response interceptor
|
|
|
|
|
|
service.interceptors.response.use(
|
2023-07-06 20:59:22 +08:00
|
|
|
|
(response) => {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 获取请求返回结果
|
|
|
|
|
|
const data: Result = response.data;
|
2023-08-16 17:37:33 +08:00
|
|
|
|
if (data.code === ResultEnum.SUCCESS) {
|
|
|
|
|
|
return data.data;
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 如果提示没有权限,则移除token,使其重新登录
|
|
|
|
|
|
if (data.code === ResultEnum.NO_PERMISSION) {
|
2021-06-09 16:58:57 +08:00
|
|
|
|
router.push({
|
|
|
|
|
|
path: '/401',
|
|
|
|
|
|
});
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-08-16 17:37:33 +08:00
|
|
|
|
return Promise.reject(data);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
},
|
|
|
|
|
|
(e: any) => {
|
2023-08-16 17:37:33 +08:00
|
|
|
|
const rejectPromise = Promise.reject(e);
|
|
|
|
|
|
|
|
|
|
|
|
const statusCode = e.response?.status;
|
|
|
|
|
|
if (statusCode == 500) {
|
|
|
|
|
|
notifyErrorMsg('服务器未知异常');
|
|
|
|
|
|
return rejectPromise;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (statusCode == 404) {
|
|
|
|
|
|
notifyErrorMsg('请求接口未找到');
|
|
|
|
|
|
return rejectPromise;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-07 17:22:07 +08:00
|
|
|
|
if (e.message) {
|
|
|
|
|
|
// 对响应错误做点什么
|
|
|
|
|
|
if (e.message.indexOf('timeout') != -1) {
|
2023-08-16 17:37:33 +08:00
|
|
|
|
notifyErrorMsg('网络请求超时');
|
|
|
|
|
|
return rejectPromise;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (e.message == 'Network Error') {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
notifyErrorMsg('网络连接错误');
|
2023-08-16 17:37:33 +08:00
|
|
|
|
return rejectPromise;
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-16 17:37:33 +08:00
|
|
|
|
notifyErrorMsg('网络请求错误');
|
|
|
|
|
|
return rejectPromise;
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-07-06 20:59:22 +08:00
|
|
|
|
);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 请求uri
|
|
|
|
|
|
* 该方法已处理请求结果中code != 200的message提示,如需其他错误处理(取消加载状态,重置对象状态等等),可catch继续处理
|
2023-07-06 20:59:22 +08:00
|
|
|
|
*
|
2021-06-07 17:22:07 +08:00
|
|
|
|
* @param {Object} method 请求方法(GET,POST,PUT,DELTE等)
|
|
|
|
|
|
* @param {Object} uri uri
|
|
|
|
|
|
* @param {Object} params 参数
|
|
|
|
|
|
*/
|
2022-04-12 17:14:44 +08:00
|
|
|
|
function request(method: string, url: string, params: any = null, headers: any = null, options: any = null): Promise<any> {
|
2023-07-06 20:59:22 +08:00
|
|
|
|
if (!url) throw new Error('请求url不能为空');
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 简单判断该url是否是restful风格
|
2023-07-06 20:59:22 +08:00
|
|
|
|
if (url.indexOf('{') != -1) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
url = templateResolve(url, params);
|
|
|
|
|
|
}
|
|
|
|
|
|
const query: any = {
|
|
|
|
|
|
method,
|
|
|
|
|
|
url: url,
|
2023-07-06 20:59:22 +08:00
|
|
|
|
...options,
|
2021-06-07 17:22:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
if (headers) {
|
2023-07-06 20:59:22 +08:00
|
|
|
|
query.headers = headers;
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// post和put使用json格式传参
|
2023-04-13 20:11:22 +08:00
|
|
|
|
if (method === 'post' || method === 'put') {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
query.data = params;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
query.params = params;
|
|
|
|
|
|
}
|
2023-07-06 20:59:22 +08:00
|
|
|
|
return service
|
|
|
|
|
|
.request(query)
|
|
|
|
|
|
.then((res) => res)
|
|
|
|
|
|
.catch((e) => {
|
2023-09-16 17:07:48 +08:00
|
|
|
|
// 如果返回的code不为成功,则会返回对应的错误msg,则直接统一通知即可。忽略登录超时或没有权限的提示(直接跳转至401页面)
|
|
|
|
|
|
if (e.msg && e?.code != ResultEnum.NO_PERMISSION) {
|
2023-07-06 20:59:22 +08:00
|
|
|
|
notifyErrorMsg(e.msg);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(e);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-06 20:59:22 +08:00
|
|
|
|
* get请求uri
|
|
|
|
|
|
* 该方法已处理请求结果中code != 200的message提示,如需其他错误处理(取消加载状态,重置对象状态等等),可catch继续处理
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param {Object} url uri
|
|
|
|
|
|
* @param {Object} params 参数
|
2021-06-07 17:22:07 +08:00
|
|
|
|
*/
|
2023-07-06 20:59:22 +08:00
|
|
|
|
function get(url: string, params: any = null, headers: any = null, options: any = null): Promise<any> {
|
|
|
|
|
|
return request('get', url, params, headers, options);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-06 20:59:22 +08:00
|
|
|
|
function post(url: string, params: any = null, headers: any = null, options: any = null): Promise<any> {
|
|
|
|
|
|
return request('post', url, params, headers, options);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function put(url: string, params: any = null, headers: any = null, options: any = null): Promise<any> {
|
|
|
|
|
|
return request('put', url, params, headers, options);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function del(url: string, params: any = null, headers: any = null, options: any = null): Promise<any> {
|
|
|
|
|
|
return request('delete', url, params, headers, options);
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getApiUrl(url: string) {
|
|
|
|
|
|
// 只是返回api地址而不做请求,用在上传组件之类的
|
2023-10-18 15:24:29 +08:00
|
|
|
|
return baseUrl + url + '?' + joinClientParams();
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
request,
|
2023-07-06 20:59:22 +08:00
|
|
|
|
get,
|
|
|
|
|
|
post,
|
|
|
|
|
|
put,
|
|
|
|
|
|
del,
|
|
|
|
|
|
getApiUrl,
|
|
|
|
|
|
};
|