| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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';
- import { usePay } from '@/hooks/usePay';
- 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();
- const { getPrice } = usePay();
- // 预加载字典数据
- dictStore.loadDicts(preloadDicts).then(finish => { });
- const userStore = useUserStore();
- if (userStore.isLogin) {
- await userStore.getUserInfo();
- getPrice();
- }
- 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;
- },
- loadFromLocal(state: string) {
- const value = JSON.parse(state);
- if (value) {
- const { appConfig, activeTabbar, statusBarHeight, sysInfo, isInitialized } = value;
- this.appConfig = appConfig || [];
- this.activeTabbar = activeTabbar || 0;
- this.statusBarHeight = statusBarHeight || 0;
- this.sysInfo = sysInfo || null;
- this.isInitialized = isInitialized || false;
- return true;
- }
- return false;
- }
- },
- persist: {
- storage: {
- getItem: uni.getStorageSync,
- setItem: uni.setStorageSync,
- },
- // 不需要持久化的
- // 'appConfig'
- omit: [],
- }
- });
|