api.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import axios from 'axios';
  2. export default {
  3. send: (options) => {
  4. axios.post(options.url, options.data).
  5. then((response) => {
  6. const data = response.data;
  7. if (!data || data.code !== 0) {
  8. options.error && options.error(data && data.msg);
  9. return;
  10. }
  11. options.success && options.success(data);
  12. }).
  13. catch((e) => {
  14. console.error(e);
  15. options.error && options.error();
  16. });
  17. },
  18. read: (options) => {
  19. axios.get(options.url).
  20. then((response) => {
  21. const data = response.data;
  22. if (!data || data.code !== 0) {
  23. options.error && options.error(data && data.msg);
  24. return;
  25. }
  26. options.success && options.success(data.data.map((item) => ({
  27. time: item[0],
  28. type: item[1],
  29. color: item[2],
  30. author: item[3],
  31. text: item[4]
  32. })));
  33. }).
  34. catch((e) => {
  35. console.error(e);
  36. options.error && options.error();
  37. });
  38. }
  39. };