jinpengyong
2021-03-18 b64946fb4c4b32592633ff1dc0b95a77cb440246
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
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.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
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.service.GroupService;
import com.moral.api.service.UserService;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.TokenUtils;
import com.moral.util.WebUtils;
 
@Slf4j
@Api(tags = {"登陆"})
@RestController
public class LoginController {
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private GroupService groupService;
 
    @ApiOperation(value = "登陆", notes = "登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = false, 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(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> result = userService.login(parameters);
        if (!result.containsKey("data")) {
            return ResultMessage.fail(Integer.parseInt(result.get("code").toString()), result.get("msg").toString());
        }
        return ResultMessage.ok(result.get("data"));
    }
 
    @ApiOperation(value = "注销", notes = "注销")
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public ResultMessage logout(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        if (!parameters.containsKey("uid")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        String userId = parameters.get("uid").toString();
        String token = request.getHeader("token");
        TokenUtils.destoryToken(userId, token);
        return ResultMessage.ok();
    }
 
    @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);
    }
 
}