api.ts 980 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. export function randomID(length = 6) {
  2. return Number(
  3. Math.random().toString().substr(3, length) + Date.now()
  4. ).toString(36);
  5. }
  6. const COLORS = ["#409EFF", "#67C23A", "#E6A23C", "#F56C6C", "#909399"];
  7. function getRandomNum(min: number, max: number) {
  8. return Math.floor(Math.random() * (max - min + 1)) + min;
  9. }
  10. function randomColor() {
  11. return COLORS[getRandomNum(0, 4)];
  12. }
  13. const website = "https://wanderprints.com";
  14. export const getList = ({ page = 1, pageSize = 20 }) => {
  15. const url = `${website}/products.json?page=${page}&limit=${pageSize}`;
  16. return fetch(url)
  17. .then(res => res.json())
  18. .then(res => res.products)
  19. .then(res => {
  20. return res.map((item: any) => {
  21. return {
  22. id: randomID(),
  23. star: false,
  24. price: item.variants[0].price,
  25. src: {
  26. original: item.images[0].src
  27. },
  28. backgroundColor: randomColor(),
  29. name: item.title
  30. };
  31. });
  32. });
  33. };