123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- "use strict";
- import axios from "axios";
- import router from "@/router";
- import CryptoJS from '@/utils/crypto'
- import { ElMessage } from 'element-plus'
- // Full config: https://github.com/axios/axios#request-config
- // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
- // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
- // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
- let config = {
- baseURL: import.meta.env.VITE_APP_API_URL,
- timeout: 10*60 * 1000, // Timeout
- // withCredentials: true, // Check cross-site Access-Control
- };
- const _axios = axios.create(config);
- _axios.interceptors.request.use(
- function (config) {
- // Do something before request is sent
- let auth = localStorage.getItem('token')
- if(auth) {
- config.headers.Authorization = auth;
- }
- // config.headers.Authorization=localStorage.getItem('token')||''
-
- // 检查请求路径并设置responseType
- if (config.url.endsWith('/user/potential/export/excel')) {
- config.responseType = 'blob'; // 如果是导出excel的请求,则设置responseType为blob
- }
- return config;
- },
- function (error) {
- // Do something with request error
- return Promise.reject(error);
- }
- );
- // Add a response interceptor
- _axios.interceptors.response.use(
- function (response) {
- // Do something with response data
- let data
- // 如果是是导出excel的请求,则不进行解密操作
- if(import.meta.env.MODE==='production' && !response.config.url.endsWith('/user/potential/export/excel')){
- data=JSON.parse(CryptoJS.Des3Decrypt(response.data,import.meta.env.VITE_APP_RESPONSE_DES_KEY));//解密
- }else{
- data=response.data
- }
-
- if(response.status!==200){
- setTimeout(() => {
- ElMessage.error('网络异常')
- }, 100);
- }
- if(!data){
- setTimeout(() => {
- ElMessage.error('服务器开了个小差')
- }, 100);
- }
- if(data.Ret===408){//token失效
- ElMessage.error(data.Msg)
- router.replace('/login')
- }
- if(data.Ret===403){
- setTimeout(() => {
- ElMessage.error(data.Msg||'网络异常')
- }, 100);
- }
- return data;
- },
- function (error) {
- console.log(error);
- ElMessage.error(error.message)
- // Do something with response error
- return Promise.reject(error);
- }
- );
- /**
- * 导出get请求方法
- * @url 请求地址
- * @params get请求参数
- */
- export const get = (url, params) => {
- return _axios.get(url, { params });
- };
- /**
- * 导出post请求方法
- * @url 请求地址
- * @params post请求参数
- */
- export const post = (url, params) => {
- return _axios.post(url, params);
- };
|