| | |
| | | import axios from 'axios' |
| | | import store from '@/store/' |
| | | import { serialize } from '@/utils/util' |
| | | import { getToken, removeToken, removeRefreshToken } from '@/utils/auth' |
| | | import { isURL, validatenull } from '@/utils/validate' |
| | | import { ElMessage } from 'element-plus' |
| | | import website from '@/config/website' |
| | | import { Base64 } from 'js-base64' |
| | | import { baseUrl } from '@/config/env' |
| | | import crypto from '@/utils/crypto' |
| | | // 全局未授权错误提示状态,只提示一次 |
| | | const { VITE_APP_API_XT } = import.meta.env |
| | | |
| | | // 创建作用域的 axios 实例 |
| | | const serviceXT = axios.create({ |
| | | // 超时时间设置为10分钟,部分接口上传比较慢,如固件上传 |
| | | timeout: 600000, |
| | | baseURL: VITE_APP_API_XT, |
| | | //返回其他状态码 |
| | | validateStatus: function (status) { |
| | | return status >= 200 && status <= 500 // 默认的 |
| | | }, |
| | | //跨域请求,允许保存cookie |
| | | withCredentials: true, |
| | | withCredentials: false, |
| | | }) |
| | | |
| | | //http request拦截 |
| | | serviceXT.interceptors.request.use( |
| | | config => { |
| | | //headers判断是否需要 |
| | | const authorization = config.authorization === false |
| | | if (!authorization) { |
| | | config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}` |
| | | const xtToken = store.state.user.xtToken |
| | | if (xtToken && !authorization) { |
| | | config.headers['Authorization'] = xtToken |
| | | } |
| | | const meta = config.meta || {} |
| | | |
| | |
| | | //http response拦截 |
| | | serviceXT.interceptors.response.use( |
| | | res => { |
| | | const status = res.data.error_code || res.data.code || res.status |
| | | const statusWhiteList = website.statusWhiteList || [] |
| | | const status = res?.data?.code || res?.data?.error_code || res?.status |
| | | const message = res?.data?.msg || res?.data?.message || '系统错误' |
| | | //如果在白名单里则自行catch逻辑处理 |
| | | if (statusWhiteList.includes(status)) return Promise.reject(res) |
| | | // 如果请求为非200默认统一处理 |
| | | if (status !== 200 && status !== 0) { |
| | | ElMessage({ |
| | | message: message, |
| | | type: 'error', |
| | | }) |
| | | ElMessage({ message: message, type: 'error', }) |
| | | return Promise.reject(new Error(message)) |
| | | } |
| | | return res |