webViewCommon.js
5.76 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
// pages/webView/webViewCommon.js — 与 webView、h5Full(全屏 web-view)共用逻辑
import commonApi from '~/api/api'
const WEBVIEW_H5_TOKENS_KEY = 'dj_boutique_webview_h5_tokens'
let time
export function createWebViewPage(sharePagePath) {
return {
data: {
flag: true,
isH5Bind: false,
shareTitle: '报告详情'
},
getBaseUrl(url) {
if (!url || typeof url !== 'string') return ''
const idx = url.indexOf('?')
return idx >= 0 ? url.slice(0, idx) : url
},
appendParamsToUrl(url, params) {
if (!params || typeof params !== 'object' || Object.keys(params).length === 0)
return url
const searchParams = Object.entries(params)
.filter(([, v]) => v != null && v !== '')
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
.join('&')
if (!searchParams) return url
const separator = url.includes('?') ? '&' : '?'
return `${url}${separator}${searchParams}`
},
getParams(str) {
let params = str.split('?')
if (params.length < 2) return {}
let param = params[1].split('&')
let obj = {}
for (let i = 0; i < param.length; i++) {
let paramsA = param[i].split('=')
let key = paramsA[0]
let value = paramsA[1]
obj[key] = value
}
return obj
},
onLoad: function (options) {
wx.$util.showLoading('加载中...')
let decodeUrl = decodeURIComponent(options.url)
const baseUrl = this.getBaseUrl(decodeUrl)
try {
const stored = wx.getStorageSync(WEBVIEW_H5_TOKENS_KEY)
if (stored) {
const tokens = JSON.parse(stored)
const storedData = tokens[baseUrl]
if (storedData && typeof storedData === 'object') {
decodeUrl = this.appendParamsToUrl(decodeUrl, storedData)
}
}
} catch (e) {}
let param = this.getParams(decodeUrl)
if (param.urlParam) {
let query = JSON.parse(param.urlParam)
this.refresh = query.refresh
}
console.log(decodeUrl)
this.setData({
url: decodeUrl
})
wx.hideLoading()
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']
})
if (param.needIntercept) {
wx.enableAlertBeforeUnload &&
wx.enableAlertBeforeUnload({
message: '您还没有完成本次短课程学习,是否确定退出?',
success: modalRes => {
if (modalRes.confirm) {
wx.navigateBack()
}
}
})
this.setTime(param)
}
// AI收车报告,特殊处理跳转,将登录人名称带入
if (decodeUrl.includes('/ai-purchase') && options?.pushed) {
let userInfo = wx.$util.getUserInfo()
this.setData({
url: `${decodeUrl}?pushed=${options.pushed}&source=${options.source}&carConditionCode=${options.carConditionCode}&operator=${userInfo.name}`
})
}
},
setTime(param) {
time = setTimeout(() => {
commonApi
.getShortCourseDetail({
id: param?.studyId
})
.then(res => {
let { code, data } = res
this.setTime()
if (code == 0) {
let flag = data?.aiReviewReport.every(item => item.examStatus === 2)
if (flag) wx.disableAlertBeforeUnload()
}
})
}, 5 * 1000)
},
onReady: function () {},
onShow: function () {
if (this.refresh) {
this.setData(
{
flag: true
},
() => {
this.setData({
url: this.data.url
})
}
)
}
},
onHide: function () {
if (this.refresh) {
this.setData({
flag: false
})
}
},
handleMessage(e) {
const { data } = e.detail
console.log(data, 'handleMessage')
if (data) {
const eventChannel = this.getOpenerEventChannel()
eventChannel.emit('webViewMessage', data[0])
this.setData({
isH5Bind: true
})
const msgData = data[0]
if (msgData && typeof msgData === 'object' && msgData !== null) {
const baseUrl = this.getBaseUrl(this.data.url)
if (baseUrl) {
try {
const stored = wx.getStorageSync(WEBVIEW_H5_TOKENS_KEY)
const tokens = stored ? JSON.parse(stored) : {}
tokens[baseUrl] = msgData
wx.setStorageSync(WEBVIEW_H5_TOKENS_KEY, JSON.stringify(tokens))
} catch (err) {}
}
}
}
},
onUnload: function () {
let { url } = this.data
if (this.refresh) {
this.setData({
flag: false
})
}
if (url.includes('zyUpload')) {
const eventChannel = this.getOpenerEventChannel()
if (!this.data.isH5Bind) {
eventChannel.emit('webViewMessage', 'cancel')
}
}
if (url.includes('LearningExamTraineePage')) {
wx.setStorageSync('FROM_BEISENWEBVIEW', '1')
}
time && clearTimeout(time)
},
onPullDownRefresh: function () {},
onReachBottom: function () {},
onShareAppMessage(options) {
const { url, shareTitle } = this.data
if (options.webviewUrl.includes('/vehicle-detail')) shareTitle = '车况信息'
const encoded = encodeURIComponent(url || '')
return {
title: shareTitle,
path: `${sharePagePath}?url=${encoded}`
}
},
onShareTimeline() {
const { url, shareTitle } = this.data
if (options.webviewUrl.includes('/vehicle-detail')) shareTitle = '车况信息'
const encoded = encodeURIComponent(url || '')
return {
title: shareTitle,
query: `url=${encoded}`
}
}
}
}