优化管理员登录的逻辑,增加提醒功能

This commit is contained in:
jishenghua
2025-09-03 22:37:18 +08:00
parent 541f8c3fb5
commit d49f584b72
5 changed files with 68 additions and 50 deletions

View File

@@ -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

View File

@@ -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', '');
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服务器', '');

View File

@@ -128,6 +128,11 @@
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>

View File

@@ -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);
}
}

View File

@@ -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<Byte> userStatus = new ArrayList<>();