This repository was archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.js
More file actions
59 lines (58 loc) · 1.92 KB
/
handler.js
File metadata and controls
59 lines (58 loc) · 1.92 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
// 获取页面路径
const getPath = function (url) {
// 判断是否存在页面路径参数
if (url.indexOf('path=') !== -1) {
// 暂存页面路径参数
let path = url.substr(url.indexOf('path=') + 5);
// 判断页面路径后是否存在更多的参数 ( 通常是推广后缀 )
if (path.indexOf('&') !== -1) {
// 将参数与路径中间的 & 符号替换为 ?, 以便携带参数进行跳转
return path.substr(0, path.indexOf('&')) + '?' + path.substr(path.indexOf('&')+1);
} else {
// 返回路径
return path;
}
} else {
// 入参中不存在页面路径
return false;
}
}
Page({
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.showLoading({ title: '跳转中' });
if (options.q !== undefined) {
const scanURL = decodeURIComponent(options.q);
if (getPath(scanURL)) {
// 跳转
wx.redirectTo({ url: getPath(scanURL) });
} else {
// 报错并跳转到首页
wx.hideLoading();
wx.showModal({
title: '出错啦',
content: '参数不正确',
confirmText: '返回首页',
showCancel: false,
success() {
wx.redirectTo({ url: 'pages/index/index' });
}
})
}
} else {
// 报错并跳转到首页
wx.hideLoading();
wx.showModal({
title: '出错啦',
content: '参数不正确',
confirmText: '返回首页',
showCancel: false,
success() {
wx.redirectTo({ url: 'pages/index/index' });
}
})
}
}
})