package com.moral.api.controller; import com.moral.api.pojo.dto.login.LoginDTO; import com.moral.api.pojo.form.LoginForm; import com.moral.api.pojo.form.LogoutForm; import com.moral.api.pojo.vo.login.LoginVO; import com.moral.api.service.ManageAccountService; import com.moral.constant.ResponseCodeEnum; import com.moral.constant.ResultMessage; import com.moral.pojo.VerificationCode; import com.moral.util.KaptchaUtils; 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 = {"登陆模块"}) @RestController public class LoginController { @Autowired ManageAccountService accountService; /** * @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: [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 public ResultMessage verifyConfig(){ return 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(); } }