| | |
| | | /* |
| | | * @Author: shuishen 1109946754@qq.com |
| | | * @Date: 2022-07-29 15:19:13 |
| | | * @LastEditors: shuishen 1109946754@qq.com |
| | | * @LastEditTime: 2023-04-21 10:16:58 |
| | | * @FilePath: \web\bigScreen\src\router\axios.js |
| | | * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE |
| | | */ |
| | | /** |
| | | * 全站http配置 |
| | | * |
| | | * axios参数说明 |
| | | * isSerialize是否开启form表单提交 |
| | | * isToken是否需要token |
| | | */ |
| | | import axios from "axios"; |
| | | import store from '@/store/'; |
| | | import router from '@/router/page'; |
| | | import { getToken } from '@/utils/auth'; |
| | | import { MessageBox } from 'element-ui'; |
| | | import website from '@/config/website'; |
| | | import { Base64 } from 'js-base64'; |
| | | import NProgress from 'nprogress'; |
| | | import 'nprogress/nprogress.css'; |
| | | |
| | | // 调用后台管理的 |
| | | const service = axios.create({ |
| | | timeout: 600000, // request timeout |
| | | timeout: 600000, |
| | | }); |
| | | |
| | | // 返回其他状态码 |
| | | service.defaults.validateStatus = function (status) { |
| | | return status >= 200 && status <= 500; |
| | | }; |
| | | |
| | | // 跨域请求,允许保存cookie |
| | | // service.defaults.withCredentials = true |
| | | service.defaults.withCredentials = true; |
| | | |
| | | // http request拦截 |
| | | NProgress.configure({ |
| | | showSpinner: false |
| | | }); |
| | | |
| | | service.interceptors.request.use( |
| | | (config) => { |
| | | if (config.apiKey) { |
| | | config.headers["apikey"] = |
| | | "F1DBECD719108635189480CF60E6553ADB3109616426BD537F25A430DFC613B491A025C4A51E77FD08C6E5B7CBE05917A461286E7B6D69F1AB1B14F946149D2065B0C675F8FEDF4B9B05C1496881BC5A"; |
| | | NProgress.start(); |
| | | const meta = (config.meta || {}); |
| | | const isToken = meta.isToken === false; |
| | | config.headers['Authorization'] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`; |
| | | |
| | | if (getToken() && !isToken) { |
| | | config.headers[website.tokenHeader] = 'bearer ' + getToken() |
| | | } |
| | | |
| | | if (config.text === true) { |
| | | config.headers["Content-Type"] = "text/plain"; |
| | | } |
| | | |
| | | config.cancelToken = new axios.CancelToken((cancel) => { |
| | | window._axiosPromiseArr.push({ cancel }); |
| | | }); |
| | |
| | | } |
| | | ); |
| | | |
| | | // http response 拦截 |
| | | service.interceptors.response.use( |
| | | (res) => { |
| | | const code = res.data.code; |
| | | NProgress.done(); |
| | | const status = res.data.code || res.status; |
| | | const statusWhiteList = website.statusWhiteList || []; |
| | | const message = res.data.msg || res.data.error_description || '未知错误'; |
| | | |
| | | if (statusWhiteList.includes(status)) return Promise.reject(res); |
| | | |
| | | if (status === 401) { |
| | | store.dispatch('FedLogOut').then(() => { |
| | | router.push({ path: '/login' }); |
| | | }); |
| | | } |
| | | |
| | | if (status !== 200 && status !== 201) { |
| | | MessageBox.alert(message); |
| | | return Promise.reject(new Error(message)) |
| | | } |
| | | |
| | | if (res.data.resultList && res.data.resultList.length > 0) { |
| | | res.data.resultList = res.data.resultList.map((item) => { |
| | |
| | | return res; |
| | | }, |
| | | (error) => { |
| | | NProgress.done(); |
| | | return Promise.reject(new Error(error)); |
| | | } |
| | | ); |
| | | |
| | | export default service; |
| | | export default service; |