# **查**
## **路由**
```
router.get("/admin/user/list/:id?", UserController.getUserList); // 获取用户列表,动态传参,可选的路由参数
```
## **controller**
```
getUserList: async (req, res) => {
let id = req.query.id || ""; // 是否根据单个id获取指定id的用户信息 默认为空 /admin/user/list?id=1
if (!id) {
id = req.params.id || ""; // 是否根据单个id获取指定id的用户信息 默认为空 /admin/user/list/1
}
let { results, total } = await UserServices.getUserList(id, req.query);
res.send({
code: 200,
data: {
lists: id ? results[0] : results,
total,
},
});
},
```
## **service**
```
getUserList: async (id, query) => {
let idQuery = {
username: { $ne: "admin" }, // 排除username是admin的用户
};
let queryFields = ["username", "gender", "role", "description", "avatar"];
if (id) {
idQuery._id = id; // 查询指定id的用户
queryFields = null; // 返回全部字段
}
const skipValue = ((query.page || 1) - 1) * (query.limit || 2); // 从数据库第n条数据开始查询
let data = await UserModel.find(idQuery, queryFields)
.skip(skipValue)
.limit(query.limit || 2).sort({ gender: -1 });// 分页查询,根据性别降序排序(1:升序,-1:降序)
return {
results: data,
total: await UserModel.countDocuments(idQuery),// 查询总数
};
}
```
# **删**
## **路由**
```
router.delete("/admin/user/delete/:id", UserController.deleteUser);
```
## **controller**
```
deleteUser: async (req, res) => {
await UserServices.deleteUser(req.params.id);
res.send({ code: 200, msg: "删除成功" });
},
```
## **service**
```
deleteUser: async (_id) => {
return UserModel.deleteOne({ _id });
},
```
# **增**
## **路由**
```
router.post("/admin/user/add", upload.single("file"), UserController.addUser); // 添加用户
```
## **controller**
```
addUser: async (req, res) => {
let data = {
username: req.body.username,
password: req.body.password,
instanceof: req.body.instanceof,
role: Number(req.body.role),
gender: Number(req.body.gender),
avatar: `/uploads/avatar/${req.file.filename}`,
};
await UserServices.addUser(data);
res.send({ code: 200, msg: "添加成功" });
},
```
## **service**
```
addUser: async (userData) => {
return UserModel.create(userData);
},
```
# **改**
## **路由**
```
// 修改指定id用户
router.put("/admin/user/updateUser", UserController.updateUser);
```
```
// 修改当前登录用户信息
router.post(
"/admin/user/update",
upload.single("file"),
UserController.updateUserInfo
);
```
## **controller**
```
// 修改指定id用户
updateUser: async (req, res) => {
let data = {
username: req.body.username,
password: req.body.password,
role: Number(req.body.role),
instanceof: req.body.instanceof,
};
await UserServices.updateUser(req.body._id, data);
res.send({ code: 200, msg: "更新成功" });
},
```
```
// 修改当前登录用户信息
updateUserInfo: async (req, res) => {
const token = req.headers.authorization.split(" ")[1];
const payload = jwt.verify(token);
let data = {
username: req.body.username,
gender: Number(req.body.gender),
};
if (req.file) {
data.avatar = `/uploads/avatar/${req.file.filename}`;
}
await UserServices.updateUserInfo(payload._id, data);
res.send({ code: 200, msg: "更新成功" });
},
```
## **service**
```
// 修改指定id用户
updateUser: async (_id, updateData) => {
return UserModel.updateOne({ _id }, updateData);
},
```
```
// 修改当前登录用户信息
updateUserInfo: async (_id, updateData) => {
return UserModel.updateOne(_id, updateData);
},
```