- 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;
- }
- //findOne方法,第一个参数数条件,第二个参数是字段投影,第三那个参数是回调函数
- 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(统一返回数据格式)文件内容如下:
[javascript] view plain copy
- 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;
时间: 2024-11-07 10:20:04