Merge remote-tracking branch 'origin/master'

This commit is contained in:
虚空之主 2024-10-09 16:29:45 +08:00
commit 2be50cc73b
12 changed files with 285 additions and 9 deletions

16
pom.xml
View File

@ -127,6 +127,22 @@
<artifactId>jansi</artifactId> <artifactId>jansi</artifactId>
<version>2.4.0</version> <version>2.4.0</version>
</dependency> </dependency>
<!-- 邮件发送-->
<!-- <dependency>-->
<!-- <groupId>com.sun.mail</groupId>-->
<!-- <artifactId>javax.mail</artifactId>-->
<!-- <version> 1.6.2</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version> 2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>3.3.4</version>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,55 @@
package com.guaiguailang.harmony.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
@Data
@Configuration
@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")
public class MailConfig {
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Value("${spring.mail.default-encoding}")
private String defaultEncoding;
@Value("${spring.mail.protocol}")
private String protocol;
@Value("${spring.mail.properties.mail.debug}")
private String debug;
@Value("[和生]消息通知")
private String nickname;
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", protocol);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "false"); // 关闭STARTTLS
props.put("mail.debug", debug);
return mailSender;
}
}

View File

@ -39,4 +39,6 @@ public interface AuthMapper {
List<SystemPermission> getUserPermissionListByRoleId(String roleId); List<SystemPermission> getUserPermissionListByRoleId(String roleId);
List<SystemAction> getActionByPermissionIdAndUid(String permissionId, Long uid); List<SystemAction> getActionByPermissionIdAndUid(String permissionId, Long uid);
@Select("SELECT system_role.level from system_role where role_id=#{roleId}")
int getRoleLevelByRole(String roleId);
} }

View File

@ -0,0 +1,5 @@
package com.guaiguailang.harmony.service;
public interface EmailService {
void emailSend(String nickname,String to,String subject,String content);
}

View File

@ -0,0 +1,38 @@
package com.guaiguailang.harmony.service.impl;
import com.guaiguailang.harmony.config.MailConfig;
import com.guaiguailang.harmony.service.EmailService;
import com.guaiguailang.harmony.utils.KeyBase64;
import jakarta.mail.internet.MimeMessage;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
MailConfig mailConfig;
@Override
public void emailSend(String nickname,String to,String subject,String content) {
nickname = KeyBase64.strToBase64(nickname);
nickname = "=?UTF-8?B?"+nickname+"?=";
JavaMailSender mailSender = mailConfig.getJavaMailSender();
try {
MimeMessage ms = mailSender.createMimeMessage();
MimeMessageHelper msi = new MimeMessageHelper(ms,true);
msi.setFrom(nickname+' '+'<'+mailConfig.getUsername()+'>');//From 合法性检查 https://service.mail.qq.com/detail/0/995
msi.setTo(to);
msi.setSubject(subject);
msi.setText(content,true);//true开启HTML解析
mailSender.send(ms);
}catch (Exception e){
System.out.println(e);
}
}
}

View File

