| | |
| | | import store from '@/store/' |
| | | import router from '@/router/' |
| | | import { serialize } from '@/utils/util' |
| | | import { getToken, removeToken, removeRefreshToken } from '@/utils/auth' |
| | | import { getToken, removeRefreshToken, removeToken } from '@/utils/auth' |
| | | import { isURL, validatenull } from '@/utils/validate' |
| | | import { ElMessage } from 'element-plus' |
| | | import website from '@/config/website' |
| | | import NProgress from 'nprogress' // progress bar |
| | | import 'nprogress/nprogress.css' // progress bar style |
| | | import { Base64 } from 'js-base64' |
| | | import crypto from '@/utils/crypto' |
| | | import { getStore } from '@/utils/store' |
| | | import { showToast } from 'vant' |
| | | import { baseUrl, baseUrlJals } from '@/config/env' |
| | | |
| | | let baseUrl = import.meta.env.VITE_APP_API |
| | | let { VITE_APP_ENV,VITE_APP_XT_URL,VITE_APP_API } = import.meta.env |
| | | // 全局未授权错误提示状态,只提示一次 |
| | | let isErrorShown = false |
| | | // 全局锁机制相关变量 |
| | | window.isRefreshing = false // 标记当前是否正在刷新token |
| | | let refreshTokenPromise = null // 刷新token的Promise,避免重复请求 |
| | | |
| | | axios.defaults.timeout = 60000 |
| | | //返回其他状态码 |
| | | axios.defaults.validateStatus = function (status) { |
| | | return status >= 200 && status <= 500 // 默认的 |
| | | } |
| | | //跨域请求,允许保存cookie |
| | | axios.defaults.withCredentials = true |
| | | // NProgress Configuration |
| | | // 创建作用域的 axios 实例 |
| | | const service = axios.create({ |
| | | // 超时时间设置为10分钟,部分接口上传比较慢,如固件上传 |
| | | timeout: 600000, |
| | | //返回其他状态码 |
| | | validateStatus: function (status) { |
| | | return status >= 200 && status <= 500 // 默认的 |
| | | }, |
| | | //跨域请求,允许保存cookie |
| | | withCredentials: true, |
| | | }) |
| | | NProgress.configure({ |
| | | showSpinner: false, |
| | | }) |
| | | |
| | | //http request拦截 |
| | | axios.interceptors.request.use( |
| | | service.interceptors.request.use( |
| | | config => { |
| | | const isBase = config.isBase === false |
| | | // start progress bar |
| | | // NProgress.start() |
| | | // 初始化错误提示状态 |
| | | isErrorShown = false |
| | | // if (VITE_APP_ENV === 'development') { |
| | | // config.url = VITE_APP_API + config.url |
| | | // } else { |
| | | // config.url = VITE_APP_XT_URL + config.url |
| | | // } |
| | | |
| | | if (config.isUseJals) { |
| | | if (!isURL(config.url) && !config.url.startsWith(baseUrlJals)) { |
| | | config.url = baseUrlJals + config.url |
| | | } |
| | | } |
| | | |
| | | //地址为已经配置状态则不添加前缀 |
| | | if (!isBase && !isURL(config.url) && !config.url.startsWith(baseUrl)) { |
| | | if (!isURL(config.url) && !config.url.startsWith(baseUrl) && !config.isUseJals) { |
| | | config.url = baseUrl + config.url |
| | | } |
| | | |
| | |
| | | //安全请求header |
| | | config.headers['Blade-Requested-With'] = 'BladeHttpRequest' |
| | | //headers判断是否需要 |
| | | const authorization = config.authorization === false |
| | | if (!authorization) { |
| | | config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}` |
| | | } |
| | | // const authorization = config.authorization === false |
| | | // if (!authorization) { |
| | | // config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}` |
| | | // } |
| | | |
| | | config.headers['Authorization'] = store.state.user.token |
| | | // 根据后端要求,post的data为空的话传{} |
| | | if (config.method === 'post') { |
| | | config.data = config.data || {} |
| | |
| | | ) |
| | | |
| | | //http response拦截 |
| | | axios.interceptors.response.use( |
| | | service.interceptors.response.use( |
| | | res => { |
| | | // NProgress.done() |
| | | //获取配置信息 |
| | |
| | | return Promise.reject(new Error(error)) |
| | | } |
| | | ) |
| | | window._request = axios |
| | | export default axios |
| | | window._request = service |
| | | export default service |