DataFetcher.js 577 B

12345678910111213141516171819202122232425
  1. // 数据读取工具类
  2. class DataFetcher {
  3. constructor(basePath) {
  4. this.basePath = basePath;
  5. }
  6. // 读取文件的异步方法
  7. async fetchData(farmId, key) {
  8. const filePath = `${this.basePath}/${farmId}/${key}.json`;
  9. try {
  10. const response = await fetch(filePath);
  11. if (!response.ok) {
  12. throw new Error(`无法读取文件: ${filePath}`);
  13. }
  14. const data = await response.json();
  15. return data;
  16. } catch (error) {
  17. console.error(error);
  18. return null;
  19. }
  20. }
  21. }
  22. // 模块化导出
  23. module.exports = DataFetcher;