提交 | 用户 | 时间
|
325d3d
|
1 |
<template> |
L |
2 |
<Dialog v-model="dialogVisible" :title="dialogTitle" width="50%"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
v-loading="formLoading" |
|
6 |
:model="formData" |
|
7 |
:rules="formRules" |
|
8 |
label-width="120px" |
|
9 |
> |
|
10 |
<el-row> |
|
11 |
<el-col :span="12"> |
|
12 |
<el-form-item label="实例名称" prop="instanceName"> |
dea4e4
|
13 |
<el-input v-model="formData.instanceName" placeholder="请输入实例名称"/> |
325d3d
|
14 |
</el-form-item> |
L |
15 |
</el-col> |
|
16 |
</el-row> |
|
17 |
<el-row> |
|
18 |
<el-col :span="12"> |
|
19 |
<el-form-item label="IP地址" prop="address"> |
dea4e4
|
20 |
<el-input v-model="formData.address" placeholder="请输入IP地址"/> |
325d3d
|
21 |
</el-form-item> |
L |
22 |
</el-col> |
|
23 |
<el-col :span="12"> |
|
24 |
<el-form-item label="端口" prop="port"> |
dea4e4
|
25 |
<el-input v-model="formData.port" placeholder="请输入端口"/> |
325d3d
|
26 |
</el-form-item> |
L |
27 |
</el-col> |
|
28 |
</el-row> |
|
29 |
<el-row> |
|
30 |
<el-col :span="12"> |
|
31 |
<el-form-item label="用户名" prop="username"> |
dea4e4
|
32 |
<el-input v-model="formData.username" placeholder="请输入用户名"/> |
325d3d
|
33 |
</el-form-item> |
L |
34 |
</el-col> |
|
35 |
<el-col :span="12"> |
|
36 |
<el-form-item label="密码" prop="password"> |
dea4e4
|
37 |
<el-input type="password" v-model="formData.password" placeholder="请输入密码"/> |
325d3d
|
38 |
</el-form-item> |
L |
39 |
</el-col> |
|
40 |
</el-row> |
|
41 |
</el-form> |
|
42 |
<template #footer> |
|
43 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
44 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
45 |
</template> |
|
46 |
</Dialog> |
|
47 |
</template> |
|
48 |
<script lang="ts" setup> |
dea4e4
|
49 |
import * as KioApi from '@/api/data/channel/kio' |
潘 |
50 |
import {isIP, isPositiveInteger} from '@/utils/validate' |
325d3d
|
51 |
|
dea4e4
|
52 |
defineOptions({name: 'DataKioDeviceForm'}) |
325d3d
|
53 |
|
dea4e4
|
54 |
const {t} = useI18n() // 国际化 |
325d3d
|
55 |
const message = useMessage() // 消息弹窗 |
L |
56 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
57 |
const dialogTitle = ref('') // 弹窗的标题 |
|
58 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
59 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
60 |
const formData = ref({ |
|
61 |
id: undefined, |
|
62 |
instanceName: undefined, |
|
63 |
address: undefined, |
|
64 |
port: undefined, |
|
65 |
username: undefined, |
|
66 |
password: undefined, |
|
67 |
}) |
dea4e4
|
68 |
const validateIP = (rule, value, callback) => { |
潘 |
69 |
if (!isIP(value)) { |
|
70 |
callback(new Error('IP地址不正确')) |
|
71 |
} else { |
|
72 |
callback() |
|
73 |
} |
|
74 |
} |
|
75 |
const validateNum = (rule, value, callback) => { |
|
76 |
if (!isPositiveInteger(value)) { |
|
77 |
callback(new Error('格式不正确')) |
|
78 |
} else { |
|
79 |
callback() |
|
80 |
} |
|
81 |
} |
325d3d
|
82 |
const formRules = reactive({ |
dea4e4
|
83 |
instanceName: [{required: true, message: '设备名称不能为空', trigger: 'blur'}], |
潘 |
84 |
address: [ |
|
85 |
{required: true, message: 'IP地址不能为空', trigger: 'blur'}, |
|
86 |
{validator: validateIP, trigger: 'blur'} |
|
87 |
], |
|
88 |
port: [ |
|
89 |
{required: true, message: '端口不能为空', trigger: 'blur'}, |
|
90 |
{validator: validateNum, trigger: 'blur'} |
|
91 |
], |
|
92 |
username: [{required: true, message: '用户名不能为空', trigger: 'blur'}], |
|
93 |
password: [{required: true, message: '密码不能为空', trigger: 'blur'}] |
325d3d
|
94 |
}) |
L |
95 |
const formRef = ref() // 表单 Ref |
|
96 |
|
|
97 |
/** 打开弹窗 */ |
|
98 |
const open = async (type: string, id?: number) => { |
|
99 |
dialogVisible.value = true |
|
100 |
dialogTitle.value = t('action.' + type) |
|
101 |
formType.value = type |
|
102 |
resetForm() |
|
103 |
// 修改时,设置数据 |
|
104 |
if (id) { |
|
105 |
formLoading.value = true |
|
106 |
try { |
|
107 |
formData.value = await KioApi.getKioDevice(id) |
|
108 |
} finally { |
|
109 |
formLoading.value = false |
|
110 |
} |
|
111 |
} |
|
112 |
} |
dea4e4
|
113 |
defineExpose({open}) // 提供 open 方法,用于打开弹窗 |
325d3d
|
114 |
|
L |
115 |
/** 提交表单 */ |
|
116 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
117 |
const submitForm = async () => { |
|
118 |
// 校验表单 |
|
119 |
if (!formRef) return |
|
120 |
const valid = await formRef.value.validate() |
|
121 |
if (!valid) return |
|
122 |
// 提交请求 |
|
123 |
formLoading.value = true |
|
124 |
try { |
|
125 |
const data = formData.value as unknown as KioApi.KioDeviceVO |
|
126 |
if (formType.value === 'create') { |
|
127 |
await KioApi.createKioDevice(data) |
|
128 |
message.success(t('common.createSuccess')) |
|
129 |
} else { |
|
130 |
await KioApi.updateKioDevice(data) |
|
131 |
message.success(t('common.updateSuccess')) |
|
132 |
} |
|
133 |
dialogVisible.value = false |
|
134 |
// 发送操作成功的事件 |
|
135 |
emit('success') |
|
136 |
} finally { |
|
137 |
formLoading.value = false |
|
138 |
} |
|
139 |
} |
|
140 |
|
|
141 |
/** 重置表单 */ |
|
142 |
const resetForm = () => { |
|
143 |
formData.value = { |
|
144 |
id: undefined, |
|
145 |
instanceName: undefined, |
|
146 |
address: undefined, |
|
147 |
port: undefined, |
|
148 |
username: undefined, |
|
149 |
password: undefined |
|
150 |
} |
|
151 |
formRef.value?.resetFields() |
|
152 |
} |
|
153 |
</script> |