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.
|
|
//vxvdvdsvsdvvds3
import router from "@/router";import axios from "axios";// import Promise from 'promise'
import { yztoken } from "@/utlis/istoken";import { Message, Loading } from "element-ui";import store from "../store/index";
const sysConfig = getSysConfig()
function getSysConfig(){ console.log('store',store.state.sysConfig) if(store.state.sysConfig && store.state.sysConfig.apiurl){ window.sessionStorage.setItem('sysConfig', JSON.stringify(store.state.sysConfig)) return store.state.sysConfig }else{ return JSON.parse(window.sessionStorage.getItem('sysConfig')) }}
const instance = axios.create({ baseURL: sysConfig.apiurl, //timeout: 500000,
withCredentials: true, // 允许跨域
});let loading = nulllet needLoadingRequestCount = 0
function startLoading() { loading = Loading.service({ lock: true, text: "正在加载中,请稍后", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0)", customClass: "boxStyle" });}
function endLoading() { loading.close()}export function showFullScreenLoading() { if (needLoadingRequestCount === 0) { startLoading() } needLoadingRequestCount++}export function tryHideFullScreenLoading() { if (needLoadingRequestCount <= 0) return needLoadingRequestCount-- if (needLoadingRequestCount === 0) { endLoading() }}//请求拦截
instance.interceptors.request.use( async function(config) { //console.log('old token:',window.sessionStorage.getItem("token"))
//console.log('old expires_in',window.sessionStorage.getItem("expires_in"))
await yztoken(); showFullScreenLoading() let token = window.sessionStorage.getItem("token");
let tokentype = window.sessionStorage.getItem("tokentype"); config.headers["Access-Control-Allow-Origin"] = "*"; // config.headers.ContentType = 'application/x-www-form-urlencoded'
config.headers.Authorization = `${tokentype} ${token}`; return config; }, function(err) { return Promise.reject(err); });
//响应拦截
instance.interceptors.response.use( (res) => { tryHideFullScreenLoading() // console.log(res);
if (res.data.code < 0) { Message.error({ showClose: true, message: res.data.message }); } return res.data; }, (err) => { // console.log(err);
// if (response.data.code == -1) {
// router.push({ path: '/login' })
// // this.$router.push({path:'/login'})
// // console.log('状态失效')
// Message.error('状态失效请重新登录')
// console.log('token失效返回登录');
// }
tryHideFullScreenLoading() Message.error({ showClose: true, message: err.message }); return Promise.reject(err); } // function (response) {
// console.log(response,10010)
// const {data} = response
// if (data.status == 401) {
// window.location.href = '#/login'
// this.$message.error('状态失效');
// }
// return data
// },
// function (err) {
// if (err.response.status === 401) {
// window.location.href = '#/login'
// this.$router.push({ path: '/login' })
// this.$message.error('登录失效请重新登录');
// }
// return err
// }
);
export default instance;
|