提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<Dialog :title="dialogTitle" v-model="dialogVisible"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
:model="formData" |
|
6 |
:rules="formRules" |
|
7 |
label-width="100px" |
|
8 |
v-loading="formLoading" |
|
9 |
> |
|
10 |
<el-form-item label="角色名称" prop="name"> |
|
11 |
<el-input v-model="formData.name" placeholder="请输入角色名称" /> |
|
12 |
</el-form-item> |
|
13 |
<el-form-item label="角色头像" prop="avatar"> |
|
14 |
<UploadImg v-model="formData.avatar" height="60px" width="60px" /> |
|
15 |
</el-form-item> |
|
16 |
<el-form-item label="绑定模型" prop="modelId" v-if="!isUser"> |
|
17 |
<el-select v-model="formData.modelId" placeholder="请选择模型" clearable> |
|
18 |
<el-option |
|
19 |
v-for="chatModel in chatModelList" |
|
20 |
:key="chatModel.id" |
|
21 |
:label="chatModel.name" |
|
22 |
:value="chatModel.id" |
|
23 |
/> |
|
24 |
</el-select> |
|
25 |
</el-form-item> |
|
26 |
<el-form-item label="角色类别" prop="category" v-if="!isUser"> |
|
27 |
<el-input v-model="formData.category" placeholder="请输入角色类别" /> |
|
28 |
</el-form-item> |
|
29 |
<el-form-item label="角色描述" prop="description"> |
|
30 |
<el-input type="textarea" v-model="formData.description" placeholder="请输入角色描述" /> |
|
31 |
</el-form-item> |
|
32 |
<el-form-item label="角色设定" prop="systemMessage"> |
|
33 |
<el-input type="textarea" v-model="formData.systemMessage" placeholder="请输入角色设定" /> |
|
34 |
</el-form-item> |
|
35 |
<el-form-item label="是否公开" prop="publicStatus" v-if="!isUser"> |
|
36 |
<el-radio-group v-model="formData.publicStatus"> |
|
37 |
<el-radio |
|
38 |
v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)" |
|
39 |
:key="dict.value" |
|
40 |
:label="dict.value" |
|
41 |
> |
|
42 |
{{ dict.label }} |
|
43 |
</el-radio> |
|
44 |
</el-radio-group> |
|
45 |
</el-form-item> |
|
46 |
<el-form-item label="角色排序" prop="sort" v-if="!isUser"> |
|
47 |
<el-input-number v-model="formData.sort" placeholder="请输入角色排序" class="!w-1/1" /> |
|
48 |
</el-form-item> |
|
49 |
<el-form-item label="开启状态" prop="status" v-if="!isUser"> |
|
50 |
<el-radio-group v-model="formData.status"> |
|
51 |
<el-radio |
|
52 |
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" |
|
53 |
:key="dict.value" |
|
54 |
:label="dict.value" |
|
55 |
> |
|
56 |
{{ dict.label }} |
|
57 |
</el-radio> |
|
58 |
</el-radio-group> |
|
59 |
</el-form-item> |
|
60 |
</el-form> |
|
61 |
<template #footer> |
|
62 |
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> |
|
63 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
64 |
</template> |
|
65 |
</Dialog> |
|
66 |
</template> |
|
67 |
<script setup lang="ts"> |
|
68 |
import { getIntDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict' |
|
69 |
import { ChatRoleApi, ChatRoleVO } from '@/api/ai/model/chatRole' |
|
70 |
import { CommonStatusEnum } from '@/utils/constants' |
|
71 |
import { ChatModelApi, ChatModelVO } from '@/api/ai/model/chatModel' |
|
72 |
import {FormRules} from "element-plus"; |
|
73 |
|
|
74 |
/** AI 聊天角色 表单 */ |
|
75 |
defineOptions({ name: 'ChatRoleForm' }) |
|
76 |
|
|
77 |
const { t } = useI18n() // 国际化 |
|
78 |
const message = useMessage() // 消息弹窗 |
|
79 |
|
|
80 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
81 |
const dialogTitle = ref('') // 弹窗的标题 |
|
82 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
83 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
84 |
const formData = ref({ |
|
85 |
id: undefined, |
|
86 |
modelId: undefined, |
|
87 |
name: undefined, |
|
88 |
avatar: undefined, |
|
89 |
category: undefined, |
|
90 |
sort: undefined, |
|
91 |
description: undefined, |
|
92 |
systemMessage: undefined, |
|
93 |
publicStatus: true, |
|
94 |
status: CommonStatusEnum.ENABLE |
|
95 |
}) |
|
96 |
const formRef = ref() // 表单 Ref |
|
97 |
const chatModelList = ref([] as ChatModelVO[]) // 聊天模型列表 |
|
98 |
|
|
99 |
/** 是否【我】自己创建,私有角色 */ |
|
100 |
const isUser = computed(() => { |
|
101 |
return formType.value === 'my-create' || formType.value === 'my-update' |
|
102 |
}) |
|
103 |
|
|
104 |
const formRules = reactive<FormRules>({ |
|
105 |
name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }], |
|
106 |
avatar: [{ required: true, message: '角色头像不能为空', trigger: 'blur' }], |
|
107 |
category: [{ required: true, message: '角色类别不能为空', trigger: 'blur' }], |
|
108 |
sort: [{ required: true, message: '角色排序不能为空', trigger: 'blur' }], |
|
109 |
description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }], |
|
110 |
systemMessage: [{ required: true, message: '角色设定不能为空', trigger: 'blur' }], |
|
111 |
publicStatus: [{ required: true, message: '是否公开不能为空', trigger: 'blur' }] |
|
112 |
}) |
|
113 |
|
|
114 |
/** 打开弹窗 */ |
|
115 |
// TODO @fan:title 是不是收敛到 type 判断生成 title,会更合理 |
|
116 |
const open = async (type: string, id?: number, title?: string) => { |
|
117 |
dialogVisible.value = true |
|
118 |
dialogTitle.value = title || t('action.' + type) |
|
119 |
formType.value = type |
|
120 |
resetForm() |
|
121 |
// 修改时,设置数据 |
|
122 |
if (id) { |
|
123 |
formLoading.value = true |
|
124 |
try { |
|
125 |
formData.value = await ChatRoleApi.getChatRole(id) |
|
126 |
} finally { |
|
127 |
formLoading.value = false |
|
128 |
} |
|
129 |
} |
|
130 |
// 获得下拉数据 |
|
131 |
chatModelList.value = await ChatModelApi.getChatModelSimpleList(CommonStatusEnum.ENABLE) |
|
132 |
} |
|
133 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
134 |
|
|
135 |
/** 提交表单 */ |
|
136 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
137 |
const submitForm = async () => { |
|
138 |
// 校验表单 |
|
139 |
await formRef.value.validate() |
|
140 |
// 提交请求 |
|
141 |
formLoading.value = true |
|
142 |
try { |
|
143 |
const data = formData.value as unknown as ChatRoleVO |
|
144 |
// tip: my-create、my-update 是 chat 角色仓库调用 |
|
145 |
// tip: create、else 是后台管理调用 |
|
146 |
if (formType.value === 'my-create') { |
|
147 |
await ChatRoleApi.createMy(data) |
|
148 |
message.success(t('common.createSuccess')) |
|
149 |
} else if (formType.value === 'my-update') { |
|
150 |
await ChatRoleApi.updateMy(data) |
|
151 |
message.success(t('common.updateSuccess')) |
|
152 |
} else if (formType.value === 'create') { |
|
153 |
await ChatRoleApi.createChatRole(data) |
|
154 |
message.success(t('common.createSuccess')) |
|
155 |
} else { |
|
156 |
await ChatRoleApi.updateChatRole(data) |
|
157 |
message.success(t('common.updateSuccess')) |
|
158 |
} |
|
159 |
dialogVisible.value = false |
|
160 |
// 发送操作成功的事件 |
|
161 |
emit('success') |
|
162 |
} finally { |
|
163 |
formLoading.value = false |
|
164 |
} |
|
165 |
} |
|
166 |
|
|
167 |
/** 重置表单 */ |
|
168 |
const resetForm = () => { |
|
169 |
formData.value = { |
|
170 |
id: undefined, |
|
171 |
modelId: undefined, |
|
172 |
name: undefined, |
|
173 |
avatar: undefined, |
|
174 |
category: undefined, |
|
175 |
sort: undefined, |
|
176 |
description: undefined, |
|
177 |
systemMessage: undefined, |
|
178 |
publicStatus: true, |
|
179 |
status: CommonStatusEnum.ENABLE |
|
180 |
} |
|
181 |
formRef.value?.resetFields() |
|
182 |
} |
|
183 |
</script> |