H5 调用支付宝小程序 Alipay JSAPI

参考链接 官方文档:
demo:

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script>
<script src="https://cdn.bootcss.com/vConsole/3.2.0/vconsole.min.js"></script>

<title>Alipay-demo</title>
</head>
<body>
<div class="parent">
hello
<div class="child" contenteditable="true">world</div>
<button class="btn1" onclick="fn()">点击1</button>
<button class="btn2">点击2</button>
</div>
<script>
var vConsole = new VConsole();
function ready(callback) {
if (window.AlipayJSBridge) {
callback && callback();
console.log(1);
} else {
console.log("请在支付宝打开");
document.addEventListener("AlipayJSBridgeReady", callback, false);
window.AlipayJSBridge = {
call: function () {
console.log("mock AlipayJSBridge call");
},
};
}
}
ready(function () {
document.querySelector(".btn2").addEventListener("click", function () {
AlipayJSBridge.call(
"beehiveOptionsPicker",
{
title: "还款日选择",
optionsOne: [
"每周一",
"每周二",
"每周三",
"每周四",
"每周五",
"每周六",
"每周日",
],
selectedOneIndex: 2,
},
function (result) {
alert(JSON.stringify(result));
}
);
// AlipayJSBridge.call(
// "chooseContact",
// {
// title: "choose contacts", // title show on the title bar
// multiMax: 2, // max contact items size, default 50
// multiMaxText: "max!", // message if selected more than max
// },
// function (result) {
// alert(JSON.stringify(result));
// }
// );
});
});
function fn() {
AlipayJSBridge.call("toast", {
content: "请填写正确的姓名",
type: "none",
duration: 2000,
});
}
</script>
</body>
</html>

用 open with live server 方式打开生成二维码,然后用支付宝扫码预览