smartplus.js
20.2 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
const app = getApp();
const restapi = require('./restapi');
const util = require('../../../utils/util.js');
const filter = require('../../../utils/filter.js');
import mapapi from '../../../utils/mapapi.js'
const lat = 22.587337 // 默认公司
const lng = 113.991485
Page(filter.identityFilter({
/**
* 页面的初始数据
*/
data: {
authToast: false, // 是否显示授权提示框
// authType: true,
isBinded: false, //当前设备是否绑定
curDevType: 6, //当前的设备类型
shebList: [{
name: "记录仪",
type: 6,
index: 0
}, {
name: "驾宝OBD",
type: 1,
index: 1
}, {
name: "GPS",
type: 8,
index: 0
}, {
name: "云导航",
type: 2,
index: 2
}, {
name: "智能云镜",
type: 12134,
index: 2
}],
tabList: [{
label: '车安全',
children: [{
name: "记录仪",
type: 6
}, {
name: "GPS",
type: 8
}]
}, {
label: '车诊断',
children: [{
name: "驾宝OBD",
type: 1
}]
}, {
label: '车娱乐',
children: [{
name: "云导航",
type: 2
}, {
name: "智能云镜",
type: 12134
}]
}],
tabIndex: 0,
mapiconList: [{
url: '../../resources/images/icon-traffic.png',
activeUrl: '../../resources/images/icon-traffic-show.png',
name: '路 况',
callback: 'openTraffic'
}],
markers: [{
// 当前位置
id: 0,
iconPath: '../../resources/images/location.png',
width: '24px',
height: '33px',
longitude: lng,
latitude: lat,
}, {
id: 1,
iconPath: '../../resources/images/car.png',
width: '28px',
height: '50px',
longitude: lng,
latitude: lat,
}], // 地图标记
latitude: lat, //纬度
longitude: lng, //经度
includePoints: [{
latitude: lat,
longitude: lng
},
{
latitude: lat,
longitude: lng
}
],
carBanner: "../../resources/images/ad-img.png", //广告ban
defaultUserId: null, //默认的嘀嘀号,方便默认设备功能获取demo
bindDevList: [], //获取到的所有绑定设备列表
iconList: [], //获取到的设备功能列表
oldFunLists: [{ //默认设备功能列表
appName: "爱车追踪",
weappIcon: "../../resources/images/plus_aczz.png",
weappPath: `/packageSmartPlus/pages/vehicleTracking/index/index?protocolId=`
}, {
appName: "行车轨迹",
weappIcon: "../../resources/images/plus_xcgj.png",
weappPath: `/packageSmartPlus/pages/journey/index/index?userId={defaultUserId}`
}, {
appName: "云防盗",
weappIcon: "../../resources/images/plus_yfd.png",
weappPath: `/packageSmartPlus/pages/cloudSecurity/index/index?protocolId={defaultUserId}`
}],
myMileageVO: {},
tabHeight: 0,
// scrollTop: 500,
isTraffic: false,
isCar: true,
startY: 0,
titleText: '车联网+',
vehicles: {}, // 车辆信息
driving: {}, // 行驶数据
hasHeight: false,
windowHeight: 0
},
// 获取导航栏高度
getNavHeight(e) {
console.log(e)
this.setData({
tabHeight: e.detail
})
},
// 切换路况
openTraffic(e) {
this.setData({
isTraffic: !this.data.isTraffic
})
},
//栏目切换
chooseCar(e) {
console.log(e)
// let curT = e.currentTarget.dataset.type;
let index = e.currentTarget.dataset.index;
// 有绑定设备
let userId = ''
let type = ''
let name = ''
const arr = this.data.tabList[index].children
const bindList = this.data.bindDevList
let showIndex = -1
let bindListArr = bindList.map(item => item.deviceType)
if (bindList.length != 0) {
let bindObj = {}
for(let x in arr) {
showIndex = bindListArr.indexOf(arr[x].type)
if(showIndex != -1) break
}
bindObj = showIndex != -1 ? bindList[showIndex] : {}
if (bindObj) {
userId = bindObj.userId
type = bindObj.deviceType
name = bindObj.deviceTypeName
this.setData({
isBinded: true
})
}
if (userId) {
this.refreshDeviceInfo(userId)
this.getCarPosit(userId)
this.checkDevice({
userId: userId,
typeId: type,
name: name
})
} else {
this.setData({
isBinded: false,
iconList: this.data.oldFunLists
})
}
}
this.setData({
tabIndex: index
})
},
//获取车智能助手数据 /smartcar/getIndexInfo
refreshDeviceInfo: function (userId) {
let _this = this;
if (userId) {
wx.showLoading({
title: '加载中',
mask: true
})
restapi.getSmartCarindexInfo(userId, function (response) {
wx.hideLoading();
if (response.returnCode == "0" && response.data && response.data.deviceStatusVO && response.data.deviceStatusVO.userId && response.data.deviceStatusVO.deviceType) {
let smartCarAppList = response.data.smartCarAppList;
let deviceStatusVO = response.data.deviceStatusVO;
let myMileageVO = response.data.myMileageVO;
_this.setData({
curDevType: deviceStatusVO.deviceType,
isBinded: true,
myMileageVO: myMileageVO
});
if (deviceStatusVO && deviceStatusVO.userId) {
app.globalData.selectedDevice = {
userId: deviceStatusVO.userId,
typeNmae: deviceStatusVO.deviceName,
typeId: deviceStatusVO.deviceType
}
}
if (smartCarAppList && smartCarAppList.length > 0) {
smartCarAppList.forEach(function (value, index) {
switch (value.appName) {
case '爱车追踪':
value.weappPath = `/packageSmartPlus/pages/vehicleTracking/index/index?protocolId=${deviceStatusVO.userId}`;
break;
case '行车轨迹':
value.weappPath = `/packageSmartPlus/pages/journey/index/index?userId=${deviceStatusVO.userId}&merchantName=${myMileageVO.merchantName}&activeDuration=${myMileageVO.activeDuration}&time=${myMileageVO.showTime}`;
break;
case '云防盗':
value.weappPath = `/packageSmartPlus/pages/cloudSecurity/index/index?protocolId=${deviceStatusVO.userId}`;
break;
case '直播':
value.weappPath = `/packageSmartPlus/pages/live/livePlay/livePlay?protocolId=${deviceStatusVO.userId}`;
break;
case '回放列表':
value.weappPath = `/packageSmartPlus/pages/live/reviewPlayList/reviewPlayList?protocolId=${deviceStatusVO.userId}`;
break;
}
})
_this.setData({
// curUserId: deviceStatusVO.userId,
iconList: smartCarAppList
});
app.globalData.smartCarAppList = smartCarAppList;
}
} else {
_this.setData({
iconList: _this.data.oldFunLists
})
}
}, function () {
})
}
},
//检查设备状态 /smartcar/checkDeviceStatus
getSmartCarInfo() {
let _this = this;
//当前全局userId和当前页记录的userId不一致,根据全局的userId重新请求主页信息
let userId = app.globalData.selectedDevice.userId;
if (!userId || userId == '' || userId == 'undefined') {
restapi.checkDeviceStatus(userId, function (response) {
wx.hideLoading();
if (response.returnCode == "0" && response.data && response.data.userId && response.data.deviceType) {
let deviceStatus = response.data;
_this.data.tabList.forEach((item, index) => {
item.children.forEach(i => {
if (i.type == deviceStatus.deviceType) {
_this.setData({
tabIndex: index,
isBinded: true
})
return
}
})
})
_this.setData({
curDevType: deviceStatus.deviceType,
isBinded: true
})
_this.refreshDeviceInfo(deviceStatus.userId);
app.globalData.selectedDevice = {
userId: deviceStatus.userId,
typeNmae: deviceStatus.deviceName,
typeId: deviceStatus.deviceType
}
_this.getCarPosit(userId)
} else { //如果接口没返回数据
_this.setData({
iconList: _this.data.oldFunLists,
isBinded: false
})
}
}, function () {})
} else {
_this.refreshDeviceInfo(userId);
}
},
//获取设备绑定列表 /smartcar/getBindInfo
refreshBindingStatus() {
let _this = this;
let sourcefrom = '';
restapi.getDeviceBindInfo(sourcefrom, function (response) {
if (response && response.returnCode == 0 && response.bindInfoList && response.bindInfoList.length > 0) {
let bindInfoList = response.bindInfoList;
_this.setData({
bindDevList: bindInfoList
})
} else {
_this.setData({
bindDevList: [],
isBinded: false
})
}
})
},
//获取爱车位置
getCarPosit: function (id) {
let userId = id || app.globalData.selectedDevice.userId;
console.log(userId);
console.log(app.globalData.userInfo);
var _this = this
if (app.globalData.userInfo && app.globalData.userInfo.phone) {
restapi.getVehicles(function (res) {
if (res.returnCode == '0' && res.data && res.data[0]) {
_this.setData({
['vehicles.brandLogo']: res.data[0].brandLogo,
['vehicles.brandName']: res.data[0].brandName,
['vehicles.plateNumber']: res.data[0].plateNumber
})
console.log(_this.data.vehicles)
}
})
}
if (userId) {
restapi.getCarPosition(userId, function (res) {
if (res.returnCode == '0' && res.data) {
const position = res.data
const lat1 = position.googleLat ? position.googleLat : lat
const lng1 = position.googleLng ? position.googleLng : lng
mapapi.getDetailedAdrress(lng1, lat1, res => {
console.log(res)
_this.setData({
['vehicles.address']: res.address
})
console.log(_this.data.vehicles)
})
let distance = ''
if (!position.googleLat) {
distance = '获取车辆位置信息失败!'
_this.setData({
['markers[1].label.content']: distance,
})
} else {
const latitude1 = _this.data.markers[0].latitude
const longitude1 = _this.data.markers[0].longitude
const latitude2 = lat1
const longitude2 = lng1
mapapi.calculateDistan(longitude1, latitude1, longitude2, latitude2, res => {
console.log(res)
let mode = ''
if (res > 2000) {
mode = 'driving'
} else {
mode = 'walking'
}
mapapi.routePlanning(longitude1, latitude1, longitude2, latitude2, res => {
console.log(res)
if (res.mode == 'DRIVING') {
distance = `距您${res.distance / 1000}公里 驾车${res.duration}分钟`
} else {
distance = `距您${res.distance / 1000}公里 步行${res.duration}分钟`
}
_this.setData({
['markers[1].label.content']: distance,
})
}, mode)
})
}
let includePoints = [{
longitude: _this.data.includePoints[0].longitude,
latitude: _this.data.includePoints[0].latitude
},
{
longitude: lng1,
latitude: lat1
}
]
position.gpsTime = position.gpsTime ? new Date(position.gpsTime).format('yyyy-MM-dd hh:mm:ss') : ''
_this.setData({
['vehicles.gpsTime']: position.gpsTime,
latitude: lat1,
longitude: lng1,
includePoints,
['markers[1].longitude']: lng1,
['markers[1].latitude']: lat1
})
// _this.setIncludePoints(includePoints)
} else {
const lat1 = lat
const lng1 = lng
console.log(lng1, lat1)
mapapi.getDetailedAdrress(lng1, lat1, res => {
console.log(res)
_this.setData({
['vehicles.address']: res.address
})
})
}
})
} else {
const lat1 = lat
const lng1 = lng
console.log(lng1, lat1)
mapapi.getDetailedAdrress(lng1, lat1, res => {
console.log(res)
_this.setData({
['vehicles.address']: res.address
})
})
}
},
// 添加车辆
verifyMileage() {
console.log('1212')
if (!app.globalData.userInfo) {
this.jumpAuth();
return;
}
var vehicle = this.data.vehicles
var url = '/packagePersonal/pages/vehicle/vehicle?vehicle=true'
util.navigateTo(url, vehicle)
},
//获取ban广告图
getCarBanner: function () {
var _this = this;
restapi.getCarBanner(function (response) {
if (response.returnCode == "0" && response.data.length > 0) {
_this.setData({
carBanner: response.data[0]
})
}
})
},
jumpAuth() {
wx.navigateTo({
url: "/pages/auth/index/index"
})
},
//切换设备
checkDevice(device) {
console.log(app.globalData)
let accountId = app.globalData.accountId;
let unionId = app.globalData.unionId;
let presentDevice = {
accountId: accountId,
protocolId: device.userId,
deviceType: device.typeId,
unionId: unionId,
createdBy: accountId,
updatedBy: accountId
}
restapi.checkDevice(presentDevice, function (data) {
if (data.returnCode == '0' && data.data) {
app.globalData.selectedDevice = {
userId: device.userId,
typeNmae: device.name,
typeId: device.typeId
}
} else {
console.log('切换失败,请稍后重试');
}
}, function () {})
},
//跳转智能助手
jumpSmart(e) {
let weapppath = e.currentTarget.dataset.weapppath;
let pathUrl = e.currentTarget.dataset.pathurl;
let userId = app.globalData.selectedDevice.userId;
if (weapppath) {
wx.navigateTo({
url: weapppath
})
} else {
if (userId) {
pathUrl = pathUrl.format({
protocolId: userId
});
}
restapi.smartCarJumpToH5(pathUrl, function (response) {
if (response && response.returnCode == '0') {
wx.navigateTo({
url: '/pages/webpage/webpage?path=' + encodeURIComponent(response.data)
});
}
}, function (res) {})
}
},
//获取商户名称
getAppInfoName() {
var _this = this;
let merchantId = app.globalData.extConfig.merchantId;
if (!merchantId || merchantId == 'undefined') {
merchantId = 0;
}
restapi.getAppInfo(merchantId, function (res) {
if (res.returnCode == 0 && res.data) {
_this.setData({
titleText: res.data.appName
})
// wx.setNavigationBarTitle({
// title: res.data.appName
// })
}
})
},
//获取默认嘀嘀号
getTempleUserID() {
var _this = this;
restapi.getTempleUserID(function (res) {
if (res.returnCode == 0 && res.data) {
console.log(res.data);
_this.data.oldFunLists.map(function (item) {
item.weappPath = item.weappPath.replace("{defaultUserId}", res.data);
})
app.globalData.selectedDevice.userId = res.data
// wx.setNavigationBarTitle({
// defaultUserId: res.data,
// oldFunLists: _this.data.oldFunLists
// })
}
})
},
// 设置include-points
setIncludePoints(points) {
let _this = this
let MapContent = wx.createMapContext('map4select', _this)
MapContent.includePoints({
points: points,
padding: [200],
success: (e) => {
console.log(e)
}
})
},
// 当前定位
getLocation() {
console.log('getLocation')
const _this = this
wx.getLocation({
type: 'gcj02',
success(res) {
console.log(res)
const latitude1 = res.latitude
const longitude1 = res.longitude
const latitude2 = _this.data.markers[1].latitude
const longitude2 = _this.data.markers[1].longitude
let distance = ''
let includePoints = [{
latitude: latitude1,
longitude: longitude1
},
{
latitude: latitude2,
longitude: longitude2
}
]
// 设置include-points
// _this.setIncludePoints(includePoints)
mapapi.calculateDistan(longitude1, latitude1, longitude2, latitude2, res => {
let mode = ''
if (res > 2000) {
mode = 'driving'
} else {
mode = 'walking'
}
mapapi.routePlanning(longitude1, latitude1, longitude2, latitude2, res => {
console.log(res)
if (res.mode == 'DRIVING') {
distance = `距您${res.distance/1000}公里 驾车${res.duration}分钟`
} else {
distance = `距您${res.distance/1000}公里 步行${res.duration}分钟`
}
let currentPonit = {
id: 0,
iconPath: '../../resources/images/location.png',
width: '24px',
height: '33px',
longitude: longitude1,
latitude: latitude1
}
let label = {
content: distance,
color: '#FF7D11',
fontSize: '12px',
borderRadius: '10px',
bgColor: '#fff',
padding: ' 3px 15px'
}
_this.setData({
'markers[0]': currentPonit,
'markers[1].label': label,
longitude: longitude1,
latitude: latitude1,includePoints,
includePoints
})
// _this.setIncludePoints(includePoints)
}, mode)
})
}
})
},
// 切换定位
switchLocation(e) {
const status = e.currentTarget.dataset.status
if (status) {
this.setData({
longitude: this.data.markers[1].longitude,
latitude: this.data.markers[1].latitude,
isCar: status
})
} else {
this.setData({
longitude: this.data.markers[0].longitude,
latitude: this.data.markers[0].latitude,
isCar: status
})
}
},
// 导航
openLocation(e) {
const latitude = JSON.parse(this.data.markers[1].latitude)
const longitude = JSON.parse(this.data.markers[1].longitude)
wx.openLocation({
latitude,
longitude,
scale: 18,
address: this.data.vehicles.address
})
},
touchstart(e) {
this.setData({
startY: e.changedTouches[0].clientY
})
},
touchmove(e) {
const _this = this
let moveY = e.changedTouches[0].clientY
if (moveY < this.data.startY) {
util.getViewInfo('.device', res => {
_this.setData({
scrollTop: (res.height + this.data.tabHeight)
})
})
// this.setData({
// scrollTop: 238
// })
} else {
// util.getViewInfo('.device', res => {
// util.getViewInfo('.smartplus-content', info => {
// _this.setData({
// scrollTop: res.height + info.height
// })
// })
// })
this.setData({
scrollTop: this.data.windowHeight - 120
})
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
const _this = this
console.log('onload', this.data.scale)
wx.setNavigationBarTitle({ //修改headerTitle
title: app.globalData.extConfig.merchantName
})
wx.showLoading({
title: '加载中',
mask: true
})
this.getCarPosit(); //获取爱车位置
this.getSmartCarInfo(); //获取当前绑定设备信息
this.getAppInfoName() //这里获取到的值跟app公共里面的一样
this.getTempleUserID() //获取默认嘀嘀号
this.getLocation();
if (!this.data.hasHeight) {
util.promise(resolve => {
wx.getSystemInfo({
success(res) {
_this.setData({
scrollTop: res.windowHeight - 120,
windowHeight: res.windowHeight
})
console.log('onload', res, res.windowHeight)
}
})
resolve()
}).then(() => {
util.getViewInfo('.smartplus-content', info => {
console.log(info)
util.getViewInfo('.device', device => {
console.log(this.data.windowHeight, info.height, this.data.tabHeight, device.height)
this.setData({
infoHeight: JSON.parse(this.data.windowHeight - 120 - this.data.tabHeight - device.height),
hasHeight: true
})
})
})
})
}
// 设置默认
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
console.log('onshow', app.globalData)
const _this = this
if (app.globalData.selectedDevice.userId) {
this.data.shebList.forEach(item => {
console.log(item)
if (item.type == app.globalData.selectedDevice.typeId) {
_this.setData({
tabIndex: item.index
})
}
})
}
this.getLocation();
this.getCarPosit(); //获取爱车位置
util.promise(resolve => {
this.refreshBindingStatus()
resolve()
}).then(() => {
this.getSmartCarInfo(); //获取当前绑定设备信息
})
},
/**
* 监听登录成功--页面onShow时触发
*/
onLogin: function () {
},
// 去绑定设备
toBindDevice: function () {
console.log(this.data.authToast)
if (this.data.authToast) {
wx.navigateTo({
url: "/pages/auth/index/index"
})
} else {
wx.navigateTo({
url: '/packageSmartPlus/pages/device/bindDevice/index',
})
}
}
}))