var express = require('express');
var router = express.Router();
var register = require('../config/register');
var crypto = require('crypto');


router.post('/user/register', function(req, res) {
    //查表
    console.log(req.body.email,req.body.password)
    register.regsiterModel.find({
        "email": req.body.email
    }, function(err, doc) {
        if (doc.length == 1) {
            res.json({
                "ret": -1,
                "data": "用户已注册"
            })
        } else {
            //对密码  md5加密
            var md5 = crypto.createHash('md5');
            md5.update(req.body.password);
            var password = md5.digest('hex')

            //查表

            register.regsiterModel.create({
                "email": req.body.email,
                "password": password
            }, function(err, docs) {
                console.log(docs);

                res.json({
                    "ret": 1,
                    "data": docs
                })
            })
        }
    })

});


//校验邮箱是否已注册
router.post("/validate/email",function(req,res){
    var email=req.body.email;
    register.regsiterModel.find({
        "email": email
    },function(err,doc){
        if(doc.length==0){
            res.json({
                ret:1,
                "data":"未注册"
            })
        }else{
            res.json({
                "ret": -1,
                "data": "用户已注册"
            })
        }
    })

})

module.exports = router;
