From d49f584b7235151efb2275691482e163c6f3df5b Mon Sep 17 00:00:00 2001
From: jishenghua <752718920@qq.com>
Date: Wed, 3 Sep 2025 22:37:18 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AE=A1=E7=90=86=E5=91=98?=
=?UTF-8?q?=E7=99=BB=E5=BD=95=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E6=8F=90=E9=86=92=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
jshERP-boot/docs/jsh_erp.sql | 3 +
.../docs/数据库更新记录-首次安装请勿使用.txt | 10 ++-
jshERP-boot/pom.xml | 5 ++
.../erp/service/PlatformConfigService.java | 68 +++++++++----------
.../java/com/jsh/erp/service/UserService.java | 32 ++++++---
5 files changed, 68 insertions(+), 50 deletions(-)
diff --git a/jshERP-boot/docs/jsh_erp.sql b/jshERP-boot/docs/jsh_erp.sql
index fc0eacef..7e89551c 100644
--- a/jshERP-boot/docs/jsh_erp.sql
+++ b/jshERP-boot/docs/jsh_erp.sql
@@ -682,6 +682,9 @@ INSERT INTO `jsh_platform_config` VALUES ('15', 'aliOss_accessKeySecret', '阿
INSERT INTO `jsh_platform_config` VALUES ('16', 'aliOss_bucketName', '阿里OSS-bucketName', '');
INSERT INTO `jsh_platform_config` VALUES ('17', 'aliOss_linkUrl', '阿里OSS-linkUrl', '');
INSERT INTO `jsh_platform_config` VALUES ('18', 'bill_excel_url', '单据Excel地址', '');
+INSERT INTO `jsh_platform_config` VALUES ('19', 'email_from', '邮件发送端-发件人', '');
+INSERT INTO `jsh_platform_config` VALUES ('20', 'email_auth_code', '邮件发送端-授权码', '');
+INSERT INTO `jsh_platform_config` VALUES ('21', 'email_smtp_host', '邮件发送端-SMTP服务器', '');
-- ----------------------------
-- Table structure for jsh_role
diff --git a/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt b/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt
index 7788356a..37124fb8 100644
--- a/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt
+++ b/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt
@@ -1690,8 +1690,12 @@ alter table jsh_material change other_field2 other_field2 varchar(500) DEFAULT N
alter table jsh_material change other_field3 other_field3 varchar(500) DEFAULT NULL COMMENT '自定义3';
-- --------------------------------------------------------
--- 时间 2025年8月26日
+-- 时间 2025年9月3日
-- by jishenghua
--- 给平台表增加微信订阅-登录模板ID
+-- 给平台表增加邮件发送端-发件人
+-- 给平台表增加邮件发送端-授权码
+-- 给平台表增加邮件发送端-SMTP服务器
-- --------------------------------------------------------
-insert into jsh_platform_config (platform_key, platform_key_info, platform_value) VALUES ('login_temp_id', '微信订阅-登录模板ID', '');
\ No newline at end of file
+insert into jsh_platform_config (platform_key, platform_key_info, platform_value) VALUES ('email_from', '邮件发送端-发件人', '');
+insert into jsh_platform_config (platform_key, platform_key_info, platform_value) VALUES ('email_auth_code', '邮件发送端-授权码', '');
+insert into jsh_platform_config (platform_key, platform_key_info, platform_value) VALUES ('email_smtp_host', '邮件发送端-SMTP服务器', '');
\ No newline at end of file
diff --git a/jshERP-boot/pom.xml b/jshERP-boot/pom.xml
index 344b8bcb..94eb03fb 100644
--- a/jshERP-boot/pom.xml
+++ b/jshERP-boot/pom.xml
@@ -128,6 +128,11 @@
pagehelper-spring-boot-starter
1.2.13
+
+ com.sun.mail
+ javax.mail
+ 1.6.2
+
diff --git a/jshERP-boot/src/main/java/com/jsh/erp/service/PlatformConfigService.java b/jshERP-boot/src/main/java/com/jsh/erp/service/PlatformConfigService.java
index 471afcee..93b5a9d5 100644
--- a/jshERP-boot/src/main/java/com/jsh/erp/service/PlatformConfigService.java
+++ b/jshERP-boot/src/main/java/com/jsh/erp/service/PlatformConfigService.java
@@ -10,7 +10,6 @@ import com.jsh.erp.exception.JshException;
import com.jsh.erp.utils.HttpClient;
import com.jsh.erp.utils.PageUtils;
import com.jsh.erp.utils.StringUtil;
-import com.jsh.erp.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
@@ -19,8 +18,10 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
-import java.util.Date;
+import javax.mail.*;
+import javax.mail.internet.*;
import java.util.List;
+import java.util.Properties;
@Service
public class PlatformConfigService {
@@ -220,45 +221,38 @@ public class PlatformConfigService {
}
/**
- * 发送微信订阅消息(该方法将在一个单独的线程中执行)
+ * 发送邮件(该方法将在一个单独的线程中执行)
* @return
* @throws Exception
*/
@Async
- public void sendSubscribeMessage(String accessToken, String weixinUrl, String platformName, String templateId, String page, String openId) throws Exception {
- String weixinMessageSend = weixinUrl + BusinessConstants.WEIXIN_MESSAGE_SEND;
- String url = weixinMessageSend + "?access_token=" + accessToken;
- JSONObject paramObj = new JSONObject();
- paramObj.put("template_id", templateId);
- if (StringUtil.isNotEmpty(page)) {
- paramObj.put("page", page);
+ public void sendEmail(String emailFrom, String emailAuthCode, String emailSmtpHost, String toEmail, String emailSubject, String emailBody) {
+ // 配置邮件服务器属性
+ Properties properties = new Properties();
+ properties.put("mail.smtp.host", emailSmtpHost); // 网易邮箱SMTP服务器
+ properties.put("mail.smtp.port", "465"); // SSL端口
+ properties.put("mail.smtp.auth", "true"); // 需要认证
+ properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // 使用SSL
+ properties.put("mail.smtp.socketFactory.port", "465"); // SSL端口
+ try {
+ // 创建会话
+ Session session = Session.getInstance(properties, new Authenticator() {
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(emailFrom, emailAuthCode);
+ }
+ });
+ // 创建邮件
+ Message message = new MimeMessage(session);
+ message.setFrom(new InternetAddress(emailFrom));
+ message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
+ message.setSubject(emailSubject);
+ message.setText(emailBody);
+ // 发送邮件
+ Transport.send(message);
+ logger.info("邮件发送成功!");
+ } catch (Exception e) {
+ logger.error("邮件发送失败: " + e.getMessage());
}
- paramObj.put("touser", openId);
- JSONObject dataObj = new JSONObject();
- //登录状态
- JSONObject phraseObj = new JSONObject();
- //登陆方式
- JSONObject thing2Obj = new JSONObject();
- //登陆应用
- JSONObject thing3Obj = new JSONObject();
- //登录时间
- JSONObject time4Obj = new JSONObject();
- //备注
- JSONObject thing5Obj = new JSONObject();
- phraseObj.put("value", "已登录");
- thing2Obj.put("value", "账号登录");
- thing3Obj.put("value", platformName);
- time4Obj.put("value", Tools.getCenternTime(new Date()));
- thing5Obj.put("value", "欢迎" + BusinessConstants.DEFAULT_MANAGER + "登录!");
- dataObj.put("phrase1", phraseObj);
- dataObj.put("thing2", thing2Obj);
- dataObj.put("thing3", thing3Obj);
- dataObj.put("time4", time4Obj);
- dataObj.put("thing5", thing5Obj);
- paramObj.put("data", dataObj);
- paramObj.put("miniprogram_state", "formal");
- paramObj.put("lang", "zh_CN");
- String param = paramObj.toJSONString();
- HttpClient.httpPost(url, param);
}
}
diff --git a/jshERP-boot/src/main/java/com/jsh/erp/service/UserService.java b/jshERP-boot/src/main/java/com/jsh/erp/service/UserService.java
index 2b8d5ed8..3c01da04 100644
--- a/jshERP-boot/src/main/java/com/jsh/erp/service/UserService.java
+++ b/jshERP-boot/src/main/java/com/jsh/erp/service/UserService.java
@@ -377,16 +377,8 @@ public class UserService {
}
user.setPassword(null);
if(BusinessConstants.DEFAULT_MANAGER.equals(user.getLoginName())) {
- //如果是管理员,则发送订阅消息
- //1-获取token
- String accessToken = platformConfigService.getAccessToken();
- //2-发送订阅消息
- String templateId = platformConfigService.getPlatformConfigByKey("login_temp_id").getPlatformValue();
- String weixinUrl = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue();
- String platformName = platformConfigService.getPlatformConfigByKey("platform_name").getPlatformValue();
- if(StringUtil.isNotEmpty(accessToken) && StringUtil.isNotEmpty(user.getWeixinOpenId()) && StringUtil.isNotEmpty(templateId)) {
- platformConfigService.sendSubscribeMessage(accessToken, weixinUrl, platformName, templateId, null, user.getWeixinOpenId());
- }
+ //如果是管理员,则发送登录邮件
+ sendEmailToCurrentUser(request, user);
}
redisService.storageObjectBySession(token,"clientIp", Tools.getLocalIp(request));
logService.insertLogWithUserId(user.getId(), user.getTenantId(), "用户",
@@ -459,6 +451,26 @@ public class UserService {
return user;
}
+ /**
+ * 发送邮件给当前用户
+ * @param request
+ * @param user
+ * @throws Exception
+ */
+ private void sendEmailToCurrentUser(HttpServletRequest request, User user) throws Exception {
+ String platformName = platformConfigService.getPlatformConfigByKey("platform_name").getPlatformValue();
+ String emailFrom = platformConfigService.getPlatformConfigByKey("email_from").getPlatformValue();
+ String emailAuthCode = platformConfigService.getPlatformConfigByKey("email_auth_code").getPlatformValue();
+ String emailSmtpHost = platformConfigService.getPlatformConfigByKey("email_smtp_host").getPlatformValue();
+ if(StringUtil.isNotEmpty(emailFrom) && StringUtil.isNotEmpty(emailAuthCode) && StringUtil.isNotEmpty(emailSmtpHost)
+ && StringUtil.isNotEmpty(user.getEmail())) {
+ String emailSubject = "用户" + user.getLoginName() + "成功登录" + platformName;
+ String emailBody = "用户" + user.getLoginName() + "成功登录" + platformName + ",登录时间:" + Tools.getCenternTime(new Date())
+ + ",登录IP:" + Tools.getLocalIp(request);
+ platformConfigService.sendEmail(emailFrom, emailAuthCode, emailSmtpHost, user.getEmail(), emailSubject, emailBody);
+ }
+ }
+
public int checkIsNameExist(Long id, String name)throws Exception {
UserExample example = new UserExample();
List userStatus = new ArrayList<>();