|
|
@@ -30,6 +30,10 @@ export function useDict(...args: string[]) {
|
|
|
export function useDictValue<T>(dictType: T, searchValue: string) {
|
|
|
if (!isString(searchValue)) searchValue = String(searchValue);
|
|
|
const dictStore = useDictStore();
|
|
|
+ // 统一触发获取(内存/持久层/后端),不做重复的预检查
|
|
|
+ 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;
|
|
|
@@ -38,7 +42,43 @@ export function useDictValue<T>(dictType: T, searchValue: string) {
|
|
|
export function useDictClass<T>(dictType: T, searchValue: string) {
|
|
|
if (!isString(searchValue)) searchValue = String(searchValue);
|
|
|
const dictStore = useDictStore();
|
|
|
+ // 统一触发获取(内存/持久层/后端),不做重复的预检查
|
|
|
+ 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.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;
|
|
|
+}
|
|
|
+
|
|
|
+export async function useDictClassAsync<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.cssClass : null;
|
|
|
+}
|