初始化
This commit is contained in:
172
src/utils/axios.js
Normal file
172
src/utils/axios.js
Normal file
@@ -0,0 +1,172 @@
|
||||
/**
|
||||
* axios请求封装
|
||||
* https://rudon.blog.csdn.net/
|
||||
*/
|
||||
import axios from 'axios'
|
||||
import { getToken, getUser } from '@/utils/storage'
|
||||
import { useRouter } from 'vue-router';
|
||||
import { ElMessage } from 'element-plus';
|
||||
|
||||
const router = useRouter();
|
||||
// axios.defaults.withCredentials = true;
|
||||
// 请求地址前缀
|
||||
let baseURL = ''
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
// 生产环境
|
||||
// baseURL = "http://120.26.251.180:8085/"
|
||||
baseURL = "http://120.26.251.180:15330"
|
||||
} else {
|
||||
// 开发环境
|
||||
baseURL = "http://120.26.251.180:8085/"
|
||||
}
|
||||
|
||||
// 请求拦截器
|
||||
axios.interceptors.request.use((config) => {
|
||||
// if (getToken()) {
|
||||
// config.headers['token'] = getToken();
|
||||
// }
|
||||
|
||||
// 请求超时时间 - 毫秒
|
||||
config.timeout = 60000
|
||||
config.baseURL = baseURL
|
||||
// 自定义Content-type
|
||||
config.headers['Content-type'] = 'application/json'
|
||||
return config;
|
||||
}, (error) => {
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
// 响应拦截器
|
||||
axios.interceptors.response.use((response) => {
|
||||
return response
|
||||
}, (error) => {
|
||||
// 可添加请求失败后的处理逻辑
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
|
||||
|
||||
// axios的get请求
|
||||
export function getAxios({ url, params }) {
|
||||
// 使用axios发送GET请求
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.get(url, {
|
||||
params
|
||||
// 请求成功,将返回的数据传递给resolve函数
|
||||
}).then(res => {
|
||||
resolve(res.data)
|
||||
// 请求失败,将错误信息传递给reject函数
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// axios的post请求
|
||||
export function postAxios({ url, data }) {
|
||||
// if (url != 'api/account/login') {
|
||||
|
||||
// throttledCheekalive();
|
||||
|
||||
// }
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.post(
|
||||
url,
|
||||
data,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
}
|
||||
).then(res => {
|
||||
resolve(res.data)
|
||||
}).catch(err => {
|
||||
if (err.message == "Network Error") {
|
||||
// alert("网络错误,请检查网络连接")
|
||||
// ElMessage.error('网络连接错误');
|
||||
reject('网络连接错误')
|
||||
|
||||
} else {
|
||||
ElMessage.error(err.message);
|
||||
reject(err.message)
|
||||
|
||||
}
|
||||
// console.log(err)
|
||||
// reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
export const downFile = async (urlstr, data) => {
|
||||
|
||||
// 发送请求,获取文件流
|
||||
const response = await axios.post(urlstr, data, { responseType: 'blob' });
|
||||
|
||||
// 获取文件名(如果后端设置了 Content-Disposition)
|
||||
const contentDisposition = response.headers['content-disposition'];
|
||||
let fileName = 'default-file-name'; // 默认文件名
|
||||
console.log(contentDisposition)
|
||||
console.log(response)
|
||||
if (contentDisposition) {
|
||||
// 从响应头中提取文件名
|
||||
const fileNameMatch = contentDisposition.match(/filename="(.+)"/);
|
||||
if (fileNameMatch && fileNameMatch.length > 1) {
|
||||
fileName = fileNameMatch[1];
|
||||
}
|
||||
}
|
||||
|
||||
// 创建一个临时的下载链接
|
||||
const blob = new Blob([response.data], { type: response.headers['content-type'] });
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = fileName; // 设置下载的文件名
|
||||
a.click();
|
||||
|
||||
// 释放 URL 对象
|
||||
window.URL.revokeObjectURL(url);
|
||||
|
||||
|
||||
}
|
||||
//请求前验证
|
||||
function cheekalive() {
|
||||
axios.post('api/account/cheekalive', {
|
||||
userId: getUser().userId,
|
||||
currcode: getToken(),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
}
|
||||
).then(res => {
|
||||
console.log(res.data)
|
||||
if (res.data) {
|
||||
|
||||
} else {
|
||||
alert("账号在其他地方登录!")
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
//节流函数
|
||||
function throttle(func, limit) {
|
||||
let inThrottle;
|
||||
return function () {
|
||||
const args = arguments;
|
||||
const context = this;
|
||||
if (!inThrottle) {
|
||||
func.apply(context, args);
|
||||
inThrottle = true;
|
||||
setTimeout(() => inThrottle = false, limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const throttledCheekalive = throttle(cheekalive, 5000);
|
||||
|
||||
export default axios
|
||||
Reference in New Issue
Block a user