init ai api key

This commit is contained in:
xie7654
2025-07-10 15:32:00 +08:00
parent bf07ff5744
commit 65bdda6377
26 changed files with 1825 additions and 6 deletions

View File

@@ -0,0 +1,7 @@
{
"title": "AI Management",
"ai_api_key": {
"title": "API KEY",
"name": "API KEY"
}
}

View File

@@ -0,0 +1,7 @@
{
"title": "AI大模型",
"ai_api_key": {
"title": "API 密钥",
"name": "API 密钥"
}
}

View File

@@ -0,0 +1,24 @@
import { BaseModel } from '#/models/base';
export namespace AiAIApiKeyApi {
export interface AiAIApiKey {
id: number;
remark: string;
creator: string;
modifier: string;
update_time: string;
create_time: string;
is_deleted: boolean;
name: string;
platform: string;
api_key: string;
url: string;
status: number;
}
}
export class AiAIApiKeyModel extends BaseModel<AiAIApiKeyApi.AiAIApiKey> {
constructor() {
super('/ai/ai_api_key/');
}
}

View File

@@ -0,0 +1,184 @@
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { AiAIApiKeyApi } from '#/models/ai/ai_api_key';
import { z } from '#/adapter/form';
import { $t } from '#/locales';
import { op } from '#/utils/permission';
/**
* 获取编辑表单的字段配置
*/
export function useSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'name',
label: 'name',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['name']))
.max(100, $t('ui.formRules.maxLength', ['name', 100])),
},
{
component: 'Input',
fieldName: 'platform',
label: 'platform',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['platform']))
.max(100, $t('ui.formRules.maxLength', ['platform', 100])),
},
{
component: 'Input',
fieldName: 'api_key',
label: 'api key',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['api key']))
.max(100, $t('ui.formRules.maxLength', ['api key', 100])),
},
{
component: 'Input',
fieldName: 'url',
label: 'url',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['url']))
.max(100, $t('ui.formRules.maxLength', ['url', 100])),
},
{
component: 'InputNumber',
fieldName: 'status',
label: '状态',
},
{
component: 'Input',
fieldName: 'remark',
label: 'remark',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['remark']))
.max(100, $t('ui.formRules.maxLength', ['remark', 100])),
},
];
}
/**
* 获取编辑表单的字段配置
*/
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'name',
label: 'name',
},
{
component: 'Input',
fieldName: 'platform',
label: 'platform',
},
{
component: 'Input',
fieldName: 'api_key',
label: 'api key',
},
{
component: 'Input',
fieldName: 'url',
label: 'url',
},
{
component: 'InputNumber',
fieldName: 'status',
label: '状态',
},
{
component: 'Input',
fieldName: 'remark',
label: 'remark',
},
];
}
/**
* 获取表格列配置
* @description 使用函数的形式返回列数据而不是直接export一个Array常量是为了响应语言切换时重新翻译表头
* @param onActionClick 表格操作按钮点击事件
*/
export function useColumns(
onActionClick?: OnActionClickFn<AiAIApiKeyApi.AiAIApiKey>,
): VxeTableGridOptions<AiAIApiKeyApi.AiAIApiKey>['columns'] {
return [
{
field: 'id',
title: 'ID',
},
{
field: 'name',
title: 'name',
},
{
field: 'platform',
title: 'platform',
},
{
field: 'api_key',
title: 'api key',
},
{
field: 'url',
title: 'url',
},
{
field: 'status',
title: '状态',
},
{
field: 'remark',
title: 'remark',
},
{
field: 'creator',
title: 'creator',
},
{
field: 'modifier',
title: 'modifier',
},
{
field: 'update_time',
title: 'update time',
},
{
field: 'create_time',
title: 'create time',
},
{
field: 'is_deleted',
title: 'is deleted',
},
{
align: 'center',
cellRender: {
attrs: {
nameField: 'name',
nameTitle: $t('ai.ai_api_key.name'),
onClick: onActionClick,
},
name: 'CellOperation',
options: [
op('ai:ai_api_key:edit', 'edit'),
op('ai:ai_api_key:delete', 'delete'),
],
},
field: 'action',
fixed: 'right',
title: '操作',
width: 120,
},
];
}

