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.1 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
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. // rqrq - 增加超时时间到10分钟,支持大数据量导出
  103. const instance2 = axios.create({
  104. baseURL: (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl),
  105. timeout: 1000 * 60 * 10, // 10分钟超时 - rqrq
  106. withCredentials: true,
  107. responseType: 'blob',
  108. headers: {
  109. 'Content-Type': 'application/json; charset=utf-8'
  110. }
  111. })
  112. /**
  113. * 请求拦截
  114. */
  115. instance2.interceptors.request.use(config => {
  116. config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
  117. return config
  118. }, error => {
  119. return Promise.reject(error)
  120. })
  121. /**
  122. * 响应拦截
  123. */
  124. instance2.interceptors.response.use(response => {
  125. if (response.data && response.data.code === 401) { // 401, token失效
  126. clearLoginInfo()
  127. router.push({name: 'login'})
  128. }
  129. return response
  130. }, error => {
  131. return Promise.reject(error)
  132. })
  133. export const createAPI = (url, method, data, type) => {
  134. let config = {}
  135. if (method === 'get') {
  136. config.params = data
  137. } else {
  138. config.data = data
  139. }
  140. if (data === 777 || type === 'download'){ // 下载功能的请求
  141. return instance2({
  142. url,
  143. method,
  144. ...config
  145. })
  146. }
  147. return instance({
  148. url,
  149. method,
  150. ...config
  151. })
  152. }