jinpengyong
2021-03-12 e442e98a198d67994e6dce01fd88e163291fd40c
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.moral.api.controller;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
 
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.moral.api.entity.Group;
import com.moral.api.entity.User;
import com.moral.api.service.GroupService;
import com.moral.api.service.UserService;
import com.moral.constant.ResultMessage;
import com.moral.redis.RedisUtil;
import com.moral.util.WebUtils;
 
@Slf4j
@Api(tags = {"大屏"})
@RestController
@RequestMapping("/api")
public class WebController {
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private GroupService groupService;
 
    @ApiOperation(value = "登陆", notes = "登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ResultMessage login(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        if (!(parameters.containsKey("account") && parameters.containsKey("password"))) {
            return ResultMessage.fail("用户名及密码不允许为空!");
        }
        Map<String, Object> map = userService.login(parameters);
        if (map.get("token") == null) {
            return ResultMessage.fail(map.get("msg").toString());
        }
        return ResultMessage.ok(map);
    }
 
    @ApiOperation(value = "注销", notes = "注销")
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public ResultMessage logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return ResultMessage.fail("未登陆");
        }
        RedisUtil.del(token);
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "添加账户", notes = "添加账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "账户名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信", required = false, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    public ResultMessage addUser(User user, HttpServletRequest request) {
        Integer currentUserId = Integer.parseInt(request.getHeader("uid"));
        Map<String, Object> map = userService.addUser(user, currentUserId);
        String msg = map.get("msg").toString();
        boolean flag = Boolean.parseBoolean(map.get("flag").toString());
        if (flag) {
            return ResultMessage.ok(msg);
        }
        return ResultMessage.fail(msg);
    }
 
    @ApiOperation(value = "删除账户", notes = "删除账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "path", dataType = "String")
    })
    @RequestMapping(value = "deleteUser/{userId}", method = RequestMethod.GET)
    public ResultMessage deleteUser(@PathVariable("userId") String userId, HttpServletRequest request) {
        Integer currentUserId = Integer.parseInt(request.getHeader("uid"));
        Map<String, Object> map = userService.deleteUser(Integer.parseInt(userId), currentUserId);
        String msg = map.get("msg").toString();
        boolean flag = Boolean.parseBoolean(map.get("flag").toString());
        if (flag) {
            return ResultMessage.ok(msg);
        }
        return ResultMessage.fail(msg);
    }
 
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "账户名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信", required = false, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    public ResultMessage updateUser(User user, HttpServletRequest request) {
        Integer currentUserId = Integer.parseInt(request.getHeader("uid"));
        Map<String, Object> map = userService.updateUser(user, currentUserId);
        String msg = map.get("msg").toString();
        boolean flag = Boolean.parseBoolean(map.get("flag").toString());
        if (flag) {
            return ResultMessage.ok(msg);
        }
        return ResultMessage.fail(msg);
    }
 
    @ApiOperation(value = "查询用户信息", notes = "查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, paramType = "path", dataType = "String")
    })
    @RequestMapping(value = "getUserInfo/{userId}", method = RequestMethod.GET)
    public ResultMessage getUserInfo(@PathVariable("userId") String userId, HttpServletRequest request) {
        Integer currentUserId = Integer.parseInt(request.getHeader("uid"));
        if (userId == null) {
            List<User> users = userService.getUsersByOrgId(currentUserId);
            return ResultMessage.ok(users);
        }
        User user = userService.getUserById(Integer.parseInt(userId), currentUserId);
        return ResultMessage.ok(user);
    }
 
    @ApiOperation(value = "添加组", notes = "添加组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupName", value = "组名", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "addGroup", method = RequestMethod.POST)
    private ResultMessage addGroup(Group group, HttpServletRequest request) {
        String currentUserId = request.getHeader("uid");
        Map<String, Object> map = groupService.addGroup(group, currentUserId);
        String msg = map.get("msg").toString();
        boolean flag = Boolean.parseBoolean(map.get("flag").toString());
        if (flag) {
            return ResultMessage.ok(msg);
        }
        return ResultMessage.fail(msg);
    }
 
}