chenlu
2024-06-07 2f640b1038fa331954f78ed1f4317212cf5bb34d
north-glass-erp/src/main/java/com/example/erp/controller/userInfo/UserController.java
New file
@@ -0,0 +1,65 @@
package com.example.erp.controller.userInfo;
import com.example.erp.common.CacheUtil;
import com.example.erp.common.Constants;
import com.example.erp.common.Result;
import com.example.erp.controller.dto.UserDTO;
import com.example.erp.entity.userInfo.User;
import com.example.erp.exception.ServiceException;
import com.example.erp.mapper.userInfo.UserMapper;
import com.example.erp.service.userInfo.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController //注解user控制器,挂载到springboot当中
@RequestMapping("/user")  //注解前端异步请求如:localhost:8080/user
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private CacheUtil cacheUtil;
    @GetMapping
    public List<User> getUser(){
        System.out.println(cacheUtil.getCacheData("admin"));
        return userMapper.findAll();
    }
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){
        //UserDTO getUserCacheDTO = cacheUtil.getCacheData(userDTO.getUserId());
        userService.deleteCache(userDTO.getUserId());
        UserDTO getUserDTO=userService.login(userDTO);
        if(getUserDTO!=null){
            return Result.seccess(userDTO);
        }else{
            throw new ServiceException(Constants.Code_600,"用户名或密码错误");
        }
    }
    @ApiOperation("注册")
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        User getUser= userService.register(user);
        if(getUser == null){
            throw new ServiceException(Constants.Code_500,"注册失败");
        }else {
            return Result.seccess(getUser);
        }
        //return
    }
    @ApiOperation("修改密码")
    @PostMapping("/updatePassWord")
    public Result updatePassWord(@RequestBody Map<String,Object> object){
        return Result.seccess( userService.updatePassWord(object));
    }
}