diff --git a/src/main/java/com/example/catchTheLetters/controller/AuthController.java b/src/main/java/com/example/catchTheLetters/controller/AuthController.java index d1baac9..9469a5b 100644 --- a/src/main/java/com/example/catchTheLetters/controller/AuthController.java +++ b/src/main/java/com/example/catchTheLetters/controller/AuthController.java @@ -5,11 +5,13 @@ import com.example.catchTheLetters.model.dto.RegisterDto; import com.example.catchTheLetters.entity.User; import com.example.catchTheLetters.model.vo.UserVo; import com.example.catchTheLetters.service.AuthService; +import com.example.catchTheLetters.service.EmailService; import com.example.catchTheLetters.utils.R; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import jakarta.annotation.Resource; +import jakarta.validation.constraints.Email; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -27,6 +29,9 @@ public class AuthController { @Resource private AuthService authService; + @Resource + private EmailService emailService; + @ApiOperation(value = "用户登录") @PostMapping("/login") public R login(@RequestBody LoginDto loginDto) { @@ -67,4 +72,17 @@ public class AuthController { public R update(@RequestBody User user, @RequestHeader("token") String token) { return authService.update(user, token); } + + @ApiOperation(value = "发送邮件") + @PostMapping("/email") + public R emailSend(@RequestHeader("token") String token, String email) { + return emailService.sendEmail(email); + } + + + @ApiOperation(value = "验证邮箱") + @PostMapping("/verify-email") + public R emailVerify(@RequestHeader("token") String token, String email, String code) { + return emailService.verifyEmail(email, code); + } } diff --git a/src/main/java/com/example/catchTheLetters/controller/LevelController.java b/src/main/java/com/example/catchTheLetters/controller/LevelController.java index 9940125..0adf7e0 100644 --- a/src/main/java/com/example/catchTheLetters/controller/LevelController.java +++ b/src/main/java/com/example/catchTheLetters/controller/LevelController.java @@ -99,4 +99,5 @@ public class LevelController { public R version() { return R.ok(versionService.getVersion("level")); } + } diff --git a/src/main/java/com/example/catchTheLetters/service/EmailService.java b/src/main/java/com/example/catchTheLetters/service/EmailService.java new file mode 100644 index 0000000..96b3f18 --- /dev/null +++ b/src/main/java/com/example/catchTheLetters/service/EmailService.java @@ -0,0 +1,14 @@ +package com.example.catchTheLetters.service; + +import com.example.catchTheLetters.utils.R; + +public interface EmailService { + /** + * 发送邮件 + * @param email + * @return + */ + R sendEmail(String email); + + R verifyEmail(String email,String emailCode); +} diff --git a/src/main/java/com/example/catchTheLetters/service/LevelService.java b/src/main/java/com/example/catchTheLetters/service/LevelService.java index aa1ac77..1012378 100644 --- a/src/main/java/com/example/catchTheLetters/service/LevelService.java +++ b/src/main/java/com/example/catchTheLetters/service/LevelService.java @@ -70,4 +70,6 @@ public interface LevelService { */ boolean update(Level level); + + } diff --git a/src/main/java/com/example/catchTheLetters/service/impl/EmailServiceImpl.java b/src/main/java/com/example/catchTheLetters/service/impl/EmailServiceImpl.java new file mode 100644 index 0000000..b042843 --- /dev/null +++ b/src/main/java/com/example/catchTheLetters/service/impl/EmailServiceImpl.java @@ -0,0 +1,78 @@ +package com.example.catchTheLetters.service.impl; + +import com.example.catchTheLetters.service.EmailService; +import com.example.catchTheLetters.utils.R; +import jakarta.annotation.Resource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import java.util.Date; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Service +public class EmailServiceImpl implements EmailService { + + /** + * 注入邮件工具类 + */ + @Resource + JavaMailSenderImpl javaMailSender; + + String str = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$"; + Pattern pattern = Pattern.compile(str); + + @Resource + RedisServiceImpl redisService; + + @Value("${spring.mail.username}") + private String sendMailer; + + + @Override + public R sendEmail(String email) { + try { + if (!StringUtils.hasText(email)) { + return R.fail("邮箱为空"); + } + Matcher m = pattern.matcher(email); + if (!m.matches()){ + return R.fail("格式错误,请重试"); + } + //生成邮箱验证码 + String emailCode = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(3, 9); + //新建邮件对象 + MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true); + //邮件发信人 + mimeMessageHelper.setFrom(sendMailer); + //邮件收信人 + mimeMessageHelper.setTo(email); + //邮件内容 + mimeMessageHelper.setText("你的验证码为:" + emailCode); + //邮件信息 + mimeMessageHelper.setSentDate(new Date()); + + //发送邮件 + javaMailSender.send(mimeMessageHelper.getMimeMessage()); + //将验证码和注册邮箱存入redis + redisService.set(email,emailCode); + return R.ok( "发送成功"); + }catch (Exception e){ + e.printStackTrace(); + return R.fail("邮箱异常,请重试"); + } + } + + @Override + public R verifyEmail(String email, String emailCode) { + String verify = redisService.get(emailCode).toString(); + if (verify.equals(email)){ + return R.ok("验证通过"); + } + return R.fail("验证失败,请重试"); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 71dbbce..0c116ec 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,13 @@ spring: + mail: + password: hvhxwetpqqqqddhd + host: smtp.qq.com + username: 2960474346@qq.com + properties: + mail: + smtp: + ssl: + enable: true application: name: catch-the-letters data: