今天為昨天寫的登陸注冊頁面增加了前端和后端邏輯,后端采用express甲棍,前端則是angularJS迟螺,數(shù)據(jù)庫使用mongodb,沒有加緩存層荆萤。
- Angular的服務(wù)部分
var Services=angular.module('myApp.Services',[]);
Services.factory('checkUser',function(){
//檢查用戶登陸和注冊時填寫的格式
function checkUserName(str, minLen, maxLen) {
var reg = /^[(\u4e00-\u9fa5)a-z][(\u4e00-\u9fa5)a-zA-Z0-9_]{1,15}$/;
var len = str.length;
return reg.test(str) && len >= minLen && len <= maxLen;
}
function checkEmail(str) {
var reg = /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/;
return reg.test(str) && str.length >= 6 && str.length <= 64;
}
return function(form,option) {
if(!form.username) return '用戶名缺省';
if(!form.password) return '密碼缺省';
if(!checkUserName(form.username, 4, 8)) return '用戶名格式有誤';
if(!checkUserName(form.password,6,15)) return '密碼格式有誤';
if(option=='signup'){
if(!form.email) return 'Email缺省';
if(!form.repeatPassword) return '重復(fù)密碼缺省';
if(!checkEmail(form.email)) return 'Email格式錯誤';
if(form.password!==form.repeatPassword) return '密碼不一致';
}
return null;
};
}).factory('extend',function(){
//簡單深度復(fù)制對象
return function(dst,src){
for(var i in src){
dst[i]= typeof src[i]==='object'?cloneObject(src[i]):src[i];
}
};
}).factory('CryptoJS',function(){
//加密模塊
return window.CryptoJS;
});
- Angular的控制器部分
Controllers.controller('SignCtrl',function($scope,$http,$window,extend,CryptoJS,checkUser){
$scope.regForm={};
$scope.logForm={};
$scope.msg='';
$scope.appear=false;
$scope.signup=function(){
var err=checkUser($scope.regForm,'signup');
if(err){
$scope.msg=err;
$scope.appear=true;
}
else{
var tmp={};
extend(tmp,$scope.regForm);
tmp.password=CryptoJS.SHA256(tmp.password).toString();
tmp.password=CryptoJS.HmacSHA256(tmp.password,'ustc').toString();
delete tmp.repeatPassword;
// console.log($scope.regForm);
// console.log(tmp);
$http.post('/signup',tmp).success(function(data){
if(data.err){
console.log(data.err.message);
$scope.msg=data.err.message;
$scope.appear=true;//使alert展現(xiàn)
}
else{
$scope.msg='注冊成功镊靴,請登陸';
$scope.appear=true;
$scope.logForm.username=$scope.regForm.username;
$scope.logForm.password=$scope.regForm.password;
//自動填充登陸框
}
}).error(function(data){
$scope.msg='未知錯誤,請重試';
$scope.appear=true;
});
}
};
$scope.signin=function(){
var err=checkUser($scope.logForm,'signin');
if(err){
$scope.msg=err;
$scope.appear=true;
}
else{
var tmp={};
extend(tmp,$scope.logForm);
tmp.password=CryptoJS.SHA256(tmp.password).toString();
tmp.password=CryptoJS.HmacSHA256(tmp.password,'ustc').toString();
$http.post('/signin',tmp).success(function(data){
console.log(data);
if(data.err){
console.log(data.err.message);
$scope.msg=data.err.message;
$scope.appear=true;
}
else{
$window.location.href='/';
}
}).error(function(data){
$scope.msg='未知錯誤链韭,請重試';
$scope.appear=true;
});
}
};
});
- 后端部分
function signup(req,res){
var tmp=req.body.password;
req.body.password=tool.MD5(tmp);
//addUser負(fù)責(zé)與數(shù)據(jù)庫交互偏竟,會檢查用戶名和電子郵件是否重復(fù)
userDB.addUser(req.body).then(function(cont,user){
res.json({
success:true,
err:null
});
}).fail(function(cont,err){
res.json({
success:false,
err:err
});
});
}
function signin(req,res){
var tmp=req.body.password;
var uob=req.body;
uob.password=tool.MD5(tmp);
Then(function(cont){
User.findOne({username:uob.username},cont);
}).then(function(cont,doc){
if(!doc) return cont(new Err(msg.USER.userNone));
if(doc.password!==uob.password) return cont(new Err(msg.USER.userPasswd));
req.session.uname=doc.username;
res.cookie('user',{
username:doc.username,
password:tmp,
email:doc.email
},{
signed:true,
maxAge:900000
});
res.json({
success:true,
err:null
});
}).fail(function(cont,err){
console.log(err);
res.json({
success:false,
err:err
});
});
}
- 數(shù)據(jù)庫部分
function addUser(userobj){
return Then(function(cont){
User.findOne({username:userobj.username},cont);
}).then(function(cont,doc){
if(doc) return cont(new Err(msg.USER.userNameExist));
else User.findOne({email:userobj.email},cont);
}).then(function(cont,doc){
if(doc) return cont(new Err(msg.USER.userEmailExist));
else (new User(userobj)).save(cont);
}).fail(errorhandler);
}