| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { defineStore } from 'pinia'
- import { commonApi } from '~/api/common'
- import type { BaseConstsConfig } from '~/types/api'
- interface BaseConstsState {
- /** 通用常量配置(后端 /base/consts/config 返回的 data 对象) */
- config: BaseConstsConfig | 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 即为 BaseConstsConfig
- this.config = res ?? {}
- this.initialized = true
- }
- catch (error) {
- // 业务错误已在 http 拦截器统一处理,这里仅记录日志
- console.error('Failed to load base consts config:', error)
- }
- finally {
- this.loading = false
- }
- },
- },
- })
|