|
|
@@ -0,0 +1,46 @@
|
|
|
+import { defineStore } from 'pinia'
|
|
|
+import { commonApi } from '~/api/common'
|
|
|
+
|
|
|
+interface BaseConstsState {
|
|
|
+ /** 通用常量配置(后端返回的 result 对象) */
|
|
|
+ config: Record<string, unknown> | null
|
|
|
+ /** 是否正在加载配置 */
|
|
|
+ loading: boolean
|
|
|
+ /** 是否已经初始化过(避免重复请求) */
|
|
|
+ initialized: boolean
|
|
|
+}
|
|
|
+
|
|
|
+export const useBaseConstsStore = defineStore('baseConsts', {
|
|
|
+ state: (): BaseConstsState => ({
|
|
|
+ config: null,
|
|
|
+ loading: false,
|
|
|
+ initialized: false,
|
|
|
+ }),
|
|
|
+
|
|
|
+ actions: {
|
|
|
+ /**
|
|
|
+ * 加载通用常量配置
|
|
|
+ * 默认只在首次调用时请求,可以通过 force=true 强制刷新
|
|
|
+ */
|
|
|
+ async loadConfig(force = false) {
|
|
|
+ if (this.initialized && !force)
|
|
|
+ return
|
|
|
+
|
|
|
+ this.loading = true
|
|
|
+ try {
|
|
|
+ const res = await commonApi.getBaseConstsConfig()
|
|
|
+
|
|
|
+ // http 拦截器已将外层 ApiResponse.data 解包,这里的 res 即为 ResultVOMapObjectObject
|
|
|
+ this.config = res?.result ?? {}
|
|
|
+ this.initialized = true
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ // 业务错误已在 http 拦截器统一处理,这里仅记录日志
|
|
|
+ console.error('Failed to load base consts config:', error)
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ this.loading = false
|
|
|
+ }
|
|
|
+ },
|
|
|
+ },
|
|
|
+})
|