| | |
| | | |
| | | import axios from 'axios' |
| | | import { uuidv4 } from '/@/utils/uuid' |
| | | import { CURRENT_CONFIG } from './config' |
| | |
| | | function getAuthToken () { |
| | | return localStorage.getItem(ELocalStorageKey.Token) |
| | | } |
| | | |
| | | const instance = axios.create({ |
| | | // withCredentials: true, |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | }, |
| | | baseURL: import.meta.env.VITE_BASE_API, |
| | | // timeout: 12000, |
| | | }) |
| | | |
| | | instance.interceptors.request.use( |
| | | config => { |
| | | config.headers[ELocalStorageKey.Token] = getAuthToken() |
| | | config.headers[ELocalStorageKey.Token] = localStorage.getItem(ELocalStorageKey.Token) |
| | | // config.headers[REQUEST_ID] = uuidv4() |
| | | // config.baseURL = CURRENT_CONFIG.baseURL |
| | | return config |
| | |
| | | |
| | | instance.interceptors.response.use( |
| | | response => { |
| | | |
| | | |
| | | // console.info('URL: ' + response.config.baseURL + response.config.url, '\nData: ', response.data, '\nResponse:', response) |
| | | |
| | | //处理消息返回 |
| | | // if (response.data.code && !response.data.success) { |
| | | const whiteList = ['/manage/api/v1/live/streams/stop'] |
| | | if (whiteList.includes(response.config.url)) { |
| | | return Promise.reject(response.data) |
| | | } |
| | | // 处理消息返回 |
| | | // if (response.data.code && !response.data.success) { |
| | | // message.error(response.data.message) |
| | | // } |
| | | if (response.data.code != 5000) { |
| | | message.error(response.data.message) |
| | | } |
| | | if (response.data.code && response.data.code !== 0) { |
| | | message.error(response.data.message) |
| | | } |
| | | return response |
| | | }, |
| | | err => { |
| | | message.error(`ERROR_INFO: ${err}`) |
| | | // const requestId = err?.config?.headers && err?.config?.headers[REQUEST_ID] |
| | | // if (requestId) { |
| | | // console.info(REQUEST_ID, ':', requestId) |
| | | // } |
| | | // console.info('url: ', err?.config?.url, `【${err?.config?.method}】 \n>>>> err: `, err) |
| | | const requestId = err?.config?.headers && err?.config?.headers[REQUEST_ID] |
| | | if (requestId) { |
| | | console.info(REQUEST_ID, ':', requestId) |
| | | } |
| | | console.info('url: ', err?.config?.url, `【${err?.config?.method}】 \n>>>> err: `, err) |
| | | |
| | | // let description = '-' |
| | | // if (err.response?.data && err.response.data.message) { |
| | | // description = err.response.data.message |
| | | let description = '-' |
| | | if (err.response?.data && err.response.data.message) { |
| | | description = err.response.data.message |
| | | } |
| | | if (err.response?.data && err.response.data.result) { |
| | | description = err.response.data.result.message |
| | | } |
| | | // @See: https://github.com/axios/axios/issues/383 |
| | | if (!err.response || !err.response.status) { |
| | | message.error('网络异常,请检查后端服务后重试') |
| | | return |
| | | } |
| | | if (err.response?.status !== 200) { |
| | | message.error(`错误码: ${err.response?.status}`) |
| | | } |
| | | // if (err.response?.status === 403) { |
| | | // // window.location.href = '/' |
| | | // } |
| | | // if (err.response?.data && err.response.data.result) { |
| | | // description = err.response.data.result.message |
| | | // } |
| | | // // @See: https://github.com/axios/axios/issues/383 |
| | | // if (!err.response || !err.response.status) { |
| | | // message.error('The network is abnormal, please check the backend service and try again') |
| | | // return |
| | | // } |
| | | // if (err.response?.status !== 200) { |
| | | // message.error(`ERROR_CODE: ${err.response?.status}`) |
| | | // } |
| | | // // if (err.response?.status === 403) { |
| | | // // // window.location.href = '/' |
| | | // // } |
| | | // if (err.response?.status === 401) { |
| | | // console.error(err.response) |
| | | // const flag: number = Number(localStorage.getItem(ELocalStorageKey.Flag)) |
| | | // switch (flag) { |
| | | // case EUserType.Web: |
| | | // router.push(ERouterName.LOGIN) |
| | | // break |
| | | // case EUserType.Pilot: |
| | | // router.push(ERouterName.PILOT) |
| | | // break |
| | | // } |
| | | // } |
| | | if (err.response?.status === 401) { |
| | | console.error(err.response) |
| | | const flag: number = Number(localStorage.getItem(ELocalStorageKey.Flag)) |
| | | switch (flag) { |
| | | case EUserType.Web: |
| | | router.push(ERouterName.PROJECT_LIST) |
| | | break |
| | | case EUserType.Pilot: |
| | | router.push(ERouterName.PILOT) |
| | | break |
| | | } |
| | | } |
| | | return Promise.reject(err) |
| | | }, |
| | | ) |