严智鑫
2024-03-31 895f490f4223d5b15ec948ba2155b005018aa91d
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
package com.mes.controller.userInfo;
 
import com.mes.common.CacheUtil;
import com.mes.common.Constants;
import com.mes.common.Result;
import com.mes.controller.dto.UserDTO;
import com.mes.entity.userInfo.User;
import com.mes.exception.ServiceException;
import com.mes.mapper.userInfo.UserMapper;
import com.mes.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;
@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
    }
}