办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!--
使用antd原生Form生成 详细用法参考ant-design-vue Form组件文档
vscode默认配置文件会自动格式化/移除未使用依赖
-->
<script setup lang="ts">
import type { RuleObject } from 'ant-design-vue/es/form';
 
import type { ModelForm } from '#/api/system/model/model';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { cloneDeep, getPopupContainer } from '@vben/utils';
 
import { Form, FormItem, Input, Select, Textarea } from 'ant-design-vue';
import { pick } from 'lodash-es';
 
import { modelAdd, modelInfo, modelUpdate } from '#/api/system/model';
 
const emit = defineEmits<{ reload: [] }>();
 
const isUpdate = ref(false);
const title = computed(() => {
  return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
 
/**
 * 定义默认值 用于reset
 */
const defaultValues: Partial<ModelForm> = {
  id: undefined,
  category: undefined,
  modelName: undefined,
  modelDescribe: undefined,
  modelPrice: undefined,
  modelType: undefined,
  modelShow: undefined,
  systemPrompt: undefined,
  apiHost: undefined,
  apiKey: undefined,
  remark: undefined,
};
 
/**
 * 表单数据ref
 */
const formData = ref(defaultValues);
 
type AntdFormRules<T> = Partial<Record<keyof T, RuleObject[]>> & {
  [key: string]: RuleObject[];
};
 
/**
 * 表单校验规则
 */
const formRules = ref<AntdFormRules<ModelForm>>({
  modelName: [{ required: true, message: '模型名称不能为空' }],
  modelDescribe: [{ required: true, message: '模型描述不能为空' }],
  modelPrice: [{ required: true, message: '模型价格不能为空' }],
  modelType: [{ required: true, message: '计费类型不能为空' }],
  modelShow: [{ required: true, message: '是否显示不能为空' }],
  apiHost: [{ required: true, message: '请求地址不能为空' }],
  apiKey: [{ required: true, message: '密钥不能为空' }],
});
 
/**
 * useForm解构出表单方法
 */
const { validate, validateInfos, resetFields } = Form.useForm(
  formData,
  formRules,
);
 
const [BasicModal, modalApi] = useVbenModal({
  class: 'w-[550px]',
  fullscreenButton: false,
  closeOnClickModal: false,
  onClosed: handleCancel,
  onConfirm: handleConfirm,
  onOpenChange: async (isOpen) => {
    if (!isOpen) {
      return null;
    }
    modalApi.modalLoading(true);
 
    const { id } = modalApi.getData() as { id?: number | string };
    isUpdate.value = !!id;
 
    if (isUpdate.value && id) {
      const record = await modelInfo(id);
      // 只赋值存在的字段
      const filterRecord = pick(record, Object.keys(defaultValues));
      formData.value = filterRecord;
    }
 
    modalApi.modalLoading(false);
  },
});
 
async function handleConfirm() {
  try {
    modalApi.modalLoading(true);
    await validate();
    // 可能会做数据处理 使用cloneDeep深拷贝
    const data = cloneDeep(formData.value);
    await (isUpdate.value ? modelUpdate(data) : modelAdd(data));
    emit('reload');
    await handleCancel();
  } catch (error) {
    console.error(error);
  } finally {
    modalApi.modalLoading(false);
  }
}
 
async function handleCancel() {
  modalApi.close();
  formData.value = defaultValues;
  resetFields();
}
 
const getmodelShow = ref([
  { label: '隐藏', value: '1' },
  { label: '显示', value: '0' },
]);
 
const getmodelType = ref([
  { label: 'token计费', value: '1' },
  { label: '次数计费', value: '2' },
]);
</script>
 
<template>
  <BasicModal :title="title">
    <Form :label-col="{ span: 4 }">
      <FormItem label="模型分类" v-bind="validateInfos.category">
        <Input
          v-model:value="formData.category"
          :placeholder="$t('ui.formRules.required')"
        />
      </FormItem>
      <FormItem label="模型名称" v-bind="validateInfos.modelName">
        <Input
          v-model:value="formData.modelName"
          :placeholder="$t('ui.formRules.required')"
        />
      </FormItem>
      <FormItem label="模型描述" v-bind="validateInfos.modelDescribe">
        <Input
          v-model:value="formData.modelDescribe"
          :placeholder="$t('ui.formRules.required')"
        />
      </FormItem>
      <FormItem label="模型价格" v-bind="validateInfos.modelPrice">
        <Input
          v-model:value="formData.modelPrice"
          :placeholder="$t('ui.formRules.required')"
        />
      </FormItem>
      <FormItem label="计费类型" v-bind="validateInfos.modelType">
        <Select
          v-model:value="formData.modelType"
          :options="getmodelType"
          :get-popup-container="getPopupContainer"
          :placeholder="$t('ui.formRules.selectRequired')"
        />
      </FormItem>
 
      <FormItem label="是否显示" v-bind="validateInfos.modelShow">
        <Select
          v-model:value="formData.modelShow"
          :options="getmodelShow"
          :get-popup-container="getPopupContainer"
          :placeholder="$t('ui.formRules.selectRequired')"
        />
      </FormItem>
 
      <FormItem label="请求地址" v-bind="validateInfos.apiHost">
        <Input
          v-model:value="formData.apiHost"
          :placeholder="$t('ui.formRules.required')"
        />
      </FormItem>
 
      <FormItem label="密钥" v-bind="validateInfos.apiKey">
        <Input
          v-model:value="formData.apiKey"
          :placeholder="$t('ui.formRules.required')"
        />
      </FormItem>
 
      <FormItem label="备注" v-bind="validateInfos.remark">
        <Textarea
          v-model:value="formData.remark"
          :placeholder="$t('ui.formRules.required')"
          :rows="4"
        />
      </FormItem>
    </Form>
  </BasicModal>
</template>