提交
This commit is contained in:
parent
d02c26b830
commit
f2487fa4ad
@ -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;
|
package me.zhengjie.modules.system.service;
|
||||||
|
|
||||||
import me.zhengjie.domain.dto.EmailDto;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Tz
|
* @author Tz
|
||||||
* @date 2018-12-26
|
* @date 2018-12-26
|
||||||
*/
|
*/
|
||||||
public interface VerifyService {
|
public interface VerifyService {
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送验证码
|
|
||||||
* @param email /
|
|
||||||
* @param key /
|
|
||||||
* @return /
|
|
||||||
*/
|
|
||||||
EmailDto sendEmail(String email, String key);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证
|
* 验证
|
||||||
* @param code /
|
* @param code /
|
||||||
|
@ -15,21 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package me.zhengjie.modules.system.service.impl;
|
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 lombok.RequiredArgsConstructor;
|
||||||
import me.zhengjie.domain.dto.EmailDto;
|
|
||||||
import me.zhengjie.exception.BadRequestException;
|
import me.zhengjie.exception.BadRequestException;
|
||||||
import me.zhengjie.modules.system.service.VerifyService;
|
import me.zhengjie.modules.system.service.VerifyService;
|
||||||
import me.zhengjie.utils.RedisUtils;
|
import me.zhengjie.utils.RedisUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Tz
|
* @author Tz
|
||||||
@ -43,31 +34,6 @@ public class VerifyServiceImpl implements VerifyService {
|
|||||||
private Long expiration;
|
private Long expiration;
|
||||||
private final RedisUtils redisUtils;
|
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
|
@Override
|
||||||
public void validated(String key, String code) {
|
public void validated(String key, String code) {
|
||||||
String value = redisUtils.get(key, String.class);
|
String value = redisUtils.get(key, String.class);
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
<module>eladmin-common</module>
|
<module>eladmin-common</module>
|
||||||
<module>eladmin-logging</module>
|
<module>eladmin-logging</module>
|
||||||
<module>eladmin-system</module>
|
<module>eladmin-system</module>
|
||||||
<module>eladmin-tools</module>
|
|
||||||
<module>eladmin-generator</module>
|
<module>eladmin-generator</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user