优化全局注入dict_data

This commit is contained in:
XIE7654
2025-07-16 13:56:02 +08:00
parent baa650ea64
commit 3fe272ffca
11 changed files with 219 additions and 24 deletions

View File

@@ -7,12 +7,17 @@ import { preferences, usePreferences } from '@vben/preferences';
import { App, ConfigProvider, theme } from 'ant-design-vue';
import { antdLocale } from '#/locales';
import { useDictStore } from '#/store/dict';
defineOptions({ name: 'App' });
const { isDark } = usePreferences();
const { tokens } = useAntdDesignTokens();
const dictStore = useDictStore();
dictStore.fetchDictData();
const tokenTheme = computed(() => {
const algorithm = isDark.value
? [theme.darkAlgorithm]

View File

@@ -0,0 +1,23 @@
import { useDictStore } from '#/store/dict';
export function useDictOptions(dictType: string) {
const dictStore = useDictStore();
return dictStore.getOptionsByType(dictType);
}
/**
* 通用字典 value 转 label
* @param dictType 字典类型
* @param value 字典值
* @returns label
*/
export function useDictLabel(dictType: string, value: any): string {
const options = useDictOptions(dictType);
const item = options.find((opt) => opt.value === value);
return item ? item.label : value;
}
export function dictFormatter(dictType: string) {
return ({ cellValue }: { cellValue: any }) =>
useDictLabel(dictType, cellValue);
}

View File

@@ -11,6 +11,7 @@ export namespace AiAIApiKeyApi {
is_deleted: boolean;
name: string;
platform: string;
model_type: string;
api_key: string;
url: string;
status: number;

View File

@@ -0,0 +1,18 @@
// src/store/dict.ts
import { defineStore } from 'pinia';
import { getDictDataSimple } from '#/api/system/dict_data'; // 根据实际路径调整
export const useDictStore = defineStore('dict', {
state: () => ({
dictData: [] as any[],
}),
actions: {
async fetchDictData() {
this.dictData = await getDictDataSimple(); // 根据接口返回结构调整
},
getOptionsByType(type: string) {
return this.dictData.filter((item) => item.dict_type === type);
},
},
});

View File

@@ -28,8 +28,8 @@ export function useSchema(): VbenFormSchema[] {
label: '平台',
componentProps: {
options: PLATFORM_OPTIONS,
style: { minWidth: '180px' },
dropdownStyle: { minWidth: '180px' },
class: 'w-full',
placeholder: '请选择',
},
rules: z.string(),
},

View File

@@ -5,6 +5,7 @@ import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { AiAIModelApi } from '#/models/ai/ai_model';
import { z } from '#/adapter/form';
import { dictFormatter, useDictOptions } from '#/hooks/useDictOptions';
import { $t } from '#/locales';
import { AiAIApiKeyModel } from '#/models/ai/ai_api_key';
import { op } from '#/utils/permission';
@@ -27,15 +28,27 @@ export function useSchema(): VbenFormSchema[] {
},
fieldName: 'key',
label: 'API 秘钥',
rules: z.number(),
},
{
component: 'Input',
component: 'ApiSelect',
fieldName: 'platform',
componentProps: {
options: useDictOptions('ai_platform'),
class: 'w-full',
},
label: '模型平台',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['模型平台']))
.max(100, $t('ui.formRules.maxLength', ['模型平台', 100])),
rules: z.string(),
},
{
component: 'ApiSelect',
fieldName: 'model_type',
componentProps: {
options: useDictOptions('ai_model_type'),
class: 'w-full',
},
label: '模型类型',
rules: z.string(),
},
{
component: 'Input',
@@ -59,6 +72,9 @@ export function useSchema(): VbenFormSchema[] {
component: 'InputNumber',
fieldName: 'sort',
label: '排序',
componentProps: {
class: 'w-full',
},
},
{
component: 'RadioGroup',
@@ -82,12 +98,18 @@ export function useSchema(): VbenFormSchema[] {
{
component: 'InputNumber',
fieldName: 'max_tokens',
label: '回复Token 数',
label: '回复Token数',
componentProps: {
class: 'w-full',
},
},
{
component: 'InputNumber',
fieldName: 'max_contexts',
label: '上下文数量',
componentProps: {
class: 'w-full',
},
},
{
component: 'Input',
@@ -162,6 +184,12 @@ export function useColumns(
{
field: 'platform',
title: '模型平台',
formatter: dictFormatter('ai_platform'),
},
{
field: 'model_type',
title: '模型类型',
formatter: dictFormatter('ai_model_type'),
},
{
field: 'model',