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

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

@@ -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 typeName = VenueTypeEnum.getByValue(venue.getType()).name;
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.sv.mapper.ConfigMapper;
import com.sv.mapper.WxConfigMapper;
import com.sv.service.utils.SpringUtils;
import com.ydd.oms.entity.sys.Config;
import com.ydd.oms.entity.sys.WxConfig;
import org.springframework.util.StringUtils;
import java.util.List;
public class NotifyAdminMsgThread extends Thread{
private Integer venueId = 0;
private String venueName = "";
private String lessonName = "";
private String lessonTime = "";
private String venueType = "";
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.lessonName = lessonName;
this.lessonTime = lessonTime;
@@ -24,6 +28,14 @@ public class NotifyAdminMsgThread extends Thread{
this.url = url;
}
public Integer getVenueId() {
return venueId;
}
public void setVenueId(Integer venueId) {
this.venueId = venueId;
}
public String getUrl() {
return url;
}
@@ -72,13 +84,9 @@ public class NotifyAdminMsgThread extends Thread{
if (all == null || all.size() <= 0 ){
return;
}
String openId = null;
String webHook = null;
String dingSecret = null;
for (Config config : all) {
if ("WX_ADMIN".equals(config.getKey())) {
openId = config.getValue();
}
if ("DING_WEBHOOK".equals(config.getKey())) {
webHook = config.getValue();
}
@@ -87,10 +95,12 @@ public class NotifyAdminMsgThread extends Thread{
}
}
SendMsg sendMsg = new SendMsg(venueName,lessonName,lessonTime,venueType,url);
if (!StringUtils.isEmpty(openId)) {
String[] split = openId.split(",");
for (String s : split) {
WeiXinSendUtils.sendCard(sendMsg,s);
WxConfigMapper wxConfigMapper = SpringUtils.getBean(WxConfigMapper.class);
List<WxConfig> configs = wxConfigMapper.list();
for (WxConfig config : configs) {
if (venueId == config.getVenueId()) {
WeiXinSendUtils xinSendUtils = SpringUtils.getBean(WeiXinSendUtils.class);
xinSendUtils.sendCard(sendMsg,config.getWxOpenId());
}
}
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.WeChatHelper;
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.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;
public class WeiXinSendUtils {
@Component
public class WeiXinSendUtils implements InitializingBean {
public static void sendCard(SendMsg sendMsg,String openId){
AccessTokenResponse accessToken = WeChatHelper.getAccessToken("wx51d82eba3f5f4858", "d28320bcb885cc208b6fde2253d8663e");
String token = accessToken.getAccessToken();
if (!StringUtils.isEmpty(token)){
WeChat weChat = WeChatHelper.getWeChat(token);
TemplateMessage templateMessage = new TemplateMessage();
templateMessage.setTemplateId("vPRolOW7D3jnXhT4uqN4FOcIIuHcsHuresbZjlPBg9Y");
templateMessage.setToUser(openId);
templateMessage.setUrl(sendMsg.getViewUrl());
HashMap<String, Attribute> attr = new HashMap<>();
attr.put("thing2",new Attribute(sendMsg.getVenueName(),""));
attr.put("thing3",new Attribute(sendMsg.getLessonName(),""));
attr.put("time5",new Attribute(sendMsg.getLessonTime(),""));
attr.put("phrase7",new Attribute(sendMsg.getVenueType(),""));
templateMessage.setAttributes(attr);
private String token;
public void sendCard(SendMsg sendMsg,String openId){
WeChat weChat = WeChatHelper.getWeChat(token);
TemplateMessage templateMessage = new TemplateMessage();
templateMessage.setTemplateId("vPRolOW7D3jnXhT4uqN4FOcIIuHcsHuresbZjlPBg9Y");
templateMessage.setToUser(openId);
templateMessage.setUrl(sendMsg.getViewUrl());
HashMap<String, Attribute> attr = new HashMap<>();
attr.put("thing2",new Attribute(sendMsg.getVenueName(),""));
attr.put("thing3",new Attribute(sendMsg.getLessonName(),""));
attr.put("time5",new Attribute(sendMsg.getLessonTime(),""));
attr.put("phrase7",new Attribute(sendMsg.getVenueType(),""));
templateMessage.setAttributes(attr);
TemplateResponse templateResponse = weChat.sendTemplate(templateMessage);
if (templateResponse.getErrCode() == 42001) {
//access_token expired
getToken();
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();
}
}