From 35dc181e87fa93765f059db5b5ea3367df82283d Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 17 Jan 2016 15:03:58 +0800 Subject: [PATCH] refactor index.php and dependencies --- assets/js/ajax.js | 112 +++++++++++++++++---------------------- assets/js/login_utils.js | 13 +---- index.php | 63 +++++++++++++++++----- 3 files changed, 101 insertions(+), 87 deletions(-) diff --git a/assets/js/ajax.js b/assets/js/ajax.js index 00413851..2d21dc87 100755 --- a/assets/js/ajax.js +++ b/assets/js/ajax.js @@ -1,45 +1,33 @@ -function checkToken(token, handler) { - $.ajax({ - type: "POST", - url: "check.php?action=token", - dataType: "json", - data: {"token":token}, - success: function(json) { - handler(json); - } - }); -} - function checkForm(type, uname, passwd, passwd2) { if (type == "login") { - if (uname === "") { + if (uname === "") { showMsg("alert-warning", "Empty Username!"); - $("#uname").focus(); - return false; - } else if (passwd === ""){ + $("#uname").focus(); + return false; + } else if (passwd === ""){ showMsg("alert-warning", "Empty Password!"); - $("#passwd").focus(); - return false; + $("#passwd").focus(); + return false; } else { return true; } } else if (type == "register") { - if (uname === "") { + if (uname === "") { showMsg("alert-warning", "Empty Username!"); - $("#uname").focus(); - return false; - } else if (passwd === ""){ + $("#uname").focus(); + return false; + } else if (passwd === ""){ showMsg("alert-warning", "Empty Password!"); - $("#passwd").focus(); - return false; - } else if (passwd2 === ""){ + $("#passwd").focus(); + return false; + } else if (passwd2 === ""){ showMsg("alert-warning", "Empty Comfirming Password!"); - $("#cpasswd").focus(); - return false; - } else if (passwd != passwd2){ + $("#cpasswd").focus(); + return false; + } else if (passwd != passwd2){ showMsg("alert-warning", "Non-equal password comfirming!"); - $("#cpasswd").focus(); - return false; + $("#cpasswd").focus(); + return false; } else { return true; } @@ -47,31 +35,31 @@ function checkForm(type, uname, passwd, passwd2) { } // Login Button Click Event $("body").on("click", "#login", function(){ - var uname = $("#uname").val(); - var passwd = $("#passwd").val(); + var uname = $("#uname").val(); + var passwd = $("#passwd").val(); if (checkForm("login", uname, passwd)) { - $.ajax({ - type: "POST", - url: "check.php?action=login", - dataType: "json", - data: {"uname":uname,"passwd":passwd}, - beforeSend: function() { - showMsg("alert-info", "Logging in..."); + $.ajax({ + type: "POST", + url: "ajax.php?action=login", + dataType: "json", + data: {"uname":uname,"passwd":passwd}, + beforeSend: function() { + showMsg("alert-info", "Logging in..."); }, - success: function(json) { - if (json.success == 1) { - docCookies.setItem("uname", uname); - docCookies.setItem("token", json.token); + success: function(json) { + if (json.errno == 0) { + docCookies.setItem("uname", uname, '/'); + docCookies.setItem("token", json.token, '/'); if ($("#keep").prop("checked")) { - docCookies.setItem("uname", uname, 604800); + docCookies.setItem("uname", uname, 604800, '/'); // 设置长效 token (7天) - docCookies.setItem("token", json.token, 604800); + docCookies.setItem("token", json.token, 604800, '/'); } showMsg("alert-success", "Logging succeed!"); - window.setTimeout("window.location = './user.php'", 1000); - } else { + // window.setTimeout("window.location = './user.php'", 1000); + } else { showMsg("alert-danger", json.msg); - } + } } }); } @@ -79,25 +67,25 @@ $("body").on("click", "#login", function(){ // Register Button Click Event $("body").on("click", "#register", function(){ - var uname = $("#uname").val(); - var passwd = $("#passwd").val(); + var uname = $("#uname").val(); + var passwd = $("#passwd").val(); if (checkForm("register", uname, passwd, $("#cpasswd").val())) { - - $.ajax({ - type: "POST", - url: "check.php?action=register", - dataType: "json", - data: {"uname":uname,"passwd":passwd}, - beforeSend: function() { - showMsg("alert-info", "Registering..."); + + $.ajax({ + type: "POST", + url: "ajax.php?action=register", + dataType: "json", + data: {"uname":uname, "passwd":passwd}, + beforeSend: function() { + showMsg("alert-info", "Registering..."); }, - success: function(json) { - if (json.success == 1) { + success: function(json) { + if (json.errno == 0) { showMsg("alert-success", json.msg); window.setTimeout("window.location = './index.php?action=login&msg=Successfully Registered, please log in.'", 1000); - } else { + } else { showMsg("alert-danger", json.msg); - } + } } }); diff --git a/assets/js/login_utils.js b/assets/js/login_utils.js index 04c50015..e4399609 100755 --- a/assets/js/login_utils.js +++ b/assets/js/login_utils.js @@ -1,16 +1,5 @@ // Auto login -$(document).ready(function(){ - if (docCookies.hasItem("uname") && docCookies.hasItem("token") && $("#login-reg").html() == 'Register') { - checkToken(docCookies.getItem("token"),function(json) { - if (json.success == 1) { - showMsg("alert-success", json.msg); - window.location = "./user.php"; - } else { - showMsg("alert-danger", json.msg); - } - }); - } -}); +// null $('body').css('height', document.documentElement.clientHeight); diff --git a/index.php b/index.php index b3e59e87..72623f57 100755 --- a/index.php +++ b/index.php @@ -1,5 +1,35 @@ getToken()) { + $_SESSION['uname'] = $_COOKIE['uname']; + $_SESSION['token'] = $user -> getToken(); + } +} + +function getValue($key, $array) { + if (array_key_exists($key, $array)) { + return $array[$key]; + } + return false; +} + +function echoScript($script) { + echo ""; +} ?> @@ -21,13 +51,13 @@
  • Link
  • --> @@ -71,11 +101,18 @@ changeForm(1);"; +if (getValue('action', $_GET) == "register") { + echoScript("changeForm(1);"); } -if ($_GET["msg"]) { - echo ""; -}?> +if ($msg = getValue('msg', $_GET)) { + echoScript("showMsg('alert-warning','".$msg."');"); +} + +if (getValue('uname', $_SESSION)) { + echoScript("$('.login-title').html('Welcome');"); + echoScript("$('#login-form').html('User Center');"); + echoScript("$('.navbar-right').html('
  • Welcome,".$_SESSION['uname']."!
  • ');"); +} +?>