Prechádzať zdrojové kódy

Add ResultVOMapObjectObject interface for handling generic map structure results

- Introduced `ResultVOMapObjectObject` interface in `common.ts` to represent a generic map structure for API responses, aligning with backend specifications for improved data handling.
0es 4 mesiacov pred
rodič
commit
c15ef5683b

+ 18 - 0
app/api/common.ts

@@ -0,0 +1,18 @@
+/**
+ * Common / Base config API module
+ * 通用常量配置等相关接口
+ */
+
+import { http } from '~/utils/request'
+import type { ResultVOMapObjectObject } from '~/types/api'
+
+export const commonApi = {
+  /**
+   * 获取通用常量配置
+   * 对应后端接口:POST /base/consts/config
+   * 免登录,按后端要求使用 application/x-www-form-urlencoded
+   */
+  getBaseConstsConfig() {
+    return http.post<ResultVOMapObjectObject>('/base/consts/config')
+  },
+}

+ 12 - 0
app/plugins/baseConsts.client.ts

@@ -0,0 +1,12 @@
+export default defineNuxtPlugin({
+  name: 'baseConsts',
+  setup(nuxtApp) {
+    const baseConstsStore = useBaseConstsStore()
+
+    // 在应用挂载后拉取通用常量配置(仅调用一次)
+    nuxtApp.hook('app:mounted', () => {
+      // fire and forget,错误在 http 拦截器中统一处理
+      baseConstsStore.loadConfig()
+    })
+  },
+})

+ 46 - 0
app/stores/baseConsts.ts

@@ -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
+      }
+    },
+  },
+})

+ 10 - 0
app/types/api/common.ts

@@ -28,3 +28,13 @@ export interface PaginationResponse<T> {
   page: number
   pageSize: number
 }
+
+/**
+ * 通用 Map 结构结果
+ * 对应后端:ResultVOMapObjectObject.result
+ */
+export interface ResultVOMapObjectObject {
+  /** 结果对象,内部结构由后端约定 */
+  result: Record<string, unknown>
+}
+