LicensePlate.js
3.16 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
// components/LicensePlate/LicensePlate.js
const suffix = '港澳学挂',
city = '京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼',
platNumberRex = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼][A-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学港澳])$/
let timer
Component({
options: {
styleIsolation: 'shared'
},
/**
* 组件的属性列表
*/
properties: {
showNavHeight:Boolean,
navheight:Number
},
/**
* 组件的初始数据
*/
data: {
plateNoErr: true,
plateIndex: 0,
plateNo: new Array(8).fill(''),
hidden: true
},
/**
* 组件的方法列表
*/
methods: {
noScroll(){},
changeIndex(e) {
let {
index
} = e.currentTarget.dataset, {
plateIndex
} = this.data
if (plateIndex == index) return
plateIndex = index
this.setData({
plateIndex
})
},
changePlateNo(e) {
let {
value
} = e.currentTarget.dataset, {
plateIndex
} = this.data
const index = plateIndex
if (plateIndex < 7) ++plateIndex
this.setData({
[`plateNo[${index}]`]: value,
plateIndex
})
this.check()
},
async paste() {
let {
data
} = await wx.getClipboardData(),
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.setData({
plateNo
})
this.check()
}
},
del() {
let {
plateIndex,
plateNo
} = this.data
const index = plateIndex
if(!plateNo[index] && index == 0) return
if (plateIndex > 0) --plateIndex
this.setData({
[`plateNo[${index}]`]: '',
plateIndex
})
this.check()
},
check() {
let {
plateNo
} = this.data,
plateNoErr = false
/**车牌后缀 */
if (plateNo[6] && plateNo[7] && suffix.includes(plateNo[6])) this.setData({
[`plateNo[${7}]`]: ''
})
if (!platNumberRex.test(plateNo.join(''))) plateNoErr = true
this.setData({
plateNoErr,
keyboardDisabled: suffix.includes(plateNo[6])
})
wx.vibrateShort()
},
toggle() {
let {
hidden
} = this.data
this.setData({
hidden: !hidden
})
if (!hidden) {
if (timer) clearTimeout(timer)
this.setData({
animation: true
}, () => {
timer = setTimeout(() => {
this.setData({
animation: false
})
}, 500)
})
}
},
done() {
let {
plateNo
} = this.data,
value = plateNo.join('')
this.triggerEvent('done', {
value
})
this.toggle()
}
},
lifetimes: {
ready() {
this.setData({
cityList: [...city],
letter: [...'ABCDEFGHJKLMNPQRSTUVWXYZ'],
suffix: [...suffix]
})
}
}
})