配置场馆和管理员挂钩发送微信消息

This commit is contained in:
2023-07-25 22:59:42 +08:00
parent 4c09422a45
commit d1fbab35ac
10 changed files with 323 additions and 31 deletions

View File

@@ -4,6 +4,7 @@ import com.enums.VenueTypeEnum;
import com.sv.entity.Constants; import com.sv.entity.Constants;
import com.sv.entity.Venue; import com.sv.entity.Venue;
import com.sv.entity.VenueLesson; import com.sv.entity.VenueLesson;
import com.sv.mapper.WxConfigMapper;
import com.sv.service.api.VenueLessonService; import com.sv.service.api.VenueLessonService;
import com.sv.service.api.VenueService; import com.sv.service.api.VenueService;
import com.sv.service.api.util.DateUtilCard; import com.sv.service.api.util.DateUtilCard;
@@ -12,12 +13,14 @@ import com.sv.service.message.SendMsg;
import com.sv.service.message.WeiXinSendUtils; import com.sv.service.message.WeiXinSendUtils;
import com.ydd.framework.core.common.dto.ResponseDTO; import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.controller.BaseApiController; import com.ydd.framework.core.controller.BaseApiController;
import com.ydd.oms.entity.sys.WxConfig;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -65,6 +68,8 @@ public class SmsController extends BaseApiController {
VenueLessonService venueLessonService; VenueLessonService venueLessonService;
@Resource @Resource
VenueService venueService; VenueService venueService;
@Resource
WeiXinSendUtils weiXinSendUtils;
@RequestMapping(value = "/test/send/wx/message",method = RequestMethod.GET) @RequestMapping(value = "/test/send/wx/message",method = RequestMethod.GET)
public String sendTestMessage() { public String sendTestMessage() {
VenueLesson venueLesson = venueLessonService.findById(30858); VenueLesson venueLesson = venueLessonService.findById(30858);
@@ -82,10 +87,24 @@ public class SmsController extends BaseApiController {
if (!StringUtils.isEmpty(openId)) { if (!StringUtils.isEmpty(openId)) {
String[] split = openId.split(","); String[] split = openId.split(",");
for (String s : split) { for (String s : split) {
WeiXinSendUtils.sendCard(sendMsg,s); weiXinSendUtils.sendCard(sendMsg,s);
} }
} }
return "OK"; return "OK";
} }
@Resource
WxConfigMapper wxConfigMapper;
@RequestMapping(value = "/test/add/wx/openid",method = RequestMethod.GET)
public String addWxConfig(@RequestParam("open") String open,@RequestParam("id") Integer id) {
WxConfig wxConfig = new WxConfig();
wxConfig.setWxOpenId(open);
wxConfig.setVenueId(id);
wxConfig.setCreatedId(999);
wxConfig.setModifiedId(999);
wxConfig.setVenueId(id);
wxConfigMapper.insert(wxConfig);
return "OK";
}
} }

View File

