java調用個人微信接口收發消息
/**
* 接受微信好友發來聊天消息
* @author wechatno:tangjinjinwx
* @param ctx
* @param vo
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
try {
FriendTalkNoticeMessage req = vo.getContent().unpack(FriendTalkNoticeMessage.class);
log.debug(JsonFormat.printer().print(req));
log.debug(LocalDateTime.now()+" 微信好友發來聊天消息 對應的線程名: "+Thread.currentThread().getName());
//攔截消息
asyncTaskService.msgAopTask(ctx,req,vo.getAccessToken(), vo.getId());
//消息轉發到pc端
asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendTalkNotice, req);
// 告訴客戶端消息已收到
MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
WxAccountInfo account = weChatAccountService.findWeChatAccountInfoByWeChatId(req.getWeChatId());
//消息記錄數據庫
if (null != account){
asyncTaskService.saveMessage(account, req);
}
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
}
}
//調用參考http://www.wlkankan.cn/cate40/229.html
/**
* 服務端、客服客戶端發給設備的指令類消息
* 給好友,群聊發送消息接口
* @author wechatno:tangjinjinwx
* @param ctx
* @param vo
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
TalkToFriendTaskMessage.Builder bd = TalkToFriendTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
TalkToFriendTaskMessage req = bd.build();
// 消息記錄數據庫
asyncTaskService.savePcMessage(req);
// 將消息轉發送給手機客戶端
asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TalkToFriendTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}