今天为昨天写的登陆注册页面增加了前端和后端逻辑,后端采用express,前端则是angularJS,数据库使用mongodb,没有加缓存层。
- Angular的服务部分
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 '重复密码缺省';
if(!checkEmail(form.email)) return 'Email格式错误';
if(form.password!==form.repeatPassword) return '密码不一致';
}
return null;
};
}).factory('extend',function(){
//简单深度复制对象
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展现
}
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负责与数据库交互,会检查用户名和电子邮件是否重复
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
});
});
}
- 数据库部分
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);
}