123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import { MessagePlugin } from 'tdesign-vue-next';
- import axios from 'axios';
- import { getCookie } from '@/services/cookie';
- import router from './router';
- import i18n from './i18n';
- const $t = i18n.global.t;
- import { axiosRequest, axiosResponse} from '@le5le/auth-token';
- // axios 配置
- axios.defaults.timeout = 60000;
- axios.defaults.withCredentials = false;
- axios.defaults.baseURL = `${import.meta.env.VITE_BASE_API}/api-dashboard`
- // axiosRequest(axios);
- // http request 拦截器
- // axios.interceptors.request.use(
- // (config: any) => {
- // config.headers.Authorization =
- // 'Bearer ' + ( getCookie('token') || new URLSearchParams(location.search).get('token') || '');
- // return config;
- // },
- // (err: any) => Promise.reject(err)
- // );
- // http response 拦截器
- // axiosResponse(axios);
- // axios.interceptors.response.use(
- // (response: any) => {
- // if (response && response.data && response.data.error) {
- // MessagePlugin.error(response.data.error);
- // return;
- // }
- // if (response) {
- // if (response.status == undefined) {
- // return response;
- // }
- // return response.data;
- // }
- // return;
- // },
- // (error: any) => {
- // if (error && !error.response) {
- // return;
- // }
- // if (error && error.response) {
- // if (
- // error.response.config.url === '/api/account/profile' ||
- // error.response.data.error === $t('此为付费数据,请购买后访问')
- // ) {
- // return;
- // }
- // if (
- // error.response.status !== 401 &&
- // error.response &&
- // error.response.data &&
- // error.response.data.error
- // ) {
- // MessagePlugin.error(error.response.data.error);
- // return;
- // }
- // switch (error.response.status) {
- // case 401:
- // if (error.response.config.url==='/api/account/statistics') {
- // break;
- // }
- // sessionStorage.setItem('cb', encodeURIComponent(location.href));
- // MessagePlugin.error($t('请先登录!'));
- // // router.replace({ path: '/login' });
- // break;
- // case 403:
- // MessagePlugin.error($t('请求错误,不合法的请求!'));
- // break;
- // case 404:
- // if (error.response.config.url.indexOf('/data/') !== 0) {
- // MessagePlugin.error($t('访问数据不存在,请检查后重试!'));
- // }
- // break;
- // case 500:
- // MessagePlugin.error($t('请求服务错误,请稍后重试!'));
- // break;
- // case 504:
- // MessagePlugin.error($t('网络超时,请检测你的网络!'));
- // break;
- // default:
- // MessagePlugin.error($t('未知网络错误!'));
- // break;
- // }
- // }
- // // return error.response ? error.response.data : error;
- // }
- // );
- axios.interceptors.request.use(
- (config: any) => {
- config.headers.Authorization = 'Bearer ' + import.meta.env.VITE_TEMP_TOKEN;
- return config;
- },
- (err: any) => Promise.reject(err)
- );
- axios.interceptors.response.use(
- (response: any) => {
- if (!response || !response.data) {
- console.error('Empty Response')
- return;
- }
- if (!response.headers['content-type'].startsWith('application/json')) {
- return response.data
- }
- const { data, code, msg } = response.data;
- if (code !== 0) {
- MessagePlugin.error(msg);
- return;
- }
- if (data === null || data === undefined) {
- MessagePlugin.error($t("请求数据为空"));
- return;
- }
- return data;
- },
- (error: any) => {
- if (!error?.response) {
- return;
- }
- const { status, data } = error.response
- if (status === 401) {
- MessagePlugin.error($t('请先登录!'));
- } else {
- const errMsg: string = data.msg || data.error || '';
- MessagePlugin.error(`HTTP Error: ${status} ${errMsg}`);
- }
- }
- );
- export default axios;
|