<template>
|
<Dialog v-model="dialogVisible" :title="dialogTitle">
|
<el-form
|
ref="formRef"
|
v-loading="formLoading"
|
:model="formData"
|
:rules="formRules"
|
label-width="80px"
|
>
|
<el-divider content-position="left">模型信息</el-divider>
|
<el-row :gutter="8">
|
<el-col :span="12">
|
<el-form-item label="模型名称" prop="pyName">
|
<el-input disabled v-model="formData.pyName" placeholder=""/>
|
</el-form-item>
|
</el-col>
|
<el-col :span="12">
|
<el-upload
|
ref="uploadRef"
|
v-model:file-list="fileList"
|
:show-file-list="false"
|
:action="importUrl"
|
:auto-upload="true"
|
:disabled="uploadLoading"
|
:before-upload="beforeUpload"
|
:headers="uploadHeaders"
|
:on-error="submitFormError"
|
:on-success="submitFormSuccess"
|
accept=".pyd"
|
>
|
<el-button type="primary"><Icon icon="ep:upload" />模型上传</el-button>
|
</el-upload>
|
</el-col>
|
</el-row>
|
<el-row :gutter="8">
|
<el-col :span="12">
|
<el-form-item label="包名" prop="pkgName">
|
<el-input v-model="formData.pkgName" placeholder=""/>
|
</el-form-item>
|
</el-col>
|
<el-col :span="12">
|
<el-form-item label="模型路径" prop="pyModule">
|
<el-input v-model="formData.pyModule" placeholder=""/>
|
</el-form-item>
|
</el-col>
|
</el-row>
|
<el-row :gutter="20">
|
<el-col :span="24">
|
<el-form-item label="备注" prop="remark">
|
<el-input v-model="formData.remark" placeholder="" type="textarea"/>
|
</el-form-item>
|
</el-col>
|
</el-row>
|
<el-divider content-position="left">模型方法</el-divider>
|
<el-row :gutter="20">
|
<el-col :span="4">
|
<el-button type="primary" size="small" @click="addRow()">新增</el-button>
|
</el-col>
|
</el-row>
|
<el-table :data="formData.modelMethods" border>
|
<el-table-column
|
prop=""
|
label="方法名"
|
align="center"
|
width="250">
|
<template #default="scope">
|
<el-select size="small" v-model="scope.row.methodName">
|
<el-option
|
v-for="item in getDictOptions(DICT_TYPE.MODEL_METHOD)"
|
:key="item.value"
|
:label="item.label"
|
:value="item.value"
|
:disabled="methodSelectDisabled(item.value)"
|
/>
|
</el-select>
|
</template>
|
</el-table-column>
|
<el-table-column
|
prop=""
|
label="输入个数"
|
align="center">
|
<template #default="scope">
|
<el-input-number size="small" v-model="scope.row.dataLength" :min="1" :max="50"/>
|
</template>
|
</el-table-column>
|
<el-table-column
|
prop=""
|
label="是否有model"
|
align="center">
|
<template #default="scope">
|
<el-switch size="small" v-model="scope.row.model" :active-value="1"
|
:inactive-value="0"/>
|
</template>
|
</el-table-column>
|
<el-table-column label="操作" fixed="right" header-align="center" align="center" width="100">
|
<template #default="scope">
|
<el-button
|
@click="deleteRow(scope.$index)"
|
key="danger"
|
type="danger"
|
link
|
>删除
|
</el-button>
|
</template>
|
</el-table-column>
|
</el-table>
|
</el-form>
|
<template #footer>
|
<el-button type="primary" @click="submitForm">确 定</el-button>
|
<el-button @click="dialogVisible = false">取 消</el-button>
|
</template>
|
</Dialog>
|
</template>
|
<script lang="ts" setup>
|
import {DICT_TYPE, getDictOptions} from '@/utils/dict'
|
import * as MpkApi from '@/api/mpk/mpk'
|
import {FormRules} from 'element-plus'
|
import {getAccessToken, getTenantId} from "@/utils/auth";
|
|
|
const {t} = useI18n() // 国际化
|
const message = useMessage() // 消息弹窗
|
|
const dialogVisible = ref(false) // 弹窗的是否展示
|
const dialogTitle = ref('') // 弹窗的标题
|
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
|
const formType = ref('') // 表单的类型:create - 新增;update - 修改
|
const formData = ref({
|
id: undefined,
|
pyName: undefined,
|
pkgName: undefined,
|
pyType: undefined,
|
className: undefined,
|
pyModule: undefined,
|
remark: undefined,
|
modelMethods: [],
|
filePath: undefined,
|
})
|
|
const formRules = reactive<FormRules>({
|
pyName: [
|
{required: true, message: '模型名称不能为空,请上传模型文件', trigger: 'blur'}
|
],
|
pyType: [
|
{required: true, message: '模型类型不能为空', trigger: 'blur'}
|
],
|
pkgName: [
|
{required: true, message: '包名不能为空', trigger: 'blur'}
|
],
|
className: [
|
{required: true, message: '类名不能为空', trigger: 'blur'}
|
],
|
pyModule: [
|
{required: true, message: '模型路径不能为空', trigger: 'blur'}
|
],
|
})
|
|
const formRef = ref() // 表单 Ref
|
|
/** 打开弹窗 */
|
const open = async (type: string, id?: number) => {
|
dialogVisible.value = true
|
dialogTitle.value = t('action.' + type)
|
formType.value = type
|
resetForm()
|
// 修改时,设置数据
|
if (id) {
|
formLoading.value = true
|
try {
|
formData.value = await MpkApi.getMpk(id)
|
} finally {
|
formLoading.value = false
|
}
|
}
|
}
|
defineExpose({open}) // 提供 open 方法,用于打开弹窗
|
|
/** 提交表单 */
|
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
|
const submitForm = async () => {
|
// 校验表单
|
if (!formRef) return
|
const valid = await formRef.value.validate()
|
if (!valid) return
|
// 模型方法校验
|
if (formData.value.modelMethods?.length <= 0) {
|
message.error('模型方法为空')
|
return
|
}
|
// 模型方法名称校验
|
if (formData.value.modelMethods.some(e => e.methodName === undefined || e.methodName === '')) {
|
message.error('存在不合法模型方法名')
|
return
|
}
|
// 提交请求
|
formLoading.value = true
|
try {
|
const data = formData.value as unknown as MpkApi.MpkVO
|
if (formType.value === 'create') {
|
await MpkApi.createMpk(data)
|
message.success(t('common.createSuccess'))
|
} else {
|
await MpkApi.updateMpk(data)
|
message.success(t('common.updateSuccess'))
|
}
|
dialogVisible.value = false
|
// 发送操作成功的事件
|
emit('success')
|
} finally {
|
formLoading.value = false
|
}
|
}
|
|
/** 重置表单 */
|
const resetForm = () => {
|
formData.value = {
|
id: undefined,
|
pyName: undefined,
|
pkgName: undefined,
|
pyType: undefined,
|
className: undefined,
|
pyModule: undefined,
|
remark: undefined,
|
modelMethods: [],
|
filePath: undefined
|
}
|
formRef.value?.resetFields()
|
}
|
|
const addRow = function () {
|
formData.value.modelMethods.push({
|
methodName: undefined,
|
dataLength: 1,
|
model: 0
|
})
|
}
|
const deleteRow = function (index) {
|
formData.value.modelMethods.splice(index, 1)
|
}
|
|
// 模型方法下拉禁用
|
const methodSelectDisabled = (value) => {
|
if (formData.value.modelMethods.some(e => e.methodName === value)) {
|
return true;
|
}
|
return false;
|
}
|
|
const fileList = ref([]) // 文件列表
|
const importUrl = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/model/mpk/file/upload'
|
const uploadLoading = ref(false) // 表单的加载中
|
const uploadHeaders = ref() // 上传 Header 头
|
const beforeUpload = function (file) {
|
// 提交请求
|
uploadHeaders.value = {
|
Authorization: 'Bearer ' + getAccessToken(),
|
'tenant-id': getTenantId()
|
}
|
uploadLoading.value = true
|
return true;
|
}
|
const submitFormError = (): void => {
|
message.error('上传失败!')
|
uploadLoading.value = false
|
}
|
const submitFormSuccess = (response: any) => {
|
if (response.code !== 0) {
|
message.error(response.msg)
|
uploadLoading.value = false
|
return
|
}
|
const data = response.data;
|
formData.value.filePath = data.filePath
|
formData.value.pyName = data.fileName.replace('.pyd','')
|
message.success('上传成功')
|
uploadLoading.value = false
|
}
|
</script>
|