request.js

    1. import axios from 'axios'
    2. import { MessageBox, Message } from 'element-ui'
    3. import store from '@/store'
    4. import router from '@/router'
    5. import { removeStorage, throttle } from '@/utils/index'
    6. // create an axios instance
    7. const service = axios.create({
    8. // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    9. withCredentials: true, // send cookies when cross-domain requests
    10. validateStatus: null,
    11. timeout: 10000, // request timeout
    12. // headers: {'Content-Type': 'application/json'}
    13. })
    14. // request interceptor
    15. service.interceptors.request.use(
    16. config => {
    17. return config
    18. },
    19. error => {
    20. // do something with request error
    21. console.log(error) // for debug
    22. return Promise.reject(error)
    23. }
    24. )
    25. var goLoginPageThrottle = throttle(() => {
    26. console.log('goLoginPageThrottle')
    27. MessageBox('登录状态已超时,请重新登录!', '提示', {
    28. confirmButtonText: '确定',
    29. center: true
    30. }).then((action) => {
    31. console.log(location)
    32. removeStorage('USER_INFO')
    33. router.push(`/login?redirect=${location.pathname + location.search}`)
    34. }).catch(err => {
    35. console.log('cancle', err)
    36. removeStorage('USER_INFO')
    37. router.push(`/login?redirect=${location.pathname + location.search}`)
    38. })
    39. }, 2000)
    40. // response interceptor
    41. service.interceptors.response.use(
    42. /**
    43. * If you want to get http information such as headers or status
    44. * Please return response => response
    45. */
    46. /**
    47. * Determine the request status by custom code
    48. * Here is just an example
    49. * You can also judge the status by HTTP Status Code
    50. */
    51. response => {
    52. const res = response.data
    53. // console.log('response', response)
    54. // if the custom code is not 20000, it is judged as an error.
    55. switch (response.status) {
    56. case 200:
    57. return res
    58. case 401:
    59. if (location.pathname === '/login') {
    60. return Promise.reject(res)
    61. } else {
    62. // 加入缓冲,避免多次弹窗
    63. goLoginPageThrottle()
    64. // if (loginGoing === false) {
    65. // loginGoing = true
    66. // // router.push(`/login?redirect=${location.pathname + location.search}`)
    67. // // router.push(`/login`)
    68. // } else {
    69. // console.log('loginGoing', loginGoing)
    70. // }
    71. return Promise.reject(res)
    72. }
    73. case 504:
    74. Message({
    75. message: res.message || '请求超时,请稍后再试',
    76. type: 'error',
    77. duration: 5 * 1000
    78. })
    79. return Promise.reject(res)
    80. default:
    81. Message({
    82. message: res.message || '服务器开小差了,请稍后再试',
    83. type: 'error',
    84. duration: 5 * 1000
    85. })
    86. return Promise.reject(res)
    87. }
    88. },
    89. error => {
    90. console.log('err' + error) // for debug
    91. Message({
    92. message: error.message,
    93. type: 'error',
    94. duration: 5 * 1000
    95. })
    96. return Promise.reject(error)
    97. }
    98. )
    99. /*
    100. * @params {string} url 请求地址
    101. * @params {object} resOpts 请求配置参数
    102. */
    103. export const download = (url, resOpts = {}) => {
    104. const { type = 'get', data = {}} = resOpts
    105. const queryArgs = {
    106. url,
    107. method: type,
    108. data,
    109. responseType: 'blob',
    110. headers: {
    111. 'Content-Type': 'application/json;charset=utf-8',
    112. withCredentials: true,
    113. },
    114. }
    115. // tips: 这里直接返回的是response整体!
    116. return axios.request(queryArgs).catch(err => console.log(err))
    117. }
    118. export default service