kaiyu
2021-04-07 89c6943973621c6dfc23844999245cc35ee477b4
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
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.io.IOException;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.SysConfig;
import com.moral.api.entity.UserLog;
import com.moral.api.service.SysConfigService;
import com.moral.api.service.UserLogService;
import com.moral.api.service.UserService;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.pojo.VerificationCode;
import com.moral.util.KaptchaUtils;
import com.moral.util.TokenUtils;
import com.moral.util.WebUtils;
 
@Slf4j
@Api(tags = {"登陆"})
@RestController
public class LoginController {
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private SysConfigService sysConfigService;
 
    @Autowired
    private UserLogService userLogService;
 
    @ApiOperation(value = "登陆", notes = "登陆")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ResultMessage login(@RequestBody Map<String, Object> parameters, HttpServletRequest request) {
        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((int) result.get("code"), (String) result.get("msg"));
        }
        Map<String, Object> data = (Map<String, Object>) result.get("data");
        Map<String, Object> userInfo = (Map<String, Object>) data.get("user");
        UserLog userLog = new UserLog();
        String ip = WebUtils.getIpAddr(request);
        userLog.setIp(ip);
        userLog.setOperateId((Integer) userInfo.get("userId"));
        userLog.setOrganizationId((Integer) userInfo.get("organizationId"));
        userLog.setContent(userInfo.get("account") + "登陆了");
        userLogService.save(userLog);
        return ResultMessage.ok(data);
    }
 
    @ApiOperation(value = "退出", notes = "退出")
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    public ResultMessage logout(@RequestBody Map<String, Object> parameters, HttpServletRequest request) {
        if (!parameters.containsKey("uid")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        String token = request.getHeader("token");
        TokenUtils.destoryToken(parameters.get("uid").toString(), token);
        return ResultMessage.ok();
    }
 
    /**
     * @Description: 获取验证码是否开启
     * @Param: []
     * @return: com.moral.constant.ResultMessage
     * @Author: 陈凯裕
     * @Date: 2021/3/18
     */
 
    @ApiOperation(value = "获取验证码是否开启", notes = "获取验证码是否开启")
    @RequestMapping(value = "verificationCode/config", method = RequestMethod.GET)
    public void verifyConfig(HttpServletResponse response) {
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.VERIFICATION_TYPE);
        SysConfig sysConfig = sysConfigService.getOne(queryWrapper);
        String code = sysConfig.getCode();
        if (Constants.VERFICATIONCODE_OPEN.equals(code)) {
            try {
                response.sendRedirect("/verificationCode/get");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @RequestMapping(value = "verificationCode/get", method = RequestMethod.GET)
    public ResultMessage getVerificationCode() {
        VerificationCode verificationCode = null;
        try {
            verificationCode = KaptchaUtils.createVerificationCode();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        if (ObjectUtils.isEmpty(verificationCode))
            return ResultMessage.fail();
        return ResultMessage.ok(verificationCode);
    }
 
    @ApiOperation(value = "校验验证码", notes = "校验验证码")
    @RequestMapping(value = "verificationCode/verify", method = RequestMethod.GET)
    public ResultMessage gverifyVerificationCode(VerificationCode verificationCode) {
        if (!verificationCode.valid())
            return ResultMessage.fail();
        if (KaptchaUtils.verify(verificationCode))
            return ResultMessage.ok();
        return ResultMessage.fail();
    }
}