login.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <!DOCTYPE html>
  2. <html class="ui-page-login">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <title></title>
  7. <link href="css/mui.min.css" rel="stylesheet" />
  8. <link href="css/style.css" rel="stylesheet" />
  9. <style>
  10. .area {
  11. margin: 20px auto 0px auto;
  12. }
  13. .mui-input-group {
  14. margin-top: 10px;
  15. }
  16. .mui-input-group:first-child {
  17. margin-top: 20px;
  18. }
  19. .mui-input-group label {
  20. width: 22%;
  21. }
  22. .mui-input-row label~input,
  23. .mui-input-row label~select,
  24. .mui-input-row label~textarea {
  25. width: 78%;
  26. }
  27. .mui-checkbox input[type=checkbox],
  28. .mui-radio input[type=radio] {
  29. top: 6px;
  30. }
  31. .mui-content-padded {
  32. margin-top: 25px;
  33. }
  34. .mui-btn {
  35. padding: 10px;
  36. }
  37. .link-area {
  38. display: block;
  39. margin-top: 25px;
  40. text-align: center;
  41. }
  42. .spliter {
  43. color: #bbb;
  44. padding: 0px 8px;
  45. }
  46. .oauth-area {
  47. position: absolute;
  48. bottom: 20px;
  49. left: 0px;
  50. text-align: center;
  51. width: 100%;
  52. padding: 0px;
  53. margin: 0px;
  54. }
  55. .oauth-area .oauth-btn {
  56. display: inline-block;
  57. width: 50px;
  58. height: 50px;
  59. background-size: 30px 30px;
  60. background-position: center center;
  61. background-repeat: no-repeat;
  62. margin: 0px 20px;
  63. /*-webkit-filter: grayscale(100%); */
  64. border: solid 1px #ddd;
  65. border-radius: 25px;
  66. }
  67. .oauth-area .oauth-btn:active {
  68. border: solid 1px #aaa;
  69. }
  70. .oauth-area .oauth-btn.disabled {
  71. background-color: #ddd;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <header class="mui-bar mui-bar-nav">
  77. <h1 class="mui-title">登录</h1>
  78. </header>
  79. <div class="mui-content">
  80. <form id='login-form' class="mui-input-group">
  81. <div class="mui-input-row">
  82. <label>账号</label>
  83. <input id='account' type="text" class="mui-input-clear mui-input" placeholder="请输入账号">
  84. </div>
  85. <div class="mui-input-row">
  86. <label>密码</label>
  87. <input id='password' type="password" class="mui-input-clear mui-input" placeholder="请输入密码">
  88. </div>
  89. </form>
  90. <form class="mui-input-group">
  91. <ul class="mui-table-view mui-table-view-chevron">
  92. <li class="mui-table-view-cell">
  93. 自动登录
  94. <div id="autoLogin" class="mui-switch">
  95. <div class="mui-switch-handle"></div>
  96. </div>
  97. </li>
  98. </ul>
  99. </form>
  100. <div class="mui-content-padded">
  101. <button id='login' class="mui-btn mui-btn-block mui-btn-primary">登录</button>
  102. <div class="link-area"><a id='reg'>注册账号</a> <span class="spliter">|</span> <a id='forgetPassword'>忘记密码</a>
  103. </div>
  104. </div>
  105. <div class="mui-content-padded oauth-area">
  106. </div>
  107. </div>
  108. <script src="js/mui.min.js"></script>
  109. <script src="js/mui.enterfocus.js"></script>
  110. <script src="js/app.js"></script>
  111. <script>
  112. (function($, doc) {
  113. $.init({
  114. statusBarBackground: '#f7f7f7'
  115. });
  116. $.plusReady(function() {
  117. plus.screen.lockOrientation("portrait-primary");
  118. var settings = app.getSettings();
  119. var state = app.getState();
  120. var mainPage = $.preload({
  121. "id": 'main',
  122. "url": 'main.html'
  123. });
  124. var main_loaded_flag = false;
  125. mainPage.addEventListener("loaded",function () {
  126. main_loaded_flag = true;
  127. });
  128. var toMain = function() {
  129. //使用定时器的原因:
  130. //可能执行太快,main页面loaded事件尚未触发就执行自定义事件,此时必然会失败
  131. var id = setInterval(function () {
  132. if(main_loaded_flag){
  133. clearInterval(id);
  134. $.fire(mainPage, 'show', null);
  135. mainPage.show("pop-in");
  136. }
  137. },20);
  138. };
  139. //检查 "登录状态/锁屏状态" 开始
  140. if (settings.autoLogin && state.token && settings.gestures) {
  141. $.openWindow({
  142. url: 'unlock.html',
  143. id: 'unlock',
  144. show: {
  145. aniShow: 'pop-in'
  146. },
  147. waiting: {
  148. autoShow: false
  149. }
  150. });
  151. } else if (settings.autoLogin && state.token) {
  152. toMain();
  153. } else {
  154. app.setState(null);
  155. //第三方登录
  156. var authBtns = ['qihoo', 'weixin', 'sinaweibo', 'qq']; //配置业务支持的第三方登录
  157. var auths = {};
  158. var oauthArea = doc.querySelector('.oauth-area');
  159. plus.oauth.getServices(function(services) {
  160. for (var i in services) {
  161. var service = services[i];
  162. auths[service.id] = service;
  163. if (~authBtns.indexOf(service.id)) {
  164. var isInstalled = app.isInstalled(service.id);
  165. var btn = document.createElement('div');
  166. //如果微信未安装,则为不启用状态
  167. btn.setAttribute('class', 'oauth-btn' + (!isInstalled && service.id === 'weixin' ? (' disabled') : ''));
  168. btn.authId = service.id;
  169. btn.style.backgroundImage = 'url("images/' + service.id + '.png")'
  170. oauthArea.appendChild(btn);
  171. }
  172. }
  173. $(oauthArea).on('tap', '.oauth-btn', function() {
  174. if (this.classList.contains('disabled')) {
  175. plus.nativeUI.toast('您尚未安装微信客户端');
  176. return;
  177. }
  178. var auth = auths[this.authId];
  179. var waiting = plus.nativeUI.showWaiting();
  180. auth.login(function() {
  181. waiting.close();
  182. plus.nativeUI.toast("登录认证成功");
  183. auth.getUserInfo(function() {
  184. plus.nativeUI.toast("获取用户信息成功");
  185. var name = auth.userInfo.nickname || auth.userInfo.name;
  186. app.createState(name, function() {
  187. toMain();
  188. });
  189. }, function(e) {
  190. plus.nativeUI.toast("获取用户信息失败:" + e.message);
  191. });
  192. }, function(e) {
  193. waiting.close();
  194. plus.nativeUI.toast("登录认证失败:" + e.message);
  195. });
  196. });
  197. }, function(e) {
  198. oauthArea.style.display = 'none';
  199. plus.nativeUI.toast("获取登录认证失败:" + e.message);
  200. });
  201. }
  202. // close splash
  203. setTimeout(function() {
  204. //关闭 splash
  205. plus.navigator.closeSplashscreen();
  206. }, 600);
  207. //检查 "登录状态/锁屏状态" 结束
  208. var loginButton = doc.getElementById('login');
  209. var accountBox = doc.getElementById('account');
  210. var passwordBox = doc.getElementById('password');
  211. var autoLoginButton = doc.getElementById("autoLogin");
  212. var regButton = doc.getElementById('reg');
  213. var forgetButton = doc.getElementById('forgetPassword');
  214. loginButton.addEventListener('tap', function(event) {
  215. var loginInfo = {
  216. account: accountBox.value,
  217. password: passwordBox.value
  218. };
  219. app.login(loginInfo, function(err) {
  220. if (err) {
  221. plus.nativeUI.toast(err);
  222. return;
  223. }
  224. toMain();
  225. });
  226. });
  227. $.enterfocus('#login-form input', function() {
  228. $.trigger(loginButton, 'tap');
  229. });
  230. autoLoginButton.classList[settings.autoLogin ? 'add' : 'remove']('mui-active')
  231. autoLoginButton.addEventListener('toggle', function(event) {
  232. setTimeout(function() {
  233. var isActive = event.detail.isActive;
  234. settings.autoLogin = isActive;
  235. app.setSettings(settings);
  236. }, 50);
  237. }, false);
  238. regButton.addEventListener('tap', function(event) {
  239. $.openWindow({
  240. url: 'reg.html',
  241. id: 'reg',
  242. preload: true,
  243. show: {
  244. aniShow: 'pop-in'
  245. },
  246. styles: {
  247. popGesture: 'hide'
  248. },
  249. waiting: {
  250. autoShow: false
  251. }
  252. });
  253. }, false);
  254. forgetButton.addEventListener('tap', function(event) {
  255. $.openWindow({
  256. url: 'forget_password.html',
  257. id: 'forget_password',
  258. preload: true,
  259. show: {
  260. aniShow: 'pop-in'
  261. },
  262. styles: {
  263. popGesture: 'hide'
  264. },
  265. waiting: {
  266. autoShow: false
  267. }
  268. });
  269. }, false);
  270. //
  271. window.addEventListener('resize', function() {
  272. oauthArea.style.display = document.body.clientHeight > 400 ? 'block' : 'none';
  273. }, false);
  274. //
  275. var backButtonPress = 0;
  276. $.back = function(event) {
  277. backButtonPress++;
  278. if (backButtonPress > 1) {
  279. plus.runtime.quit();
  280. } else {
  281. plus.nativeUI.toast('再按一次退出应用');
  282. }
  283. setTimeout(function() {
  284. backButtonPress = 0;
  285. }, 1000);
  286. return false;
  287. };
  288. });
  289. }(mui, document));
  290. </script>
  291. </body>
  292. </html>