-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
69 lines (60 loc) · 1.51 KB
/
index.js
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
import Qrcode from "qrcode";
import * as $ from "./lib/zepto.min.js";
const $tips = $(".wechat-tips");
// const $card = $(".download-card")
const $link = $(".download-link");
const $qrcode = $(".insert-qrcode");
const ua = navigator.userAgent;
const isWechat = /micromessenger/i.test(ua);
const isDingTalk = /DingTalk/i.test(ua);
function downloadApp(link) {
// 使用location.assign 兼容 ios safari跳转
$link.attr("href", link);
if ($.os.ios) link && location.assign(link);
}
function getData(node) {
const data = {
android: node[0].dataset.android,
ios: node[0].dataset.ios,
};
return data;
}
function getNowLink(downloadLinks) {
const ANDROID = "android";
const IOS = "ios";
return $.os.android
? downloadLinks[ANDROID]
: $.os.ios
? downloadLinks[IOS]
: "";
}
function generateQrcode(text) {
return new Promise((resolve, reject) => {
Qrcode.toDataURL(text, { errorCorrectionLevel: "L" }, function(err, url) {
if (url) {
resolve(url);
} else {
reject(err);
}
});
});
}
$(document).ready(function() {
if (!$.os.phone) {
generateQrcode(location.href)
.then((url) => {
$qrcode.forEach(function(item) {
$(item).attr("src", url);
});
})
.catch(console.error);
}
})
window.onload = function() {
if (($.os.android && isWechat) || ($.os.ios && isDingTalk)) {
return $tips.show();
}
const downloadLinks = getData($link);
const link = getNowLink(downloadLinks);
downloadApp(link);
};