h5唤醒App
# 微信浏览器唤醒
- 微信开放平台 - appid1
- 已认证的服务号 - appid2
- 安卓对话框的链接无法唤醒、卡片、扫码可以唤醒
android 采用以下方案
<wx-open-launch-app
id="launch-btn1"
appid="appid1"
:extinfo=""
>
<!-- vue中必须这种格式 -->
<script type="text/wxtag-template">
<style>.btn { width: 79px; height: 32px; background: #e6f0ff linear-gradient(132deg, #e2d5c4 0%, #c4a27b 100%); border-radius: 22px; text-align: center; font-size: 12px; font-weight: bold; color: #212121; line-height: 32px; }</style>
<div class="btn">打开App</div>
</script>
</wx-open-launch-app>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
wx.config({
appId: appid2,
timestamp, // 接口获取
nonceStr, // 接口获取
signature, // 接口获取
jsApiList: [], // api能力
openTagList: ["wx-open-launch-app"], // 开放标签
});
var btn = document.getElementById("launch-btn");
btn.addEventListener("launch", function (e) {
console.log("微信唤醒成功--1");
});
// 实际上唤醒失败error并没有触发
btn.addEventListener("error", function (e) {
console.log("微信唤醒失败--1", e.detail);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ios 使用普通的 href链接可以直接唤醒
# 普通浏览器唤醒
ios 使用普通的 href链接可以直接唤醒
安卓自定义协议头唤醒
// 判断设备
function getDeviceType() {
const userAgent = navigator.userAgent;
let deviceType;
if (/Android/i.test(userAgent)) {
deviceType = "android";
} else if (/iPhone|iPad|iPod/i.test(userAgent)) {
deviceType = "ios";
} else {
deviceType = "other";
}
return deviceType;
}
// 唤醒或者失败后下载
function openOrDownload(schema, downloadUrl) {
window.location.href = schema;
let deviceType = getDeviceType();
if (deviceType == "android") {
window.location.href = schema;
let timer = null;
let openTime = Date.now();
const success = () => {
clearInterval(timer);
};
timer = setInterval(() => {
if (document.hidden || document.webkitHidden) {
success();
} else if (Date.now() - openTime > 2500) {
clearInterval(timer);
// 打开下载页
location.href = downloadUrl;
}
}, 200);
}
if (deviceType == "ios") {
var loadDateTime = new Date();
window.location = schema;
window.setTimeout(function () {
var timeOutDateTime = new Date();
if (timeOutDateTime - loadDateTime < 5000) {
window.location = downloadUrl;
} else {
window.close();
}
}, 500);
}
}
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
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
上次更新: 2023/07/20, 16:40:29