axios_instance.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const axios_instance = axios.create({
  2. baseURL: baseUrl + "home",
  3. timeout: 30000,
  4. headers: {'X-Requested-With':'XMLHttpRequest'},
  5. withCredentials: true,
  6. });
  7. async function postJson(url,data,error) {
  8. return new Promise((resolve, reject) => {
  9. axios_instance.post(url, JSON.stringify(data), {
  10. headers: { 'Content-Type': 'application/json;charset=UTF-8' }
  11. }).then((result) => {
  12. const data = result.data;
  13. if (data.code === 1) {
  14. if (typeof error === 'function') {
  15. error(data);
  16. return false;
  17. } else {
  18. ElMessage.error(data.msg);
  19. return false;
  20. }
  21. } else if (data.code === 401) {
  22. location.href = '/home/login/login';
  23. }
  24. return resolve(data);
  25. });
  26. });
  27. }
  28. async function postFile(url,data,error) {
  29. return new Promise((resolve, reject) => {
  30. axios_instance.post(url, data, {
  31. headers: { 'Content-Type': 'application/form-data' }
  32. }).then((result) => {
  33. const data = result.data;
  34. if (data.code === 1) {
  35. if (typeof error === 'function') {
  36. error(data);
  37. return false;
  38. } else {
  39. ElMessage.error(data.msg);
  40. return false;
  41. }
  42. } else if (data.code === 401) {
  43. location.href = '/home/login/login';
  44. }
  45. return resolve(data);
  46. });
  47. });
  48. }