mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-23 15:11:39 +08:00
部分代码优化调整
This commit is contained in:
parent
ab8850cd12
commit
79a41e96d1
@ -0,0 +1,38 @@
|
||||
package com.github.binarywang.demo.spring.handler;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.github.binarywang.demo.spring.config.WxConfig;
|
||||
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Binary Wang
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class LogHandler extends AbstractHandler {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
|
||||
Map<String, Object> context, WxMpService wxMpService,
|
||||
WxSessionManager sessionManager) {
|
||||
this.logger.info("接收到请求消息,内容:【{}】" ,wxMessage.toString());
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxConfig getWxConfig() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -31,9 +31,9 @@ public abstract class MsgHandler extends AbstractHandler {
|
||||
//TODO 可以选择将消息保存到本地
|
||||
}
|
||||
|
||||
//当用户输入关键词如“你好”,“在吗”等并且有客服在线时,把消息转发给在线客服
|
||||
if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "在吗")
|
||||
&& weixinService.isCustomerServiceOnline()) {
|
||||
//当用户输入关键词如“是”,“客服”等并且有客服在线时,把消息转发给在线客服
|
||||
if (StringUtils.startsWithAny(wxMessage.getContent(), "是","客服")
|
||||
&& weixinService.hasKefuOnline()) {
|
||||
return WxMpXmlOutMessage
|
||||
.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName()).build();
|
||||
|
@ -11,10 +11,9 @@ import com.github.binarywang.demo.spring.handler.AbstractHandler;
|
||||
import com.github.binarywang.demo.spring.handler.MenuHandler;
|
||||
import com.github.binarywang.demo.spring.handler.MsgHandler;
|
||||
import com.github.binarywang.demo.spring.handler.NullHandler;
|
||||
import com.github.binarywang.demo.spring.handler.LogHandler;
|
||||
import com.github.binarywang.demo.spring.handler.SubscribeHandler;
|
||||
import com.github.binarywang.demo.spring.handler.UnsubscribeHandler;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
|
||||
@ -22,6 +21,7 @@ import me.chanjar.weixin.mp.api.WxMpMessageRouter;
|
||||
import me.chanjar.weixin.mp.api.WxMpServiceImpl;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.customerservice.result.WxMpKfOnlineList;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -31,6 +31,9 @@ import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||
public abstract class BaseWxService extends WxMpServiceImpl {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Autowired
|
||||
protected LogHandler logHandler;
|
||||
|
||||
@Autowired
|
||||
protected NullHandler nullHandler;
|
||||
|
||||
@ -65,9 +68,12 @@ public abstract class BaseWxService extends WxMpServiceImpl {
|
||||
|
||||
final WxMpMessageRouter newRouter = new WxMpMessageRouter(this);
|
||||
|
||||
//记录所有事件的日志
|
||||
newRouter.rule().handler(this.logHandler).next();
|
||||
|
||||
// 自定义菜单事件
|
||||
newRouter.rule().async(false).event(WxConsts.BUTTON_CLICK)
|
||||
.handler(this.getMenuHandler()).end();
|
||||
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
|
||||
.event(WxConsts.BUTTON_CLICK).handler(this.getMenuHandler()).end();
|
||||
|
||||
// 点击菜单连接事件
|
||||
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
|
||||
@ -112,13 +118,10 @@ public abstract class BaseWxService extends WxMpServiceImpl {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isCustomerServiceOnline() {
|
||||
public boolean hasKefuOnline() {
|
||||
try {
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist";
|
||||
String executeResult = this.get(url, null);
|
||||
JsonArray jsonArray = new JsonParser().parse(executeResult)
|
||||
.getAsJsonObject().get("kf_online_list").getAsJsonArray();
|
||||
return jsonArray.size() > 0;
|
||||
WxMpKfOnlineList kfOnlineList = this.getKefuService().kfOnlineList();
|
||||
return kfOnlineList != null && kfOnlineList.getKfOnlineList().size() > 0;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("获取客服在线状态异常: " + e.getMessage(), e);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user