123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /**
- * long description for the file
- *
- * @summary 拦截器平台操作
- * @author wu <308822989@qq.com>
- *
- * Created at : 2018-08-27 10:50:48
- * Last modified : 2020-01-07 10:18:23
- */
- import Vue from 'vue';
- import querystring from 'querystring';
- // import store from '@/store/store.js';
- import { ajaxCtx } from '@/config/config.js';
- import {
- getCookie,
- setCookie
- } from '@/utils/util.js';
- import store from '@/store/index.js';
- import ElementUI from 'element-ui';
- // noToken 不用添加token的接口
- export const noToken = ['auth/login'];
- // token
- const hasNoToken = function (config) {
- let arr = noToken.filter(item => {
- if (config.url.indexOf(item) > -1) {
- return true;
- } else {
- return false;
- }
- });
- return arr;
- }
- // 前台项目名称、接口项目名称,用于判断请求接口是哪个项目
- export const APIPro = ['pass/', 'multEntry', 'edgeDevice'];
- /**
- * url中是否有项目名称
- * @param {String} url url地址
- * @param {String} APIPro 项目地址目录名称
- */
- export const apiIsPassProject = function (url, APIPro) {
- if (Array.isArray(APIPro)) {
- let items = APIPro.filter((val, i) => {
- if (url.indexOf(val) > -1) {
- return true;
- } else {
- return false;
- }
- });
- if (items.length > 0) {
- return true;
- } else {
- return false;
- }
- }
- if (url.indexOf(APIPro) > -1) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 如果是平台接口,修改拦截数据
- * @param {Object} config request请求
- * @return {Object} config axios配置文件
- */
- export const requestConfig = function (config) {
- if (!apiIsPassProject(config.url, APIPro)) {
- // 不是平台的接口 , 原样返回
- if (hasNoToken(config).length < 1) {
- // config.headers.authorization = 'Bearer ' + getCookie('accessToken');
- }
- return config;
- }
- // 是平台接口
- //
- // 是否自定义Content-Type,config.diy为true则不对Content-Type与数据进行操作
- if (!config.diy || config.diy !== true) {
- // 请求类型
- if (config.method === 'get' || config.individualType === 'json') {
- // do
- } else if (config.method === 'post' && config.headers['Content-Type'] !== 'multipart/form-data') {
- config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
- } else if (config.method === 'put') {
- config.headers['Content-Type'] = 'application/json;charset=UTF-8';
- } else if (config.method === 'delete' && config.headers['Content-Type'] !== 'multipart/form-data') {
- config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
- }
- // 请求数据
- if ((config.method === 'post' && config.individualType !== 'json') || config.method === 'delete') {
- if (config.headers['Content-Type'] === 'multipart/form-data') {
- //
- } else {
- config.data = querystring.stringify(config.data);
- }
- }
- }
- // token
- if (hasNoToken(config).length < 1) {
- config.headers.authorization = 'Bearer ' + getCookie('accessToken');
- }
- // http
- if (!(config.url.indexOf('http') === 0 || config.url.indexOf('https') === 0)) {
- config.url = store.getters.API + config.url;
- }
- return config;
- }
- /**
- * 整理返回如果是平台则做处理,不是平台原样返回
- * @param {Object} response 返回response
- * @return {Object} response
- */
- export const responseConfig = function (response) {
- if (!apiIsPassProject(response.config.url, APIPro)) {
- // 不是平台的接口 , 原样返回
- return response;
- }
- // 是平台接口整理数据
- //
- // 消息
- let data = response.data;
- if (data.code === '401') {
- ElementUI.Message({
- message: '登录已过期,请重新登录',
- type: 'error',
- onClose: function () {
- // 弹窗关闭时,跳转到登录
- setCookie('accessToken', '', -1, '/');
- setCookie('refreshToken', '', -1, '/');
- setCookie('workDate', '', -1);
- setCookie('ticket', '', -1, '/');
- window.top.location.href = store.getters.ctx;
- }
- });
- }
- // 数据
- response = response.data;
- return response;
- }
- // 适配axios引用
- export const axios = Vue.axios;
|