axiosPass.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * long description for the file
  3. *
  4. * @summary 拦截器平台操作
  5. * @author wu <308822989@qq.com>
  6. *
  7. * Created at : 2018-08-27 10:50:48
  8. * Last modified : 2020-01-07 10:18:23
  9. */
  10. import Vue from 'vue';
  11. import querystring from 'querystring';
  12. // import store from '@/store/store.js';
  13. import { ajaxCtx } from '@/config/config.js';
  14. import {
  15. getCookie,
  16. setCookie
  17. } from '@/utils/util.js';
  18. import store from '@/store/index.js';
  19. import ElementUI from 'element-ui';
  20. // noToken 不用添加token的接口
  21. export const noToken = ['auth/login'];
  22. // token
  23. const hasNoToken = function (config) {
  24. let arr = noToken.filter(item => {
  25. if (config.url.indexOf(item) > -1) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. });
  31. return arr;
  32. }
  33. // 前台项目名称、接口项目名称,用于判断请求接口是哪个项目
  34. export const APIPro = ['pass/', 'multEntry', 'edgeDevice'];
  35. /**
  36. * url中是否有项目名称
  37. * @param {String} url url地址
  38. * @param {String} APIPro 项目地址目录名称
  39. */
  40. export const apiIsPassProject = function (url, APIPro) {
  41. if (Array.isArray(APIPro)) {
  42. let items = APIPro.filter((val, i) => {
  43. if (url.indexOf(val) > -1) {
  44. return true;
  45. } else {
  46. return false;
  47. }
  48. });
  49. if (items.length > 0) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. if (url.indexOf(APIPro) > -1) {
  56. return true;
  57. } else {
  58. return false;
  59. }
  60. }
  61. /**
  62. * 如果是平台接口,修改拦截数据
  63. * @param {Object} config request请求
  64. * @return {Object} config axios配置文件
  65. */
  66. export const requestConfig = function (config) {
  67. if (!apiIsPassProject(config.url, APIPro)) {
  68. // 不是平台的接口 , 原样返回
  69. if (hasNoToken(config).length < 1) {
  70. // config.headers.authorization = 'Bearer ' + getCookie('accessToken');
  71. }
  72. return config;
  73. }
  74. // 是平台接口
  75. //
  76. // 是否自定义Content-Type,config.diy为true则不对Content-Type与数据进行操作
  77. if (!config.diy || config.diy !== true) {
  78. // 请求类型
  79. if (config.method === 'get' || config.individualType === 'json') {
  80. // do
  81. } else if (config.method === 'post' && config.headers['Content-Type'] !== 'multipart/form-data') {
  82. config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  83. } else if (config.method === 'put') {
  84. config.headers['Content-Type'] = 'application/json;charset=UTF-8';
  85. } else if (config.method === 'delete' && config.headers['Content-Type'] !== 'multipart/form-data') {
  86. config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  87. }
  88. // 请求数据
  89. if ((config.method === 'post' && config.individualType !== 'json') || config.method === 'delete') {
  90. if (config.headers['Content-Type'] === 'multipart/form-data') {
  91. //
  92. } else {
  93. config.data = querystring.stringify(config.data);
  94. }
  95. }
  96. }
  97. // token
  98. if (hasNoToken(config).length < 1) {
  99. config.headers.authorization = 'Bearer ' + getCookie('accessToken');
  100. }
  101. // http
  102. if (!(config.url.indexOf('http') === 0 || config.url.indexOf('https') === 0)) {
  103. config.url = store.getters.API + config.url;
  104. }
  105. return config;
  106. }
  107. /**
  108. * 整理返回如果是平台则做处理,不是平台原样返回
  109. * @param {Object} response 返回response
  110. * @return {Object} response
  111. */
  112. export const responseConfig = function (response) {
  113. if (!apiIsPassProject(response.config.url, APIPro)) {
  114. // 不是平台的接口 , 原样返回
  115. return response;
  116. }
  117. // 是平台接口整理数据
  118. //
  119. // 消息
  120. let data = response.data;
  121. if (data.code === '401') {
  122. ElementUI.Message({
  123. message: '登录已过期,请重新登录',
  124. type: 'error',
  125. onClose: function () {
  126. // 弹窗关闭时,跳转到登录
  127. setCookie('accessToken', '', -1, '/');
  128. setCookie('refreshToken', '', -1, '/');
  129. setCookie('workDate', '', -1);
  130. setCookie('ticket', '', -1, '/');
  131. window.top.location.href = store.getters.ctx;
  132. }
  133. });
  134. }
  135. // 数据
  136. response = response.data;
  137. return response;
  138. }
  139. // 适配axios引用
  140. export const axios = Vue.axios;