webpage.js
4.96 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
// packagePersonal/pages/webpage/webpage.js
const util = require('../../utils/util.js')
const restapi = require('../../utils/restapi.js')
var app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
jumpLink: '',
//爱车追踪分享参数
lat: '',
lng: '',
shareTime: '',
nickName: '',
shareUrl: '',
currentPage: '',
//车辆信息浏览开始时间
vehicleStartTime: 0,
driverStartTime: 0,
gascardStartTime: 0
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log(options)
var self = this
if (options.scene) {
let id = decodeURIComponent(options.scene).split('=')[1]
return this.getSceneInfo(id)
}
var decodePath = decodeURIComponent(options.path)
self.setData({
currentPage: decodePath
})
var shareLink = decodeURIComponent(options.shareLink)
if (decodePath.indexOf('ddh-smartcar/czn/aczj') > -1) {
self.setData({
shareUrl: shareLink
})
}
// 链接如果包含sessionId则进行替换
var flag = decodePath.indexOf('sessionId=') > 0
if (flag) {
if (decodePath.indexOf('{sessionId}') > 0) {
decodePath = decodePath.format({ sessionId: restapi.getSessionId() })
} else {
decodePath = decodePath.format(restapi.getSessionId())
}
}
// 链接是否要求登录才能访问
var needLogin = decodePath.endsWith('#need_login')
if (needLogin) {
decodePath = decodePath.replace('#need_login', '')
wx.setStorageSync('WebPagePath', decodePath)
restapi.checkLogin(function (response) {
if (response) {
wx.removeStorageSync('WebPagePath')
self.setData({
jumpLink: decodePath + '#miniProgram_redirect'
})
}
})
} else {
self.setData({
jumpLink: decodePath + '#miniProgram_redirect'
})
}
//如果是爱车追踪则要设置分享的微信昵称
var indexNum = decodePath.indexOf('ddh-smartcar/czn/aczj/vehicleIndex')
if (indexNum > -1) {
if (!app.globalData.userInfo) {
app.userInfoReadyCallback = userInfo => {
if (userInfo != null) {
this.setData({
nickName: app.globalData.userInfo.nickName
})
}
}
} else {
this.setData({
nickName: app.globalData.userInfo.nickName
})
}
}
},
/**
* 接收H5传过来的参数
*/
onMessage: function (e) {
console.log('onMessage')
console.log(e)
var obj = e.detail.data[0]
console.log(obj)
if (obj.source && obj.source === 'renew') {
var userId = obj.userId
app.globalData.renewDevice = userId
} else {
var lat = obj.lat
var lng = obj.lng
var shareTime = obj.startTime
this.setData({
lat: lat,
lng: lng,
shareTime: shareTime
})
}
},
getSceneInfo(id) {
restapi.qrCodeScene(
{
id
},
res => {
console.log(res)
let { returnCode, data } = res
let op = {}
if (returnCode == '0') {
if (data) {
data.split('&').map(item => {
const [key, val] = item.split('=')
op[key] = val
})
this.setData({
...op
})
console.log(op)
}
wx.hideHomeButton()
}
}
)
},
onShow: function () {
//不同页面埋点
var alias = this.data.currentPage
if (alias.indexOf('vehicle/toEdit') > 0) {
var vehicleStartTime = Date.now()
this.setData({
vehicleStartTime: vehicleStartTime
})
var timeStr = util.dateToStr(new Date(), 'hh:mm')
app.recordClickEvent('车智能使用行为', {
车辆信息访问时间点: timeStr
})
} else if (alias.indexOf('gd/home') > 0) {
var driverStartTime = Date.now()
this.setData({
driverStartTime: driverStartTime
})
} else if (alias.indexOf('shop/toCommodityDetail') > 0) {
var gascardStartTime = Date.now()
this.setData({
gascardStartTime: gascardStartTime
})
}
},
onUnload: function () {
var path = this.data.currentPage
if (path.indexOf('vehicle/toEdit') > 0) {
var vehicle = this.data.vehicleStartTime
if (vehicle > 0) {
app.globalData.myViehicleDuration += Date.now() - vehicle
}
} else if (path.indexOf('gd/home') > 0) {
var driver = this.data.driverStartTime
if (driver > 0) {
app.globalData.goodDriverDuration += Date.now() - driver
}
} else if (path.indexOf('shop/toCommodityDetail') > 0) {
var gas = this.data.gascardStartTime
if (gas > 0) {
app.globalData.buyRefuelCardDuration += Date.now() - gas
}
}
}
})