axios_instance.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. ElementPlus.ElMessage(data.msg);
  19. return false;
  20. }
  21. }
  22. return resolve(data);
  23. });
  24. });
  25. }
  26. async function postFile(url,data,error) {
  27. return new Promise((resolve, reject) => {
  28. axios_instance.post(url, data, {
  29. headers: { 'Content-Type': 'application/form-data' }
  30. }).then((result) => {
  31. const data = result.data;
  32. if (data.code === 1) {
  33. if (typeof error === 'function') {
  34. error(data);
  35. return false;
  36. } else {
  37. ElementPlus.ElMessage(data.msg);
  38. return false;
  39. }
  40. }
  41. return resolve(data);
  42. });
  43. });
  44. }