企微消息,小程序消息

This commit is contained in:
陈宁 2023-05-29 20:04:29 +08:00
parent aebfc317bd
commit 214262cf40
8 changed files with 238 additions and 8 deletions

View File

@ -0,0 +1,16 @@
package cn.axzo.msgcenter.common.consts;
import lombok.experimental.UtilityClass;
/**
* @author cn
* @version 1.0
* @description
* @date 2023/3/27 14:27
*/
@UtilityClass
public class ConstPool {
public static final String REDIS_KEY_PREFIX = "msg_center";
public static final String MODIFIERS = "modifiers";
public static final String LAST_ONE = "limit 1";
}

11
pom.xml
View File

@ -15,7 +15,6 @@
<packaging>pom</packaging>
<version>${revision}</version>
<modules>
<module>message-notices</module>
<module>wx-notices</module>
<module>inside-notices</module>
<module>msg-center-api</module>
@ -58,6 +57,16 @@
<artifactId>weixin-java-cp</artifactId>
<version>${binarywang.weixin-java.version}</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>${binarywang.weixin-java.version}</version>
</dependency>
<dependency>
<groupId>cn.axzo.msgcenter</groupId>
<artifactId>msg-center-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -30,5 +30,25 @@
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-cp</artifactId>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
</dependency>
<dependency>
<groupId>cn.axzo.msgcenter</groupId>
<artifactId>msg-center-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,7 +0,0 @@
package cn.axzo.wx.notices;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@ -0,0 +1,49 @@
package cn.axzo.wx.notices.compont;
import cn.axzo.msgcenter.common.consts.ConstPool;
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.SneakyThrows;
import me.chanjar.weixin.cp.api.impl.WxCpServiceOkHttpImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @author cn
* @version 1.0
* @description
* @date 2023/5/25 14:19
*/
@Service
public class WxCpProcessor extends WxCpServiceOkHttpImpl {
//初始化为微信的公共包数据
static {
//改变threadLocal为ttl的
setTTlThreadPool();
}
@SneakyThrows
@SuppressWarnings({"rawtypes", "unchecked"})
private static void setTTlThreadPool() {
TransmittableThreadLocal<String> ttlThreadLocal = new TransmittableThreadLocal() {
@Override
protected String initialValue() {
return "default";
}
};
Field threadLocal = ReflectionUtils.findField(WxMaConfigHolder.class, "THREAD_LOCAL");
if (threadLocal != null) {
ReflectionUtils.makeAccessible(threadLocal);
Field modifiers = ReflectionUtils.findField(threadLocal.getClass(), ConstPool.MODIFIERS);
if (modifiers != null) {
ReflectionUtils.makeAccessible(modifiers);
ReflectionUtils.setField(modifiers, threadLocal, threadLocal.getModifiers() ^ Modifier.FINAL);
}
ReflectionUtils.setField(threadLocal, WxMaConfigHolder.class, ttlThreadLocal);
}
}
}

View File

@ -0,0 +1,49 @@
package cn.axzo.wx.notices.compont;
import cn.axzo.msgcenter.common.consts.ConstPool;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceOkHttpImpl;
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @author cn
* @version 1.0
* @description
* @date 2023/5/25 14:19
*/
@Service
public class WxMaProcessor extends WxMaServiceOkHttpImpl {
//初始化为微信的公共包数据
static {
//改变threadLocal为ttl的
setTTlThreadPool();
}
@SneakyThrows
@SuppressWarnings({"rawtypes", "unchecked"})
private static void setTTlThreadPool() {
TransmittableThreadLocal<String> ttlThreadLocal = new TransmittableThreadLocal() {
@Override
protected String initialValue() {
return "default";
}
};
Field threadLocal = ReflectionUtils.findField(WxMaConfigHolder.class, "THREAD_LOCAL");
if (threadLocal != null) {
ReflectionUtils.makeAccessible(threadLocal);
Field modifiers = ReflectionUtils.findField(threadLocal.getClass(), ConstPool.MODIFIERS);
if (modifiers != null) {
ReflectionUtils.makeAccessible(modifiers);
ReflectionUtils.setField(modifiers, threadLocal, threadLocal.getModifiers() ^ Modifier.FINAL);
}
ReflectionUtils.setField(threadLocal, WxMaConfigHolder.class, ttlThreadLocal);
}
}
}

View File

@ -0,0 +1,55 @@
package cn.axzo.wx.notices.cp;
import cn.axzo.wx.notices.compont.WxCpProcessor;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.message.TemplateCardMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 企微消息处理器
*
* @author cn
* @version 1.0
* @description
* @date 2023/5/25 16:12
*/
@Service
@RequiredArgsConstructor
public class WxCpMessageHandler {
private final WxCpProcessor wxProcessor;
public WxCpMessageHandler switchConfig(String corpId, Integer agentId, String token) {
WxCpDefaultConfigImpl wxMaConfig = new WxCpDefaultConfigImpl();
wxMaConfig.setCorpId(corpId);
wxMaConfig.setAgentId(agentId);
wxMaConfig.setAccessToken(token);
//修改过过期时间为5分钟
wxMaConfig.setExpiresTime(System.currentTimeMillis() + 300000);
wxProcessor.setWxCpConfigStorage(wxMaConfig);
return this;
}
public WxCpMessageSendResult send(WxCpMessage msg) throws WxErrorException {
return wxProcessor.getMessageService().send(msg);
}
public void updateTemplateCardButton(TemplateCardMessage msg) throws WxErrorException {
wxProcessor.getTaskCardService().updateTemplateCardButton(msg);
}
public void updateTemplateCardButton(List<String> userIds, String taskId, String replaceName) throws WxErrorException {
wxProcessor.getTaskCardService().update(userIds, taskId, replaceName);
}
public void updateTemplateCardButton(List<String> userIds, List<Integer> partyIds,
List<Integer> tagIds, Integer atAll, String responseCode,
String replaceName) throws WxErrorException {
wxProcessor.getTaskCardService().updateTemplateCardButton(userIds, partyIds, tagIds, atAll, responseCode, replaceName);
}
}

View File

@ -0,0 +1,39 @@
package cn.axzo.wx.notices.miniapp;
import cn.axzo.wx.notices.compont.WxMaProcessor;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Service;
/**
* 微信小程序消息处理器
*
* @author cn
* @version 1.0
* @description
* @date 2023/5/25 16:12
*/
@Service
@RequiredArgsConstructor
public class WxMiniMessageHandler {
private final WxMaProcessor wxProcessor;
public WxMiniMessageHandler switchConfig(String appid, String token) {
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
wxMaConfig.setAppid(appid);
wxMaConfig.setAccessToken(token);
//修改过过期时间为5分钟
wxMaConfig.setExpiresTime(System.currentTimeMillis() + 300000);
wxProcessor.setWxMaConfig(wxMaConfig);
wxProcessor.switchoverTo(appid);
return this;
}
public void sendSubscribeMsg(WxMaSubscribeMessage msg) throws WxErrorException {
wxProcessor.getMsgService().sendSubscribeMsg(msg);
}
}