个人微信api接口java调用源代码

简介: 个人微信api接口java调用源代码

个人微信api接口,微信sdk

1、微信好友收发消息

/**
* 给微信好友发消息
* @author wechatno:tangjinjinwx
* @blog http://www.wlkankan.cn
*/
@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);
  }
}
 
 
/**
 * 微信好友发来聊天消息通知
 * @author wechatno:tangjinjinwx
     * @blog http://www.wlkankan.cn
 */
@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());
  }
}

2、触发手机推送微信好友列表及返回

/**
* 触发手机推送微信好友列表
* @author wechatno:tangjinjinwx
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
TriggerFriendPushTaskMessage.Builder bd = TriggerFriendPushTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
TriggerFriendPushTaskMessage req = bd.build();
//TriggerFriendPushTaskMessage req = vo.getContent().unpack(TriggerFriendPushTaskMessage.class);
//将消息转发送给手机客户端
      asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TriggerFriendPushTask, vo, req);
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
    }
}
 
 
/**
 * 微信好友列表消息推送
 * @author wechatno:tangjinjinwx
     * @blog http://www.wlkankan.cn
 */
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
  try {
    FriendPushNoticeMessage req = vo.getContent().unpack(FriendPushNoticeMessage.class);
    log.debug(JsonFormat.printer().print(req));
    // 把消息转发给pc端
    asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendPushNotice, req);
     
    // 异步保存到数据库
    asyncTaskService.friendListSave(req);
     
    // 告诉客户端消息已收到
    MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
 
  } catch (Exception e) {
    e.printStackTrace();
    MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam, vo.getId(), Constant.ERROR_MSG_DECODFAIL);
  }
}

3、触发推送微信群聊列表及返回

/**
* 触发推送微信群聊列表
* @author wechatno:tangjinjinwx
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
TriggerChatRoomPushTaskMessage.Builder bd = TriggerChatRoomPushTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
TriggerChatRoomPushTaskMessage req = bd.build();
// 将消息转发送给手机客户端
asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TriggerChatroomPushTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}
/**
 * 推送微信群聊列表
 * @author wechatno:tangjinjinwx
     * @blog http://www.wlkankan.cn
 */
@Async
public  void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
    try {
      ChatRoomPushNoticeMessage req = vo.getContent().unpack(ChatRoomPushNoticeMessage.class);
      log.debug(JsonFormat.printer().print(req));
    
    asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.ChatroomPushNotice, req);
     
    asyncTaskService.qunListSave(req);
      
    // 告诉客户端消息已收到
    MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), Constant.ERROR_MSG_DECODFAIL);
    }
}

4、推送微信朋友圈、发朋友圈

/**
* 触发推送朋友圈列表
* @author wechatno:tangjinjinwx
* startTime传秒
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
TriggerCirclePushTaskMessage.Builder bd = TriggerCirclePushTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
TriggerCirclePushTaskMessage req = bd.build();
// TriggerCirclePushTaskMessage req =
// vo.getContent().unpack(TriggerCirclePushTaskMessage.class);
// 将消息转发送给手机客户端
asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TriggerCirclePushTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}
/**
 * 回传手机微信朋友圈数据
 * @author wechatno:tangjinjinwx
     * @blog http://www.wlkankan.cn
 */
@Async
public  void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
    try {
      CirclePushNoticeMessage req = vo.getContent().unpack(CirclePushNoticeMessage.class);
      log.debug(JsonFormat.printer().print(req));
      //把消息转发给pc端
    asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.CirclePushNotice, req);
    
    //保存朋友圈信息
    asyncTaskService.asyncSaveCircleMsg(req, circleService, weChatContactService);
    
      //告诉客户端消息已收到
    MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), Constant.ERROR_MSG_DECODFAIL);
    }
}
 
/**
 * 发微信朋友圈
 * @author wechatno:tangjinjinwx
 * @blog http://www.wlkankan.cn
 */
