jinpengyong
2023-09-06 e2411fee13ff406db4aa5d994d7dcffdf4482cab
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
package com.moral.api.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.SysConfig;
import com.moral.api.pojo.dto.login.LoginDTO;
import com.moral.api.pojo.form.login.LoginForm;
import com.moral.api.pojo.form.login.LogoutForm;
import com.moral.api.pojo.redisBean.AccountInfoDTO;
import com.moral.api.pojo.vo.login.AccountInfoVO;
import com.moral.api.pojo.vo.login.LoginVO;
import com.moral.api.service.ManageAccountService;
import com.moral.api.service.SysConfigService;
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 io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
/**
 * @ClassName LoginController
 * @Description TODO
 * @Author 陈凯裕
 * @Date 2021/3/18 15:28
 * @Version TODO
 **/
@Slf4j
@Api(tags = {"登陆模块"})
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
public class LoginController {
    @Autowired
    ManageAccountService accountService;
    @Autowired
    SysConfigService sysConfigService;
 
    /**
     * @Description: 登陆接口
     * @Param: [loginForm]
     * @return: com.moral.constant.ResultMessage
     * @Author: 陈凯裕
     * @Date: 2021/3/18
     */
    @PostMapping("login")
    public ResultMessage login(@RequestBody LoginForm loginForm) {
        if (!loginForm.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
 
        LoginDTO loginDTO = accountService.login(loginForm);
 
        LoginVO loginVO = LoginVO.convert(loginDTO);
 
        return new ResultMessage(loginDTO.getCode(), loginDTO.getMsg(), loginVO);
    }
 
    /**
    * @Description: 获取用户信息接口
            * @Param: []
            * @return: com.moral.constant.ResultMessage
            * @Author: 陈凯裕
            * @Date: 2021/6/9
            */
    @GetMapping("getAccountInfo")
    public ResultMessage getAccountInfo(){
        AccountInfoDTO dto = (AccountInfoDTO) TokenUtils.getUserInfo();
        AccountInfoVO vo = AccountInfoVO.convert(dto);
        return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(), vo);
    }
 
 
    /**
     * @Description: 退出接口
     * @Param: [logoutForm, request]
     * @return: com.moral.constant.ResultMessage
     * @Author: 陈凯裕
     * @Date: 2021/3/18
     */
    @PostMapping("logout")
    public ResultMessage logout(@RequestBody LogoutForm logoutForm, HttpServletRequest request) {
        if (!logoutForm.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        String token = request.getHeader("token");
        logoutForm.setToken(token);
 
        if (accountService.logout(logoutForm))
            return ResultMessage.ok();
        return ResultMessage.fail();
    }
 
    /**
    * @Description: 获取验证码是否开启
            * @Param: []
            * @return: com.moral.constant.ResultMessage
            * @Author: 陈凯裕
            * @Date: 2021/3/18
            */
    @GetMapping("verificationCode/config")
    public ResultMessage verifyConfig(){
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();
        wrapper.eq(Constants.MANAGE_VERIFICATIONCODE_COLUMN,Constants.MANAGE_VERIFICATIONCODE_VALUE);
        SysConfig config = sysConfigService.getOne(wrapper);
        if(config.getCode().equals(Constants.VERFICATIONCODE_OPEN))
            return new ResultMessage(ResponseCodeEnum.VERIFICATION_OPEN,null);
        return new ResultMessage(ResponseCodeEnum.VERIFICATION_CLOSE,null);
    }
 
    /**
     * @Description: 获取验证码接口
     * @Param: []
     * @return: com.moral.constant.ResultMessage
     * @Author: 陈凯裕
     * @Date: 2021/3/18
     */
    @GetMapping("verificationCode/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();
        System.out.println(verificationCode.getEncode());
        return ResultMessage.ok(verificationCode);
    }
 
    /**
    * @Description: 校验验证码
            * @Param: [verificationCode]
            * @return: com.moral.constant.ResultMessage
            * @Author: 陈凯裕
            * @Date: 2021/3/18
            */
    @GetMapping("verificationCode/verify")
    public ResultMessage gverifyVerificationCode(VerificationCode verificationCode) {
        if(!verificationCode.valid())
            return ResultMessage.fail();
        if(KaptchaUtils.verify(verificationCode))
            return ResultMessage.ok();
        return ResultMessage.fail();
    }
}