userController.js
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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)
}
}
userController.prototype.getStu = async(ctx, next) =>{
const stuType = ctx.params.type;
if(!stuType){
return status.paramError('type');
}
try {
let stu = await userService.getStu(stuType);
return stu
} catch (error) {
throw new Error(error)
}
}
userController.prototype.updateUserByUserId = async(ctx, next) =>{
const userId = ctx.params.userId;
const updateData = ctx.request.body;
if(!userId){
return status.paramError('userId');
}
if(!updateData){
return {code:0,msg:'修改内容不能为空'}
}
if(updateData.id||updateData.password||updateData.salt||updateData.userType){
return {code:3,msg:'用户id、密码、类型是不能修改的'}
}
try {
let stu = await userService.updateUserByUserId(userId,updateData);
return stu
} catch (error) {
throw new Error(error)
}
}
module.exports = new userController();