微信开发SDK使用教程--手机微信联系人信息上传服务端

本文介绍了一种利用微信SDK实现手机微信联系人信息上传服务端的方法。通过解析ContactsInfoNoticeMessage消息,该服务能将指定微信ID的联系人信息同步到服务器,并通知PC端进行后续处理。

微信开发SDK使用教程--手机微信联系人信息上传服务端

case ContactsInfoNotice : {// 微信联系人信息上传
log.debug("socket:msgtype=ContactsInfoNotice");
contactsInfoNoticeHandler.handleMsg(ctx, msgVo);
break;
}

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

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.service.AccountService;
import com.jubotech.business.web.service.WeChatAccountService;
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.ContactsInfoNotice.ContactsInfoNoticeMessage;
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 ContactsInfoNoticeHandler{
private final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private WeChatAccountService weChatAccountService;
@Autowired
private AccountService accountService;

/**
 * 联系人信息上传
 * @author wechatno:tangjinjinwx
 * @param ctx
 * @param vo
 */

public  void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
    try {
        ContactsInfoNoticeMessage req = vo.getContent().unpack(ContactsInfoNoticeMessage.class);
        log.info(JsonFormat.printer().print(req));
        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);
        } 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 ,ContactsInfoNoticeMessage req){
    MessageUtil.sendJsonMsg(chx, EnumMsgType.ContactsInfoNotice,NettyConnectionUtil.getNettyId(chx), null, req);
}

}

项目地址:https://www.wuliaokankan.cn/url301/138.html
接口参考:http://www.yunlauncher.com/Blog/articles/119.html

转载于:https://www.cnblogs.com/wuliaokankan/p/10607964.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值