李林
2024-03-13 bc81d955b9b79170aacfbab43d072660f9f35b50
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
155
156
157
158
package com.yuanchu.mom.controller;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanchu.mom.annotation.ValueAuth;
import com.yuanchu.mom.dto.Custom;
import com.yuanchu.mom.dto.UserPageDto;
import com.yuanchu.mom.pojo.User;
import com.yuanchu.mom.service.CustomService;
import com.yuanchu.mom.service.PowerService;
import com.yuanchu.mom.service.UserService;
import com.yuanchu.mom.utils.JackSonUtil;
import com.yuanchu.mom.utils.Jwt;
import com.yuanchu.mom.utils.RedisUtil;
import com.yuanchu.mom.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.Map;
 
@RequestMapping("/user")
@RestController
@Api(tags = "用户模块")
@AllArgsConstructor
public class UserController {
 
    private UserService userService;
 
    private PowerService powerService;
 
    private Jwt jwt;
 
    private CustomService customService;
 
    @ApiOperation(value = "登录")
    @PostMapping("/enter")
    @ValueAuth
    public Result login(String account, String password) {
        User user = userService.selectUserByPwd(account, password);
        if (user == null) return Result.fail("账号密码错误或者账号已被停用");
        String jwtToken;
        String jwtReToken;
        if(RedisUtil.hasKey("" + user.getId())){
            jwtToken = RedisUtil.get("" + user.getId()) + "";
        }else{
            jwtToken = jwt.createJwt(user.getName(), user.getId(), 24 * 60);
            RedisUtil.set("" + user.getId(), jwtToken, 24 * 60);
        }
        if(RedisUtil.hasKey(user.getId() + "Re")){
            jwtReToken = RedisUtil.get(user.getId() + "Re") + "";
        }else{
            jwtReToken = jwt.createJwt(user.getName(), user.getId(), 48 * 60);
            RedisUtil.set(user.getId() + "Re", jwtReToken, 48 * 60);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token", jwtToken);
        map.put("reToken", jwtReToken);
        map.put("name", user.getName());
        map.put("power", powerService.selectPowerByRoleId(user.getRoleId()));
        return Result.success("登录成功", map);
    }
 
    @ApiOperation(value = "token刷新")
    @PostMapping("/refresh")
    @ValueAuth
    public Result refresh(String reToken) throws Exception {
        Map<String, String> map = new Jwt().readJWT(reToken);
        Map<String, String> maps = JackSonUtil.unmarshal(map.get("data"), Map.class);
        if (map.get("state").equals("0")) {
            String redisToken = String.valueOf(RedisUtil.get(maps.get("id") + "Re"));
            if (!redisToken.equals(reToken)) {
                return Result.success("不存在的token", "1");
            } else if (redisToken.equals(reToken)) {
                int id = Integer.parseInt(maps.get("id").replaceAll("\"", ""));
                String name = maps.get("name").replaceAll("\"", "");
                String jwtToken = jwt.createJwt(name, id,  24 * 60);
                String jwtReToken = jwt.createJwt(name, id, 48 * 60);
                RedisUtil.set("" + id, jwtToken, 24 * 60);
                RedisUtil.set(id + "Re", jwtReToken, 48 * 60);
                Map<String, String> map1 = new HashMap<>();
                map1.put("token", jwtToken);
                map1.put("reToken", jwtReToken);
                return Result.success("刷新成功", map1);
            }
        }
        return Result.fail(map.get("info"));
    }
 
    @ApiOperation(value = "获取用户列表")
    @PostMapping("/selectUserList")
    public Result selectUserList(@RequestBody Map<String, Object> data) throws Exception {
        Page page = JackSonUtil.unmarshal(JackSonUtil.marshal(data.get("page")), Page.class);
        UserPageDto user = JackSonUtil.unmarshal(JackSonUtil.marshal(data.get("entity")), UserPageDto.class);
        return Result.success(userService.selectUserList(page, user));
    }
 
    @ApiOperation(value = "修改用户信息")
    @PostMapping("/updateUser")
    public Result<?> updateUser(@RequestBody User user) {
        if ("".equals(user.getPassword())) user.setPassword(null);
        else user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        return Result.success(userService.updateUser(user));
    }
 
    @ApiOperation(value = "添加用户信息")
    @PostMapping("/addUser")
    public Result<?> addUser(@RequestBody User user) {
        if ("".equals(user.getPassword())) user.setPassword(null);
        else user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        return Result.success(userService.addUser(user));
    }
 
    @ApiOperation(value = "获取用户信息枚举")
    @GetMapping("/getUserMenu")
    @ValueAuth
    public Result<?> getUserMenu() {
        return Result.success(userService.getUserMenu());
    }
 
    @ApiOperation(value = "获取客户列表")
    @PostMapping("/selectCustomPageList")
    public Result selectCustomPageList(@RequestBody Map<String, Object> data) throws Exception {
        Page page = JackSonUtil.unmarshal(JackSonUtil.marshal(data.get("page")), Page.class);
        Custom custom = JackSonUtil.unmarshal(JackSonUtil.marshal(data.get("entity")), Custom.class);
        return Result.success(customService.selectCustomPageList(page, custom));
    }
 
    @ApiOperation(value = "删除客户信息")
    @PostMapping("/delCustomById")
    public Result<?> delCustomById(Integer id) {
        return Result.success(customService.delCustomById(id));
    }
 
    @ApiOperation(value = "新增客户信息")
    @PostMapping("/addCustom")
    public Result<?> addCustom(@RequestBody Custom custom) {
        return Result.success(customService.addCustom(custom));
    }
 
    //获取设备负责人
    @ValueAuth
    @ApiOperation(value = "获取设备负责人")
    @GetMapping("/getDeviceManager")
    public Result<?> getDeviceManager() {
        return Result.success(userService.getDeviceManager());
    }
 
    @ValueAuth
    @GetMapping("/getUserNow")
    @ApiOperation(value = "获取当前登录的用户信息")
    public Result<?> getUserNow(){
        return null;
    }
 
}