fetchApp.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import axios from 'axios';
  2. import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  3. import { message, notification } from 'ant-design-vue';
  4. import { h } from 'vue';
  5. const fetchApp = axios.create({
  6. baseURL: import.meta.env.VITE_API_APP_URL,
  7. headers: {
  8. 'Content-Type': 'application/json'
  9. }
  10. // timeout: 5000, // 请求超时时间
  11. });
  12. /**
  13. * 请求拦截器
  14. */
  15. fetchApp.interceptors.request.use(
  16. config => {
  17. // 可以在这里添加请求头等信息
  18. config.headers = {
  19. // token: localStorage.getItem("token"),
  20. 'Content-Type': 'application/json'
  21. };
  22. return config;
  23. },
  24. error => {
  25. // 请求错误处理
  26. console.log(error); // for debug
  27. Promise.reject(error);
  28. }
  29. );
  30. /**
  31. * 响应拦截器
  32. */
  33. fetchApp.interceptors.response.use(
  34. response => {
  35. const data = response.data;
  36. //console.log(response);
  37. // 对响应数据做处理,例如只返回data部分
  38. if (response.status === 200) {
  39. if (data.code === 0) {
  40. } else if (data.code === 401) {
  41. Promise.reject(data);
  42. } else if (data.code === -1) {
  43. message.error(data.errorMsg);
  44. }
  45. } else {
  46. notification.open({
  47. message: '提示',
  48. description: data.errorMsg,
  49. icon: () => h(ExclamationCircleOutlined, { style: 'color: #ff4d4f' }),
  50. onClick: () => {
  51. console.log('Notification Clicked!');
  52. }
  53. });
  54. }
  55. return data;
  56. },
  57. error => {
  58. // 响应错误处理
  59. console.log('err' + error); // for debug
  60. return Promise.reject(error);
  61. }
  62. );
  63. export default fetchApp;