LicensePlate.vue
10 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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<template>
<van-action-sheet
v-model="hidden"
lock-scroll
>
<div class="license-plate">
<div class="license-plate-box">
<div class="license-plate-title flex">
<button
class="cancel btn"
@click="toggle"
>
取消
</button>
<button
class="done btn"
:disabled="plateNoErr"
hover-class="btn-hover"
@click="done"
>
完成
</button>
</div>
<div class="license-plate-content">
<div class="title">
请输入车牌号码
</div>
<div
class="plate-box flex"
:key="plateKey"
>
<template v-for="(item,index) in plateNo">
<div
:key="index"
:class="['item flex', {'active':index==plateIndex}, {'has-icon':index==2}]"
@click="changeIndex(index)"
>
{{item}}
<template v-if="index==2">
<div class="arc y-center"></div>
</template>
</div>
</template>
</div>
<template v-if="showPasteButton">
<button
class="paste"
@click="paste"
@touchstart="touchstart"
@touchend="touchend"
>
一键粘贴
</button>
</template>
</div>
<div class="license-plate-bottom">
<div
:class="['license-plate-keyboard' , {'type-2':plateIndex != 0}]"
:key="dataKey"
>
<template v-if="plateIndex == 0">
<div class="grid city">
<template v-for="(item,index) in cityList">
<div
:key="index"
class="item"
@click="changePlateNo(item)"
>
{{item}}
</div>
</template>
</div>
</template>
<template v-else>
<div class="keyboard-left">
<div class="grid number">
<template v-for="(item,index) in 10">
<button
:key="index"
class="item"
@click="changePlateNo(item - 1)"
:disabled="plateIndex == 1 || (keyboardDisabled && plateIndex == 7)"
>
{{item - 1}}
</button>
</template>
</div>
<div class="grid letter">
<template v-for="(item,index) in letter">
<button
:key="index"
class="item"
@click="changePlateNo(item)"
:disabled="keyboardDisabled && plateIndex == 7"
>
{{item}}
</button>
</template>
</div>
<div class="grid suffix">
<template v-for="(item,index) in suffix">
<button
:key="index"
class="item"
@click="changePlateNo(item)"
:disabled="plateIndex < 6 || (keyboardDisabled && plateIndex == 7)"
>
{{item}}
</button>
</template>
</div>
</div>
</template>
<div
class="keyboard-del flex"
@click="del"
>
<img
src="https://img02.glsx.com.cn/weapp/resource/miniprogram/crowdfunding/KeyboardDeleteOutlined.png?v=1.0.3"
class="icon"
>
</div>
</div>
</div>
</div>
</div>
</van-action-sheet>
</template>
<script type='text/ecmascript-6'>
const suffix = '港澳学挂',
city = '京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼',
platNumberRex = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼][A-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学港澳])$/
export default {
name: 'LicensePlate',
data () {
return {
plateKey: Date.now(),
dataKey: Date.now(),
plateNoErr: true,
plateIndex: 0,
plateNo: new Array(8).fill(''),
hidden: true,
cityList: [],
letter: [],
suffix: [],
keyboardDisabled: false,
hidden: false,
showPasteButton: false
}
},
mounted () {
/**navigator.clipboard 只支持https */
if (navigator.clipboard) this.showPasteButton = true
this.cityList = [...city]
this.letter = [...'ABCDEFGHJKLMNPQRSTUVWXYZ']
this.suffix = [...suffix]
this.dataKey = Date.now()
},
methods: {
changeIndex (index) {
let {
plateIndex
} = this
if (plateIndex == index) return
plateIndex = index
this.plateIndex = index
},
changePlateNo (value) {
let {
plateIndex
} = this
const index = plateIndex
if (plateIndex < 7) ++plateIndex
this.plateNo[index] = value
this.plateIndex = plateIndex
this.check()
},
del () {
let {
plateIndex,
plateNo
} = this
const index = plateIndex
if (!plateNo[index] && index == 0) return
if (plateIndex > 0) --plateIndex
this.plateNo[index] = ''
this.plateIndex = plateIndex
this.check()
},
check () {
let {
plateNo
} = this,
plateNoErr = false
/**车牌后缀 */
if (plateNo[6] && plateNo[7] && suffix.includes(plateNo[6])) this.plateNo[7] = ''
if (!platNumberRex.test(plateNo.join(''))) plateNoErr = true
this.plateNoErr = plateNoErr
this.keyboardDisabled = suffix.includes(plateNo[6])
this.plateKey = Date.now()
},
toggle () {
let {
hidden
} = this
this.hidden = !hidden
},
done () {
let {
plateNo
} = this,
value = plateNo.join('')
this.$emit('done', { value })
this.toggle()
},
async paste () {
if (navigator.clipboard) {
try {
let data = await navigator.clipboard.readText(),
value = data.toUpperCase(),
plateNo = [...value].filter(e => e && e != '' && /[\u4e00-\u9fa5\w]/.test(e))
while (plateNo.length < 8) plateNo.push('')
plateNo = plateNo.splice(0, 8)
if (platNumberRex.test(plateNo.join(''))) {
this.plateNo = plateNo
this.check()
}
} catch (error) {
if(error.name == 'NotAllowedError') this.showToast('粘贴板被禁用,请先去开启浏览器的粘贴板使用权限')
else this.showToast(error.name || '粘贴失败')
console.log(error)
}
}
}
}
}
</script>
<style scoped lang='scss'>
.license-plate {
&-box {
position: relative;
width: 100vw;
height: 80vh;
background-color: #fff;
}
&-title {
padding: 10px 10px;
justify-content: space-between;
}
.cancel {
background-color: transparent !important;
}
.done {
background-color: #f13c3d;
color: #fff;
}
.btn {
height: 30px;
margin: 0 !important;
font-size: 14px;
display: flex;
align-items: center;
border: none;
padding: 0 14px;
border-radius: 5px;
}
.btn[disabled] {
background-color: #f7f7f7;
color: rgba(0, 0, 0, 0.3);
}
.btn-hover {
opacity: 0.75;
}
&-content {
.title {
font-size: 20px;
font-weight: 700;
text-align: center;
padding: 10px 0 20px;
}
.plate-box {
justify-content: center;
.item {
width: 30px;
height: 40px;
background: rgba($color: #b6b6b6, $alpha: 0.26);
border-radius: 4px;
border: 1px solid rgba($color: #b6b6b6, $alpha: 0.26);
margin-right: 5px;
justify-content: center;
}
.has-icon {
position: relative;
margin-left: 9px;
}
.active {
border-color: #f13c3d;
}
.arc {
width: 4px;
height: 4px;
border-radius: 50%;
background-color: #000;
left: -9px;
}
}
.paste {
background-color: #f13c3d;
color: #fff;
margin: 30px !important;
width: calc(100% - 60px);
height: 46px;
border: none;
border-radius: 5px;
}
}
&-bottom {
position: absolute;
width: 100%;
min-height: 400rpx;
left: 0;
bottom: 0;
background: rgba($color: #b6b6b6, $alpha: 0.16);
}
&-keyboard {
padding: 10px;
display: flex;
.keyboard-del {
width: 60px;
height: auto;
margin-left: auto;
background-color: #fff;
justify-content: center;
border-radius: 8rpx;
.icon {
width: 30px;
height: 30px;
}
}
.city {
width: calc(100% - 70px);
grid-template-columns: repeat(6, 1fr);
}
.grid {
display: grid;
gap: 7px;
}
.letter {
grid-template-columns: repeat(7, 1fr);
}
.number {
grid-template-columns: repeat(10, 1fr);
margin-bottom: 7px;
}
.suffix {
grid-template-columns: repeat(4, 1fr);
margin-top: 7px;
}
.item {
height: 30px;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
padding: 0 !important;
font-size: 12px;
margin: 0 !important;
border: none;
}
.item[disabled] {
opacity: 0.2;
}
}
.type-2 {
display: block;
position: relative;
.keyboard-left {
width: 100%;
}
.number {
width: 100%;
}
.letter,
.suffix {
width: calc(100% - 70px);
}
.keyboard-del {
position: absolute;
right: 10px;
top: 47px;
height: calc(100% - 57px);
}
}
}
</style>