context.jsx 1.26 KB
Newer Older
Maofei94's avatar
Maofei94 committed
1
import { getImageBases } from '@/services/common/api';
张烨's avatar
张烨 committed
2 3 4
import React, { useEffect, useState } from 'react';
const UploadContext = React.createContext();

张烨's avatar
张烨 committed
5 6 7 8 9 10 11
const sleep = time =>
  new Promise((res, rej) => {
    setTimeout(() => {
      res();
    }, time);
  });

张烨's avatar
张烨 committed
12 13 14 15
const PictureWallProvider = props => {
  const { children, ...rest } = props;
  const [imgBed, setImgBed] = useState([]);

张烨's avatar
张烨 committed
16
  const update = () =>
17
    getImageBases('icon,androidMenu,menuNew,logo,CityTemp')
张烨's avatar
张烨 committed
18 19
      .then(res => {
        if (res.code === 0) {
Maofei94's avatar
Maofei94 committed
20 21
          const { data } = res;
          let arr = data.filter(item => item.moduleName !== 'menu') || [];
Maofei94's avatar
Maofei94 committed
22 23 24 25 26 27 28
          data.forEach(item => {
            if (item.baseUrl && item.moduleName !== 'CityTemp') {
              if (!localStorage.getItem('pd2-baseUrl')) {
                localStorage.setItem('pd2-baseUrl', item.baseUrl);
              }
            }
          });
Maofei94's avatar
Maofei94 committed
29
          setImgBed(arr);
张烨's avatar
张烨 committed
30 31 32 33 34
        }
      })
      .catch(err => {
        // eslint-disable-next-line no-console
        console.error(err);
35
        return sleep(10000).then(update);
张烨's avatar
张烨 committed
36
      });
张烨's avatar
张烨 committed
37 38 39
  useEffect(() => {
    update();
  }, []);
40
  return <UploadContext.Provider value={{ imgBed, update }}>{children}</UploadContext.Provider>;
张烨's avatar
张烨 committed
41 42 43
};

export { UploadContext as default, PictureWallProvider };