View File

@@ -0,0 +1,141 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { AiAIApiKeyApi } from '#/models/ai/ai_api_key';
import { Page, useVbenModal } from '@vben/common-ui';
import { Plus } from '@vben/icons';
import { Button, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { $t } from '#/locales';
import { AiAIApiKeyModel } from '#/models/ai/ai_api_key';
import { useColumns, useGridFormSchema } from './data';
import Form from './modules/form.vue';
const formModel = new AiAIApiKeyModel();
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
/**
* 编辑AI API 密钥
*/
function onEdit(row: AiAIApiKeyApi.AiAIApiKey) {
formModalApi.setData(row).open();
}
/**
* 创建新AI API 密钥
*/
function onCreate() {
formModalApi.setData(null).open();
}
/**
* 删除AI API 密钥
*/
function onDelete(row: AiAIApiKeyApi.AiAIApiKey) {
const hideLoading = message.loading({
content: '删除AI API 密钥',
duration: 0,
key: 'action_process_msg',
});
formModel
.delete(row.id)
.then(() => {
message.success({
content: '删除成功',
key: 'action_process_msg',
});
refreshGrid();
})
.catch(() => {
hideLoading();
});
}
/**
* 表格操作按钮的回调函数
*/
function onActionClick({
code,
row,
}: OnActionClickParams<AiAIApiKeyApi.AiAIApiKey>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
submitOnChange: true,
},
gridEvents: {},
gridOptions: {
columns: useColumns(onActionClick),
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: true,
},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await formModel.list({
page: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
toolbarConfig: {
custom: true,
export: false,
refresh: { code: 'query' },
zoom: true,
search: true,
},
} as VxeTableGridOptions,
});
/**
* 刷新表格
*/
function refreshGrid() {
gridApi.query();
}
</script>
<template>
<Page auto-content-height>
<FormModal @success="refreshGrid" />
<Grid table-title="AI API 密钥">
<template #toolbar-tools>
<Button
type="primary"
@click="onCreate"
v-permission="'ai:ai_api_key:create'"
>
<Plus class="size-5" />
{{ $t('ui.actionTitle.create', [$t('ai.ai_api_key.name')]) }}
</Button>
</template>
</Grid>
</Page>
</template>

View File

@@ -0,0 +1,79 @@
<script lang="ts" setup>
import type { AiAIApiKeyApi } from '#/models/ai/ai_api_key';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { Button } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { $t } from '#/locales';
import { AiAIApiKeyModel } from '#/models/ai/ai_api_key';
import { useSchema } from '../data';
const emit = defineEmits(['success']);
const formModel = new AiAIApiKeyModel();
const formData = ref<AiAIApiKeyApi.AiAIApiKey>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', [$t('ai.aiapikey.name')])
: $t('ui.actionTitle.create', [$t('ai.aiapikey.name')]);
});
const [Form, formApi] = useVbenForm({
layout: 'horizontal',
schema: useSchema(),
showDefaultActions: false,
});
function resetForm() {
formApi.resetForm();
formApi.setValues(formData.value || {});
}
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (valid) {
modalApi.lock();
const data = await formApi.getValues();
try {
await (formData.value?.id
? formModel.update(formData.value.id, data)
: formModel.create(data));
await modalApi.close();
emit('success');
} finally {
modalApi.lock(false);
}
}
},
onOpenChange(isOpen) {
if (isOpen) {
const data = modalApi.getData<AiAIApiKeyApi.AiAIApiKey>();
if (data) {
formData.value = data;
formApi.setValues(formData.value);
}
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form />
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
</div>
</template>
</Modal>
</template>
<style lang="css" scoped></style>