|
|
import axios from "axios";
|
|
|
import { useRouter } from "vue-router"
|
|
|
import Cookies from "js-cookie";
|
|
|
import qs from "qs";
|
|
|
import {
|
|
|
Modal,
|
|
|
message
|
|
|
} from 'ant-design-vue';
|
|
|
import commonJS from './common.js';
|
|
|
const router = useRouter()
|
|
|
// const baseURL = "https://admin.ibocai.com.cn/api_prod"; // 正式环境线上
|
|
|
const baseURL = "http://192.168.3.83:8003"; // 正式环境线上
|
|
|
// const baseURL2 = "https://a.matripe.com.cn/api_stock"; // 正式环境线上
|
|
|
let protocol = location.protocol
|
|
|
let trueUrl = protocol == 'http:' ? baseURL : baseURL2
|
|
|
console.log(protocol);
|
|
|
axios.defaults.withCredentials = true; //添加这行代码
|
|
|
const service = axios.create({
|
|
|
baseURL: trueUrl, // baseURL: "/api", // url = base url + request url 正式
|
|
|
withCredentials: true, // send cookies when cross-domain requests
|
|
|
timeout: 60000, // request timeout
|
|
|
crossDomain: true,
|
|
|
});
|
|
|
const getUrlParams = () => {
|
|
|
let that = this,
|
|
|
$str = window.location.href,
|
|
|
_list = $str.split('?'),
|
|
|
arr = _list[1],
|
|
|
newArr = [],
|
|
|
_arrs = [],
|
|
|
_params = {};
|
|
|
if (arr == undefined) {
|
|
|
|
|
|
} else {
|
|
|
newArr = arr.split('&');
|
|
|
newArr.forEach((item, index) => {
|
|
|
_arrs.push({
|
|
|
$key: item.split('=')[0],
|
|
|
$value: item.split('=')[1]
|
|
|
});
|
|
|
});
|
|
|
_arrs.forEach((item, index) => {
|
|
|
_params[item.$key] = item.$value;
|
|
|
})
|
|
|
}
|
|
|
return _params;
|
|
|
};
|
|
|
service.interceptors.request.use(
|
|
|
(req) => {
|
|
|
/* 新版:不同项目通过url获取token */
|
|
|
if (import.meta.env.MODE == "development") {
|
|
|
// 测试环境,通过URL获取token
|
|
|
if (getUrlParams().token) {
|
|
|
console.log('getUrlParams().userInfo', getUrlParams().userInfo);
|
|
|
// console.log(decodeURIComponent(getUrlParams().userInfo));
|
|
|
localStorage.setItem('DAOTIAN_token', getUrlParams().token);
|
|
|
localStorage.setItem('DAOTIAN_userinfo_name', getUrlParams().username)
|
|
|
localStorage.setItem('DAOTIAN_userinfo_avatar', getUrlParams().avatar)
|
|
|
localStorage.setItem('DAOTIAN_userinfo_agencyid', getUrlParams().agencyid)
|
|
|
localStorage.setItem('DAOTIAN_userinfo', decodeURIComponent(getUrlParams().userInfo))
|
|
|
localStorage.setItem('DAOTIAN_userinfo_roleid', getUrlParams().roleid.split("#/")[0])
|
|
|
}
|
|
|
} else {
|
|
|
// 正式环境,通过缓存获取
|
|
|
if (localStorage.getItem("daotian_to_admin_token")) {
|
|
|
localStorage.setItem('DAOTIAN_token', localStorage.getItem("daotian_to_admin_token"));
|
|
|
localStorage.setItem('DAOTIAN_userinfo', localStorage.getItem("LOGIN_DATA"));
|
|
|
localStorage.setItem('DAOTIAN_userinfo_name', localStorage.getItem("daotian_to_admin_username"))
|
|
|
localStorage.setItem('DAOTIAN_userinfo_avatar', localStorage.getItem("daotian_to_admin_avatar"))
|
|
|
localStorage.setItem('DAOTIAN_userinfo_roleid', localStorage.getItem("daotian_to_admin_roleid"))
|
|
|
localStorage.setItem('DAOTIAN_userinfo_agencyid', localStorage.getItem("daotian_to_admin_agencyid"))
|
|
|
}
|
|
|
}
|
|
|
req.headers.token = localStorage.getItem("DAOTIAN_token");
|
|
|
req.headers.Authorization = "Bearer " + localStorage.getItem("DAOTIAN_token");
|
|
|
return req;
|
|
|
},
|
|
|
(err) => {
|
|
|
console.log(err);
|
|
|
}
|
|
|
);
|
|
|
service.interceptors.response.use(
|
|
|
(response) => {
|
|
|
console.log(response);
|
|
|
let { msg, code } = response.data;
|
|
|
if (code == 9999) {
|
|
|
localStorage.removeItem("LOGIN_DATA");
|
|
|
localStorage.removeItem("DAOTIAN_token")
|
|
|
commonJS.goWiondowOpenPage('login')
|
|
|
// 跳转到登录页面
|
|
|
return Promise.reject(new Error("token过期"));
|
|
|
}
|
|
|
if (response.status === 200 || response.status === 201) {
|
|
|
return response.data;
|
|
|
} else {
|
|
|
return Promise.reject(new Error(msg));
|
|
|
}
|
|
|
},
|
|
|
(error) => {
|
|
|
console.log(error);
|
|
|
}
|
|
|
);
|
|
|
service.userCustom = trueUrl
|
|
|
export default service;
|