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.

168 lines
4.0 KiB

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