ajax.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import axios from 'axios'
  2. import qs from 'qs'
  3. import router from '../router'
  4. import { Message } from 'element-ui'
  5. const token = function (headers) {
  6. let JWT
  7. if (headers) {
  8. localStorage.setItem('JWT', headers.authorization)
  9. JWT = headers.authorization
  10. }
  11. console.log(JWT)
  12. return JWT ? JWT : localStorage.getItem('JWT') // eslint-disable-line
  13. }
  14. const Axios = axios.create({
  15. timeout: 60000,
  16. responseType: 'json',
  17. withCredentials: true,
  18. queryData: {},
  19. urlData: {},
  20. headers: {
  21. 'Content-Type': 'multipart/form-data',
  22. 'authorization': token()
  23. }
  24. })
  25. Axios.interceptors.request.use((config) => {
  26. config.headers.authorization = token()
  27. config.urlData = { ...config.urlData, org: 'MQ' }
  28. // 处理请求data,若为get请求,拼到url后面,若为post请求,直接添加到body中
  29. let urlData = qs.stringify(config.urlData)
  30. let queryData = qs.stringify(config.queryData)
  31. // 判断是通过斜杠传参
  32. if (config.url.indexOf(':') > -1) {
  33. if (typeof config.urlData === 'object') {
  34. config.url = Object.keys(config.urlData).reduce((url, k) => { // 此方法对每个元素进行处理
  35. const re = new RegExp(`:${k}(?!w)`, 'i')
  36. return url.replace(re, config.urlData[k])
  37. }, config.url)
  38. } else {
  39. config.url = config.url.slice(0, config.url.indexOf(':')) + urlData
  40. }
  41. }
  42. if (queryData) {
  43. config.url += '?' + queryData
  44. }
  45. let fm = new FormData()
  46. for (let k in config.data) {
  47. if (Array.isArray(config.data[k])) {
  48. fm.append(k, ...config.data[k].map(v => `${k}=${encodeURIComponent(v)}`))
  49. } else {
  50. fm.append(k, config.data[k])
  51. }
  52. }
  53. config.data = fm
  54. return config
  55. }, (error) => {
  56. console.log(error)
  57. })
  58. // Axios.interceptors.response.use((res) => {
  59. // // return new Promise((resolve, reject) => {
  60. // // if (res.data.code === 200) {
  61. // // resolve(res.data.result)
  62. // // } else if (res.data.code === 401) {
  63. // // router.push({ name: 'login' })
  64. // // reject(res.data)
  65. // // } else {
  66. // // Message({
  67. // // message: res.data.message,
  68. // // type: 'error'
  69. // // })
  70. // // reject(res.data)
  71. // // }
  72. // // })
  73. // }, (error) => {
  74. // console.log(error)
  75. // })
  76. const ajax = (...args) => {
  77. return new Promise((resolve, reject) => {
  78. Axios(...args).then(({ data, headers }) => {
  79. if (headers.authorization) {
  80. token(headers)
  81. }
  82. const { code, message, result } = data
  83. if (code === 200) {
  84. resolve(result)
  85. } else if (code === 401) {
  86. router.push({ name: 'login' })
  87. } else {
  88. Message({
  89. message: message,
  90. type: 'error'
  91. })
  92. }
  93. }).catch(reject)
  94. })
  95. }
  96. export default ajax