const {User, validateUser} = require('../../model/user'); const bcrypt = require('bcrypt'); module.exports = async (req, res, next) => { try{ // 实施验证 await validateUser(req.body); }catch(e) { // 验证未通过 // 重定向会用户添加页面 return next(JSON.stringify({path: '/admin/user-edit', message: e.message})); } // 根据邮箱地址查询用户是否存在 const user = await User.findOne({email: req.body.email}); if(user){ // 重定向会用户添加页面 return next(JSON.stringify({path: '/admin/user-edit', message: '邮箱账号被占用'})); } // 对密码进行加密 const salt = await bcrypt.genSalt(10); req.body.password = await bcrypt.hash(req.body.password, salt); await User.create(req.body); res.redirect('/admin/user'); }