sysConfig.ts 967 Bytes
const sysConfigStore = defineStore(
  // 唯一ID
  'config',
  () => {
    let configMap: any = {};

    // 封装请求配置文件的函数
    const loadConfig = async () => {
      try {
        const response = await fetch('/config.json');
        if (!response.ok) {
          throw new Error(`请求配置失败,状态码: ${response.status}`);
        }
        const config = await response.json();
        return config;
      } catch (error) {
        console.error('加载配置时出错:', error);
        throw error;
      }
    };
    const setConfig = (val) => {
      configMap = val
    }

    const getConfig = (field) => {
      if (import.meta.env.MODE == 'nginx' || import.meta.env.MODE == 'development') {
        return import.meta.env.VITE_appKey
      }
      return field ? configMap[field] : configMap;
    }

    return {
      configMap,
      loadConfig,
      setConfig,
      getConfig
    }
  },
)

export default sysConfigStore