|
@@ -1,99 +1,91 @@
|
|
-import axios from 'axios';
|
|
|
|
-import qs from 'qs';
|
|
|
|
-import NProgress from 'nprogress'; //页面顶部加载进度条
|
|
|
|
-import 'nprogress/nprogress.css';
|
|
|
|
|
|
+import axios from "axios";
|
|
|
|
+import qs from "qs";
|
|
|
|
+import NProgress from "nprogress"; //页面顶部加载进度条
|
|
|
|
+import "nprogress/nprogress.css";
|
|
|
|
|
|
-import {
|
|
|
|
- ElNotification,
|
|
|
|
- ElMessageBox,
|
|
|
|
- ElMessage,
|
|
|
|
- ElLoading
|
|
|
|
-} from 'element-plus';
|
|
|
|
|
|
+import { ElNotification, ElMessageBox, ElMessage, ElLoading } from "element-plus";
|
|
//此处注释store
|
|
//此处注释store
|
|
// import store from '@/store';
|
|
// import store from '@/store';
|
|
-import cache from '@/utils/cache';
|
|
|
|
|
|
+import cache from "@/utils/cache";
|
|
|
|
|
|
// axios实例
|
|
// axios实例
|
|
const request = axios.create({
|
|
const request = axios.create({
|
|
- // axios中请求配置有baseURL选项,表示请求URL公共部分
|
|
|
|
- baseURL: import.meta.env.VITE_API_URL,
|
|
|
|
- // 超时
|
|
|
|
- timeout: 60000,
|
|
|
|
- headers: {
|
|
|
|
- 'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
- }
|
|
|
|
|
|
+ // axios中请求配置有baseURL选项,表示请求URL公共部分
|
|
|
|
+ baseURL: import.meta.env.NODE_ENV === "development" ? import.meta.env.VUE_APP_BASE_API : import.meta.env.VITE_API_URL,
|
|
|
|
+ // 超时
|
|
|
|
+ timeout: 60000,
|
|
|
|
+ headers: {
|
|
|
|
+ "Content-Type": "application/json; charset=utf-8"
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
NProgress.configure({
|
|
NProgress.configure({
|
|
- showSpinner: true
|
|
|
|
|
|
+ showSpinner: true
|
|
});
|
|
});
|
|
|
|
|
|
// 请求拦截器
|
|
// 请求拦截器
|
|
request.interceptors.request.use(
|
|
request.interceptors.request.use(
|
|
- (config) => {
|
|
|
|
- // const userStore = store.userStore
|
|
|
|
- // if (userStore?.token) {
|
|
|
|
- // config.headers.Authorization = userStore.token
|
|
|
|
- // }
|
|
|
|
|
|
+ (config) => {
|
|
|
|
+ // const userStore = store.userStore
|
|
|
|
+ // if (userStore?.token) {
|
|
|
|
+ // config.headers.Authorization = userStore.token
|
|
|
|
+ // }
|
|
|
|
|
|
- // 追加时间戳,防止GET请求缓存
|
|
|
|
- if (config.method?.toUpperCase() === 'GET') {
|
|
|
|
- config.params = {
|
|
|
|
- ...config.params,
|
|
|
|
- t: new Date().getTime()
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // 追加时间戳,防止GET请求缓存
|
|
|
|
+ if (config.method?.toUpperCase() === "GET") {
|
|
|
|
+ config.params = {
|
|
|
|
+ ...config.params,
|
|
|
|
+ t: new Date().getTime()
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
|
|
- if (Object.values(config.headers).includes('application/x-www-form-urlencoded')) {
|
|
|
|
- config.data = qs.stringify(config.data)
|
|
|
|
- }
|
|
|
|
|
|
+ if (Object.values(config.headers).includes("application/x-www-form-urlencoded")) {
|
|
|
|
+ config.data = qs.stringify(config.data);
|
|
|
|
+ }
|
|
|
|
|
|
- return config
|
|
|
|
- },
|
|
|
|
- error => {
|
|
|
|
- return Promise.reject(error)
|
|
|
|
- }
|
|
|
|
-)
|
|
|
|
|
|
+ return config;
|
|
|
|
+ },
|
|
|
|
+ (error) => {
|
|
|
|
+ return Promise.reject(error);
|
|
|
|
+ }
|
|
|
|
+);
|
|
|
|
|
|
// 响应拦截器
|
|
// 响应拦截器
|
|
request.interceptors.response.use(
|
|
request.interceptors.response.use(
|
|
- (response) => {
|
|
|
|
- if (response.status !== 200) {
|
|
|
|
- return Promise.reject(new Error(response.statusText || 'Error'));
|
|
|
|
- }
|
|
|
|
- // console.log('输出结果', response)
|
|
|
|
|
|
+ (response) => {
|
|
|
|
+ if (response.status !== 200) {
|
|
|
|
+ return Promise.reject(new Error(response.statusText || "Error"));
|
|
|
|
+ }
|
|
|
|
+ // console.log('输出结果', response)
|
|
|
|
|
|
- const res = response.data;
|
|
|
|
- // 响应成功
|
|
|
|
- if (res.success == true) {
|
|
|
|
- return res;
|
|
|
|
- }
|
|
|
|
- // 错误提示
|
|
|
|
- ElMessage.error(res.message);
|
|
|
|
|
|
+ const res = response.data;
|
|
|
|
+ // 响应成功
|
|
|
|
+ if (res.success == true) {
|
|
|
|
+ return res;
|
|
|
|
+ }
|
|
|
|
+ // 错误提示
|
|
|
|
+ ElMessage.error(res.message);
|
|
|
|
|
|
- // 没有权限,如:未登录、登录过期等,需要跳转到登录页
|
|
|
|
- // if (res.code === 401) {
|
|
|
|
- // store.userStore?.setToken('')
|
|
|
|
- // location.reload();
|
|
|
|
- // }
|
|
|
|
|
|
+ // 没有权限,如:未登录、登录过期等,需要跳转到登录页
|
|
|
|
+ // if (res.code === 401) {
|
|
|
|
+ // store.userStore?.setToken('')
|
|
|
|
+ // location.reload();
|
|
|
|
+ // }
|
|
|
|
|
|
- return Promise.reject(new Error(res.msg || 'Error'));
|
|
|
|
- },
|
|
|
|
- async (error) => {
|
|
|
|
- NProgress.done();
|
|
|
|
|
|
+ return Promise.reject(new Error(res.msg || "Error"));
|
|
|
|
+ },
|
|
|
|
+ async (error) => {
|
|
|
|
+ NProgress.done();
|
|
|
|
|
|
- if (error.response) {
|
|
|
|
- const {
|
|
|
|
- status,
|
|
|
|
- config
|
|
|
|
- } = error.response;
|
|
|
|
- }
|
|
|
|
|
|
+ if (error.response) {
|
|
|
|
+ const { status, config } = error.response;
|
|
|
|
+ }
|
|
|
|
|
|
- return Promise.reject({
|
|
|
|
- message: error.message
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ return Promise.reject({
|
|
|
|
+ message: error.message
|
|
|
|
+ });
|
|
|
|
+ }
|
|
);
|
|
);
|
|
|
|
|
|
// 导出 axios 实例
|
|
// 导出 axios 实例
|
|
-export default request
|
|
|
|
|
|
+export default request;
|