抖音sdk,抖音api接口java調用源代碼


抖音sdk,抖音api接口

1、抖音上線下線

/** 
 * 抖音上線通知 
 * @author wechat:happybabby110
 * @blog http://www.wlkankan.cn
 */
public  void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
    try {
        ImOnlineNoticeMessage req = vo.getContent().unpack(ImOnlineNoticeMessage.class);
        log.debug(JsonFormat.printer().print(req));
        //1、校驗用戶信息
        if(null != req){
            //2、存儲全局id 與通道
            NettyConnectionUtil.registerUserid(req.getImUid(),ctx);
               
            DeviceInfo  device = deviceService.getByDeviceid(req.getImei());
            if(null != device){
                //做個保護,如果當前微信號在其他設備上登陸過,就把之前那條記錄刪除
                if(!StringUtils.isBlank(req.getImUid()) && !StringUtils.isBlank(req.getImei())){
                     if(!StringUtils.isEmpty(device.getImuid()) && !req.getImUid().equals(device.getImuid())){
                         device.setAvatar("");
                         device.setImuid("");
                         device.setNickname("");
                         device.setIsonline(1);
                         deviceService.update(device);
                     }
                }
                //設置新的參數
                device.setImuid(req.getImUid());
                device.setNickname(req.getNickName());
                device.setAvatar(req.getAvatar());
                device.setGender(req.getGenderValue());
                device.setPhone(req.getPhone());
                device.setUniqueid(req.getUniqueId());
                device.setProvince(req.getProvince());
                device.setCity(req.getCity());
                device.setDistrict(req.getDistrict());
                device.setSignature(req.getSignature());
                device.setAwemecount(req.getAwemeCount());
                device.setFollowingcount(req.getFollowingCount());
                device.setFollowercount(req.getFollowerCount());
                device.setFriendcount(req.getFriendCount());
                     
                //改為上線狀態
                device.setIsonline(0);//上線
                deviceService.update(device);
                //3、告訴客戶端消息已收到
                MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
                
                asyncTaskService.msgSend2pc(req.getImUid(), EnumMsgType.ImOnlineNotice, req);
            }
             
        } 
     
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
    }
}

/**
* 抖音下線通知
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
try {
ImOfflineNoticeMessage req = vo.getContent().unpack(ImOfflineNoticeMessage.class);
log.debug(JsonFormat.printer().print(req));
if (null != req) {
// 把消息轉發給pc端
DeviceInfo account = deviceService.getByImUid(req.getImUid());
if (null != account) {
account.setIsonline(1);// 下線
deviceService.update(account);

                asyncTaskService.msgSend2pc(req.getImUid(), EnumMsgType.ImOfflineNotice, req);
            }
            // 3、告訴客戶端消息已收到
            MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
        } else {
            MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam, vo.getId(), Constant.ERROR_MSG_ILLEGALDEVICE);
        }

    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam, vo.getId(), Constant.ERROR_MSG_DECODFAIL);
    }
}

2、抖音粉絲或好友收發消息

/**
* 給抖音粉絲或好友發消息
* @author wechat:happybabby110
* @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.msgSend2Phone(ctx, req.getImUid(), EnumMsgType.TalkToFriendTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}

/**
* 抖音聊天消息實時推送
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
try {
ChatMsgNoticeMessage req = vo.getContent().unpack(ChatMsgNoticeMessage.class);
log.debug(JsonFormat.printer().print(req));

        log.debug(LocalDateTime.now()+" ChatMsgNoticeMessage  對應的線程名: "+Thread.currentThread().getName());
           
        //消息轉發到pc端
        asyncTaskService.msgSend2pc(req.getImUid(), EnumMsgType.ChatMsgNotice, req);
         
        // 告訴客戶端消息已收到
        MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
          
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
    }
}

3、關注與取消關注抖音號

/**
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
* 關注抖音號
*/
@Async
public void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
FollowTaskMessage.Builder bd = FollowTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
FollowTaskMessage req = bd.build();
//將消息轉發送給手機客戶端
asyncTaskService.msgSend2Phone(ctx, req.getImUid(), EnumMsgType.FollowTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}

/**
* 取消關注抖音號
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
UnFollowTaskMessage.Builder bd = UnFollowTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
UnFollowTaskMessage req = bd.build();
//將消息轉發送給手機客戶端
asyncTaskService.msgSend2Phone(ctx, req.getImUid(), EnumMsgType.UnFollowTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}

4、同步抖音推薦的好友

/**
* 同步抖音推薦的好友
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
SyncRecFriendsTaskMessage.Builder bd = SyncRecFriendsTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
SyncRecFriendsTaskMessage req = bd.build();
//將消息轉發送給手機客戶端
asyncTaskService.msgSend2Phone(ctx, req.getImUid(), EnumMsgType.SyncRecFriendsTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}

/**
* 推送抖音推薦的好友
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
try {
RecFriendsPushNoticeMessage req = vo.getContent().unpack(RecFriendsPushNoticeMessage.class);
log.debug(JsonFormat.printer().print(req));

        log.debug(LocalDateTime.now()+" RecFriendsPushNoticeMessage  對應的線程名: "+Thread.currentThread().getName());
           
        //消息轉發到pc端
        asyncTaskService.msgSend2pc(req.getImUid(), EnumMsgType.RecFriendsPushNotice, req);
         
        // 告訴客戶端消息已收到
        MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
         
         
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
    }
}

5、同步抖音聊天會話列表

/**
* 同步抖音會話列表
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
SyncConversationTaskMessage.Builder bd = SyncConversationTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
SyncConversationTaskMessage req = bd.build();
//將消息轉發送給手機客戶端
asyncTaskService.msgSend2Phone(ctx, req.getImUid(), EnumMsgType.SyncConversationTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}

/**
* 推送抖音會話列表
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
try {
ConversationPushNoticeMessage req = vo.getContent().unpack(ConversationPushNoticeMessage.class);
log.debug(JsonFormat.printer().print(req));

        log.debug(LocalDateTime.now()+" ConversationPushNoticeMessage  對應的線程名: "+Thread.currentThread().getName());
           
        //消息轉發到pc端
        asyncTaskService.msgSend2pc(req.getImUid(), EnumMsgType.ConversationPushNotice, req);
         
        // 告訴客戶端消息已收到
        MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
         
         
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
    }
}

6、同步抖音粉絲列表

/**
* 同步抖音粉絲
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
SyncFollowersTaskMessage.Builder bd = SyncFollowersTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
SyncFollowersTaskMessage req = bd.build();
//將消息轉發送給手機客戶端
asyncTaskService.msgSend2Phone(ctx, req.getImUid(), EnumMsgType.SyncFollowersTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}

/**
* 推送抖音粉絲
* @author wechat:happybabby110
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
try {
FollowersPushNoticeMessage req = vo.getContent().unpack(FollowersPushNoticeMessage.class);
log.debug(JsonFormat.printer().print(req));

        log.debug(LocalDateTime.now()+this.getClass().getName()+"對應的線程名: "+Thread.currentThread().getName());
           
        //消息轉發到pc端
        asyncTaskService.msgSend2pc(req.getImUid(), EnumMsgType.FollowersPushNotice, req);
         
        // 告訴客戶端消息已收到
        MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
         
    } catch (Exception e) {
        e.printStackTrace();
        MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM