给登录接口增加发送订阅消息的功能

This commit is contained in:
jishenghua
2025-08-26 22:53:05 +08:00
parent 1042057fa0
commit 8bfe8193d5
6 changed files with 119 additions and 6 deletions

View File

@@ -1688,3 +1688,10 @@ 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_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_field2 other_field2 varchar(500) DEFAULT NULL COMMENT '自定义2';
alter table jsh_material change other_field3 other_field3 varchar(500) DEFAULT NULL COMMENT '自定义3'; 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', '');

View File

@@ -2,13 +2,12 @@ package com.jsh.erp;
import com.jsh.erp.utils.ComputerInfo; import com.jsh.erp.utils.ComputerInfo;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import java.io.IOException; import java.io.IOException;
@@ -17,6 +16,7 @@ import java.io.IOException;
@MapperScan("com.jsh.erp.datasource.mappers") @MapperScan("com.jsh.erp.datasource.mappers")
@ServletComponentScan @ServletComponentScan
@EnableScheduling @EnableScheduling
@EnableAsync
public class ErpApplication{ public class ErpApplication{
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
ConfigurableApplicationContext context = SpringApplication.run(ErpApplication.class, args); ConfigurableApplicationContext context = SpringApplication.run(ErpApplication.class, args);

View File

@@ -227,4 +227,19 @@ public class BusinessConstants {
* 验证码有效期(分钟) * 验证码有效期(分钟)
*/ */
public static final Integer CAPTCHA_EXPIRATION = 2; 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";
} }

View File

@@ -7,15 +7,19 @@ import com.jsh.erp.datasource.entities.PlatformConfigExample;
import com.jsh.erp.datasource.mappers.PlatformConfigMapper; import com.jsh.erp.datasource.mappers.PlatformConfigMapper;
import com.jsh.erp.datasource.mappers.PlatformConfigMapperEx; import com.jsh.erp.datasource.mappers.PlatformConfigMapperEx;
import com.jsh.erp.exception.JshException; import com.jsh.erp.exception.JshException;
import com.jsh.erp.utils.HttpClient;
import com.jsh.erp.utils.PageUtils; import com.jsh.erp.utils.PageUtils;
import com.jsh.erp.utils.StringUtil; import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List; import java.util.List;
@Service @Service
@@ -25,6 +29,9 @@ public class PlatformConfigService {
@Resource @Resource
private UserService userService; private UserService userService;
@Resource
private RedisService redisService;
@Resource @Resource
private PlatformConfigMapper platformConfigMapper; private PlatformConfigMapper platformConfigMapper;
@@ -182,4 +189,76 @@ public class PlatformConfigService {
} }
return platformConfig; 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);
}
} }

View File

@@ -376,6 +376,18 @@ public class UserService {
pwdSimple = true; pwdSimple = true;
} }
user.setPassword(null); 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)); redisService.storageObjectBySession(token,"clientIp", Tools.getLocalIp(request));
logService.insertLogWithUserId(user.getId(), user.getTenantId(), "用户", logService.insertLogWithUserId(user.getId(), user.getTenantId(), "用户",
new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_LOGIN).append(user.getLoginName()).toString(), 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 { 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 weixinAppid = platformConfigService.getPlatformConfigByKey("weixinAppid").getPlatformValue();
String weixinSecret = platformConfigService.getPlatformConfigByKey("weixinSecret").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"; + "&grant_type=authorization_code";
JSONObject jsonObject = HttpClient.httpGet(url); JSONObject jsonObject = HttpClient.httpGet(url);
if(jsonObject!=null) { if(jsonObject!=null) {

View File

@@ -75,7 +75,7 @@ public final class HttpClient {
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity, StandardCharsets.UTF_8); String data = EntityUtils.toString(entity, StandardCharsets.UTF_8);
logger.info("状态:"+statusCode+"数据:"+data); logger.info("状态:"+statusCode+",数据:"+data);
return data; return data;
} catch(Exception e){ } catch(Exception e){
throw new RuntimeException(e.getMessage()); throw new RuntimeException(e.getMessage());