import axios from 'axios' import {Message} from "element-ui"; import store from '@/store' import {getToken} from '@/utils/auth' import Cookies from 'js-cookie'; import router from "@/router"; // axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded' // console.log(document.getElementsByTagName('body')); // var a = document.getElementsByTagName('body')[0].baseURI // // create an axios instance // console.log(a); // http://localhost:9527/#/login?redirect=%2F //https://xterm.kaiyuancloud.cn/#/product/productHome //https://www.kaiyuancloud.cn/#/login?redirect=%2F // let myUrl = window.location.href.split("/#")[0]; let testUrl = '' // window.location.href === "https://www.kaiyuancloud.cn/#/login?redirect=%2F"1 if (window.location.href.includes("dev.opencomputing.cn")) { testUrl = 'https://dev.opencomputing.cn' } else if (window.location.href.includes("https://www.kaiyuancloud.cn")) {//dev testUrl = 'https://www.kaiyuancloud.cn' } else if (window.location.href.includes("localhost")) { testUrl = '/api' } else { testUrl = window.location.href.split('/#/')[0] } // if (window.location.href === "https://www.kaiyuancloud.cn/#/login?redirect=%2F") { // testUrl = 'https://www.kaiyuancloud.cn' // } else if (window.location.href.includes("www.kaiyuancloud.cn/dev")) {//dev // testUrl = 'https://www.kaiyuancloud.cn/dev' // } else if (window.location.href.includes("localhost")) { // testUrl = '/api' // } else { // testUrl = window.location.href.split('/#/')[0] // } console.log("环境的前缀时" + testUrl); // 应该输出 'https://www.kaiyuancloud.cn/dev' const service = axios.create({ // baseURL: 'https://dev.kaiyuancloud.cn', // url = base url + request url timeout: 20000,// request timeout withCredentials: true, //原文件 // baseURL: myUrl === "http://localhost:9527" ? '/api' : myUrl, // url = base url + request url // baseURL: myUrl === "http://localhost:9527" ? '/api' : process.env.VUE_APP_BASE_API, baseURL: testUrl, // // withCredentials: true, // send cookies when cross-domain requests // timeout: 5000 ,// request timeout // headers: { // //关键 // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' // } }) // request interceptor service.interceptors.request.use( (config) => { // console.log(config); // do something before request is sent // if (store.getters.token) { config.headers['client_uuid'] = sessionStorage.getItem('client_uuid'); // } return config }, (error) => { // do something with request error console.log(error) // for debug return Promise.reject(error) } ) // response interceptor service.interceptors.response.use( /** * If you want to get http information such as headers or status * Please return response => response */ (response) => { // console.log(response) // console.log(response.headers) // console.log(response.headers['Set-Cookie']) const res = response.data // console.log("res:", res); // debugger if (response.status == 200 || response.status == 209) { // router.push({path:'/login'}) //这不是测试么.正常不用push 直接return return res } else { Message({ message: res.message || "Error", type: "error", duration: 5 * 1000, }); } // // 如果返回状态代码不是200,则判定为错误。 //response.status判断这个 // if (res.code == 200 || res.code == 209) { // return res; // } else { // Message({ // message: res.message || "Error", // type: "error", // duration: 5 * 1000, // }); // // 50008:非法令牌;50012:已登录的其他客户端;50014:令牌过期; // if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // // to re-login // MessageBox.con0firm( // "You have been logged out, you can cancel to stay on this page, or log in again", // "Confirm logout", // { // confirmButtonText: "Re-Login", // cancelButtonText: "Cancel", // type: "warning", // } // ).then(() => { // store.dispatch("user/resetToken").then(() => { // location.reload(); // }); // }); // } // return Promise.reject(new Error(res.message || "Error")); // } }, (error) => { console.log(error.message) // for debug //在这呢,走到这了 let code = parseInt(error.message.split('code')[1]) // console.log(code); if (code == 401) { alert('请重新登录') router.push({path: '/login'}) // window.location.href = 'https://www.opencomputing.cn/' } else if (code == 403) { // this.$message({ // message:"您没有权限哦", // type: 'error'1 // }) Message({ message: "您没有权限哦", type: 'error', // duration: 5 * 1000 }) } // Message({ // message: error.message, // type: 'error', // duration: 5 * 1000 // }) return Promise.reject(error) } ) export default service