123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import axios from 'axios'
- import VueAxios from 'vue-axios'
- import Vue from 'vue'
- import ElementUI, {
- Loading
- } from 'element-ui';
- let loading //定义loading变量
- function startLoading() { //使用Element loading-start 方法
- loading = Loading.service({
- text: '请稍等...',
- background: 'rgba(0, 0, 0, 0.2)'
- })
- }
- function endLoading() { //使用Element loading-close 方法
- loading.close()
- }
- let needLoadingRequestCount = 0
- export function showFullScreenLoading() {
- if (needLoadingRequestCount === 0) {
- startLoading()
- }
- needLoadingRequestCount++
- }
- export function tryHideFullScreenLoading() {
- if (needLoadingRequestCount <= 0) return
- needLoadingRequestCount--
- if (needLoadingRequestCount === 0) {
- endLoading()
- }
- }
- //axios配置和处理
- Vue.use(VueAxios, axios);
- axios.defaults.baseURL = ''; //项目的的基础url
- axios.defaults.headers.common['Authorization'] = ""; //请求token信息配置
- axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求type设置
- axios.defaults.timeout = 30000; //在超时前,所有请求都会等待30秒
- axios.defaults.withCredentials = true;
- // 添加请求拦截器
- axios.interceptors.request.use(function (config) {
- // 在发送请求之前处理
- // 判断token在前台session中是否存在
- config.headers.common['Authorization'] = localStorage.getItem('Authorization');
- showFullScreenLoading();
- return config;
- }, function (error) {
- // 对请求错误做处理
- return Promise.reject(error);
- });
- // 添加响应拦截器
- axios.interceptors.response.use(function (response) {
- // 对响应数据处理
- tryHideFullScreenLoading();
- if (response && (response.status == 200)) {
- if (response.data.exceptionMes) {
- ElementUI.MessageBox({
- type: 'info',
- title: response.data.exceptionMes,
- message: '网络错误,请稍后再试!'
- });
- }
- return response.data;
- }
- return response;
- }, function (error) {
- tryHideFullScreenLoading();
- console.log('网络异常');
- ElementUI.Message.error("网络异常");
- return Promise.reject(error);
- });
- Vue.prototype.$axios = axios; //定义调用方式
- // 针对上传大文件
- const instance = axios.create({
- baseURL: '',
- });
- instance.defaults.headers.common['Authorization'] = ""; //请求token信息配置
- instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求type设置
- instance.defaults.timeout = 1000 * 60 * 60; //在超时前,所有请求都会等待
- instance.defaults.withCredentials = true;
- instance.interceptors.request.use(function (config) {
- // 在发送请求之前处理
- // 判断token在前台session中是否存在
- config.headers.common['Authorization'] = localStorage.getItem('Authorization');
- showFullScreenLoading();
- return config;
- }, function (error) {
- // 对请求错误做处理
- return Promise.reject(error);
- });
- // 添加响应拦截器
- instance.interceptors.response.use(function (response) {
- // 对响应数据处理
- tryHideFullScreenLoading();
- if (response && (response.status == 200)) {
- if (response.data.exceptionMes) {
- ElementUI.MessageBox({
- type: 'info',
- title: response.data.exceptionMes,
- message: '网络错误,请稍后再试!'
- });
- }
- return response.data;
- }
- return response;
- }, function (error) {
- tryHideFullScreenLoading();
- console.log('网络异常');
- ElementUI.Message.error("网络异常");
- return Promise.reject(error);
- });
- Vue.use(VueAxios, instance);
- Vue.prototype.$instance = instance; //定义调用方式
|