@Async
public  void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
    try {
      log.debug(contentJsonStr);
      PostSNSNewsTaskMessage.Builder bd = PostSNSNewsTaskMessage.newBuilder();
      JsonFormat.parser().merge(contentJsonStr, bd);
    PostSNSNewsTaskMessage req = bd.build();
      //PostSNSNewsTaskMessage req = vo.getContent().unpack(PostSNSNewsTaskMessage.class);
        
      asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.PostSNSNewsTask, vo, req);
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
    }
}

5、加好友及通过好友请求

/** 
 * 微信自动添加好友
 * @author wechatno:tangjinjinwx
     * @blog http://www.wlkankan.cn
 */
@Async
public  void handleMsg(ChannelHandlerContext ctx ,TransportMessage vo, String contentJsonStr) {
    try {
      log.debug(contentJsonStr);
      FriendAddTaskSetting  req =  JSON.parseObject(contentJsonStr,FriendAddTaskSetting.class);
      if(null != req){
        String resp ="fail";
         
        resp = friendAddTaskService.savePcTask(req);
         
        //3、告诉PC客户端消息已收到
        MessageUtil.sendCustomJsonMsg(ctx, "AutoFriendAddTaskResp", resp);
        
      } 
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
    }
}
 
 
    /**
 * 微信新增好友通知
 * @author wechatno:tangjinjinwx
     * @blog http://www.wlkankan.cn
 */
@Async
public  void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
    try {
      FriendAddNoticeMessage req = vo.getContent().unpack(FriendAddNoticeMessage.class);
       
    //把消息转发给pc端
    asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendAddNotice, req);
    
    //保存新增好友
    asyncTaskService.saveFriendAddContactinfo(req);
    
    //告诉客户端消息已收到
    MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
      
     
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), Constant.ERROR_MSG_DECODFAIL);
    }
}


相关文章
|
2月前
|
Java API 数据处理
Java新特性:使用Stream API重构你的数据处理
Java新特性:使用Stream API重构你的数据处理
|
1月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
1月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
1月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
2月前
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
233 100
|
2月前
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
266 101
|
1月前
|
Java Go 开发工具
【Java】(9)抽象类、接口、内部的运用与作用分析,枚举类型的使用
抽象类必须使用abstract修饰符来修饰,抽象方法也必须使用abstract修饰符来修饰,抽象方法不能有方法体。抽象类不能被实例化,无法使用new关键字来调用抽象类的构造器创建抽象类的实例。抽象类可以包含成员变量、方法(普通方法和抽象方法都可以)、构造器、初始化块、内部类(接 口、枚举)5种成分。抽象类的构造器不能用于创建实例,主要是用于被其子类调用。抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类abstract static不能同时修饰一个方法。
200 1
|
2月前
|
算法 安全 Java
除了类,Java中的接口和方法也可以使用泛型吗?
除了类,Java中的接口和方法也可以使用泛型吗?
130 11
|
30天前
|
人工智能 自然语言处理 测试技术
Apipost智能搜索:只需用业务语言描述需求,就能精准定位目标接口,API 搜索的下一代形态!
在大型项目中,API 数量庞大、命名不一,导致“找接口”耗时费力。传统工具依赖关键词搜索,难以应对语义模糊或命名不规范的场景。Apipost AI 智能搜索功能,支持自然语言查询,如“和用户登录有关的接口”,系统可理解语义并精准匹配目标接口。无论是新人上手、模糊查找还是批量定位,都能大幅提升检索效率,降低协作成本。从关键词到语义理解,智能搜索让开发者少花时间找接口,多专注核心开发,真正实现高效协作。
|
2月前
|
JSON 前端开发 API
如何调用体育数据足篮接口API
本文介绍如何调用体育数据API:首先选择可靠服务商并注册获取密钥,接着阅读文档了解基础URL、端点、参数及请求头,然后使用Python等语言发送请求、解析JSON数据,最后将数据应用于Web、App或分析场景,同时注意密钥安全、速率限制与错误处理。

热门文章

最新文章