DeviceStep2.vue
5.3 KB
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
<template>
<div class="step2">
<div
><input type="checkbox" v-model="creaentialsPassword.isCreaentials" @click="checked" />
添加凭证
</div>
<a-form
ref="formRef"
:rules="rules"
:label-col="labelCol"
:wrapper-col="wrapperCol"
v-show="creaentialsPassword.isCreaentials"
:model="creaentialsPassword"
labelAlign="left"
>
<a-form-item label="凭据类型" name="creaentialsType">
<a-select
v-model:value="creaentialsPassword.creaentialsType"
style="width: 200px"
@change="handleChange"
placeholder="请选择凭据类型"
:options="options"
/>
</a-form-item>
<div v-if="creaentialsPassword.creaentialsType === 'Access token'">
<a-form-item label="访问令牌" name="token">
<a-input type="password" style="width: 200px" v-model:value="creaentialsPassword.token" />
</a-form-item>
</div>
<div v-else-if="creaentialsPassword.creaentialsType === 'X.509'">
<a-form-item label="RSA公钥" name="publicKey">
<a-input
type="password"
style="width: 200px"
v-model:value="creaentialsPassword.publicKey"
/>
</a-form-item>
</div>
<div v-else>
<a-form-item label="客户端ID" name="clientId">
<a-input type="input" style="width: 200px" v-model:value="creaentialsPassword.clientId" />
</a-form-item>
<a-form-item label="用户名" name="username">
<a-input type="input" style="width: 200px" v-model:value="creaentialsPassword.username" />
</a-form-item>
<a-form-item label="密码" name="password">
<a-input
type="password"
style="width: 200px"
v-model:value="creaentialsPassword.password"
/>
</a-form-item>
</div>
</a-form>
<div class="flex justify-start">
<a-button class="mr-5" @click="prevStep">上一步</a-button>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive, ref, watch } from 'vue';
import { Input, Form, Select, Button } from 'ant-design-vue';
export default defineComponent({
components: {
[Form.name]: Form,
[Form.Item.name]: Form.Item,
[Input.name]: Input,
[Select.name]: Select,
[Button.name]: Button,
},
emits: ['prev', 'next'],
setup(_, { emit }) {
const formRef = ref();
const creaentialsPassword = reactive({
isCreaentials: false,
creaentialsType: 'Access token',
token: '',
publicKey: '',
clientId: '',
username: '',
password: '',
});
const rules = {
token: [{ required: true, message: '请输入访问令牌', trigger: 'blur' }],
publicKey: [{ required: true, message: '请输入RSA公钥', trigger: 'blur' }],
clientId: [{ required: true, message: '请输入客户端ID', trigger: 'blur' }],
username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
};
const options = ref([
{
value: 'Access token',
label: 'Access token',
},
{
value: 'X.509',
label: 'X.509',
},
{
value: 'MQTT Basic',
label: 'MQTT Basic',
},
]);
// 切换是否添加凭证
const checked = () => {
creaentialsPassword.isCreaentials = !creaentialsPassword.isCreaentials;
formRef.value.resetFields();
creaentialsPassword.creaentialsType = 'Access token';
};
const prevStep = () => {
emit('prev');
};
watch(creaentialsPassword, (newValue) => {
emit('next', newValue);
});
// 切换凭证类型时,重置字段
const handleChange = (value: string) => {
if (value === 'Access token') {
resetCreantialsType();
} else if (value === 'X.509') {
resetCreantialsType();
} else {
resetCreantialsType();
}
};
// 切换凭证类型时,重置字段
function resetCreantialsType() {
creaentialsPassword.token = '';
creaentialsPassword.publicKey = '';
creaentialsPassword.clientId = '';
creaentialsPassword.username = '';
creaentialsPassword.password = '';
}
// 重置所有字段
function resetFields() {
creaentialsPassword.isCreaentials = false;
creaentialsPassword.creaentialsType = 'Access token';
creaentialsPassword.token = '';
creaentialsPassword.publicKey = '';
creaentialsPassword.clientId = '';
creaentialsPassword.username = '';
creaentialsPassword.password = '';
formRef.value.resetFields();
}
// 表单验证
function validate() {
if (creaentialsPassword.isCreaentials) {
return formRef.value.validate();
}
return true;
}
return {
options,
formRef,
creaentialsPassword,
handleChange,
prevStep,
checked,
labelCol: { style: { width: '150px' } },
wrapperCol: { span: 18 },
resetFields,
validate,
rules,
};
},
});
</script>
<style lang="less" scoped>
.step2 {
width: 700px;
margin: 0 auto;
}
</style>