plm前端
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.

187 lines
4.4 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
  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_plm') // 请求头带上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_plm') // 请求头带上token
  84. // 如果是 FormData,删除 Content-Type 让浏览器自动设置(包含 boundary)
  85. if (config.data instanceof FormData) {
  86. delete config.headers['Content-Type']
  87. }
  88. return config
  89. }, error => {
  90. return Promise.reject(error)
  91. })
  92. /**
  93. * 响应拦截
  94. */
  95. instance.interceptors.response.use(response => {
  96. if (response.data && response.data.code === 401) { // 401, token失效
  97. clearLoginInfo()
  98. router.push({ name: 'login' })
  99. }
  100. return response
  101. }, error => {
  102. return Promise.reject(error)
  103. })
  104. // ============================= 下载功能的请求 =============================
  105. const instance2 = axios.create({
  106. baseURL: (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl),
  107. timeout: 10000 * 30,
  108. withCredentials: true,
  109. responseType: 'blob',
  110. headers: {
  111. 'Content-Type': 'application/json; charset=utf-8'
  112. }
  113. })
  114. /**
  115. * 请求拦截
  116. */
  117. instance2.interceptors.request.use(config => {
  118. config.headers['token'] = Vue.cookie.get('token_plm') // 请求头带上token
  119. // 如果是 FormData,删除 Content-Type 让浏览器自动设置(包含 boundary)
  120. if (config.data instanceof FormData) {
  121. delete config.headers['Content-Type']
  122. }
  123. return config
  124. }, error => {
  125. return Promise.reject(error)
  126. })
  127. /**
  128. * 响应拦截
  129. */
  130. instance2.interceptors.response.use(response => {
  131. if (response.data && response.data.code === 401) { // 401, token失效
  132. clearLoginInfo()
  133. router.push({name: 'login'})
  134. }
  135. return response
  136. }, error => {
  137. return Promise.reject(error)
  138. })
  139. export const createAPI = (url, method, data, timeout) => {
  140. let config = {}
  141. if (method === 'get') {
  142. config.params = data
  143. } else {
  144. config.data = data
  145. }
  146. if (timeout) {
  147. config.timeout = timeout
  148. }
  149. if (data === 'download'){ // 下载功能的请求
  150. return instance2({
  151. url,
  152. method,
  153. ...config
  154. })
  155. }
  156. return instance({
  157. url,
  158. method,
  159. ...config
  160. })
  161. }