You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
4.8 KiB
146 lines
4.8 KiB
package com.yanzhen.controller;
|
|
|
|
import com.github.pagehelper.PageInfo;
|
|
import com.yanzhen.entity.Student;
|
|
import com.yanzhen.entity.User;
|
|
import com.yanzhen.service.StudentService;
|
|
import com.yanzhen.service.UserService;
|
|
import com.yanzhen.utils.Result;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.util.Map;
|
|
|
|
//用户管理的控制器
|
|
@RestController
|
|
@RequestMapping("/user")
|
|
public class UserController {
|
|
//注入实例
|
|
//注入用户管理的服务类
|
|
@Autowired
|
|
private UserService userService;
|
|
//注入学生服务类
|
|
@Autowired
|
|
private StudentService studentService;
|
|
//创建新的实例
|
|
@PostMapping("create")
|
|
public Result create(@RequestBody User user){
|
|
//在用户管理服务类中引用create方法
|
|
int flag = userService.create(user);
|
|
//判断是否创建成功
|
|
if(flag>0){
|
|
//成功
|
|
return Result.ok();
|
|
}else{
|
|
//失败
|
|
return Result.fail();
|
|
}
|
|
}
|
|
//删除用户信息
|
|
@GetMapping("delete")
|
|
public Result delete(String ids){
|
|
//在用户管理服务类中引用delete方法
|
|
int flag = userService.delete(ids);
|
|
//判断是否删除成功
|
|
if(flag>0){
|
|
//成功
|
|
return Result.ok();
|
|
}else{
|
|
//失败
|
|
return Result.fail();
|
|
}
|
|
}
|
|
//更新用户信息
|
|
@PostMapping("update")
|
|
public Result update(@RequestBody User user){
|
|
//在用户管理服务类中引用update方法
|
|
int flag = userService.updateSelective(user);
|
|
//判断是否更新成功
|
|
if(flag>0){
|
|
//成功
|
|
return Result.ok();
|
|
}else{
|
|
//失败
|
|
return Result.fail();
|
|
}
|
|
}
|
|
//获取用户详细信息
|
|
@GetMapping("detail")
|
|
public User detail(Integer id){
|
|
//返回结果
|
|
return userService.detail(id);
|
|
}
|
|
//获取用户信息
|
|
@GetMapping("info")
|
|
public Result info(HttpServletRequest request){
|
|
//获取当前用户对象。
|
|
User user = (User)request.getAttribute("user");
|
|
//返回结果
|
|
return Result.ok(userService.detail(user.getId()));
|
|
}
|
|
//修改用户密码
|
|
@PostMapping("pwd")
|
|
public Result pwd(@RequestBody Map<String,String> map,HttpServletRequest request){
|
|
// 获取原密码
|
|
String spassword = map.get("spassword");
|
|
// 获取新密码
|
|
String npassword = map.get("npassword");
|
|
//用户修改
|
|
if(request.getAttribute("user") != null){
|
|
// 获取当前用户对象。
|
|
User user = (User)request.getAttribute("user");
|
|
// 获取用户详细信息。
|
|
User entity = userService.detail(user.getId());
|
|
//判断原密码是否正确
|
|
if(entity.getPassword().equals(spassword)){
|
|
// 创建新的 User 对象用于更新密码
|
|
User param = new User();
|
|
// 设置用户 ID
|
|
param.setId(entity.getId());
|
|
// 设置新密码
|
|
param.setPassword(npassword);
|
|
// 更新密码
|
|
userService.updatePwd(param);
|
|
//成功
|
|
return Result.ok("修改密码成功");
|
|
}else{
|
|
//失败
|
|
return Result.fail("原密码错误");
|
|
}
|
|
}
|
|
//学生修改
|
|
if(request.getAttribute("student") != null){
|
|
// 获取当前学生对象
|
|
Student student = (Student)request.getAttribute("student");
|
|
//获取学生详细信息
|
|
Student entity = studentService.detail(student.getId());
|
|
//判断原密码是否正确
|
|
if(entity.getPassword().equals(spassword)){
|
|
// 更新密码
|
|
Student param = new Student();
|
|
// 设置学生 ID
|
|
param.setId(entity.getId());
|
|
// 设置新密码
|
|
param.setPassword(npassword);
|
|
// 更新密码
|
|
studentService.updateSelective(param);
|
|
//成功
|
|
return Result.ok("修改密码成功");
|
|
}else{
|
|
//失败
|
|
return Result.fail("原密码错误");
|
|
}
|
|
}
|
|
return Result.ok(); // 如果既不是普通用户也不是学生,返回成功的 Result 对象。
|
|
}
|
|
//查询用户信息
|
|
@PostMapping("query")
|
|
public Map<String,Object> query(@RequestBody User user){
|
|
//创建分页对象
|
|
PageInfo<User> pageInfo = userService.query(user);
|
|
//返回结果
|
|
return Result.ok(pageInfo);
|
|
}
|
|
|
|
} |