个人微信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);
    }
}


相关文章
|
10月前
|
Java API 数据处理
Java新特性:使用Stream API重构你的数据处理
Java新特性:使用Stream API重构你的数据处理
|
10月前
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
438 100
|
10月前
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
402 101
|
10月前
|
并行计算 Java 大数据
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
509 101
|
10月前
|
安全 Java API
使用 Java 构建强大的 REST API 的四个基本技巧
本文结合探险领域案例,分享Java构建REST API的四大核心策略:统一资源命名、版本控制与自动化文档、安全防护及标准化异常处理,助力开发者打造易用、可维护、安全可靠的稳健API服务。
569 116
|
10月前
|
算法 安全 Java
除了类,Java中的接口和方法也可以使用泛型吗?
除了类,Java中的接口和方法也可以使用泛型吗?
283 11
|
9月前
|
Java Go 开发工具
【Java】(9)抽象类、接口、内部的运用与作用分析,枚举类型的使用
抽象类必须使用abstract修饰符来修饰,抽象方法也必须使用abstract修饰符来修饰,抽象方法不能有方法体。抽象类不能被实例化,无法使用new关键字来调用抽象类的构造器创建抽象类的实例。抽象类可以包含成员变量、方法(普通方法和抽象方法都可以)、构造器、初始化块、内部类(接 口、枚举)5种成分。抽象类的构造器不能用于创建实例,主要是用于被其子类调用。抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类abstract static不能同时修饰一个方法。
361 1
|
10月前
|
消息中间件 人工智能 Java
抖音微信爆款小游戏大全:免费休闲/竞技/益智/PHP+Java全筏开源开发
本文基于2025年最新行业数据,深入解析抖音/微信爆款小游戏的开发逻辑,重点讲解PHP+Java双引擎架构实战,涵盖技术选型、架构设计、性能优化与开源生态,提供完整开源工具链,助力开发者从理论到落地打造高留存、高并发的小游戏产品。
|
10月前
|
存储 数据可视化 Java
Java Stream API 的强大功能
Java Stream API 是 Java 8 引入的重要特性,它改变了集合数据的处理方式。通过声明式语法,开发者可以更简洁地进行过滤、映射、聚合等操作。Stream API 支持惰性求值和并行处理,提升了代码效率和可读性,是现代 Java 开发不可或缺的工具。
205 0
Java Stream API 的强大功能
|
10月前
|
安全 NoSQL API
拼多多:通过微信支付API实现社交裂变付款的技术解析
基于微信JSAPI构建社交裂变支付系统,用户发起拼单后生成预订单与分享链接,好友代付后通过回调更新订单并触发奖励。集成微信支付、异步处理、签名验签与Redis关系绑定,提升支付成功率与裂变系数,实现高效安全的闭环支付。
1030 0