import { defineStore } from 'pinia'; import { AppStoreState, ConfigItem, DictItem, PickerItem } from '@/types'; import { getConfig, getProvinces } from '@/api/modules/system'; import { useDictStore } from '@/store/dictStore'; import { EnumDictName } from '@/common/enum'; import { useUserStore } from '@/store/userStore'; const preloadDicts: string[] = [ EnumDictName.EXAM_TYPE ]; export const useAppStore = defineStore('ie-app', { state: (): AppStoreState => { return { isInitialized: false, activeTabbar: 0, statusBarHeight: 0, sysInfo: null as UniApp.GetSystemInfoResult | null, appConfig: [] as ConfigItem[], } }, getters: { systemInfo(): UniApp.GetSystemInfoResult { if (!this.sysInfo) { return uni.getSystemInfoSync(); } return this.sysInfo; } }, actions: { async init() { this.clear(); try { const dictStore = useDictStore(); // 预加载字典数据 dictStore.loadDicts(preloadDicts).then(finish => { }); const userStore = useUserStore(); if (userStore.isLogin) { await userStore.getUserInfo(); } this.isInitialized = true; this.sysInfo = uni.getSystemInfoSync(); await this.loadPreloadData(); return Promise.resolve(true); } catch (error) { console.error('初始化数据失败:', error); return Promise.resolve(false); } }, /** * 加载前置必要数据 */ async loadPreloadData() { await this.loadConfig(); }, /** * 加载参数配置 */ async loadConfig() { const { data } = await getConfig(); const list: ConfigItem[] = []; data.forEach(item => { // 只保留需要的字段 list.push({ configKey: item.configKey, configValue: item.configValue, configType: item.configType, configName: item.configName, configId: item.configId, } as ConfigItem); }); this.appConfig = list; }, /** * 获取应用配置项的值 * @param key 配置项的 key * @returns 配置项的 value,如果不存在则返回 undefined */ getAppConfig(key: string): string | undefined { return this.appConfig.find(item => item.configKey === key)?.configValue; }, setActiveTabbar(index: number) { this.activeTabbar = index; }, clear() { this.isInitialized = false; } }, persist: { storage: { getItem: uni.getStorageSync, setItem: uni.setStorageSync, }, // 不需要持久化的 // 'appConfig' omit: [], } });