You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.9 KiB

4 years ago
/*
* @Author: your name
* @Date: 2021-10-13 09:28:02
4 years ago
* @LastEditTime: 2021-12-23 16:21:07
4 years ago
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /data-show/src/request/index.js
*/
import axios from 'axios';
import qs from 'qs';
import router from "@/permission"
4 years ago
import { message} from 'ant-design-vue'
4 years ago
function filterRequestData(obj) {
let o = {};
for(let key in obj) {
4 years ago
let b1 = obj[key] === 0;
4 years ago
let b2 = obj[key] === false;
if(obj[key] || b1 || b2) {
o[key] = obj[key]
}
}
return o;
}
3 years ago
axios.defaults.withCredentials = true;
4 years ago
//创建axios的实例
const httpService = axios.create({
4 years ago
baseURL: 'http://cloud.sws010.com',// TODO:具体的配置可以根据项目情况而来
3 years ago
// withCredentials: true
3 years ago
timeout: 50000
4 years ago
})
//axios的拦截--request
httpService.interceptors.request.use(config => {
// 请求成功处理
// if(localStorage.getItem('token')){//判断浏览器中的cookie中是否存在项目的token
// config.headers.token = localStorage.getItem('token')
3 years ago
// }\
3 years ago
// config.withCredentials = true;
4 years ago
const rqParams = filterRequestData(config.params);
4 years ago
const rqData = filterRequestData(config.data);
4 years ago
// post 'Content-Type' === 'application/x-www-form-urlencoded'
config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
if(config.method === 'post') {
4 years ago
config.data = qs.stringify(rqData);
4 years ago
} else {
4 years ago
config.data = rqData;
4 years ago
config.params = rqParams;
4 years ago
}
return config;
},err => {
Promise.reject(err);// 请求错误处理
})
//4、axios的拦截--response
httpService.interceptors.response.use(response => {
// TODO:具体的code对应的处理可继续添加修改
let data = null;
4 years ago
let data1 = null;
4 years ago
let msg = '';
4 years ago
let res = response.data;
4 years ago
let totalNum = 0;
4 years ago
if(res.Code == 1){
4 years ago
data = res.Data || [];
data1 = res.Data1 || [];
msg = res.Msg || "";
totalNum = res.totalNum || 0;
return {data,data1, msg, totalNum};
4 years ago
} else {
4 years ago
msg = res.Msg || "";
message.error(msg);
return Promise.reject(new Error(msg))
4 years ago
}
},err => {
// TODO:具体的code对应的处理可继续添加修改
if(err.response.Code === 301){
4 years ago
//登录过期跳转登录页面并将要浏览的页面fullPath传过去登录成功后跳转需要访问的页面 ---主页(index) 或者 退出到登录前的浏览的页面
4 years ago
//这样登录页面登录接口成功之后可以进行跳转 主页(index) 或者 退出到登录前的页面: let path = this.$route.query.redirect || '/index.js'; this.$router.push(path);
setTimeout(() => {
router.replace({
path: '/login',
query: {
redirect: router.currentRoute.fullPath
}
});
}, 1000);
}
return Promise.reject(err);
})
4 years ago
export default httpService
4 years ago