123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import axios, { AxiosRequestConfig } from 'axios';
- import qs from 'querystring';
- import { ElMessage } from 'element-plus';
- import useMainStore from '@/store/useMainStore';
- import { LOGIN_URL,BASE_URL} from "./constant"
- import { indexOf } from 'lodash';
- const baseURL =
- process.env.NODE_ENV === 'prod' || process.env.NODE_ENV === 'test'
- ? (import.meta.env.VITE_API_SERVER as string) + '/YJZH-SQ/api'
- : '/api';
- const CancelToken = axios.CancelToken;
- const source = CancelToken.source();
- axios.interceptors.request.use((config) => {
- const main = useMainStore();
- config.cancelToken = new axios.CancelToken((cancel) => {
- main.pushReqToken(cancel);
- });
- return config;
- });
- axios.interceptors.response.use(
- (res) => {
-
- if (res.status >= 200 && res.status < 300) {
- if (res.data?.code === 200) {
- return res.data;
- }
- if (res.data?.code === 401) {
- ElMessage.error({
- // message: `401. 没有权限访问该接口: ${res.config.url}`,
- message: `401. 没有权限访问该接口`,
- });
- if (res.data.data.indexOf('Jwt expired') != -1) {
- window.localStorage.removeItem('userdata');
- window.location.href = LOGIN_URL;
- }
- throw Error(res.statusText);
- }
- if (res.data?.code === 404) {
- ElMessage.error({ message: '404. 未找到该接口!' });
- throw Error(res.statusText);
- }
- if (res.data?.code >= 500) {
- if (res.data?.message == 'invalid ticket') {
- window.localStorage.removeItem('userdata');
- window.location.href = LOGIN_URL;
- }
- ElMessage.error({
- message: res.data?.msg ?? '系统异常, 请稍后重试!',
- });
- throw Error(res.data.msg);
- }
-
- }
- if (res.status === 401) {
- ElMessage.error({
- message: `401. 没有权限访问该接口: ${res.config.url}`,
- });
-
- throw Error(res.statusText);
- }
- if (res.status === 404) {
- ElMessage.error({ message: '404. 未找到该接口!' });
- throw Error(res.statusText);
- }
- if (res.status >= 500) {
- ElMessage.error({ message: '系统异常, 请稍后重试!' });
- throw Error(res.statusText);
- }
- },
- (error) => {
- // ElMessage.error({ message: '系统异常, 请稍后重试!' });
- // throw Error(error);
- },
- );
- export default function request<T>(
- method: AxiosRequestConfig['method'] = 'GET',
- confifg: AxiosRequestConfig & {},
- ): Promise<T> {
- var tokenkjson = window.localStorage.getItem('userdata');
- tokenkjson = tokenkjson == null ? "{}" : tokenkjson;
- tokenkjson = tokenkjson == undefined ? "{}" : tokenkjson;
- tokenkjson = JSON.parse(tokenkjson);
- return axios.request({
- method,
- baseURL,
- ...confifg,
- headers: {
- Accept: 'application/json',
- AppId: '3bcb760743ea456faba29a1dfb247bf4',
- Authorization: 'Bearer ' + (tokenkjson["accessToken"]),
- 'Content-Type': 'application/json',
- ...confifg.headers,
- },
- });
- }
|