-
Notifications
You must be signed in to change notification settings - Fork 366
/
index.html
128 lines (123 loc) · 3.6 KB
/
index.html
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>oauth</title>
<link rel="stylesheet" type="text/css" href="css/mui.css"/>
<style type="text/css">
.oauth-area {
position: absolute;
bottom: 20px;
left: 0px;
text-align: center;
width: 100%;
padding: 0px;
margin: 0px;
}
.oauth-area .oauth-btn {
display: inline-block;
width: 50px;
height: 50px;
background-size: 30px 30px;
background-position: center center;
background-repeat: no-repeat;
margin: 0px 20px;
/*-webkit-filter: grayscale(100%); */
border: solid 1px #ddd;
border-radius: 25px;
}
.oauth-area .oauth-btn:active {
border: solid 1px #aaa;
}
.oauth-area .oauth-btn.disabled {
background-color: #ddd;
}
</style>
</head>
<body>
<div class="mui-content-padded oauth-area">
</div>
<script src="js/mui.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
// 判断本地是否安装客户端
var isInstalled = function (id) {
if (id === 'qihoo' && mui.os.plus) {
return true;
}
if (mui.os.android) {
var main = plus.android.runtimeMainActivity();
var packageManager = main.getPackageManager();
var PackageManager = plus.android.importClass(packageManager)
var packageName = {
"qq": "com.tencent.mobileqq",
"weixin": "com.tencent.mm",
"sinaweibo": "com.sina.weibo"
}
try {
return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
} catch (e) {}
} else {
switch (id) {
case "qq":
var TencentOAuth = plus.ios.import("TencentOAuth");
return TencentOAuth.iphoneQQInstalled();
case "weixin":
var WXApi = plus.ios.import("WXApi");
return WXApi.isWXAppInstalled()
case "sinaweibo":
var SinaAPI = plus.ios.import("WeiboSDK");
return SinaAPI.isWeiboAppInstalled()
default:
break;
}
}
}
// 第三方登录
mui.plusReady(function () {
// 配置业务支持的第三方登录
var authBtns = ['qihoo', 'weixin', 'sinaweibo', 'qq'];
var auths = {};
var oauthArea = document.querySelector('.oauth-area');
plus.oauth.getServices(function(services) {
for (var i in services) {
var service = services[i];
auths[service.id] = service;
if (~authBtns.indexOf(service.id)) {
var btn = document.createElement('div');
// 如果微信未安装,则为不启用状态
btn.setAttribute('class', 'oauth-btn' + (!isInstalled(service.id) && service.id === 'weixin' ? (' disabled') : ''));
btn.authId = service.id;
btn.style.backgroundImage = 'url("images/' + service.id + '.png")'
oauthArea.appendChild(btn);
}
}
mui(oauthArea).on('tap', '.oauth-btn', function() {
if (this.classList.contains('disabled')) {
plus.nativeUI.toast('您尚未安装微信客户端');
return;
}
var auth = auths[this.authId];
var waiting = plus.nativeUI.showWaiting();
auth.login(function() {
waiting.close();
plus.nativeUI.toast("登录认证成功");
auth.getUserInfo(function() {
plus.nativeUI.toast("获取用户信息成功");
var name = auth.userInfo.nickname || auth.userInfo.name;
console.log(name);
}, function(e) {
plus.nativeUI.toast("获取用户信息失败:" + e.message);
});
}, function(e) {
waiting.close();
plus.nativeUI.toast("登录认证失败:" + e.message);
});
});
}, function(e) {
oauthArea.style.display = 'none';
plus.nativeUI.toast("获取登录认证失败:" + e.message);
});
})
</script>
</body>
</html>