-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathConnectSetting.vue
373 lines (341 loc) · 12.3 KB
/
ConnectSetting.vue
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<template>
<el-dialog
v-model="DialogVisilble"
:destory-on-close="true"
:before-close="closeDialog"
:width="360"
:title="$t('连接设置')"
:modal="false"
:close-on-click-modal="false"
modal-class="kk-dialog-class"
align-center
draggable
>
<div style="margin-top: -25px;"></div>
<div>
<div class="item-class" style="margin-bottom: 15px;">
<div class="no-select form-width nowrap">{{ $t('配\u00A0\u00A0\u00A0置') }}:</div>
<ToolTip :content="!(setInfo.option && setInfo.option.length > 0) ? $t('新建配置') : setInfo.option" >
<template #content>
<div class="ellipsis" style="user-select: none;" :class="!(setInfo.option && setInfo.option.length > 0) ? 'new-option': 'old-option'" >{{ !(setInfo.option && setInfo.option.length > 0) ? $t('新建配置') : setInfo.option }}</div>
</template>
</ToolTip>
<div style="flex: 1;"></div>
<div><el-button size="small" type="primary" @click="showOption(0)" style="margin-left: 10px;" ><el-icon class="el-icon--left"><Switch /></el-icon>{{ $t('切换') }}</el-button></div>
<div><el-button v-if="isForbidInput == false" size="small" type="primary" @click="showOption(1)" style="margin-left: 10px;" ><el-icon class="el-icon--left"><Finished /></el-icon>{{ $t('保存') }}</el-button></div>
<div><el-button v-if="isForbidInput == true" size="small" type="primary" @click="newOp" style="margin-left: 10px;" ><el-icon class="el-icon--left"><Edit /></el-icon>{{ $t('新建') }}</el-button></div>
</div>
<div class="item-class" style="margin-bottom: 15px;">
<div class="no-select form-width">{{ $t('主机IP') }}:</div>
<div>
<el-input :disabled="isForbidInput" v-model="setInfo.server_ip" class="w-50 m-2" :placeholder="$t('输入主机IP')">
<template #prefix>
<el-icon><HomeFilled /></el-icon>
</template>
</el-input>
</div>
<div style="cursor: pointer; margin-left: 10px;" @click="doCopy(setInfo.server_ip)"><el-icon size="15"><DocumentCopy /></el-icon></div>
</div>
<div class="item-class" style="margin-bottom: 15px;">
<div class="no-select form-width">{{ $t('端口号') }}:</div>
<div>
<el-input :disabled="isForbidInput" v-model="setInfo.server_port" class="w-50 m-2" :placeholder="$t('输入端口号')">
<template #prefix>
<el-icon><Paperclip /></el-icon>
</template>
</el-input>
</div>
<div style="cursor: pointer; margin-left: 10px;" @click="doCopy(setInfo.server_port)"><el-icon size="15"><DocumentCopy /></el-icon></div>
</div>
<div class="item-class" style="margin-bottom: 15px;">
<div class="no-select form-width">{{ $t('用户名') }}:</div>
<div>
<el-input :disabled="isForbidInput" v-model="setInfo.server_user" class="w-50 m-2" :placeholder="$t('输入用户名')">
<template #prefix>
<el-icon class="el-input__icon"><User /></el-icon>
</template>
</el-input>
</div>
<div style="cursor: pointer; margin-left: 10px;" @click="doCopy(setInfo.server_user)"><el-icon size="15"><DocumentCopy /></el-icon></div>
</div>
<div class="item-class" style="margin-bottom: 5px;">
<el-dropdown class="no-select form-width" hide-timeout="300" >
<span>{{ setInfo.authType == 0 ? $t('密码') : $t('私钥') }}<el-icon class="el-icon--right"><arrow-down style="cursor: pointer;" /></el-icon></span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item class="no-select" @click="setInfo.authType = 0" >{{ $t('密码') }}</el-dropdown-item>
<el-dropdown-item class="no-select" @click="setInfo.authType = 1" >{{ $t('私钥') }}</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
<div v-if="setInfo.authType == 0" class="item-class">
<div>
<el-input :disabled="isForbidInput" v-model="setInfo.server_password" :type="isShowPassword ? 'text': 'password'" class="w-50 m-2" :placeholder="$t('输入密码')">
<template #prefix>
<el-icon class="el-input__icon"><Lock /></el-icon>
</template>
</el-input>
</div>
<div v-if="isShowPassword == true" style="cursor: pointer; margin-left: 10px;" @click="isShowPassword = false"><el-icon size="15"><View /></el-icon></div>
<div v-else style="cursor: pointer; margin-left: 10px;" @click="isShowPassword = true"><el-icon size="15"><Hide /></el-icon></div>
</div>
<div v-else >
<el-button :disabled="isForbidInput" type="primary" @click="openprivateKeyBlock" >
<el-icon class="el-icon--left"><Key /></el-icon>{{ $t('导入') }}
</el-button>
</div>
</div>
</div>
<div class="errInfo no-select"> {{ err_msg }} </div>
<div style="margin-bottom: 5px;"></div>
<div style="display: flex; border-top: 1px solid #f1f2f4;">
<div style="flex: 1;"></div>
<el-button size="small" type="primary" @click="confirm" style="margin-bottom: -15px; margin-top: 10px;">
{{ $t('确定') }}
</el-button>
</div>
</el-dialog>
<!-- 配置管理 -->
<OptionBlock ref="optionBlockRef" @callback="doOption" :opType="optionBlockType" :sshOptions="sshOptions" @handleDeleteOption="handleDeleteOption" ></OptionBlock>
<!-- 私钥管理 -->
<PrivateKey ref="privateKeyRef" @callback="savePrivateKey" ></PrivateKey>
</template>
<script>
import { ref } from 'vue';
import useClipboard from "vue-clipboard3";
import { ElMessage } from 'element-plus';
import OptionBlock from './OptionBlock';
import PrivateKey from './PrivateKey';
import ToolTip from './ToolTip.vue';
import { HomeFilled, Paperclip, User, Lock, DocumentCopy, View, Hide, Edit, Finished, Switch, ArrowDown, Key } from '@element-plus/icons-vue';
import i18n from "@/locales/i18n";
export default {
name:'ConnectSetting',
components: {
ToolTip,
OptionBlock,
PrivateKey,
HomeFilled,
Paperclip,
User,
Lock,
DocumentCopy,
View,
Hide,
Edit,
Finished,
Switch,
ArrowDown,
Key,
},
props:['env','sshOptions'],
setup(props,context) {
// 控制Dialog显示
const DialogVisilble = ref(false);
const err_msg = ref('');
// 设置信息
const setInfo = ref({
server_ip: props.env.server_ip,
server_port: props.env.server_port,
server_user: props.env.server_user,
server_password:props.env.server_password,
server_key: props.env.server_key || null,
authType: props.env.authType || 0,
option: props.env.option,
});
// 校验参数
const verifyParams = () => {
err_msg.value = '';
// 验证IP地址
const ipRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if (!ipRegex.test(setInfo.value.server_ip)) {
err_msg.value = i18n.global.t("主机IP地址无效");
return false;
}
// 校验端口号
if (isNaN(setInfo.value.server_port) || setInfo.value.server_port < 0 || setInfo.value.server_port > 65535) {
err_msg.value = i18n.global.t("端口号无效");
return false;
}
// 校验用户名
if (!(setInfo.value.server_user && setInfo.value.server_user != '')) {
err_msg.value = i18n.global.t("用户名不能为空");
return false;
}
// 校验密码
if (setInfo.value.authType == 0 && !(setInfo.value.server_password && setInfo.value.server_password != '')) {
err_msg.value = i18n.global.t("密码不能为空");
return false;
}
// 校验私钥
if(setInfo.value.authType == 1 && !(setInfo.value.server_key && setInfo.value.server_key.content != '')) {
err_msg.value = i18n.global.t("私钥不能为空");
return false;
}
return true;
};
// 配置选项
const optionBlockRef = ref();
const optionBlockType = ref(0);
const showOption = (type) => {
if(type == 1) {
if(verifyParams() == false) return;
}
optionBlockType.value = type;
optionBlockRef.value.aimOption = '';
optionBlockRef.value.DialogVisilble = true;
};
const isForbidInput = ref(false);
if(setInfo.value.option != '') isForbidInput.value = true;
const doOption = (option) => {
// 切换
if(optionBlockType.value == 0)
{
if(!props.sshOptions[option].authType) setInfo.value.authType = 0;
setInfo.value = {...setInfo.value, ...props.sshOptions[option]};
isForbidInput.value = true;
}
// 保存
else if(optionBlockType.value == 1)
{
setInfo.value.option = option;
context.emit('saveOp', option, setInfo.value);
isForbidInput.value = true;
}
};
const newOp = () => {
isForbidInput.value = false;
setInfo.value.option = '';
};
// 删除配置
const handleDeleteOption = (name) => {
context.emit('deleteOp', name);
if(setInfo.value.option && setInfo.value.option == name) {
setInfo.value.option = '';
isForbidInput.value = false;
}
};
// 私钥相关
const privateKeyRef = ref();
const openprivateKeyBlock = () => {
privateKeyRef.value.content = setInfo.value.server_key ? setInfo.value.server_key.content : '';
privateKeyRef.value.passphrase = setInfo.value.server_key ? setInfo.value.server_key.passphrase : '';
privateKeyRef.value.DialogVisilble = true;
};
const savePrivateKey = (content,passphrase) => {
setInfo.value.server_key = {
content:content,
passphrase:passphrase,
};
};
const confirm = () => {
if(verifyParams() == false) return;
context.emit('callback',setInfo.value);
closeDialog();
};
// 拷贝
const { toClipboard } = useClipboard();
// 复制
const doCopy = async (content) => {
content += '';
if(!(content && content.length > 0)) {
ElMessage({
message: i18n.global.t('内容为空'),
type: 'warning',
grouping: true,
repeatNum: Number.MIN_SAFE_INTEGER,
});
return;
}
await toClipboard(content);
ElMessage({
message: i18n.global.t('复制成功'),
type: 'success',
grouping: true,
repeatNum: Number.MIN_SAFE_INTEGER,
});
};
const isShowPassword = ref(false);
// 重置
const reset = () => {
err_msg.value = '';
isShowPassword.value = false;
setInfo.value = {
server_ip: props.env.server_ip,
server_port: props.env.server_port,
server_user: props.env.server_user,
server_password:props.env.server_password,
server_key: props.env.server_key || null,
authType: props.env.authType || 0,
option: props.env.option,
};
if(setInfo.value.option != '') isForbidInput.value = true;
else isForbidInput.value = false;
DialogVisilble.value = false;
};
// 关闭
const closeDialog = (done) => {
if(optionBlockRef.value && optionBlockRef.value.DialogVisilble) optionBlockRef.value.closeDialog();
if(privateKeyRef.value && privateKeyRef.value.DialogVisilble) privateKeyRef.value.closeDialog();
setTimeout(() => {
reset();
},400);
DialogVisilble.value = false;
if(done) done();
};
return {
setInfo,
DialogVisilble,
err_msg,
confirm,
optionBlockRef,
showOption,
optionBlockType,
doOption,
isForbidInput,
newOp,
handleDeleteOption,
reset,
closeDialog,
doCopy,
isShowPassword,
privateKeyRef,
openprivateKeyBlock,
savePrivateKey,
}
}
}
</script>
<style scoped>
.item-class {
display: flex;
align-items: center;
}
.errInfo{
font-size: 12px;
color: rgb(234, 80, 80);
margin-top: 8px;
}
/* 文本不可选中 */
.no-select {
user-select: none;
}
/* 文本溢出省略 */
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.nowrap {
white-space: nowrap;
}
.old-option {
color: #67c23a;
}
.new-option {
color: #f56c6c;
}
.form-width {
width: 56px;
}
</style>