@@ -26,6 +26,8 @@ public class SendMessageTest {
VenueLessonService venueLessonService; VenueLessonService venueLessonService;
@Resource @Resource
VenueService venueService; VenueService venueService;
@Resource
WeiXinSendUtils weiXinSendUtils;
@Test @Test
public void testTask(){ public void testTask(){
@@ -50,7 +52,7 @@ public class SendMessageTest {
if (!StringUtils.isEmpty(openId)) { if (!StringUtils.isEmpty(openId)) {
String[] split = openId.split(","); String[] split = openId.split(",");
for (String s : split) { for (String s : split) {
WeiXinSendUtils.sendCard(sendMsg,s); weiXinSendUtils.sendCard(sendMsg,s);
} }
} }
} }

View File

@@ -0,0 +1,90 @@
package com.ydd.oms.entity.sys;
import java.util.Date;
public class WxConfig {
private Integer id;
private String wxOpenId;
private Integer venueId;
private Integer createdId;
private Integer modifiedId;
private Date createdTime;
private Date modifiedTime;
public WxConfig(Integer id, String wxOpenId, Integer venueId, Integer createdId, Integer modifiedId, Date createdTime, Date modifiedTime, Byte deleted) {
this.id = id;
this.wxOpenId = wxOpenId;
this.venueId = venueId;
this.createdId = createdId;
this.modifiedId = modifiedId;
this.createdTime = createdTime;
this.modifiedTime = modifiedTime;
}
public WxConfig() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getWxOpenId() {
return wxOpenId;
}
public void setWxOpenId(String wxOpenId) {
this.wxOpenId = wxOpenId == null ? null : wxOpenId.trim();
}
public Integer getVenueId() {
return venueId;
}
public void setVenueId(Integer venueId) {
this.venueId = venueId;
}
public Integer getCreatedId() {
return createdId;
}
public void setCreatedId(Integer createdId) {
this.createdId = createdId;
}
public Integer getModifiedId() {
return modifiedId;
}
public void setModifiedId(Integer modifiedId) {
this.modifiedId = modifiedId;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
public Date getModifiedTime() {
return modifiedTime;
}
public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
}
}

View File

@@ -0,0 +1,22 @@
package com.sv.mapper;
import com.ydd.oms.entity.sys.WxConfig;
import java.util.List;
public interface WxConfigMapper {
int deleteByPrimaryKey(Integer id);
int insert(WxConfig record);
int insertSelective(WxConfig record);
WxConfig selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(WxConfig record);
int updateByPrimaryKey(WxConfig record);
List<WxConfig> list();
}

View File

@@ -403,7 +403,7 @@ public class MemberLessonTicketService extends BaseServiceImpl {
String lessonTime = DateUtilCard.getStrFromDate(venueLesson.getDate()) + " " + venueLesson.getStartTime().toString(); String lessonTime = DateUtilCard.getStrFromDate(venueLesson.getDate()) + " " + venueLesson.getStartTime().toString();
String typeName = VenueTypeEnum.getByValue(venue.getType()).name; String typeName = VenueTypeEnum.getByValue(venue.getType()).name;
String url = Constants.VENUE_VIEW_URL + venueId; String url = Constants.VENUE_VIEW_URL + venueId;
AsyncTaskUtil.INSTANCE.submit(new NotifyAdminMsgThread(venue.getName(),venueLesson.getName(),lessonTime,typeName,url)); AsyncTaskUtil.INSTANCE.submit(new NotifyAdminMsgThread(venueId,venue.getName(),venueLesson.getName(),lessonTime,typeName,url));
} }
} }

View File

