天波用户运营管理后台系统
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.

104 lines
2.7KB

  1. /*
  2. * @Date: 2021-12-08 15:59:46
  3. * @LastEditors: JinxuChen
  4. * @LastEditTime: 2022-01-10 09:41:58
  5. * @FilePath: \GpsCardAdmin\src\utils\request.js
  6. * @description:
  7. */
  8. import axios from 'axios'
  9. import { Message } from 'element-ui'
  10. import store from '@/store'
  11. /* import { getToken } from '@/utils/auth' */
  12. // create an axios instance
  13. const service = axios.create({
  14. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  15. // withCredentials: true, // send cookies when cross-domain requests
  16. /* timeout: 5000 */// request timeout
  17. })
  18. // request interceptor
  19. service.interceptors.request.use(
  20. config => {
  21. // do something before request is sent
  22. if (store.getters.token) {
  23. // let each request carry token
  24. // ['X-Token'] is a custom headers key
  25. // please modify it according to the actual situation
  26. config.headers['AuthToken'] = store.getters.token;
  27. }
  28. return config
  29. },
  30. error => {
  31. // do something with request error
  32. console.log(error) // for debug
  33. return Promise.reject(error)
  34. }
  35. )
  36. // response interceptor
  37. service.interceptors.response.use(
  38. /**
  39. * If you want to get http information such as headers or status
  40. * Please return response => response
  41. */
  42. /**
  43. * Determine the request status by custom code
  44. * Here is just an example
  45. * You can also judge the status by HTTP Status Code
  46. */
  47. response => {
  48. const res = response.data
  49. // if the custom code is not 20000, it is judged as an error.
  50. if (res.code !== 0) {
  51. Message({
  52. message: res.message || 'Error',
  53. type: 'error',
  54. duration: 5 * 1000
  55. })
  56. // 106 token验证过期;
  57. if (res.code === 106) {
  58. // to re-login
  59. /* MessageBox.confirm('登录过期,是否重新登录?', {
  60. confirmButtonText: '确认',
  61. cancelButtonText: '取消',
  62. type: 'warning'
  63. }).then(() => {
  64. store.dispatch('user/resetToken').then(() => {
  65. location.reload()
  66. })
  67. }) */
  68. setTimeout(() => {
  69. store.dispatch('user/resetToken').then(() => {
  70. Message({
  71. message: 'token过期,请重新登录',
  72. type: 'error',
  73. duration: 1500
  74. })
  75. location.reload()
  76. })
  77. }, 1500)
  78. return Promise.reject(new Error('token过期,请重新登录'))
  79. }
  80. /* return Promise.reject(new Error(res.message)) */
  81. } else {
  82. return res
  83. }
  84. },
  85. error => {
  86. console.log('err' + error) // for debug
  87. Message({
  88. message: error.message,
  89. type: 'error',
  90. duration: 5 * 1000
  91. })
  92. /* return Promise.reject(error) */
  93. }
  94. )
  95. export default service