zouzs hace 1 semana
padre
commit
09e9c3985a
Se han modificado 2 ficheros con 92 adiciones y 23 borrados
  1. 83 19
      src/utils/dict.ts
  2. 9 4
      src/views/ipInterfaceCall/index.vue

+ 83 - 19
src/utils/dict.ts

@@ -27,10 +27,50 @@ export function useDict(...args: string[]) {
   return toRefs<any>(res.value);
 }
 
-export function useDictValue<T>(dictType: T, searchValue: string) {
+/**
+ * 统一的字典查询核心方法
+ * @param dictType 字典类型
+ * @param searchValue 查询值
+ * @param property 要返回的属性名 ('label' | 'class' | 'value' 等)
+ * @param isAsync 是否异步模式
+ * @returns 查询结果或Promise
+ */
+export async function getDictProperty<T>(
+  dictType: T,
+  searchValue: string,
+  property: string,
+  isAsync: boolean = false
+): Promise<string | null> {
   if (!isString(searchValue)) searchValue = String(searchValue);
   const dictStore = useDictStore();
-  // 统一触发获取(内存/持久层/后端),不做重复的预检查
+
+  if (isAsync) {
+    try {
+      await dictStore.fetchDictByType(dictType as string);
+    } catch (error) {
+      console.error(`获取字典${String(dictType)}失败:`, error);
+    }
+  } else {
+    // 同步模式:触发获取但不等待
+    dictStore.fetchDictByType(dictType as string).catch(error => {
+      console.error(`获取字典${String(dictType)}失败:`, error);
+    });
+  }
+
+  const dicts = dictStore.getDict(dictType as string) || [];
+  const item = dicts.find((d: any) => d.value === searchValue);
+  return item ? item[property] : null;
+}
+
+// 保持向后兼容的同步方法
+export function useDictValue<T>(
+  dictType: T,
+  searchValue: string
+): string | null {
+  // 对于同步调用,直接从store获取当前可用数据
+  if (!isString(searchValue)) searchValue = String(searchValue);
+  const dictStore = useDictStore();
+  // 触发获取但不等待
   dictStore.fetchDictByType(dictType as string).catch(error => {
     console.error(`获取字典${String(dictType)}失败:`, error);
   });
@@ -39,10 +79,13 @@ export function useDictValue<T>(dictType: T, searchValue: string) {
   return item ? item.label : null;
 }
 
-export function useDictClass<T>(dictType: T, searchValue: string) {
+export function useDictClass<T>(
+  dictType: T,
+  searchValue: string
+): string | null {
   if (!isString(searchValue)) searchValue = String(searchValue);
   const dictStore = useDictStore();
-  // 统一触发获取(内存/持久层/后端),不做重复的预检查
+  // 触发获取但不等待
   dictStore.fetchDictByType(dictType as string).catch(error => {
     console.error(`获取字典${String(dictType)}失败:`, error);
   });
@@ -51,34 +94,55 @@ export function useDictClass<T>(dictType: T, searchValue: string) {
   return item ? item.class : null;
 }
 
+// 异步方法使用统一的核心方法
 export async function useDictValueAsync<T>(
   dictType: T,
   searchValue: string
 ): Promise<string | null> {
-  if (!isString(searchValue)) searchValue = String(searchValue);
-  const dictStore = useDictStore();
-  try {
-    await dictStore.fetchDictByType(dictType as string);
-  } catch (error) {
-    console.error(`获取字典${String(dictType)}失败:`, error);
-  }
-  const dicts = dictStore.getDict(dictType as string) || [];
-  const item = dicts.find((d: any) => d.value === searchValue);
-  return item ? item.label : null;
+  return getDictProperty(dictType, searchValue, "label", true);
 }
 
 export async function useDictClassAsync<T>(
   dictType: T,
   searchValue: string
 ): Promise<string | null> {
+  return getDictProperty(dictType, searchValue, "class", true);
+}
+
+/**
+ * 通用的字典属性查询方法(新增)
+ * @param dictType 字典类型
+ * @param searchValue 查询值
+ * @param property 要返回的属性名
+ * @returns Promise<string | null>
+ */
+export async function useDictPropertyAsync<T>(
+  dictType: T,
+  searchValue: string,
+  property: string
+): Promise<string | null> {
+  return getDictProperty(dictType, searchValue, property, true);
+}
+
+/**
+ * 通用的字典属性查询方法(同步版本)
+ * @param dictType 字典类型
+ * @param searchValue 查询值
+ * @param property 要返回的属性名
+ * @returns string | null
+ */
+export function useDictProperty<T>(
+  dictType: T,
+  searchValue: string,
+  property: string
+): string | null {
   if (!isString(searchValue)) searchValue = String(searchValue);
   const dictStore = useDictStore();
-  try {
-    await dictStore.fetchDictByType(dictType as string);
-  } catch (error) {
+  // 触发获取但不等待
+  dictStore.fetchDictByType(dictType as string).catch(error => {
     console.error(`获取字典${String(dictType)}失败:`, error);
-  }
+  });
   const dicts = dictStore.getDict(dictType as string) || [];
   const item = dicts.find((d: any) => d.value === searchValue);
-  return item ? item.cssClass : null;
+  return item ? item[property] : null;
 }

+ 9 - 4
src/views/ipInterfaceCall/index.vue

@@ -32,7 +32,12 @@ import { isString } from "@pureadmin/utils";
 import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
 import { useRouter } from "vue-router";
 import { getIpInterfaceCallList } from "@/api/system/ipInterfaceCall";
-import { useDict, useDictClass, useDictValue } from "@/utils/dict";
+import {
+  useDict,
+  useDictClass,
+  useDictClassAsync,
+  useDictValue
+} from "@/utils/dict";
 
 defineOptions({
   name: "IpInterfaceCallIndex"
@@ -42,7 +47,7 @@ const router = useRouter();
 
 const plusPageInstance = ref<PlusPageInstance | null>(null);
 
-const { interface_status } = useDict("interface_status");
+// const { interface_status } = useDict("interface_status");
 
 const getList = async (query: Record<string, any>) => {
   let res = await getIpInterfaceCallList(query);
@@ -81,9 +86,9 @@ const tableConfig: PlusColumn[] = [
     hideInSearch: true,
     valueType: "tag",
     // options: computed(() => interface_status.value),
-    fieldProps: (value: string) => {
+    fieldProps: async (value: string) => {
       return {
-        type: useDictClass("interface_status", value)
+        type: await useDictClassAsync("interface_status", value)
       };
     },
     formatter: value => {