微信云linux sdk,微信云控开发SDK使用教程--手机端推送好友列表通知服务端

这篇教程详细介绍了如何处理手机端推送的好友列表通知服务端的流程。首先,通过`FriendPushNoticeHandler`类接收到消息,然后转发给PC端,并保存到数据库。同时,如果目标用户不在线,则返回错误信息。处理过程中涉及到`WeChatAccountService`、`AccountService`和`WeChatContactService`等多个服务的交互,确保好友信息的同步与更新。
摘要由CSDN通过智能技术生成

微信云控开发SDK使用教程--手机端推送好友列表通知服务端

case FriendPushNotice : {//手机端推送好友列表 log.debug("socket:msgtype=FriendPushNotice"); friendPushNoticeHandler.handleMsg(ctx, msgVo); break; }

package com.jubotech.framework.netty.handler.socket;

import java.util.List;

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service;

import com.google.protobuf.util.JsonFormat; import com.jubotech.business.web.domain.AccountInfo; import com.jubotech.business.web.domain.WeChatAccountInfo; import com.jubotech.business.web.domain.WeChatContactInfo; import com.jubotech.business.web.service.AccountService; import com.jubotech.business.web.service.WeChatAccountService; import com.jubotech.business.web.service.WeChatContactService; import com.jubotech.framework.netty.common.Constant; import com.jubotech.framework.netty.utils.MessageUtil; import com.jubotech.framework.netty.utils.NettyConnectionUtil;

import Jubo.JuLiao.IM.Wx.Proto.FriendAddNotice.FriendMessage; import Jubo.JuLiao.IM.Wx.Proto.FriendPushNotice.FriendPushNoticeMessage; import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode; import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumMsgType; import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage; import io.netty.channel.ChannelHandlerContext;

@Service public class FriendPushNoticeHandler{ private final Logger log = LoggerFactory.getLogger(getClass()); @Autowired private WeChatAccountService weChatAccountService; @Autowired private AccountService accountService; @Autowired private WeChatContactService weChatContactService;

/**

* 手机端主动推送好友列表消息

* @author wechatno:tangjinjinwx

* @param ctx

* @param vo

*/

public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {

try {

FriendPushNoticeMessage req = vo.getContent().unpack(FriendPushNoticeMessage.class);

log.info(JsonFormat.printer().print(req));

// 把消息转发给pc端

// a、根据wechatId找到accountid

// b、通过accountid找到account

// c、通过account账号找到通道

WeChatAccountInfo account = weChatAccountService.findWeChatAccountInfoByWeChatId(req.getWeChatId());

if (null != account && null != account.getAccountid() && 1 != account.getIslogined()) {

AccountInfo accInfo = accountService.findAccountInfoByid(account.getAccountid());

if (null != accInfo) {

// 转发给pc端

ChannelHandlerContext chx = NettyConnectionUtil.getClientChannelHandlerContextByUserId(accInfo.getAccount());

if (null != chx) {

asyncSendMsg(chx, req);

}

}

// 告诉客户端消息已收到

MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);

//异步保存到数据库

friendListSave(req, account);

} else {

// 对方不在线

MessageUtil.sendErrMsg(ctx, EnumErrorCode.TargetNotOnline, Constant.ERROR_MSG_NOTONLINE);

}

} catch (Exception e) {

e.printStackTrace();

MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);

}

}

@Async

private void asyncSendMsg(ChannelHandlerContext chx,FriendPushNoticeMessage req){

MessageUtil.sendJsonMsg(chx, EnumMsgType.FriendPushNotice, NettyConnectionUtil.getNettyId(chx),null, req);

}

@Async

private void friendListSave(FriendPushNoticeMessage req,WeChatAccountInfo account) {

List friendList = req.getFriendsList();

if(null != friendList && friendList.size()>0){

String wechatId = req.getWeChatId();

for(int i=0;i

FriendMessage friend = friendList.get(i);

if(null != friend){

WeChatContactInfo contactinfo = weChatContactService.findContactinfoByfriendid(account.getCid(),wechatId,friend.getFriendId());

try {

if(null != contactinfo){

setContActinfo(contactinfo, friend);

weChatContactService.update(contactinfo);

}else{

contactinfo = new WeChatContactInfo();

setContActinfo(contactinfo, friend);

contactinfo.setCid(account.getCid());

contactinfo.setWechatid(req.getWeChatId());

contactinfo.setFriendid(friend.getFriendId());

weChatContactService.insert(contactinfo);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

private static void setContActinfo(WeChatContactInfo contactinfo,FriendMessage friend){

contactinfo.setFriend_wechatno(friend.getFriendNo());

contactinfo.setNickname(friend.getFriendNick());

contactinfo.setGender(friend.getGenderValue());

contactinfo.setAvatar(friend.getAvatar());

contactinfo.setCountry(friend.getCountry());

contactinfo.setProvince(friend.getProvince());

contactinfo.setCity(friend.getCity());

contactinfo.setState(0);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值