2024-11-05 17:10:26 +08:00
|
|
|
package com.yunan.controller;
|
|
|
|
|
|
|
|
import com.yunan.constant.ResponseCode;
|
2024-11-06 18:47:00 +08:00
|
|
|
import com.yunan.response.ApiResponse;
|
2024-11-05 17:10:26 +08:00
|
|
|
import com.yunan.dto.LoginDTO;
|
|
|
|
import com.yunan.dto.RegisterDTO;
|
|
|
|
import com.yunan.service.AuthService;
|
|
|
|
import com.yunan.service.EmailService;
|
|
|
|
import com.yunan.util.VerificationCodeUtils;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2024-11-06 18:47:00 +08:00
|
|
|
import org.springframework.validation.BindingResult;
|
2024-11-05 17:10:26 +08:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.mail.MessagingException;
|
2024-11-06 18:47:00 +08:00
|
|
|
import javax.validation.Valid;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
2024-11-05 17:10:26 +08:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController //注解标识这是一个控制器类
|
|
|
|
@RequestMapping("/auth")
|
|
|
|
@Slf4j
|
|
|
|
@Api(tags = "身份验证接口")
|
|
|
|
public class AuthController {
|
|
|
|
@Resource
|
|
|
|
private AuthService authService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private EmailService emailService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户登入
|
|
|
|
* @param loginDTO
|
|
|
|
*/
|
|
|
|
@PostMapping("/login")
|
|
|
|
@ApiOperation("用户登入")
|
|
|
|
public void login(@RequestBody LoginDTO loginDTO) {
|
|
|
|
// authService.login(loginDTO.getUsername(), loginDTO.getPassword());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户注册
|
|
|
|
* @param registerDTO
|
|
|
|
*/
|
|
|
|
@PostMapping("/register")
|
|
|
|
@ApiOperation("用户注册")
|
2024-11-06 18:47:00 +08:00
|
|
|
public ResponseEntity<Object> register(@RequestBody @Valid RegisterDTO registerDTO) throws MessagingException, UnsupportedEncodingException {
|
2024-11-09 16:49:13 +08:00
|
|
|
return authService.register(registerDTO);
|
2024-11-05 17:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 存储已发送的验证码
|
|
|
|
private final Map<String, String> emailCodeMap = new ConcurrentHashMap<>(16);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送验证码
|
2024-11-06 18:47:00 +08:00
|
|
|
* @param registerDTO
|
2024-11-05 17:10:26 +08:00
|
|
|
* @return {@link ApiResponse }<{@link String }>
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
|
|
|
@PostMapping("/sendEmail")
|
|
|
|
@ApiOperation("邮箱验证码")
|
2024-11-06 18:47:00 +08:00
|
|
|
public ApiResponse<String> sendEmail(@RequestBody @Valid RegisterDTO registerDTO) throws MessagingException {
|
2024-11-05 17:10:26 +08:00
|
|
|
|
2024-11-09 16:49:13 +08:00
|
|
|
return emailService.sendMail(registerDTO.getEmail());
|
2024-11-06 18:47:00 +08:00
|
|
|
|
2024-11-05 17:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|