request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import axios from 'axios'
  2. import { MessageBox, Message } from 'element-ui'
  3. // import store from '@/store'
  4. //import { getToken } from '@/utils/auth'
  5. // import { deviceUrl } from '@/utils/domain'
  6. const qs = require('qs')
  7. // create an axios instance
  8. // const deviceUrl = "http://119.23.151.229:8006"
  9. const service = axios.create({
  10. baseURL: process.env.deviceUrl, // url = base url + request url
  11. // withCredentials: true, // send cookies when cross-domain requests
  12. timeout: 5000, // request timeout
  13. paramsSerializer: params => qs.stringify(params, { arryFormat: 'repeat' })
  14. })
  15. // request interceptor
  16. service.interceptors.request.use(
  17. config => {
  18. // do something before request is sent
  19. config.headers['Content-type'] = 'application/x-www-form-urlencoded;charset=utf-8'
  20. // 如果是put/post请求,用qs.stringify序列化参数
  21. const is_put_post = config.method === 'put' || config.method === 'post' || config.method === 'delete'
  22. const is_json = config.headers['Content-Type'] === 'application/json'
  23. if (is_put_post && is_json) {
  24. config.data = JSON.stringify(config.data)
  25. }
  26. if (is_put_post && !is_json) {
  27. config.data = qs.stringify(config.data, { arrayFormat: 'repeat' })
  28. }
  29. // if (store.getters.token) {
  30. // // let each request carry token
  31. // // ['X-Token'] is a custom headers key
  32. // // please modify it according to the actual situation
  33. // // config.headers['X-Token'] = getToken()
  34. // }
  35. return config
  36. },
  37. error => {
  38. // do something with request error
  39. // console.log(error) // for debug
  40. return Promise.reject(error)
  41. }
  42. )
  43. // response interceptor
  44. service.interceptors.response.use(
  45. /**
  46. * If you want to get http information such as headers or status
  47. * Please return response => response
  48. */
  49. /**
  50. * Determine the request status by custom code
  51. * Here is just an example
  52. * You can also judge the status by HTTP Status Code
  53. */
  54. response => {
  55. return response.data
  56. // if the custom code is not 20000, it is judged as an error.
  57. // if (res.code !== 20000) {
  58. // Message({
  59. // message: res.message || 'Error',
  60. // type: 'error',
  61. // duration: 5 * 1000
  62. // })
  63. //
  64. // // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  65. // if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  66. // // to re-login
  67. // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  68. // confirmButtonText: 'Re-Login',
  69. // cancelButtonText: 'Cancel',
  70. // type: 'warning'
  71. // }).then(() => {
  72. // store.dispatch('user/resetToken').then(() => {
  73. // location.reload()
  74. // })
  75. // })
  76. // }
  77. // return Promise.reject(new Error(res.message || 'Error'))
  78. // } else {
  79. // return res
  80. // }
  81. },
  82. error => {
  83. const error_response = error.response || {}
  84. const error_data = error_response.data || {}
  85. Message({
  86. message: error_data.message,
  87. type: 'error',
  88. duration: 5 * 1000
  89. })
  90. // return Promise.reject(error_data)
  91. }
  92. )
  93. export default service