@@ -2,21 +2,25 @@ package com.sv.service.message;
import com.dw.ccm.wechat.base.utils.JsonUtils; import com.dw.ccm.wechat.base.utils.JsonUtils;
import com.sv.mapper.ConfigMapper; import com.sv.mapper.ConfigMapper;
import com.sv.mapper.WxConfigMapper;
import com.sv.service.utils.SpringUtils; import com.sv.service.utils.SpringUtils;
import com.ydd.oms.entity.sys.Config; import com.ydd.oms.entity.sys.Config;
import com.ydd.oms.entity.sys.WxConfig;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.List; import java.util.List;
public class NotifyAdminMsgThread extends Thread{ public class NotifyAdminMsgThread extends Thread{
private Integer venueId = 0;
private String venueName = ""; private String venueName = "";
private String lessonName = ""; private String lessonName = "";
private String lessonTime = ""; private String lessonTime = "";
private String venueType = ""; private String venueType = "";
private String url = ""; private String url = "";
public NotifyAdminMsgThread(String venueName, String lessonName, String lessonTime, String venueType, String url) { public NotifyAdminMsgThread(Integer venueId,String venueName, String lessonName, String lessonTime, String venueType, String url) {
this.venueId = venueId;
this.venueName = venueName; this.venueName = venueName;
this.lessonName = lessonName; this.lessonName = lessonName;
this.lessonTime = lessonTime; this.lessonTime = lessonTime;
@@ -24,6 +28,14 @@ public class NotifyAdminMsgThread extends Thread{
this.url = url; this.url = url;
} }
public Integer getVenueId() {
return venueId;
}
public void setVenueId(Integer venueId) {
this.venueId = venueId;
}
public String getUrl() { public String getUrl() {
return url; return url;
} }
@@ -72,13 +84,9 @@ public class NotifyAdminMsgThread extends Thread{
if (all == null || all.size() <= 0 ){ if (all == null || all.size() <= 0 ){
return; return;
} }
String openId = null;
String webHook = null; String webHook = null;
String dingSecret = null; String dingSecret = null;
for (Config config : all) { for (Config config : all) {
if ("WX_ADMIN".equals(config.getKey())) {
openId = config.getValue();
}
if ("DING_WEBHOOK".equals(config.getKey())) { if ("DING_WEBHOOK".equals(config.getKey())) {
webHook = config.getValue(); webHook = config.getValue();
} }
@@ -87,10 +95,12 @@ public class NotifyAdminMsgThread extends Thread{
} }
} }
SendMsg sendMsg = new SendMsg(venueName,lessonName,lessonTime,venueType,url); SendMsg sendMsg = new SendMsg(venueName,lessonName,lessonTime,venueType,url);
if (!StringUtils.isEmpty(openId)) { WxConfigMapper wxConfigMapper = SpringUtils.getBean(WxConfigMapper.class);
String[] split = openId.split(","); List<WxConfig> configs = wxConfigMapper.list();
for (String s : split) { for (WxConfig config : configs) {
WeiXinSendUtils.sendCard(sendMsg,s); if (venueId == config.getVenueId()) {
WeiXinSendUtils xinSendUtils = SpringUtils.getBean(WeiXinSendUtils.class);
xinSendUtils.sendCard(sendMsg,config.getWxOpenId());
} }
} }
if (!StringUtils.isEmpty(webHook) && !StringUtils.isEmpty(dingSecret)) { if (!StringUtils.isEmpty(webHook) && !StringUtils.isEmpty(dingSecret)) {

View File

@@ -3,30 +3,53 @@ package com.sv.service.message;
import com.dw.ccm.wechat.base.WeChat; import com.dw.ccm.wechat.base.WeChat;
import com.dw.ccm.wechat.base.WeChatHelper; import com.dw.ccm.wechat.base.WeChatHelper;
import com.dw.ccm.wechat.base.message.response.AccessTokenResponse; import com.dw.ccm.wechat.base.message.response.AccessTokenResponse;
import com.dw.ccm.wechat.base.message.response.template.TemplateResponse;
import com.dw.ccm.wechat.base.message.template.Attribute; import com.dw.ccm.wechat.base.message.template.Attribute;
import com.dw.ccm.wechat.base.message.template.TemplateMessage; import com.dw.ccm.wechat.base.message.template.TemplateMessage;
import org.springframework.util.StringUtils; import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.HashMap; import java.util.HashMap;
public class WeiXinSendUtils { @Component
public class WeiXinSendUtils implements InitializingBean {
public static void sendCard(SendMsg sendMsg,String openId){ private String token;
AccessTokenResponse accessToken = WeChatHelper.getAccessToken("wx51d82eba3f5f4858", "d28320bcb885cc208b6fde2253d8663e");
String token = accessToken.getAccessToken(); public void sendCard(SendMsg sendMsg,String openId){
if (!StringUtils.isEmpty(token)){ WeChat weChat = WeChatHelper.getWeChat(token);
WeChat weChat = WeChatHelper.getWeChat(token); TemplateMessage templateMessage = new TemplateMessage();
TemplateMessage templateMessage = new TemplateMessage(); templateMessage.setTemplateId("vPRolOW7D3jnXhT4uqN4FOcIIuHcsHuresbZjlPBg9Y");
templateMessage.setTemplateId("vPRolOW7D3jnXhT4uqN4FOcIIuHcsHuresbZjlPBg9Y"); templateMessage.setToUser(openId);
templateMessage.setToUser(openId); templateMessage.setUrl(sendMsg.getViewUrl());
templateMessage.setUrl(sendMsg.getViewUrl()); HashMap<String, Attribute> attr = new HashMap<>();
HashMap<String, Attribute> attr = new HashMap<>(); attr.put("thing2",new Attribute(sendMsg.getVenueName(),""));
attr.put("thing2",new Attribute(sendMsg.getVenueName(),"")); attr.put("thing3",new Attribute(sendMsg.getLessonName(),""));
attr.put("thing3",new Attribute(sendMsg.getLessonName(),"")); attr.put("time5",new Attribute(sendMsg.getLessonTime(),""));
attr.put("time5",new Attribute(sendMsg.getLessonTime(),"")); attr.put("phrase7",new Attribute(sendMsg.getVenueType(),""));
attr.put("phrase7",new Attribute(sendMsg.getVenueType(),"")); templateMessage.setAttributes(attr);
templateMessage.setAttributes(attr); TemplateResponse templateResponse = weChat.sendTemplate(templateMessage);
if (templateResponse.getErrCode() == 42001) {
//access_token expired
getToken();
weChat.sendTemplate(templateMessage); weChat.sendTemplate(templateMessage);
} }
} }
@Override
public void afterPropertiesSet() throws Exception {
getToken();
}
private void getToken() {
AccessTokenResponse accessToken = WeChatHelper.getAccessToken("wx51d82eba3f5f4858", "d28320bcb885cc208b6fde2253d8663e");
token = accessToken.getAccessToken();
}
@Scheduled(fixedDelay=60*60*1000)
public void refreshToken() {
getToken();
}
} }

View File

@@ -621,7 +621,7 @@
<select id="getLessonTypes" resultType="string"> <select id="getLessonTypes" resultType="string">
select DISTINCT start_time from sv_venue_lesson a select DISTINCT start_time from sv_venue_lesson a
where DATE(a.date) <![CDATA[ >= ]]> CURRENT_DATE where DATE(a.date) <![CDATA[ >= ]]> CURRENT_DATE
AND DATE(a.date) <![CDATA[ <= ]]> DATE_ADD(CURRENT_DATE,INTERVAL 4 DAY) AND DATE(a.date) <![CDATA[ <= ]]> DATE_ADD(CURRENT_DATE,INTERVAL 7 DAY)
and a.venue_id = #{venueId} and a.venue_id = #{venueId}
ORDER BY a.start_time ORDER BY a.start_time
</select> </select>

View File

@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sv.mapper.WxConfigMapper" >
<resultMap id="BaseResultMap" type="com.ydd.oms.entity.sys.WxConfig" >
<constructor >
<idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="wx_open_id" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="venue_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="created_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="modified_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="created_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
<arg column="modified_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
id, wx_open_id, venue_id, created_id, modified_id, created_time, modified_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from sys_wx_config
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from sys_wx_config
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.ydd.oms.entity.sys.WxConfig" >
insert into sys_wx_config (id, wx_open_id, venue_id,
created_id, modified_id, created_time,
modified_time)
values (#{id,jdbcType=INTEGER}, #{wxOpenId,jdbcType=VARCHAR}, #{venueId,jdbcType=INTEGER},
#{createdId,jdbcType=INTEGER}, #{modifiedId,jdbcType=INTEGER}, #{createdTime,jdbcType=TIMESTAMP},
#{modifiedTime,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.ydd.oms.entity.sys.WxConfig" >
insert into sys_wx_config
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="wxOpenId != null" >
wx_open_id,
</if>
<if test="venueId != null" >
venue_id,
</if>
<if test="createdId != null" >
created_id,
</if>
<if test="modifiedId != null" >
modified_id,
</if>
<if test="createdTime != null" >
created_time,
</if>
<if test="modifiedTime != null" >
modified_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="wxOpenId != null" >
#{wxOpenId,jdbcType=VARCHAR},
</if>
<if test="venueId != null" >
#{venueId,jdbcType=INTEGER},
</if>
<if test="createdId != null" >
#{createdId,jdbcType=INTEGER},
</if>
<if test="modifiedId != null" >
#{modifiedId,jdbcType=INTEGER},
</if>
<if test="createdTime != null" >
#{createdTime,jdbcType=TIMESTAMP},
</if>
<if test="modifiedTime != null" >
#{modifiedTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ydd.oms.entity.sys.WxConfig" >
update sys_wx_config
<set >
<if test="wxOpenId != null" >
wx_open_id = #{wxOpenId,jdbcType=VARCHAR},
</if>
<if test="venueId != null" >
venue_id = #{venueId,jdbcType=INTEGER},
</if>
<if test="createdId != null" >
created_id = #{createdId,jdbcType=INTEGER},
</if>
<if test="modifiedId != null" >
modified_id = #{modifiedId,jdbcType=INTEGER},
</if>
<if test="createdTime != null" >
created_time = #{createdTime,jdbcType=TIMESTAMP},
</if>
<if test="modifiedTime != null" >
modified_time = #{modifiedTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ydd.oms.entity.sys.WxConfig" >
update sys_wx_config
set wx_open_id = #{wxOpenId,jdbcType=VARCHAR},
venue_id = #{venueId,jdbcType=INTEGER},
created_id = #{createdId,jdbcType=INTEGER},
modified_id = #{modifiedId,jdbcType=INTEGER},
created_time = #{createdTime,jdbcType=TIMESTAMP},
modified_time = #{modifiedTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="list" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from sys_wx_config
</select>
</mapper>

View File

@@ -90,7 +90,7 @@
domainObjectName 给表对应的 model 起名字 domainObjectName 给表对应的 model 起名字
注意:大小写敏感问题。 注意:大小写敏感问题。
--> -->
<table tableName="sv_health_docs" domainObjectName="HealthDoc" <table tableName="sys_wx_config" domainObjectName="WxConfig"
enableInsert="true" enableInsert="true"
enableDeleteByPrimaryKey="true" enableDeleteByPrimaryKey="true"
enableSelectByPrimaryKey="true" enableSelectByPrimaryKey="true"