-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
75 lines (62 loc) · 1.56 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
70
71
72
73
74
75
import "./style/bulma.css"
import "./style/common.less"
import Qrcode from "qrcode"
import * as $ from "./lib/zepto.js"
const $tips = $(".wechat-tips")
const $card = $(".download-card")
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 && location.assign(link)
}
function getData(node) {
const data = {}
node.forEach((item) => {
const {platform, link} = item.dataset
data[platform.toLowerCase()] = link
})
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)
}
})
})
}
function init() {
if (isWechat || ($.os.ios && isDingTalk)) {
return $tips.show()
}
const downloadLinks = getData($card)
const link = getNowLink(downloadLinks)
downloadApp(link)
$card.on("click", function(e) {
const $this = $(this)
const link = $this.attr("data-link")
downloadApp(link)
})
if (!$.os.phone) {
generateQrcode(location.href)
.then(url => {
$qrcode.forEach(function(item) {
$(item).attr("src", url)
})
}).catch(console.error)
}
}
init()