| | |
| | | import axios from 'axios' |
| | | import { ElNotification , ElMessageBox, ElMessage, ElLoading } from 'element-plus' |
| | | import { getToken } from '@/utils/auth' |
| | | import {getToken, removeToken} from '@/utils/auth' |
| | | import errorCode from '@/utils/errorCode' |
| | | import { tansParams, blobValidate } from '@/utils/ruoyi' |
| | | import cache from '@/plugins/cache' |
| | | import { saveAs } from 'file-saver' |
| | | import useUserStore from '@/store/modules/user' |
| | | |
| | | import router from '../router' |
| | | let downloadLoadingInstance; |
| | | // 是否显示重新登录 |
| | | export let isRelogin = { show: false }; |
| | |
| | | // axios中请求配置有baseURL选项,表示请求URL公共部分 |
| | | baseURL: import.meta.env.VITE_APP_BASE_API, |
| | | // 超时 |
| | | timeout: 10000 |
| | | timeout: 60000 |
| | | }) |
| | | |
| | | // request拦截器 |
| | |
| | | // 是否需要防止数据重复提交 |
| | | const isRepeatSubmit = (config.headers || {}).repeatSubmit === false |
| | | if (getToken() && !isToken) { |
| | | config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 |
| | | config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 |
| | | } |
| | | // get请求映射params参数 |
| | | if (config.method === 'get' && config.params) { |
| | |
| | | config.params = {}; |
| | | config.url = url; |
| | | } |
| | | if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) { |
| | | if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put' || config.method === 'get')) { |
| | | const requestObj = { |
| | | url: config.url, |
| | | data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data, |
| | |
| | | const s_url = sessionObj.url; // 请求地址 |
| | | const s_data = sessionObj.data; // 请求数据 |
| | | const s_time = sessionObj.time; // 请求时间 |
| | | const interval = 1000; // 间隔时间(ms),小于此时间视为重复提交 |
| | | if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) { |
| | | const interval = 0; // 间隔时间(ms),小于此时间视为重复提交 |
| | | if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url && s_url!=='/system/common/uploadFile') { |
| | | const message = '数据正在处理,请勿重复提交'; |
| | | console.warn(`[${s_url}]: ` + message) |
| | | return Promise.reject(new Error(message)) |
| | |
| | | // 未设置状态码则默认成功状态 |
| | | const code = res.data.code || 200; |
| | | // 获取错误信息 |
| | | const msg = errorCode[code] || res.data.msg || errorCode['default'] |
| | | const msg = errorCode[code] || res.data.message || errorCode['default'] |
| | | // 二进制数据则直接返回 |
| | | if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') { |
| | | return res.data |
| | |
| | | isRelogin.show = true; |
| | | ElMessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => { |
| | | isRelogin.show = false; |
| | | useUserStore().logOut().then(() => { |
| | | location.href = '/index'; |
| | | }) |
| | | removeToken() |
| | | location.href = '/homePage'; |
| | | // useUserStore().logOut().then(() => { |
| | | // // router.push('/homePage') |
| | | // |
| | | // location.href = '/homePage'; |
| | | // }) |
| | | }).catch(() => { |
| | | isRelogin.show = false |
| | | }); |
| | |
| | | } |
| | | return Promise.reject('无效的会话,或者会话已过期,请重新登录。') |
| | | } else if (code === 500) { |
| | | ElMessage({ message: msg, type: 'error' }) |
| | | if(msg === '文件不存在'){ |
| | | |
| | | }else { |
| | | ElMessage({ message: msg, type: 'error' }) |
| | | if(msg === 'Token已失效,请重新登录!' || msg === 'token非法无效!'){ |
| | | ElMessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => { |
| | | isRelogin.show = false; |
| | | removeToken() |
| | | location.href = '/homePage'; |
| | | }).catch(() => { |
| | | }); |
| | | } |
| | | } |
| | | |
| | | |
| | | return Promise.reject(new Error(msg)) |
| | | } else if (code === 601) { |
| | | ElMessage({ message: msg, type: 'warning' }) |