import axios from "axios"; import qs from "qs"; import { Message, Loading } from 'element-ui' import {judgmentNetworkReturnAddress } from "@/utils/public"; let loadingInstance = {}; let options = { spinner:"", background: 'rgba(255, 255, 255, 0.1)' }; let loadingCount = 0; /* */ //判定http或者https let urlText = window.location.href.split('://')[0]+'://'; // axios中请求配置有baseURL选项,表示请求URL公共部分 axios.defaults.baseURL = urlText+judgmentNetworkReturnAddress(); //获取后台浏览器跳转过来的地址携带的token let afterUrl = window.location.search.substring(1); let identity = afterUrl.substr(9, afterUrl.length); if(identity.length>0){ //Authorization localStorage.setItem('Authorization',identity) localStorage.setItem('deptLevel','2')////这个是登陆后,刘波给你的院校的类型 } //post请求头 axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"; // 表示跨域请求时是否需要使用凭证 允许跨域携带cookie信息 axios.defaults.withCredentials = false; // 允许跨域 axios.defaults.headers.post["Access-Control-Allow-Origin-Type"] = "*"; //axios.defaults.headers.common["Authorization"] = 'Bearer 1c03c353-160c-42ea-bb38-88c7a831de48';//`Bearer ${localStorage.getItem('access_token')`; axios.defaults.headers.common["Authorization"] = 'Bearer '+localStorage.getItem('Authorization')+'' //设置超时 axios.defaults.timeout = 15000; axios.interceptors.request.use( config => { loadingInstance = Loading.service(options) loadingCount ++; return config; }, error => { loadingCount --; if(loadingCount===0){ loadingInstance.close(); } return Promise.reject(error); } ); axios.interceptors.response.use( response => { loadingCount --; if(loadingCount===0) { loadingInstance.close(); } if (response.status == 200) { if(response.data.code == 200){ return Promise.resolve(response); }else{ // Message({ // message: response.data.msg, // type: 'error', // duration: 5 * 1000, // offset:100 // }) } } else { Message({ message: response.data.msg, type: 'error', duration: 5 * 1000, offset:100 }) } }, error => { loadingCount --; if(loadingCount===0){ loadingInstance.close(); } Message({ message: error, type: 'error', duration: 5 * 1000, offset:100 }) } ); export default { /** * @param {String} url * @param {Object} data * @returns Promise */ post(url, data) { return new Promise((resolve, reject) => { axios({ method: 'post', url, data: qs.stringify(data), }) .then(res => { resolve(res.data) }) .catch(err => { reject(err) }); }) }, get(url, data,type) { if(type=='video'){ axios.defaults.baseURL =localStorage.getItem('cameraExtranetAgent') }else{ axios.defaults.baseURL = urlText+judgmentNetworkReturnAddress() } return new Promise((resolve, reject) => { axios({ method: 'get', url, params: data, }) .then(res => { resolve(res.data) }) .catch(err => { reject(err) }) }) }, baseUrl: urlText+judgmentNetworkReturnAddress() };