request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import axios from 'axios'
  2. import { getToken, removeToken } from '@/utils/auth'
  3. import { anonUrls } from '../config'
  4. // 创建axios实例
  5. const service = axios.create({
  6. baseURL: '/api/', // api的base_url
  7. timeout: 15000, // 请求超时时间
  8. headers: {
  9. "Pragma": "no-cache",
  10. "Cache-Control": "must-revalidate",
  11. "Cache-Control": "no-cache" ,
  12. "Cache-Control": "no-store",
  13. }
  14. })
  15. const notSetAuthHeaderUrls = anonUrls;
  16. // request拦截器
  17. service.interceptors.request.use(config => {
  18. // debugger
  19. if (!config.headers.ANON && needSetAuthHead(config.url)) {
  20. // config.headers['Auth'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  21. // config.headers['Authorization'] = 'Bearer ' + getToken()
  22. config.headers['Authorization'] = 'Bearer ' + getToken()
  23. }
  24. return config
  25. }, error => {
  26. // Do something with request error
  27. console.log(error) // for debug
  28. Promise.reject(error)
  29. })
  30. // respone拦截器
  31. service.interceptors.response.use(
  32. response => {
  33. /**
  34. * code为非20000是抛错 可结合自己业务进行修改
  35. */
  36. if (response['headers']['content-disposition']) {
  37. const data = response.data;
  38. var fileName = response['headers']['content-disposition'].split(";")[1].split("filename=")[1];
  39. var index = fileName.lastIndexOf("\"");
  40. if (index == fileName.length - 1) {
  41. var start = 0;
  42. if (fileName.indexOf("\"") == 0) {
  43. start = 1;
  44. }
  45. fileName = fileName.substring(start, index);
  46. }
  47. return {
  48. data,
  49. fileName
  50. }
  51. }
  52. if (response['headers']['captcha-uid']) {
  53. return response;
  54. }
  55. const res = response.data;
  56. if (res['access_token'] || !res.status) {
  57. return response.data;
  58. }
  59. if (res.status != 200) {
  60. if (res.status == 401) {
  61. removeToken();
  62. // router.push({
  63. // path: '/login',
  64. // })
  65. } else {
  66. msg(res.msg);
  67. }
  68. return Promise.reject(res)
  69. } else {
  70. return response.data
  71. }
  72. },
  73. error => {
  74. if (error && error.response) {
  75. if (error.response.status == 401) {
  76. removeToken();
  77. // location.reload()
  78. } else if (error.response.status == 403) {
  79. msg("无权访问");
  80. } else if (error.response.status == 500) {
  81. msg("系统繁忙,请稍后再试");
  82. }
  83. }
  84. console.log('err' + error) // for debug
  85. // store.dispatch('user/LOGOUT').then(() => {
  86. // //location.reload()// 为了重新实例化vue-router对象 避免bug
  87. // })
  88. // Message({
  89. // message: error.message,
  90. // type: 'error',
  91. // duration: 5 * 1000
  92. // })
  93. return Promise.reject(error)
  94. }
  95. )
  96. const needSetAuthHead = (url) => {
  97. return notSetAuthHeaderUrls.indexOf(url) == -1;
  98. }
  99. const msg = (msg) => {
  100. }
  101. /* eslint-disable no-proto */
  102. service.__proto__ = axios
  103. /* eslint-enable */
  104. export default service