index.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
<template>
<div class="device-index">
<div class="sprite-icon icon-device-bg">
绑定设备SN码和车辆VIN码<br />
关联硬件设备和车辆信息
</div>
<div class="box">
<div class="step">
第一步,扫描设备SN码
</div>
<div class="sn input-item">
SN码
<div class="sn-input input">
<input
type="number"
placeholder="点击手动输入SN码"
v-model="sn"
maxlength="11"
:class="{'inputErr':snErr}"
@blur="checkInstallation('sn')"
:pattern="/^[0-9]*[1-9][0-9]*$/"
>
<div
class="err"
v-show="snErr && sn"
>{{snText}}</div>
</div>
<div
class="sprite-icon icon-device-scan"
@click="scanQRCode"
></div>
</div>
</div>
<div class="box">
<div class="step">
第二步,拍照上传车辆VIN码
</div>
<div
class="vin-box"
@click="chooseImage"
>
<template v-if="vinImg">
<img :src="vinImg">
</template>
<template v-else>
<div class="add">
<div class="sprite-icon icon-device-add"></div>
上传安装设备的车辆VIN码
<div class="tips">
注:<div>VIN码是车辆车架号,通常位于<br>汽车前挡风玻璃左/右下角</div>
</div>
</div>
</template>
</div>
<div class="vin input-item">
VIN码
<div class="vin-input input">
<input
type="text"
placeholder="点击手动输入车辆VIN码"
v-model="vin"
maxlength="17"
:class="{'inputErr':vinErr}"
@blur="checkInstallation('vin')"
@input="vinInput(vin)"
>
<div
class="err"
v-show="vinErr && vin"
>{{vinText}}</div>
</div>
</div>
</div>
<div
:class="['dev-btn',{'btn-disabled':!sn||!vin || snErr || vinErr}]"
@click="submit"
@touchstart="touchstart"
@touchend="touchend"
>提交</div>
<device-tabbar
routeName="device.list"
:tabIndex="0"
></device-tabbar>
</div>
</template>
<script type='text/ecmascript-6'>
import DeviceTabBar from '@c/DeviceTabBar'
import WxConfig from '@u/WxConfig'
const WX = new WxConfig(['scanQRCode', 'chooseImage', 'getLocation'])
import { common, device } from '@r/api'
import AMap from 'AMap'
const vin_text = '该设备码已被关联,请重新输入或扫描'
const sn_text = '该设备码已被关联,请重新输入或扫描'
export default {
name: 'index',
data () {
return {
addressInfo: {},
sn: '',
vin: '',
vinImg: '',
snErr: false,
vinErr: false,
vinText: '',
snText: ''
}
},
components: {
'device-tabbar': DeviceTabBar
},
mounted () {
this.getLocation()
},
watch: {
'sn' (n) {
n = n.replace('.', '')
this.sn = n.substring(0, 11)
this.snErr = this.sn.length < 11
},
},
methods: {
vinInput (vin) {
this.vin = vin.substring(0, 17)
let rex = /^[A-HJ-NPR-Z\d]{17}$/
this.vinErr = !rex.test(this.vin)
},
scanQRCode () {
WX.scanQRCode(1).then(res => {
let { resultStr } = res
this.sn = resultStr.split(',')[1]
this.checkInstallation('sn')
})
},
getLocation () {
WX.getLocation().then(res => {
AMap.plugin('AMap.Geocoder', () => {
const geocoder = new AMap.Geocoder()
let { longitude, latitude } = res
let lnglat = [longitude, latitude]
geocoder.getAddress(lnglat, (status, result) => {
let { info, regeocode: { formattedAddress } } = result
if (status === 'complete' && info === 'OK') {
// result为对应的地理位置详细信息
this.addressInfo = {
longitude,
latitude,
address: formattedAddress
}
}
})
})
})
},
checkInstallation (name) {
let value = this[name]
if (!value) return
let text = {
sn: sn_text,
vin: vin_text
}
device.checkInstallation({
[name]: value,
}).then(res => {
if (res) {
let { code, message } = res
if (code == 0) {
this[`${name}Err`] = false
} else {
this[`${name}Err`] = true
this[`${name}Text`] = text[name]
this.showToast(message)
}
} else {
this.showToast('系统异常')
}
console.log(res)
})
},
chooseImage () {
WX.chooseImage().then(res => {
let [data] = res
let { localData } = data
let suffix = ''
let imgUrl = ''
if (localData.indexOf(';') != -1) {
suffix = localData.split(';')[0]
imgUrl = localData.split('base64,')[1]
}
let type = suffix ? suffix.split('/')[1] : 'jpg'
if (!imgUrl) imgUrl = localData
if (type == 'unknown') type = 'jpg'
if (type.toLowerCase() != 'jpg' && type.toLowerCase() != 'jpeg' && type.toLowerCase() != 'png') {
this.showToast('图片格式错误')
return
}
this.showLoading('上传中...')
common.uploadImage({
file: `${type}@${imgUrl}`
}).then(img => {
console.log(img)
if (img) {
let { data } = img
this.showLoading('识别中...')
device.scanVin({ imageUrl: data }).then(vinRes => {
if (vinRes) {
console.log(vinRes)
let { code } = vinRes
if (code == 0) {
this.vinImg = img.data
this.vin = vinRes.data.vin
this.checkInstallation('vin')
} else {
this.showToast('识别失败请重试', 'close')
this.vinImg = ''
this.showInput = false
}
} else {
this.showToast('识别失败请重试', 'close')
this.vinImg = ''
this.showInput = false
}
})
} else {
this.showToast('上传失败')
this.vinImg = ''
this.showInput = false
}
})
})
},
submit () {
let { sn, vin, snErr, vinErr, addressInfo } = this
if (!sn || !vin || snErr || vinErr) return
console.log('提交')
let { openid } = this.$store.state
this.showLoading('绑定中...')
let params = {
sn,
vin,
openid,
...addressInfo
}
device.installation(params).then(res => {
if (res) {
let { code, message, data } = res
if (code == 0) {
this.dialog({
title: '提示',
message: '提交成功',
confirmButtonText: '返回',
showCancelButton: false
}).then(() => {
this.showToast(data)
Object.assign(this.$data, this.$options.data())
this.getLocation()
})
} else {
this.showToast(message)
}
} else {
this.showToast('系统异常')
}
})
}
}
}
</script>
<style scoped lang='scss'>
.device-index {
padding-bottom: 44px;
.icon-device-bg {
font-size: 16px;
color: #ffffff;
display: flex;
align-items: center;
justify-content: center;
line-height: 24px;
}
.box {
background: #ffffff;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
margin-bottom: 10px;
padding: 20px 16px;
.step {
padding-left: 20px;
font-size: 14px;
color: #333333;
position: relative;
margin-bottom: 20px;
&::before,
&::after {
content: "";
position: absolute;
top: 50%;
transform: translateY(-50%);
border-radius: 100%;
}
&::before {
border: 1px solid #a7d1ff;
width: 10px;
height: 10px;
left: 0;
}
&::after {
background: #4a9dff;
width: 4px;
height: 4px;
left: 4px;
}
}
.input-item {
padding: 0 21px 0 22px;
font-size: 16px;
color: #4a9dff;
display: grid;
align-items: center;
.input {
border-bottom: 1px solid #666666;
position: relative;
padding: 2px 6px;
input {
border: none;
font-size: 12px;
color: #666666;
height: 17px;
width: 100%;
font-size: 14px;
}
.inputErr {
color: #fa5151;
}
.err {
font-size: 10px;
color: #fa5151;
position: absolute;
left: 0;
bottom: -15px;
}
}
}
.sn {
grid-template-columns: 39px 1fr 20px;
&-input {
max-width: 220px;
margin: 0 20px 0 1px;
}
}
.vin {
grid-template-columns: 43px 1fr;
}
.vin-box {
width: 300px;
height: 180px;
margin: 0 auto 20px;
box-sizing: border-box;
border: 1px dotted #979797;
display: flex;
align-items: center;
justify-content: center;
.add {
font-size: 16px;
color: #999999;
text-align: center;
.icon-device-add {
margin: 0 auto 10px;
}
.tips {
margin-top: 12px;
color: #c6c6c6;
font-size: 10px;
display: flex;
align-items: flex-start;
justify-content: center;
div {
text-align: left;
}
}
}
}
}
.dev-btn {
width: 198px;
height: 32px;
display: grid;
place-items: center;
border-radius: 6px;
font-size: 14px;
color: #ffffff;
background: #4fa3ff;
margin: 20px auto;
}
.btn-disabled {
background: #d2d2d2;
}
}
</style>