captchaInput.vue
3.78 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
<template>
<div class="row-center captcha_input_wrapper">
<input
v-for="(item,index) in captchas"
:key="index"
v-model="item.num"
:id="'captcha'+index"
@input="inputFinash(index)"
@focus="adjust(index)"
@keydown="inputDirection(index)"
class="captcha_input_box row-center"
:style="customStyle"
type="tel"
maxlength="1">
</div>
</template>
<script>
export default {
name: 'CaptchaInput',
props: {
customStyle: {
type: String,
required: false,
default: ''
},
captchasLenght: {
type: Number,
required: false,
default: 4
},
captchaList: {
type: String,
required: true
}
},
data() {
return {
// 当前输入框
activeInput: 0,
captchas: [
// { num: '' },
// { num: '' },
// { num: '' },
// { num: '' },
// { num: '' },
// { num: '' }
]
}
},
watch: {
captchas: {
handler: function (value) {
console.log(value)
const str = value.map((item) => {
return item.num
}).join('')
this.$emit('update:captchaList', str)
},
deep: true
},
captchasLenght: {
handler: function (value) {
for (let index = 0; index < this.captchasLenght; index++) {
// const element = array[index];
// list[index] = this.item
// this.captchas[index] = { num: '' }
this.$set(this.captchas, index, { num: '' })
}
},
immediate: true
}
},
computed: {
test() {
const list = []
for (let index = 0; index < this.captchasLenght; index++) {
// const element = array[index];
list[index] = this.item
}
return list
// return list.map((item) => {
// item = this.item
// return item
// })
}
},
methods: {
// 自动校准输入顺序
adjust(index) {
const dom = document.getElementById('captcha' + this.activeInput)
if (index !== this.activeInput && dom) {
dom.focus()
}
},
// 控制前后方向
inputDirection(index) {
const val = this.captchas[index].num
// debugger
console.log(event.keyCode)
// 回退键处理
if (event.keyCode == 8 && val == '') {
console.log(index)
// 重新校准
const dom = document.getElementById('captcha' + (index - 1))
// 解决第一位为-1 导致能选其他空的位置输入
index = index - 1
this.activeInput = index == -1 ? 0 : index
if (dom) dom.focus()
}
if (event.keyCode != 8 && val != '') {
const dom = document.getElementById('captcha' + (index + 1))
this.activeInput = index + 1
if (dom) dom.focus()
}
},
// 输入框相互联动
inputFinash(index) {
const val = this.captchas[index].num
this.activeInput = val ? index + 1 : index - 1
const dom = document.getElementById('captcha' + this.activeInput)
if (dom) dom.focus()
if (index == this.captchas.length - 1) {
const code = this.captchas.map((x) => x.num).join('')
if (code.length == 6) {
this.$emit('finish', code)
}
}
}
}
}
</script>
<style lang='scss'>
.row-center {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.captcha_input_wrapper {
width: 100%;
}
.captcha_input_box {
width: 48px;
height: 60px;
margin-right: 12px;
background: rgba(255, 255, 255, 1);
border-radius: 6px;
border: 1px solid #dddddd;
font-size: 40px;
text-align: center;
color: #1e243a;
}
</style>