blessing-skin-server/assets/js/index.utils.js

139 lines
4.6 KiB
JavaScript
Raw Normal View History

/*
2016-03-19 10:28:18 +08:00
* @Author: printempw
* @Date: 2016-01-21 13:55:44
* @Last Modified by: printempw
2016-03-19 10:28:18 +08:00
* @Last Modified time: 2016-03-19 10:08:46
*/
'use strict';
2016-02-03 21:42:27 +08:00
var login = function() {
2016-02-03 21:15:29 +08:00
var uname = $("#uname").val();
var passwd = $("#passwd").val();
if (checkForm("login", uname, passwd)) {
2016-02-03 21:42:27 +08:00
$.ajax({
type: "POST",
url: "ajax.php?action=login",
dataType: "json",
data: { "uname": uname, "passwd": passwd },
beforeSend: function() {
2016-02-05 22:08:06 +08:00
showMsg('alert-info', '登录中。。');
2016-02-03 21:15:29 +08:00
},
2016-02-03 21:42:27 +08:00
success: function(json) {
console.log(json);
2016-02-03 21:42:27 +08:00
if (json.errno == 0) {
2016-02-05 22:08:06 +08:00
docCookies.setItem('uname', uname, null, '/');
docCookies.setItem('token', json.token, null, '/');
if ($('#keep').prop('checked')) {
docCookies.setItem('uname', uname, 604800, '/');
// 设置长效 cookie 7天
2016-02-05 22:08:06 +08:00
docCookies.setItem('token', json.token, 604800, '/');
2016-02-03 21:15:29 +08:00
}
2016-02-05 22:08:06 +08:00
showAlert(json.msg);
window.setTimeout('window.location = "./user/index.php"', 1000);
2016-02-03 21:15:29 +08:00
} else {
showAlert(json.msg);
2016-02-05 22:08:06 +08:00
showMsg('hide', '');
2016-02-03 21:15:29 +08:00
}
},
error: function(json) {
showMsg('alert-danger', '出错啦,请联系作者!<br />详细信息:'+json.responseText);
2016-02-03 21:42:27 +08:00
}
});
}
2016-02-03 21:42:27 +08:00
}
2016-02-03 21:42:27 +08:00
var register = function() {
2016-02-05 22:08:06 +08:00
var uname = $('#reg-uname').val();
var passwd = $('#reg-passwd').val();
if (checkForm('register', uname, passwd, $('#reg-passwd2').val())) {
2016-02-03 21:15:29 +08:00
$.ajax({
type: "POST",
url: "ajax.php?action=register",
dataType: "json",
2016-02-05 22:08:06 +08:00
data: {'uname':uname, 'passwd':passwd},
2016-02-03 21:15:29 +08:00
beforeSend: function() {
showMsg('alert-info', '注册中...');
2016-02-03 21:15:29 +08:00
},
success: function(json) {
if (json.errno == 0) {
showAlert(json.msg, function(){
2016-02-05 22:08:06 +08:00
showMsg('hide', '');
2016-02-03 21:15:29 +08:00
$('[data-remodal-id=register-modal]').remodal().close();
// Automatically login after registeration
2016-02-05 22:08:06 +08:00
$('#uname').val(uname);
$('#passwd').val(passwd);
login();
2016-02-03 21:15:29 +08:00
});
} else {
showAlert(json.msg);
showMsg('hide', "");
}
},
error: function(json) {
showMsg('alert-danger', '出错啦,请联系作者!<br />详细信息:'+json.responseText);
}
2016-02-03 21:15:29 +08:00
});
}
2016-02-03 21:42:27 +08:00
}
function checkForm(type, uname, passwd, passwd2) {
2016-02-03 21:15:29 +08:00
switch(type) {
case "login":
if (uname == "") {
2016-02-05 22:08:06 +08:00
showMsg('alert-warning', '用户名不能为空哦');
2016-02-03 21:15:29 +08:00
$("#uname").focus();
return false;
} else if (passwd == ""){
2016-02-05 22:08:06 +08:00
showMsg('alert-warning', '密码不能为空哦');
$('#passwd').focus();
2016-02-03 21:15:29 +08:00
return false;
} else {
return true;
}
break;
case "register":
if (uname == "") {
2016-02-05 22:08:06 +08:00
showMsg('alert-warning', '用户名不能为空哦');
$('#uname').focus();
2016-02-03 21:15:29 +08:00
return false;
} else if (passwd == ""){
2016-02-05 22:08:06 +08:00
showMsg('alert-warning', '密码不能为空哦');
$('#passwd').focus();
2016-02-03 21:15:29 +08:00
return false;
} else if (passwd2 == ""){
2016-02-05 22:08:06 +08:00
showMsg('alert-warning', '确认密码不能为空');
$('#cpasswd').focus();
2016-02-03 21:15:29 +08:00
return false;
} else if (passwd != passwd2){
2016-02-05 22:08:06 +08:00
showMsg('alert-warning', '注册密码和确认密码不一样诶');
$('#cpasswd').focus();
2016-02-03 21:15:29 +08:00
return false;
} else {
return true;
}
break;
default:
return false;
}
}
2016-02-03 21:42:27 +08:00
$('#login').click(function(){
$('[data-remodal-id=login-modal]').remodal().open();
})
$('#register').click(function(){
$('[data-remodal-id=register-modal]').remodal().open();
})
// Register Event
2016-02-05 22:08:06 +08:00
$('body').on('keypress', '[data-remodal-id=register-modal]', function(event){
2016-02-03 21:42:27 +08:00
if (event.which == 13) register();
2016-02-05 22:08:06 +08:00
}).on('click', '#register-button', register);
2016-02-03 21:42:27 +08:00
// Login Event
2016-02-05 22:08:06 +08:00
$('body').on('keypress', '[data-remodal-id=login-modal]', function(event){
2016-02-03 21:42:27 +08:00
if (event.which == 13) login();
2016-02-05 22:08:06 +08:00
}).on('click', '#login-button', login);