user.js
4.26 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
151
152
153
154
import jwt from "jsonwebtoken";
import conf from "../../config";
import userModel from "../../models/user";
module.exports = {
async login(ctx, next) {
console.log("----------------登录接口 user/login-----------------------");
let { username, pwd } = ctx.request.body;
try {
let data = await ctx.findOne(userModel, { username: username });
console.log(data);
if (!data) {
return ctx.sendError("用户名不存在!");
}
if (pwd !== data.pwd) {
return ctx.sendError("密码错误,请重新输入!");
}
await ctx.update(
userModel,
{ _id: data._id },
{ $set: { loginTime: new Date() } }
); //更新登陆时间
let payload = {
_id: data._id,
username: data.username,
roles: data.roles,
};
// token签名 有效期为24小时
let token = jwt.sign(payload, conf.auth.admin_secret, {
expiresIn: "24h",
});
// 是否只用于http请求中获取
ctx.cookies.set(conf.auth.tokenKey, token, {
httpOnly: false,
});
ctx.send({ message: "登录成功" });
} catch (e) {
if (e === "暂无数据") {
console.log("用户名不存在");
return ctx.sendError("用户名不存在");
}
ctx.throw(e);
ctx.sendError(e);
}
},
async info(ctx, next) {
console.log(
"----------------获取用户信息接口 user/getUserInfo-----------------------"
);
let token = ctx.request.query.token;
try {
let tokenInfo = jwt.verify(token, conf.auth.admin_secret);
console.log("log tokenInfo =>", tokenInfo);
ctx.send({
username: tokenInfo.username,
_id: tokenInfo._id,
roles: tokenInfo.roles,
});
} catch (e) {
if ("TokenExpiredError" === e.name) {
ctx.sendError("鉴权失败, 请重新登录!");
ctx.throw(401, "token验证失败, 请重新登录!");
}
ctx.throw(401, "invalid token");
ctx.sendError("系统异常!");
}
},
async list(ctx, next) {
console.log(
"----------------获取用户信息列表接口 user/getUserList-----------------------"
);
let { keyword, pageindex = 1, pagesize = 10 } = ctx.request.query;
console.log(
"keyword:" +
keyword +
"," +
"pageindex:" +
pageindex +
"," +
"pagesize:" +
pagesize
);
try {
let reg = new RegExp(keyword, "i");
let data = await ctx.findPage(
userModel,
{
$or: [{ username: { $regex: reg } }],
},
{ pwd: 0 },
{ limit: pagesize * 1, skip: (pageindex - 1) * pagesize }
);
ctx.send(data);
} catch (e) {
console.log(e);
ctx.sendError(e);
}
},
async add(ctx, next) {
console.log("----------------添加管理员 user/add-----------------------");
let paramsData = ctx.request.body;
try {
let data = await ctx.findOne(userModel, {
username: paramsData.username,
});
if (data) {
ctx.sendError("数据已经存在, 请重新添加!");
} else {
await ctx.add(userModel, paramsData);
ctx.send(paramsData);
}
} catch (e) {
ctx.sendError(e);
}
},
async update(ctx, next) {
console.log(
"----------------更新管理员 user/update-----------------------"
);
let paramsData = ctx.request.body;
console.log(paramsData);
try {
let data = await ctx.findOne(userModel, {
username: paramsData.username,
});
if (paramsData.old_pwd !== data.pwd) {
return ctx.sendError("密码不匹配!");
}
delete paramsData.old_pwd;
await ctx.update(userModel, { _id: paramsData._id }, paramsData);
ctx.send();
} catch (e) {
if (e === "暂无数据") {
ctx.sendError(e);
}
}
},
async del(ctx, next) {
console.log("----------------删除管理员 user/del-----------------------");
let id = ctx.request.query.id;
try {
ctx.remove(userModel, { _id: id });
ctx.send();
} catch (e) {
ctx.sendError(e);
}
},
};