乐天mes前端
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.

129 lines
3.0 KiB

4 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 * 30,
  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: 1000 * 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. export const createAPI = (url, method, data) => {
  101. let config = {}
  102. if (method === 'get') {
  103. config.params = data
  104. } else {
  105. config.data = data
  106. }
  107. return instance({
  108. url,
  109. method,
  110. ...config
  111. })
  112. }