From 8bfe8193d5c5a2562c25e57077f6621c37ec3df4 Mon Sep 17 00:00:00 2001 From: jishenghua <752718920@qq.com> Date: Tue, 26 Aug 2025 22:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99=E7=99=BB=E5=BD=95=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=91=E9=80=81=E8=AE=A2=E9=98=85=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs/数据库更新记录-首次安装请勿使用.txt | 9 ++- .../main/java/com/jsh/erp/ErpApplication.java | 4 +- .../jsh/erp/constants/BusinessConstants.java | 15 ++++ .../erp/service/PlatformConfigService.java | 79 +++++++++++++++++++ .../java/com/jsh/erp/service/UserService.java | 16 +++- .../java/com/jsh/erp/utils/HttpClient.java | 2 +- 6 files changed, 119 insertions(+), 6 deletions(-) diff --git a/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt b/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt index c89fc72c..7788356a 100644 --- a/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt +++ b/jshERP-boot/docs/数据库更新记录-首次安装请勿使用.txt @@ -1687,4 +1687,11 @@ alter table jsh_system_config add customer_static_price_flag varchar(1) DEFAULT -- -------------------------------------------------------- alter table jsh_material change other_field1 other_field1 varchar(500) DEFAULT NULL COMMENT '自定义1'; alter table jsh_material change other_field2 other_field2 varchar(500) DEFAULT NULL COMMENT '自定义2'; -alter table jsh_material change other_field3 other_field3 varchar(500) DEFAULT NULL COMMENT '自定义3'; \ No newline at end of file +alter table jsh_material change other_field3 other_field3 varchar(500) DEFAULT NULL COMMENT '自定义3'; + +-- -------------------------------------------------------- +-- 时间 2025年8月26日 +-- by jishenghua +-- 给平台表增加微信订阅-登录模板ID +-- -------------------------------------------------------- +insert into jsh_platform_config (platform_key, platform_key_info, platform_value) VALUES ('login_temp_id', '微信订阅-登录模板ID', ''); \ No newline at end of file diff --git a/jshERP-boot/src/main/java/com/jsh/erp/ErpApplication.java b/jshERP-boot/src/main/java/com/jsh/erp/ErpApplication.java index ff44e1c9..f803c9cc 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/ErpApplication.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/ErpApplication.java @@ -2,13 +2,12 @@ package com.jsh.erp; import com.jsh.erp.utils.ComputerInfo; import org.mybatis.spring.annotation.MapperScan; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; +import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import java.io.IOException; @@ -17,6 +16,7 @@ import java.io.IOException; @MapperScan("com.jsh.erp.datasource.mappers") @ServletComponentScan @EnableScheduling +@EnableAsync public class ErpApplication{ public static void main(String[] args) throws IOException { ConfigurableApplicationContext context = SpringApplication.run(ErpApplication.class, args); diff --git a/jshERP-boot/src/main/java/com/jsh/erp/constants/BusinessConstants.java b/jshERP-boot/src/main/java/com/jsh/erp/constants/BusinessConstants.java index 08dc19f5..0fb01da0 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/constants/BusinessConstants.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/constants/BusinessConstants.java @@ -227,4 +227,19 @@ public class BusinessConstants { * 验证码有效期(分钟) */ public static final Integer CAPTCHA_EXPIRATION = 2; + + /** + * 微信登录接口 + */ + public static final String WEIXIN_LOGIN = "/sns/jscode2session"; + + /** + * 微信获取token接口 + */ + public static final String WEIXIN_TOKEN = "/cgi-bin/token"; + + /** + * 微信发送订阅消息接口 + */ + public static final String WEIXIN_MESSAGE_SEND = "/cgi-bin/message/subscribe/send"; } 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 7efc817d..471afcee 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 @@ -7,15 +7,19 @@ import com.jsh.erp.datasource.entities.PlatformConfigExample; import com.jsh.erp.datasource.mappers.PlatformConfigMapper; import com.jsh.erp.datasource.mappers.PlatformConfigMapperEx; 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; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.util.Date; import java.util.List; @Service @@ -25,6 +29,9 @@ public class PlatformConfigService { @Resource private UserService userService; + @Resource + private RedisService redisService; + @Resource private PlatformConfigMapper platformConfigMapper; @@ -182,4 +189,76 @@ public class PlatformConfigService { } return platformConfig; } + + /** + * 获取微信token信息 + * @return + * @throws Exception + */ + public String getAccessToken() throws Exception { + String accessToken = ""; + if(redisService.getCacheObject("weixinToken")==null) { + //1-获取token + String weixinToken = getPlatformConfigByKey("weixinUrl").getPlatformValue() + BusinessConstants.WEIXIN_TOKEN; + String weixinAppid = getPlatformConfigByKey("weixinAppid").getPlatformValue(); + String weixinSecret = getPlatformConfigByKey("weixinSecret").getPlatformValue(); + String url = weixinToken + "?grant_type=client_credential&appid=" + weixinAppid + "&secret=" + weixinSecret; + JSONObject jsonObject = HttpClient.httpGet(url); + logger.info("获取到微信token信息:{}", jsonObject); + if (jsonObject != null) { + accessToken = jsonObject.getString("access_token"); + Long expiresIn = jsonObject.getLong("expires_in") - 10; + if (StringUtil.isNotEmpty(accessToken)) { + //存redis + redisService.storageKeyWithTime("weixinToken", accessToken, expiresIn); + } + } + } else { + accessToken = redisService.getCacheObject("weixinToken"); + } + return accessToken; + } + + /** + * 发送微信订阅消息(该方法将在一个单独的线程中执行) + * @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); + } + 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 26f98e90..2b8d5ed8 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 @@ -376,6 +376,18 @@ public class UserService { pwdSimple = true; } 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()); + } + } redisService.storageObjectBySession(token,"clientIp", Tools.getLocalIp(request)); logService.insertLogWithUserId(user.getId(), user.getTenantId(), "用户", new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_LOGIN).append(user.getLoginName()).toString(), @@ -913,10 +925,10 @@ public class UserService { } public User getUserByWeixinCode(String weixinCode) throws Exception { - String weixinUrl = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue(); + String weixinLogin = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue() + BusinessConstants.WEIXIN_LOGIN; String weixinAppid = platformConfigService.getPlatformConfigByKey("weixinAppid").getPlatformValue(); String weixinSecret = platformConfigService.getPlatformConfigByKey("weixinSecret").getPlatformValue(); - String url = weixinUrl + "?appid=" + weixinAppid + "&secret=" + weixinSecret + "&js_code=" + weixinCode + String url = weixinLogin + "?appid=" + weixinAppid + "&secret=" + weixinSecret + "&js_code=" + weixinCode + "&grant_type=authorization_code"; JSONObject jsonObject = HttpClient.httpGet(url); if(jsonObject!=null) { diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/HttpClient.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/HttpClient.java index 74e134c1..2b784179 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/utils/HttpClient.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/HttpClient.java @@ -75,7 +75,7 @@ public final class HttpClient { HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity, StandardCharsets.UTF_8); - logger.info("状态:"+statusCode+"数据:"+data); + logger.info("状态:"+statusCode+",数据:"+data); return data; } catch(Exception e){ throw new RuntimeException(e.getMessage());