Commit 1c250cc5 by 肖康

x

parent c1bea471
...@@ -28,7 +28,8 @@ switch (envs) { ...@@ -28,7 +28,8 @@ switch (envs) {
} }
export const http = (method, url, param) => { export const http = (method, url, param,loading) => {
//loading 默认加载loading 传1 不加载
param = param && typeof param === 'object' ? param : {}; param = param && typeof param === 'object' ? param : {};
let token = Util.getCookie('token') || ''; let token = Util.getCookie('token') || '';
const config = { const config = {
...@@ -39,7 +40,8 @@ export const http = (method, url, param) => { ...@@ -39,7 +40,8 @@ export const http = (method, url, param) => {
}], }],
headers: { headers: {
'Authorization': 'Bearer ' + token 'Authorization': 'Bearer ' + token
} },
loading:(loading?false:true)
}; };
// post请求时需要设定Content-Type // post请求时需要设定Content-Type
......
import Vue from 'vue';
import { Loading } from 'element-ui';
Vue.use(Loading)
let loadingCount = 0;
let loading;
const startLoading = () => {
loading = Loading.service({
lock: true,
// text: '加载中……',
background: 'rgba(255, 255, 255, 0)'
});
};
const endLoading = () => {
loading.close();
};
export const showLoading = () => {
if (loadingCount === 0) {
startLoading();
}
loadingCount += 1;
};
export const hideLoading = () => {
if (loadingCount <= 0) {
return;
}
loadingCount -= 1;
if (loadingCount === 0) {
endLoading();
}
};
\ No newline at end of file
...@@ -3,9 +3,10 @@ import App from "./App.vue"; ...@@ -3,9 +3,10 @@ import App from "./App.vue";
import router from "./router"; import router from "./router";
import filters from './filters' import filters from './filters'
import directive from './directive' import directive from './directive'
import {http} from './ajax/index.js'; import { http } from './ajax/index.js';
import axios from 'axios' import axios from 'axios'
import {Message} from 'element-ui'; import { Message } from 'element-ui';
import { showLoading, hideLoading } from './ajax/loading';
import Util from "./tool"; import Util from "./tool";
//本地环境开启提示信息 //本地环境开启提示信息
...@@ -24,7 +25,7 @@ Vue.use(directive); ...@@ -24,7 +25,7 @@ Vue.use(directive);
//处理顶部导航TAB //处理顶部导航TAB
router.afterEach((to, from, next) => { router.afterEach((to, from, next) => {
if (to.path == "/"||to.path == "/login") { if (to.path == "/" || to.path == "/login") {
return return
} }
let tabOldArr = sessionStorage.getItem('tabs'); let tabOldArr = sessionStorage.getItem('tabs');
...@@ -38,13 +39,13 @@ router.afterEach((to, from, next) => { ...@@ -38,13 +39,13 @@ router.afterEach((to, from, next) => {
if (tabNewJson.indexOf(JSON.stringify({ if (tabNewJson.indexOf(JSON.stringify({
path: to.path, path: to.path,
title: to.meta.title, title: to.meta.title,
query:to.query query: to.query
})) == -1) { })) == -1) {
console.log(tabOldJson) console.log(tabOldJson)
tabOldJson.push({ tabOldJson.push({
path: to.path, path: to.path,
title: to.meta.title, title: to.meta.title,
query:to.query query: to.query
}) })
} }
} else { } else {
...@@ -52,7 +53,7 @@ router.afterEach((to, from, next) => { ...@@ -52,7 +53,7 @@ router.afterEach((to, from, next) => {
{ {
path: to.path, path: to.path,
title: to.meta.title, title: to.meta.title,
query:to.query query: to.query
} }
] ]
...@@ -63,10 +64,24 @@ router.afterEach((to, from, next) => { ...@@ -63,10 +64,24 @@ router.afterEach((to, from, next) => {
} }
}); });
axios.interceptors.request.use((config) => {
if(config.loading){
showLoading();
}
return config;
}, (err) => {
return Promise.reject(err)
})
axios.interceptors.response.use(res => { axios.interceptors.response.use(res => {
hideLoading();
return res.data; return res.data;
}, error => { }, error => {
// Message('网络出现问题,请检查网络'); // Message('网络出现问题,请检查网络');
hideLoading();
return Promise.reject(new Error(error)) return Promise.reject(new Error(error))
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment