userController.js 1.8 KB
var userModel =  require('../model/userModel');
var saitMd5 = require('../util/saltMD5')
var status = require('../util/resTemplate') 
const userService = require('../services/userService')
const uuid = require('../util/UuidUtil')


var userController =function (){

};

/**
 * Generate documentation output.
 *
 * @param {TAFFY} ctx 
 *                       
 * @param {object} next 
 */
userController.prototype.addUser = async(ctx, next) =>{
    var params = ctx.request.body;
    const pw = saitMd5.md5AddSalt(params.password)    

    if(!params.loginName){
        return status.paramError('loginName');
    }else if(!params.password){
        return status.paramError('password','不能为空');        
    }if(params.password.length < 6){
        return status.paramError('password','不得小于6位');                
    }

    var user = {
        loginName:params.loginName,
        password:pw.md5Pass,
        salt:pw.salt,
        userType:params.type
    }

    try{
        return await userService.addUser(user)        
    }catch (e){ 
        throw new Error(e);         
    }
}
/**
 * 用户登录
 */
userController.prototype.login = async(ctx, next) =>{
    const body = ctx.request.body;
    
    if(!body.name){
        return status.paramError('name');        
    }else if(!body.password){
        return status.paramError('password');
    }

    try{
        let userData =  await userService.login(body.name ,body.password);
        var userBack = {
            id:userData.id,
            loginName:userData.loginName,
            userType:userData.userType,
            token:userData.token,
            createTime:userData.createTime
        }
        return userBack;
    }catch (we){ 
        throw new Error(we)
    }

}
module.exports = new userController();