appStore.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { defineStore } from 'pinia';
  2. import { AppStoreState, ConfigItem, DictItem, PickerItem } from '@/types';
  3. import { getConfig, getProvinces } from '@/api/modules/system';
  4. import { useDictStore } from '@/store/dictStore';
  5. import { EnumDictName } from '@/common/enum';
  6. import { useUserStore } from '@/store/userStore';
  7. import { usePay } from '@/hooks/usePay';
  8. const preloadDicts: string[] = [
  9. EnumDictName.EXAM_TYPE
  10. ];
  11. export const useAppStore = defineStore('ie-app', {
  12. state: (): AppStoreState => {
  13. return {
  14. isInitialized: false,
  15. activeTabbar: 0,
  16. statusBarHeight: 0,
  17. sysInfo: null as UniApp.GetSystemInfoResult | null,
  18. appConfig: [] as ConfigItem[],
  19. }
  20. },
  21. getters: {
  22. systemInfo(): UniApp.GetSystemInfoResult {
  23. if (!this.sysInfo) {
  24. return uni.getSystemInfoSync();
  25. }
  26. return this.sysInfo;
  27. }
  28. },
  29. actions: {
  30. async init() {
  31. this.clear();
  32. try {
  33. const dictStore = useDictStore();
  34. const { getPrice } = usePay();
  35. // 预加载字典数据
  36. dictStore.loadDicts(preloadDicts).then(finish => { });
  37. const userStore = useUserStore();
  38. if (userStore.isLogin) {
  39. await userStore.getUserInfo();
  40. getPrice();
  41. }
  42. this.isInitialized = true;
  43. this.sysInfo = uni.getSystemInfoSync();
  44. await this.loadPreloadData();
  45. return Promise.resolve(true);
  46. } catch (error) {
  47. console.error('初始化数据失败:', error);
  48. return Promise.resolve(false);
  49. }
  50. },
  51. /**
  52. * 加载前置必要数据
  53. */
  54. async loadPreloadData() {
  55. await this.loadConfig();
  56. },
  57. /**
  58. * 加载参数配置
  59. */
  60. async loadConfig() {
  61. const { data } = await getConfig();
  62. const list: ConfigItem[] = [];
  63. data.forEach(item => {
  64. // 只保留需要的字段
  65. list.push({
  66. configKey: item.configKey,
  67. configValue: item.configValue,
  68. configType: item.configType,
  69. configName: item.configName,
  70. configId: item.configId,
  71. } as ConfigItem);
  72. });
  73. this.appConfig = list;
  74. },
  75. /**
  76. * 获取应用配置项的值
  77. * @param key 配置项的 key
  78. * @returns 配置项的 value,如果不存在则返回 undefined
  79. */
  80. getAppConfig(key: string): string | undefined {
  81. return this.appConfig.find(item => item.configKey === key)?.configValue;
  82. },
  83. setActiveTabbar(index: number) {
  84. this.activeTabbar = index;
  85. },
  86. clear() {
  87. this.isInitialized = false;
  88. },
  89. loadFromLocal(state: string) {
  90. const value = JSON.parse(state);
  91. if (value) {
  92. const { appConfig, activeTabbar, statusBarHeight, sysInfo, isInitialized } = value;
  93. this.appConfig = appConfig || [];
  94. this.activeTabbar = activeTabbar || 0;
  95. this.statusBarHeight = statusBarHeight || 0;
  96. this.sysInfo = sysInfo || null;
  97. this.isInitialized = isInitialized || false;
  98. return true;
  99. }
  100. return false;
  101. }
  102. },
  103. persist: {
  104. storage: {
  105. getItem: uni.getStorageSync,
  106. setItem: uni.setStorageSync,
  107. },
  108. // 不需要持久化的
  109. // 'appConfig'
  110. omit: [],
  111. }
  112. });