@ -95,17 +95,18 @@ public class UserServiceImpl implements UserService {
// 如果是rootadmin 则加载全部数据如果是agent则加载对应的用户 // 如果是rootadmin 则加载全部数据如果是agent则加载对应的用户
UserInfo userInfo = userMapper.getUserById(uid); UserInfo userInfo = userMapper.getUserById(uid);
String merchantCode= Objects.equals(userInfo.getRoleId(), "root") || Objects.equals(userInfo.getRoleId(), "admin") ?"ALL":userInfo.getMerchantCode(); String merchantCode= Objects.equals(userInfo.getRoleId(), "root") || Objects.equals(userInfo.getRoleId(), "admin") ?"ALL":userInfo.getMerchantCode();
int currentLevel = authMapper.getRoleLevelByRole(userInfo.getRoleId());
// 计算查询limit // 计算查询limit
int limit = (userListParam.getCurrentPage()-1) * userListParam.getPageSize(); int limit = (userListParam.getCurrentPage()-1) * userListParam.getPageSize();
int end = limit + userListParam.getPageSize(); int end = limit + userListParam.getPageSize();
// 创建一个查询条件来排除当前用户 // 创建一个查询条件来排除当前用户
Map<String, Object> queryConditions = new HashMap<>(); Map<String, Object> queryConditions = new HashMap<>();
queryConditions.put("excludeUid", uid); queryConditions.put("excludeUid", uid);
queryConditions.put("currentLevel", currentLevel); // 添加当前用户的等级作为查询条件
List<UserInfo> userInfos; List<UserInfo> userInfos;
log.info("要查询的用户类型是:"+userListParam.getUserType()); log.info("要查询的用户类型是:"+userListParam.getUserType());
if (Objects.equals(userListParam.getUserType(), "activate")) { if (Objects.equals(userListParam.getUserType(), "activate")) {
// 如果有额外的激活状态条件也可以加入到queryConditions中,比如限制不能查询身份等级比自己高的 | todo 这个任务涉及到新增字段 | 已经新增字段level // 如果有额外的激活状态条件也可以加入到queryConditions中,比如限制不能查询身份等级比自己高的 | todo 这个任务涉及到新增字段 | 已经新增字段levellevel数值越小则身份权限越高后面不管怎么查都只能查询level值>当前用户level值的
userInfos = userMapper.getUserListActivate(limit, end, merchantCode, queryConditions); userInfos = userMapper.getUserListActivate(limit, end, merchantCode, queryConditions);
} else if (Objects.equals(userListParam.getUserType(), "disabled")) { } else if (Objects.equals(userListParam.getUserType(), "disabled")) {
// 同上可以加入禁用状态条件 // 同上可以加入禁用状态条件

View File

@ -0,0 +1,15 @@
package com.guaiguailang.harmony.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Base64;
public class KeyBase64 {
public static String strToBase64(String str){
try{
return Base64.getEncoder().encodeToString(str.getBytes());
}catch (Exception e){
System.out.println(e);
}
return str;
}
}

View File

@ -0,0 +1,62 @@
package com.guaiguailang.harmony.utils;
import com.guaiguailang.harmony.config.MailConfig;
import jakarta.mail.internet.MimeMessage;
import jakarta.validation.groups.Default;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class UtilEmailSent {
@Autowired
MailConfig mailConfig;
public int sendEmail(String to, String subject, String content){
return sendEmail(to,subject,content, mailConfig.getNickname());
}
public int sendEmail(String to, String subject, String content,String nickname) {
JavaMailSender mailSender = mailConfig.getJavaMailSender();
MimeMessage mimeMessage = mailSender.createMimeMessage();
String from = mailConfig.getUsername();
nickname = KeyBase64.strToBase64(nickname);
System.out.println("2:"+nickname);
nickname = "=?UTF-8?B?"+nickname+"?=";
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.setFrom(nickname+' '+'<'+from+'>');
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content, true);
mailSender.send(mimeMessage);
}catch (Exception e){
log.error("(配置邮件失败){}",e.getMessage());
return -1;
}
return 1;
}
public int sendCode(String to){
String code = VerificationCodeGenerator.generateFourDigitCode();
String title=""+mailConfig.getNickname()+"】验证码 "+code;
String context="<div style=\"margin:10px auto;padding:2em;width:90%;min-height:480px;background-color:rgba(43,174,133,0.9);color:rgba(255,255,255,1);border-radius:5px;box-shadow:1px 0 10px 1px rgba(0,0,0,0.5)\">\n" +
"<h3>尊敬的用户:</h3>\n" +
"<p style=\"text-indent:2em;\">\n" +
"您正在乖乖狼科技旗下软件产品进行操作,您的验证码为\n" +
"<font color=\"yellow\" size=\"+2\">"+code+"</font>\n" +
",有效期为十分钟,请及时填写验证码。\n" +
"</p>\n" +
"<p style=\"text-indent:2em;\">如果这不是您本人操作,请忽略此邮件</p>\n" +
"<p style=\"float:right\">乖乖狼科技&nbsp;&nbsp;&nbsp;&nbsp;</p>\n" +
"</div>";
return sendEmail(to,title,context);
}
}

View File

@ -0,0 +1,27 @@
package com.guaiguailang.harmony.utils;
import java.util.Random;
public class VerificationCodeGenerator {
public static void main(String[] args) {
String verificationCode = generateFourDigitCode();
System.out.println("Generated Verification Code: " + verificationCode);
}
/**
* 生成一个四位数的验证码
* @return 四位数的验证码字符串
*/
public static String generateFourDigitCode() {
Random random = new Random();
StringBuilder code = new StringBuilder(4);
for (int i = 0; i < 4; i++) {
int digit = random.nextInt(10); // 生成一个0到9之间的随机整数
code.append(digit);
}
return code.toString();
}
}

View File

@ -67,3 +67,44 @@ sa-token.token-style=uuid
# 是否输出操作日志 # 是否输出操作日志
sa-token.is-log=true sa-token.is-log=true
#application.properties基本配置后面我都使用此配置来发送邮件
## 基本配置
### smtp服务器主机163的
spring.mail.host=smtp.qiye.163.com
spring.mail.port=25
### 服务协议SMTP代表是发送邮件
spring.mail.protocol=smtp
### 登录服务器邮箱账号
spring.mail.username=news@mllt.cc
### 登录服务器邮箱授权码不是邮箱密码这个是我们开通SMTP、POP时得到的授权码
spring.mail.password=Qq2686485465@
### 默认邮件的编码集MimeMessage 编码默认UTF-8
spring.mail.default-encoding=UTF-8
# 补充配置这里具体可以参照Jakarta Mail的扩展配置
## 默认发送方邮箱账号(当程序未指定发件人邮箱则默认取这个)
spring.mail.properties.mail.smtp.from=news@mllt.cc
## 开启权限认证
spring.mail.properties.mail.smtp.auth=true
## 邮件接收时间的限制
spring.mail.properties.mail.smtp.timeout=60000
## 连接时间的限制
spring.mail.properties.mail.smtp.connectiontimeout=60000
## 邮件发送时间的限制(毫秒)
spring.mail.properties.mail.smtp.writetimeout=60000
## 日志打印,邮件发送过程的日志会被输出
spring.mail.properties.mail.debug=true
# 下面这条配置请去 src/main/java/com/guaiguailang/harmony/config/MailConfig.java 修改。在这里修改是无效的。因为这里写中文会导致乱码。
email.form.nickname=[和生]消息通知
# 中文乱码问题
banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
spring.messages.encoding=UTF-8

View File

@ -14,6 +14,11 @@
<if test="queryConditions.excludeUid != null"> <if test="queryConditions.excludeUid != null">
AND id != #{queryConditions.excludeUid} AND id != #{queryConditions.excludeUid}
</if> </if>
<if test="queryConditions.currentLevel != null">
AND role_id IN (
SELECT role_id FROM system_role WHERE level >= #{queryConditions.currentLevel}
)
</if>
LIMIT #{limit},#{end} LIMIT #{limit},#{end}
</select> </select>
@ -27,6 +32,11 @@
<if test="queryConditions.excludeUid != null"> <if test="queryConditions.excludeUid != null">
AND id != #{queryConditions.excludeUid} AND id != #{queryConditions.excludeUid}
</if> </if>
<if test="queryConditions.currentLevel != null">
AND role_id IN (
SELECT role_id FROM system_role WHERE level >= #{queryConditions.currentLevel}
)
</if>
LIMIT #{limit},#{end} LIMIT #{limit},#{end}
</select> </select>
@ -40,6 +50,11 @@
<if test="queryConditions.excludeUid != null"> <if test="queryConditions.excludeUid != null">
AND id != #{queryConditions.excludeUid} AND id != #{queryConditions.excludeUid}
</if> </if>
<if test="queryConditions.currentLevel != null">
AND role_id IN (
SELECT role_id FROM system_role WHERE level >= #{queryConditions.currentLevel}
)
</if>
</where> </where>
LIMIT #{limit},#{end} LIMIT #{limit},#{end}
</select> </select>

View File

@ -1,16 +1,15 @@
package com.guaiguailang.harmony; package com.guaiguailang.harmony;
import com.guaiguailang.harmony.utils.UtilEmailSent;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import javax.xml.transform.Result;
import java.util.Scanner;
@SpringBootTest @SpringBootTest
class HarmonyLifeServerApplicationTests { class HarmonyLifeServerApplicationTests {
@Autowired
UtilEmailSent utilEmailSent;
@Test @Test
void contextLoads() { void testEmailSentUtil() {
utilEmailSent.sendCode("2952458479@qq.com");
} }
} }