Prechádzať zdrojové kódy

Refactor base constants handling in API and store

- Updated the common API to use the new BaseConstsConfig type for fetching configuration data.
- Refined the useFindPartnerPopup composable to leverage the BaseConstsConfig type, improving type safety.
- Enhanced the baseConsts store to correctly handle the BaseConstsConfig structure, ensuring accurate data management.
- Introduced new types in the api/common.ts file to define the structure of the configuration data returned from the backend.
0es 3 mesiacov pred
rodič
commit
2cd824d180

+ 4 - 3
app/api/common.ts

@@ -4,15 +4,16 @@
  */
 
 import { http } from '~/utils/request'
-import type { ResultVOMapObjectObject } from '~/types/api'
+import type { BaseConstsConfig } from '~/types/api'
 
 export const commonApi = {
   /**
    * 获取通用常量配置
    * 对应后端接口:POST /base/consts/config
-   * 免登录,按后端要求使用 application/x-www-form-urlencoded
+   * 免登录
    */
   getBaseConstsConfig() {
-    return http.post<ResultVOMapObjectObject>('/base/consts/config')
+    // NOTE: http 拦截器会在 code===0 时将响应解包为 ApiResponse.data
+    return http.post<BaseConstsConfig>('/base/consts/config')
   },
 }

+ 4 - 18
app/composables/useFindPartnerPopup.ts

@@ -1,5 +1,6 @@
 import { onUnmounted, reactive, readonly, watch } from 'vue'
 import { useBaseConstsStore } from '~/stores/baseConsts'
+import type { BaseConstsConfig } from '~/types/api'
 
 export interface FindPartnerTag {
   id: string
@@ -25,15 +26,6 @@ export interface FindPartnerPopupState {
   selected: Record<FindPartnerSection['id'], string | undefined>
 }
 
-interface CommonAreaItem {
-  code: string
-  name: string
-}
-
-interface BaseConstsConfig {
-  commonAreaConsts?: CommonAreaItem[][] | CommonAreaItem[]
-}
-
 export interface FindPartnerFilterPayload {
   /** 后端 gender 参数 */
   gender: number
@@ -274,18 +266,12 @@ const setupRegionTagsFromBaseConsts = () => {
 
   const baseConstsStore = useBaseConstsStore()
 
-  const updateRegionTags = (config: Record<string, unknown> | null) => {
+  const updateRegionTags = (config: BaseConstsConfig | null) => {
     const regionSection = state.sections.find(section => section.id === 'region')
     if (!regionSection)
       return
 
-    const typedConfig = config as BaseConstsConfig | null
-    const raw = typedConfig?.commonAreaConsts
-
-    if (!raw)
-      return
-
-    const list = Array.isArray(raw[0]) ? raw[0] as CommonAreaItem[] : raw as CommonAreaItem[]
+    const list = config?.commonAreaConsts
 
     if (!Array.isArray(list) || list.length === 0)
       return
@@ -305,7 +291,7 @@ const setupRegionTagsFromBaseConsts = () => {
   watch(
     () => baseConstsStore.config,
     (config) => {
-      updateRegionTags(config as Record<string, unknown> | null)
+      updateRegionTags(config)
     },
     {
       immediate: true,

+ 5 - 4
app/stores/baseConsts.ts

@@ -1,9 +1,10 @@
 import { defineStore } from 'pinia'
 import { commonApi } from '~/api/common'
+import type { BaseConstsConfig } from '~/types/api'
 
 interface BaseConstsState {
-  /** 通用常量配置(后端返回的 result 对象) */
-  config: Record<string, unknown> | null
+  /** 通用常量配置(后端 /base/consts/config 返回的 data 对象) */
+  config: BaseConstsConfig | null
   /** 是否正在加载配置 */
   loading: boolean
   /** 是否已经初始化过(避免重复请求) */
@@ -30,8 +31,8 @@ export const useBaseConstsStore = defineStore('baseConsts', {
       try {
         const res = await commonApi.getBaseConstsConfig()
 
-        // http 拦截器已将外层 ApiResponse.data 解包,这里的 res 即为 ResultVOMapObjectObject
-        this.config = res?.result ?? {}
+        // http 拦截器已将外层 ApiResponse.data 解包,这里的 res 即为 BaseConstsConfig
+        this.config = res ?? {}
         this.initialized = true
       }
       catch (error) {

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

@@ -5,6 +5,20 @@ export interface ApiResponse<T = unknown> {
   msg: string
 }
 
+/**
+ * /base/consts/config 返回的 data 结构
+ */
+
+export interface CommonAreaConstItem {
+  code: string
+  name: string
+}
+
+export interface BaseConstsConfig {
+  commonAreaConsts?: CommonAreaConstItem[]
+  [key: string]: unknown
+}
+
 // Request config type
 export interface RequestConfig {
   method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
@@ -28,13 +42,3 @@ export interface PaginationResponse<T> {
   page: number
   pageSize: number
 }
-
-/**
- * 通用 Map 结构结果
- * 对应后端:ResultVOMapObjectObject.result
- */
-export interface ResultVOMapObjectObject {
-  /** 结果对象,内部结构由后端约定 */
-  result: Record<string, unknown>
-}
-