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.

169 lines
4.0 KiB

6 months ago
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import router from '@/router'
  4. import qs from 'qs'
  5. import merge from 'lodash/merge'
  6. import { clearLoginInfo } from '@/utils'
  7. const http = axios.create({
  8. timeout: 1000 * 300,
  9. withCredentials: true,
  10. headers: {
  11. 'Content-Type': 'application/json; charset=utf-8'
  12. }
  13. })
  14. /**
  15. * 请求拦截
  16. */
  17. http.interceptors.request.use(config => {
  18. config.headers['token'] =Vue.cookie.get('token') // 请求头带上token
  19. return config
  20. }, error => {
  21. return Promise.reject(error)
  22. })
  23. /**
  24. * 响应拦截 可自定义统一返回结果
  25. */
  26. http.interceptors.response.use(response => {
  27. if (response.data && response.data.code === 401) { // 401, token失效
  28. clearLoginInfo()
  29. router.push({ name: 'login' })
  30. }
  31. return response
  32. }, error => {
  33. return Promise.reject(error)
  34. })
  35. /**
  36. * 请求地址处理
  37. * @param {*} actionName action方法名称
  38. */
  39. http.adornUrl = (actionName) => {
  40. // 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
  41. return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName
  42. }
  43. /**
  44. * get请求参数处理
  45. * @param {*} params 参数对象
  46. * @param {*} openDefultParams 是否开启默认参数?
  47. */
  48. http.adornParams = (params = {}, openDefultParams = true) => {
  49. var defaults = {
  50. 't': new Date().getTime()
  51. }
  52. return openDefultParams ? merge(defaults, params) : params
  53. }
  54. /**
  55. * post请求数据处理
  56. * @param {*} data 数据对象
  57. * @param {*} openDefultdata 是否开启默认数据?
  58. * @param {*} contentType 数据格式
  59. * json: 'application/json; charset=utf-8'
  60. * form: 'application/x-www-form-urlencoded; charset=utf-8'
  61. */
  62. http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
  63. var defaults = {
  64. 't': new Date().getTime()
  65. }
  66. data = openDefultdata ? merge(defaults, data) : data
  67. return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
  68. }
  69. export default http
  70. // ---------------------------------------------- 封装 post 和 get
  71. const instance = axios.create({
  72. baseURL: (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) ,
  73. timeout: 10000 * 30,
  74. withCredentials: true,
  75. headers: {
  76. 'Content-Type': 'application/json; charset=utf-8'
  77. }
  78. })
  79. /**
  80. * 请求拦截
  81. */
  82. instance.interceptors.request.use(config => {
  83. config.headers['token'] =Vue.cookie.get('token') // 请求头带上token
  84. return config
  85. }, error => {
  86. return Promise.reject(error)
  87. })
  88. /**
  89. * 响应拦截
  90. */
  91. instance.interceptors.response.use(response => {
  92. if (response.data && response.data.code === 401) { // 401, token失效
  93. clearLoginInfo()
  94. router.push({ name: 'login' })
  95. }
  96. return response
  97. }, error => {
  98. return Promise.reject(error)
  99. })
  100. // ============================= 下载功能的请求 =============================
  101. const instance2 = axios.create({
  102. baseURL: (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl),
  103. timeout: 10000 * 30,
  104. withCredentials: true,
  105. responseType: 'blob',
  106. headers: {
  107. 'Content-Type': 'application/json; charset=utf-8'
  108. }
  109. })
  110. /**
  111. * 请求拦截
  112. */
  113. instance2.interceptors.request.use(config => {
  114. config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
  115. return config
  116. }, error => {
  117. return Promise.reject(error)
  118. })
  119. /**
  120. * 响应拦截
  121. */
  122. instance2.interceptors.response.use(response => {
  123. if (response.data && response.data.code === 401) { // 401, token失效
  124. clearLoginInfo()
  125. router.push({name: 'login'})
  126. }
  127. return response
  128. }, error => {
  129. return Promise.reject(error)
  130. })
  131. export const createAPI = (url, method, data, type) => {
  132. let config = {}
  133. if (method === 'get') {
  134. config.params = data
  135. } else {
  136. config.data = data
  137. }
  138. if (data === 777 || type === 'download'){ // 下载功能的请求
  139. return instance2({
  140. url,
  141. method,
  142. ...config
  143. })
  144. }
  145. return instance({
  146. url,
  147. method,
  148. ...config
  149. })
  150. }