oauth.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <!--<meta name="MobileOptimized" content="320"/>-->
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. var auths={};
  12. function plusReady(){
  13. // 获取登录认证通道
  14. plus.oauth.getServices(function(services){
  15. var content=document.getElementById('dcontent');
  16. var info=document.getElementById("info");
  17. var txt="登录认证通道信息:";
  18. for(var i in services){
  19. var service=services[i];
  20. console.log(service.id+": "+service.authResult+", "+service.userInfo);
  21. auths[service.id]=service;
  22. txt += "id:"+service.id+", ";
  23. txt += "description:"+service.description+", ";
  24. var de=document.createElement('div');
  25. de.setAttribute('class','button');
  26. de.setAttribute('onclick','login(this.id)');
  27. de.id=service.id;
  28. de.innerText=service.description+"登录";
  29. oauth.appendChild(de);
  30. }
  31. info.innerText=txt;
  32. },function(e){
  33. outLine("获取登录认证失败:"+e.message);
  34. });
  35. }
  36. document.addEventListener('plusready',plusReady,false);
  37. // 登录认证
  38. function login(id){
  39. outSet("----- 登录认证 -----");
  40. var auth=auths[id];
  41. if(auth){
  42. var w=null;
  43. if(plus.os.name=="Android"){
  44. w=plus.nativeUI.showWaiting();
  45. }
  46. document.addEventListener("pause",function(){
  47. setTimeout(function(){
  48. w&&w.close();w=null;
  49. },2000);
  50. }, false );
  51. auth.login(function(){
  52. w&&w.close();w=null;
  53. outLine("登录认证成功:");
  54. outLine(JSON.stringify(auth.authResult));
  55. userinfo(auth);
  56. },function(e){
  57. w&&w.close();w=null;
  58. outLine("登录认证失败:");
  59. outLine("["+e.code+"]:"+e.message);
  60. plus.nativeUI.alert("详情错误信息请参考授权登录(OAuth)规范文档:http://www.html5plus.org/#specification#/specification/OAuth.html",null,"登录失败["+e.code+"]:"+e.message);
  61. });
  62. }else{
  63. outLine("无效的登录认证通道!");
  64. plus.nativeUI.alert("无效的登录认证通道!",null,"登录");
  65. }
  66. }
  67. // 获取用户信息
  68. function userinfo(a){
  69. outLine("----- 获取用户信息 -----");
  70. a.getUserInfo(function(){
  71. outLine("获取用户信息成功:");
  72. outLine(JSON.stringify(a.userInfo));
  73. var nickname=a.userInfo.nickname||a.userInfo.name||a.userInfo.miliaoNick;
  74. plus.nativeUI.alert("欢迎“"+nickname+"”登录!");
  75. },function(e){
  76. outLine("获取用户信息失败:");
  77. outLine("["+e.code+"]:"+e.message);
  78. plus.nativeUI.alert("获取用户信息失败!",null,"登录");
  79. });
  80. }
  81. // 注销登录
  82. function logoutAll(){
  83. outSet("----- 注销登录认证 -----");
  84. for(var i in auths){
  85. logout(auths[i]);
  86. }
  87. }
  88. function logout(auth){
  89. auth.logout(function(){
  90. outLine("注销\""+auth.description+"\"成功");
  91. },function(e){
  92. outLine("注销\""+auth.description+"\"失败:"+e.message);
  93. });
  94. }
  95. </script>
  96. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  97. <style type="text/css">
  98. #total{
  99. -webkit-user-select:text;
  100. text-align:right;
  101. padding:0 1em;
  102. border: 0px;
  103. border-bottom:1px solid #ECB100;
  104. border-radius: 0;
  105. font-size:16px;
  106. width:30%;
  107. outline:none;
  108. }
  109. </style>
  110. </head>
  111. <body>
  112. <br/>
  113. <p id="info" style="padding: 0 1em;text-align:left;">登录认证通道信息:</p>
  114. <div style="padding: 0.5em 1em;"><hr color="#EEE"/></div>
  115. <br/>
  116. <div id="oauth"></div>
  117. <br/>
  118. <div class="button button-waring" onclick="logoutAll()">注销登录</div>
  119. <div id="outpos"/>
  120. <div id="output">
  121. OAuth模块管理客户端的用户授权登录验证功能,允许应用访问第三方平台的资源。
  122. </div>
  123. </body>
  124. </html>