腾讯即时通信 tim-sdk.js功能扩展

简介

因项目需求, 需对tim-sdk接口进行扩展。目前仅扩展了好友模块(添加,删除,校验,查询)
本文将为您详细介绍即时通信(Web & 小程序)端SDK扩展功能

  • 注意: 因TIM SDK的js已被压缩,所以只能在器内部添加相应的方法,需找到对应的地方修改代码!文档最后附加已修改的SDK文件
  • 版本:2.11.2

一、查询好友

方法名: getFriendList()
参数:

SDK中已自带该方法, 无需重写。默认传递当前登录用户为fromAccount查询好友列表。详细代码如下

// 格式化后
{
    key:"getFriendList",
    value:function(){ 
        var e=this,t="".concat(this._className,".getFriendList"),n={};
        return n.fromAccount=this._userModule.getMyAccount(),
            Ai.info("".concat(t," myAccount:").concat(n.fromAccount)),
            this._userModule.request({protocolName:Wc,requestData:n})
                .then((function(n){
                    // 详细返回数据请参考 [服务端Api-拉取好友](https://cloud.tencent.com/document/product/269/1647)
                    Ai.info("".concat(t," ok")),
                    // 对返回值格式化后填充到friends中
                    e._transformFriendList(n);
                    var o=Jn(e.friends.values());
                    return $f(o)}
                )).catch((function(e){
                    return Ai.error("".concat(t," failed. error:"),e),Yf(e)}
                ))
    }
}

// 已压缩
{key:"getFriendList",value:function(){var e=this,t="".concat(this._className,".getFriendList"),n={};return n.fromAccount=this._userModule.getMyAccount(),Ai.info("".concat(t," myAccount:").concat(n.fromAccount)),this._userModule.request({protocolName:Wc,requestData:n}).then((function(n){Ai.info("".concat(t," ok")),e._transformFriendList(n);var o=Jn(e.friends.values());return $f(o)})).catch((function(e){return Ai.error("".concat(t," failed. error:"),e),Yf(e)}))}}

使用示例

tim.getFriendList().then(({data}) => {
  for(let i = 0; i < data.length; i++) {
    console.log(data)
  }
  return data
})

二、解析添加/删除好友时的数据

方法名: _transformChangeFriend

// 已格式化
{
    key:"_transformChangeFriend",
    value:function(e,c){
        var t=e.data;
        if(!gu(t)&&!gu(t.resultItem)){
            Ai.info("".concat(this._className,"._transformChangeFriend friendNum:").concat(t.resultItem.length));
            var _t=this;
            // 返回未添加成功的好友信息(包含需要同意申请的)
            return t.resultItem.filter(function(it){
                it.resultCode!==0&&Ai.error("".concat(_t._className, "._transformChangeFriend ",c,"[").concat(it.to,"] fail:").concat(it.resultInfo));
                return it.resultCode!==0
            })
        }
    }
}
// 已压缩
{key:"_transformChangeFriend",value:function(e,c){var t=e.data;if(!gu(t)&&!gu(t.resultItem)){Ai.info("".concat(this._className,"._transformChangeFriend friendNum:").concat(t.resultItem.length));var _t=this;return t.resultItem.filter(function(it){it.resultCode!==0&&Ai.error("".concat(_t._className, "._transformChangeFriend ",c,"[").concat(it.to,"] fail:").concat(it.resultInfo));return it.resultCode!==0})}}}

三、添加好友

方法名: applyAddFriend()
参数: [{toAccount: userId, addSource: 'AddSource_Type_PC_WEB'}, {...}]

toAccount 为用户账号, addSource 要求以 AddSource_Type_ 开头,详情请参考 服务端Api-添加好友

添加的主要方法如下:

{key:"applyAddFriend",value:function(p){var t="".concat(this._className,".applyAddFriend");if(!Array.isArray(p))Ai.error("".concat(t," 参数必需是数组")),Yf({code:ql.DEL_FRIEND_INVALID_PARAM,message:Gf});p.length>1000&&(Ai.warn("".concat(t," 添加好友人数不能超过1000人")),p.length=1000);var e=this,n={};return n.fromAccount=this._userModule.getMyAccount(),n.addFriendItem=p,Ai.info("".concat(t," myAccount:").concat(n.fromAccount).concat(", addFriendItem: ").concat(p.toString())),this._userModule.request({protocolName:Fadd,requestData:n}).then((function(n){Ai.info("".concat(t," ok"));var o=e._transformChangeFriend(n,Fadd); return $f(o)})).catch((function(e){return Ai.error("".concat(t," failed. error:"),e),Yf(e)}))}}
{key:"applyAddFriend",value:function(e){return this.friendHandler.applyAddFriend(e)}}
{key:"applyAddFriend",value:function(e){return this._moduleManager.getModule(mc).applyAddFriend(e)}}
// 请求接口
this._configMap.set(Fadd,function(e){return{head:Fn(Fn({},e),{},{servcmd:"".concat(B_.NAME.FRIEND,".").concat(B_.CMD.FRIEND_ADD)}),body:{fromAccount:"",addFriendItem:[]}}}(e))

使用示例

let addFriendItem = [{toAccount: userId, addSource: 'AddSource_Type_PC_WEB'}];
tim.applyAddFriend(addFriendItem)
    .then(({data}) => {
      if (data.length > 0) {
        // 需要对方同意
        let needAgree = data.filter(it => it.resultCode === 30539);
        if (needAgree.length === data.length) {
          Message.success('添加好友申请已发送!')
        } else {
          // 对方未开启或已屏蔽好友申请, 详细错误码请参考[添加好友错误码说明](https://cloud.tencent.com/document/product/269/1643#.E9.94.99.E8.AF.AF.E7.A0.81.E8.AF.B4.E6.98.8E)
          let noNeedAgree = data.find(it => it.resultCode !== 30539);
          Message.error(`${noNeedAgree.to} 添加失败! ${noNeedAgree.resultInfo} 【${noNeedAgree.resultCode}】`)
        }
      } else {
        Message.success('好友已添加!')
      }
      dispatch('refreshList')
    })
    .catch(err => {
    })

四、删除好友

方法名: deleteFriend()
参数: [ userId, userId ] 账号数组

详情请参考 服务端Api-删除好友

添加的主要方法如下:

{key:"deleteFriend",value:function(p){var t="".concat(this._className,".deleteFriend");if(!Array.isArray(p))Ai.error("".concat(t," 参数必需是数组")),Yf({code:ql.DEL_FRIEND_INVALID_PARAM,message:Gf});p.length>1000&&(Ai.warn("".concat(t," 删除好友人数不能超过1000人")),p.length=1000);var e=this,n={};return n.fromAccount=this._userModule.getMyAccount(),Ai.info("".concat(t," myAccount:").concat(n.fromAccount).concat(", toAccounts: ").concat(p.toString())),n.toAccount=p,this._userModule.request({protocolName:Fdel,requestData:n}).then((function(n){Ai.info("".concat(t," ok"));var o=e._transformChangeFriend(n,Fdel); return $f(o)})).catch((function(e){return Ai.error("".concat(t," failed. error:"),e),Yf(e)}))}}
{key:"deleteFriend",value:function(e){return this.friendHandler.deleteFriend(e)}}
{key:"deleteFriend",value:function(e){return this._moduleManager.getModule(mc).deleteFriend(e)}}
this._configMap.set(Fdel,function(e){return{head:Fn(Fn({},e),{},{servcmd:"".concat(B_.NAME.FRIEND,".").concat(B_.CMD.FRIEND_DELETE)}),body:{fromAccount:"",toAccount:[],deleteType:"Delete_Type_Single"}}}(e))

使用示例

tim.deleteFriend(idstr.split(','))
    .then(({data: errList}) => {
      if (errList.length > 0) {
        Message.success(errList[0].to + ' 删除失败! ' + errList[0].resultInfo)
      } else {
        Message.success('删除成功!')
      }
      dispatch('refreshList')
      return resolve(errList);
    })
    .catch(err => {
      return reject(err);
    })

链接: https://pan.baidu.com/s/1DJUaTB0ePrL8DXVQL0dqjA
提取码: 57bt

你可能感兴趣的:(腾讯即时通信 tim-sdk.js功能扩展)