sysConfig.ts 1.49 KB
import { getAppKey } from '@/api/modules/queryService'
import { ElMessage } from 'element-plus';


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

    /** 动态appkey值,从接口获取 */
    let appKey = '';

    const loadingAppKey = () => {
      getAppKey().then((res: any) => {
        if (res?.code == '00000') {
          appKey = res.data;
        } else {
          ElMessage.error('appKey获取失败');
        }
      })
    }
    // 封装请求配置文件的函数
    const loadConfig = async () => {
      try {
        const response = await fetch('/config.json');
        if (!response.ok) {
          throw new Error(`请求配置失败,状态码: ${response.status}`);
        }
        const config = await response.json();
        setConfig(config);
        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) || '69afd501e4b08251f6cf6419';
    }

    const getAppKeyValue = () => {
      return appKey;
    }

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

export default sysConfigStore