userController.js
3.8 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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) =>{
try{
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,
companyName:params.companyName,
userName:params.userName,
salt:pw.salt,
userType:params.userType,
userRole:params.userRole,
userEmail:params.userEmail,
userMobile:params.userMobile,
content:params.content,
endTime:new Date(params.endTime),
groupId:params.groupId
}
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
}
if(!userData.code){
return userBack;
}else{
return userData;
}
}catch (we){
throw new Error(we)
}
}
userController.prototype.getStu = async(ctx, next) =>{
try {
const stuType = ctx.params.type;
if(!stuType){
return status.paramError('type');
}
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)
}
}
userController.prototype.delUserByUserId = async(ctx,next) => {
const userId = ctx.params.userId;
if(!userId){
return status.paramError('userId');
}
try {
let deluser = userService.delUserByUserId(userId);
return deluser
} catch (error) {
throw new Error(error)
}
}
userController.prototype.resetPasswordByUserId = async(ctx,next) =>{
const userId = ctx.params.userId;
const pw = saitMd5.md5AddSalt('123456')
if(!userId){
return status.paramError('userId');
}
try {
let deluser = userService.resetPasswordByUserId(userId,pw);
return deluser
} catch (error) {
throw new Error(error)
}
}
module.exports = new userController();