- router.post(‘/register‘,function(req,res,next){
- var restResult = new RestResult();
- var mobile = req.body.mobile;
- if (!/1\d{10}/.test(mobile)){
- restResult.errorCode = RestResult.ILLEGAL_ARGUMENT_ERROR_CODE;
- restResult.errorReason = "请填写真确的手机格式";
- res.send(restResult);
- return;
- }
- var password = req.body.password;
- if(!password || password.length < 6){
- restResult.errorCode = RestResult.ILLEGAL_ARGUMENT_ERROR_CODE;
- restResult.errorReason = "密码长度不能少于6位";
- res.send(restResult);
- return;
- }
-
-
- UserEntity.findOne({mobile:mobile},‘_id‘,function(err,user){
- if(err){
- restResult.errorCode = RestResult.SERVER_EXCEPTION_ERROR_CODE;
- restResult.errorReason = "服务器异常";
- res.send(restResult);
- return;
- }
-
- if (user){
- restResult.errorCode = RestResult.BUSINESS_ERROR_CODE;
- restResult.errorReason = "手机号已注册";
- res.send(restResult);
- return;
- }
-
- var registerUser = new UserEntity({mobile:mobile,password:password});
-
- registerUser.save(function(err,row){
- if(err){
- restResult.errorCode = RestResult.SERVER_EXCEPTION_ERROR_CODE;
- restResult.errorReason = "服务器异常";
- res.send(restResult);
- return;
- }
-
- res.send(restResult);
-
- });
-
- });
-
- });
-
-
-
- router.post(‘/login‘,function(req,res,next){
- var restResult = new RestResult();
- var mobile = req.body.mobile;
- if (!/1\d{10}/.test(mobile)){
- restResult.errorCode = RestResult.ILLEGAL_ARGUMENT_ERROR_CODE;
- restResult.errorReason = "请填写真确的手机格式";
- res.send(restResult);
- return;
- }
- var password = req.body.password;
- if(!password){
- restResult.errorCode = RestResult.ILLEGAL_ARGUMENT_ERROR_CODE;
- restResult.errorReason = "密码不能为空";
- res.send(restResult);
- return;
- }
-
- UserEntity.findOne({mobile:mobile,password:password},{password:0},function(err,user){
- if(err){
- restResult.errorCode = RestResult.SERVER_EXCEPTION_ERROR_CODE;
- restResult.errorReason = "服务器异常";
- res.send(restResult);
- return;
- }
-
- if(!user){
- restResult.errorCode = RestResult.BUSINESS_ERROR_CODE;
- restResult.errorReason = "用户名或密码错误";
- res.send(restResult);
- return;
- }
-
- restResult.returnValue = user;
- res.send(restResult);
-
-
- UserEntity.update({_id:user._id},{$set: {lastLoginTime: new Date()}}).exec();
-
- });
-
- });
-
- module.exports = router;
RestResult.js(统一返回数据格式)文件内容如下:
- var RestResult = function(){
- this.errorCode = RestResult.NO_ERROR ;
- this.returnValue = {};
- this.errorReason = "";
- };
-
-
-
- RestResult.NO_ERROR = 0;
- RestResult.ILLEGAL_ARGUMENT_ERROR_CODE = 1;
- RestResult.BUSINESS_ERROR_CODE = 2;
- RestResult.AUTH_ERROR_CODE = 3;
- RestResult.SERVER_EXCEPTION_ERROR_CODE = 5;
- RestResult.TARGET_NOT_EXIT_ERROR_CODE = 6;
-
- module.exports = RestResult;
手机号码格式校验
原文:http://www.cnblogs.com/jayruan/p/5140998.html