houzhongjian
2024-07-23 8501060c4f921d1e744c477e4dc08eb47b52693c
提交 | 用户 | 时间
850106 1 <template>
H 2   <el-dialog
3     :title="!dataForm.id ? '新增' : '修改'"
4     :close-on-click-modal="false"
5     :visible.sync="visible"
6     append-to-body
7   >
8     <el-form
9       :model="dataForm"
10       :rules="dataRule"
11       ref="dataForm"
12       @keyup.enter.native="dataFormSubmit()"
13       label-width="100px"
14     >
15       <el-row :gutter="20">
16         <el-col :span="12">
17           <el-form-item label="服务名" prop="serverName">
18             <el-input
19               v-model="dataForm.serverName"
20               placeholder="服务名"
21             ></el-input>
22           </el-form-item>
23         </el-col>
24         <el-col :span="12">
25           <el-form-item label="端点URL" prop="endpointUrl">
26             <el-input
27               v-model="dataForm.endpointUrl"
28               placeholder="端点URL"
29             ></el-input>
30           </el-form-item>
31         </el-col>
32       </el-row>
33       <el-row :gutter="20">
34         <el-col :span="12">
35           <el-form-item label="安全策略" prop="securityPolicy">
36             <el-input
37               v-model="dataForm.securityPolicy"
38               placeholder="安全策略"
39             />
40           </el-form-item>
41         </el-col>
42         <el-col :span="12">
43           <el-form-item label="安全模式" prop="securityMode">
44             <el-input v-model="dataForm.securityMode" placeholder="安全模式" />
45           </el-form-item>
46         </el-col>
47       </el-row>
48       <el-row :gutter="20">
49         <el-col :span="12">
50           <el-form-item label="重连超时" prop="reconnectInterval">
51             <el-input-number
52               :min=0
53               :max="100000"
54               v-model="dataForm.reconnectInterval"
55               placeholder="重连超时"
56             ></el-input-number>
57           </el-form-item>
58         </el-col>
59         <el-col :span="12">
60           <el-form-item
61             label="设备不活动超时时间"
62             prop="connectInactivityTimeout"
63             label-width="160px"
64           >
65             <el-input-number
66               :min=0
67               :max="1000000"
68               v-model="dataForm.connectInactivityTimeout"
69               placeholder="设备不活动超时时间"
70             />
71           </el-form-item>
72         </el-col>
73       </el-row>
74       <el-row :gutter="20">
75         <el-col :span="12">
76           <el-form-item label="连接方式" prop="connectionType">
77             <dict-select-tag
78               v-model="dataForm.connectionType"
79               placeholder="连接方式"
80               @change="onConnectionTypeChange"
81               dictCode="oadp-connection-type"
82             />
83           </el-form-item>
84         </el-col>
85
86         <el-col :span="12">
87           <el-form-item label="用户名" prop="userName" v-show="showAuth">
88             <el-input
89               v-model="dataForm.userName"
90               placeholder="用户名"
91             ></el-input>
92           </el-form-item>
93         </el-col>
94       </el-row>
95       <el-row :gutter="20">
96         <el-col :span="20">
97           <el-form-item
98             label="安全证书路径"
99             prop="certificatePath"
100             v-show="showCert"
101             label-width="120px"
102           >
103             <el-input
104               v-model="dataForm.certificatePath"
105               placeholder="安全证书路径"
106               readonly
107             ></el-input>
108           </el-form-item>
109         </el-col>
110
111         <el-col :span="12">
112           <el-form-item label="密码" prop="password" v-show="showAuth">
113             <el-input v-model="dataForm.password" placeholder="密码"></el-input>
114           </el-form-item>
115         </el-col>
116       </el-row>
117       <el-row :gutter="20">
118         <el-col style="margin-left: 20px">
119           <el-upload
120             action="/proxyApi/data/configInfo/opcua/upload"
121             method="post"
122             :on-success="handleUploadSuccess"
123             :list-type="'text'"
124             :style="{ verticalAlign: 'left' }"
125           >
126             <el-button v-show="showCert">上传文件</el-button>
127           </el-upload>
128         </el-col>
129       </el-row>
130     </el-form>
131     <span slot="footer" class="dialog-footer">
132       <el-button @click="visible = false">取消</el-button>
133       <el-button :loading="loading" type="primary" @click="dataFormSubmit()">确定</el-button>
134     </span>
135   </el-dialog>
136 </template>
137
138 <script>
139 import DictSelectTag from "@/components/dict/dict-select-tag";
140 export default {
141   components: {
142     DictSelectTag,
143   },
144   data() {
145     return {
146       loading: false,
147       visible: false,
148       dataForm: {
149         id: "",
150         serverName: "",
151         endpointUrl: "",
152         securityPolicy: "",
153         securityMode: "",
154         connectionType: "",
155         userName: "",
156         password: "",
157         certificatePath: "",
158         connectInactivityTimeout: "",
159         reconnectInterval: "",
160       },
161       dataRule: {
162         serverName: [
163           { required: true, message: "服务名不能为空", trigger: "blur" },
164         ],
165         endpointUrl: [
166           { required: true, message: "端点URL不能为空", trigger: "blur" },
167         ],
168         securityPolicy: [
169           { required: true, message: "安全策略不能为空", trigger: "blur" },
170         ],
171         securityMode: [
172           { required: true, message: "安全模式不能为空", trigger: "blur" },
173         ],
174         connectionType: [
175           { required: true, message: "连接方式不能为空", trigger: "blur" },
176         ],
177         connectInactivityTimeout: [
178           {
179             required: true,
180             message: "设备不活动超时时间不能为空",
181             trigger: "blur",
182           },
183         ],
184         reconnectInterval: [
185           { required: true, message: "重连超时时间不能为空", trigger: "blur" },
186         ],
187       },
188       showAuth: false, // 是否显示用户名、密码输入框
189       showCert: false, // 是否显示安全证书路径输入框
190     };
191   },
192   methods: {
193     init(id) {
194       this.dataForm.id = id || 0;
195       this.visible = true;
196       this.$nextTick(() => {
197         this.$refs["dataForm"].resetFields();
198         if (this.dataForm.id) {
199           this.$http({
200             url: `/data/channel/opcua/device/info/${this.dataForm.id}`,
201             method: "get",
202             params: this.$http.adornParams(),
203           }).then(({ code, data }) => {
204             if (data && code === 0) {
205               this.dataForm.serverName = data.serverName;
206               this.dataForm.endpointUrl = data.endpointUrl;
207               this.dataForm.securityPolicy = data.securityPolicy;
208               this.dataForm.securityMode = data.securityMode;
209               this.dataForm.connectionType = data.connectionType;
210               this.dataForm.userName = data.userName;
211               this.dataForm.password = data.password;
212               this.dataForm.certificatePath = data.certificatePath;
213               this.dataForm.connectInactivityTimeout =
214                 data.connectInactivityTimeout;
215               this.dataForm.reconnectInterval = data.reconnectInterval;
216             }
217           });
218         }
219       });
220     },
221     // 表单提交
222     dataFormSubmit() {
223       this.$refs["dataForm"].validate((valid) => {
224         if (valid) {
225           this.loading = true;
226           this.$http({
227             headers: {
228               "Content-Type": "application/json",
229             },
230             url: `/data/channel/opcua/device/${
231               !this.dataForm.id ? "add" : "update"
232             }`,
233             method: "post",
234             data: this.$http.adornData({
235               id: this.dataForm.id || undefined,
236               serverName: this.dataForm.serverName,
237               endpointUrl: this.dataForm.endpointUrl,
238               securityPolicy: this.dataForm.securityPolicy,
239               securityMode: this.dataForm.securityMode,
240               connectionType: this.dataForm.connectionType,
241               userName: this.dataForm.userName,
242               password: this.dataForm.password,
243               certificatePath: this.dataForm.certificatePath,
244               connectInactivityTimeout: this.dataForm.connectInactivityTimeout,
245               reconnectInterval: this.dataForm.reconnectInterval,
246             }),
247           }).then(({ code, msg }) => {
248             if (code === 0) {
249               this.$message({
250                 message: "操作成功",
251                 type: "success",
252                 duration: 1500,
253                 onClose: () => {
254                   this.visible = false;
255                   this.$emit("refreshDataList");
256                 },
257               });
258               this.loading = false;
259             } else {
260               this.$message.error(msg);
261             }
262           });
263         }
264       });
265     },
266     //控制连接方式的显示
267     onConnectionTypeChange() {
268       switch (this.dataForm.connectionType) {
269         case "0":
270           this.showAuth = false;
271           this.showCert = false;
272           this.$set(this.dataRule, "userName", [{ required: false }]);
273           this.$set(this.dataRule, "password", [{ required: false }]);
274           this.$set(this.dataRule, "certificatePath", [{ required: false }]);
275           break;
276         case "1":
277           this.showAuth = true;
278           this.showCert = false;
279           this.$set(this.dataRule, "userName", [
280             { required: true, message: "用户名不能为空", trigger: "blur" },
281           ]);
282           this.$set(this.dataRule, "password", [
283             { required: true, message: "密码不能为空", trigger: "blur" },
284           ]);
285           this.$set(this.dataRule, "certificatePath", [{ required: false }]);
286           break;
287         case "2":
288           this.showAuth = false;
289           this.showCert = true;
290           this.$set(this.dataRule, "userName", [{ required: false }]);
291           this.$set(this.dataRule, "password", [{ required: false }]);
292           this.$set(this.dataRule, "certificatePath", [
293             {
294               required: true,
295               message: "安全证书路径不能为空",
296               trigger: "blur",
297             },
298           ]);
299           break;
300       }
301     },
302     //上传安全证书
303     handleUploadSuccess(data) {
304       if (data && data.code === 0) {
305         this.dataForm.certificatePath = data.data;
306       } else {
307         this.$message.error("上传失败");
308       }
309     },
310   },
311 };
312 </script>
313 <style>
314 .el-select {
315   width: 100%;
316 }
317 .el-input-group__append {
318   padding: 0 5px 0 5px;
319 }
320 .el-input-group__prepend {
321   padding: 0 5px 0 5px;
322 }
323 </style>