This commit is contained in:
tangzh 2025-06-07 14:03:28 +08:00
parent d02c26b830
commit f2487fa4ad
3 changed files with 0 additions and 61 deletions

View File

@ -1,37 +1,11 @@
/*
* Copyright 2019-2025 Tz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.system.service;
import me.zhengjie.domain.dto.EmailDto;
/**
* @author Tz
* @date 2018-12-26
*/
public interface VerifyService {
/**
* 发送验证码
* @param email /
* @param key /
* @return /
*/
EmailDto sendEmail(String email, String key);
/**
* 验证
* @param code /

View File

@ -15,21 +15,12 @@
*/
package me.zhengjie.modules.system.service.impl;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.domain.dto.EmailDto;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.service.VerifyService;
import me.zhengjie.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
/**
* @author Tz
@ -43,31 +34,6 @@ public class VerifyServiceImpl implements VerifyService {
private Long expiration;
private final RedisUtils redisUtils;
@Override
@Transactional(rollbackFor = Exception.class)
public EmailDto sendEmail(String email, String key) {
EmailDto emailDto;
String content;
String redisKey = key + email;
// 如果不存在有效的验证码就创建一个新的
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
Template template = engine.getTemplate("email.ftl");
String oldCode = redisUtils.get(redisKey, String.class);
if(oldCode == null){
String code = RandomUtil.randomNumbers (6);
// 存入缓存
if(!redisUtils.set(redisKey, code, expiration)){
throw new BadRequestException("服务异常,请联系网站负责人");
}
content = template.render(Dict.create().set("code",code));
// 存在就再次发送原来的验证码
} else {
content = template.render(Dict.create().set("code",oldCode));
}
emailDto = new EmailDto(Collections.singletonList(email),"ELADMIN后台管理系统",content);
return emailDto;
}
@Override
public void validated(String key, String code) {
String value = redisUtils.get(key, String.class);

View File

@ -11,7 +11,6 @@
<module>eladmin-common</module>
<module>eladmin-logging</module>
<module>eladmin-system</module>
<module>eladmin-tools</module>
<module>eladmin-generator</module>
</modules>