Angular 5 整合容联云 即时通讯

1.首先要登录容联云官网,注册帐号。然后建立应用,获取appid

2.下载容联云sdk,在项目中引用。

  1.     在index.html中引用 <script src="https://app.cloopen.com/im50/ytx-web-im-min-new-5.4.0.js"></script>
  2.     在typings.d.ts文件中,声明调用对象 declare var RL_YTX: any;
  3.     在自己的component中,添加sdk的初始化,注意在初始化成功函数内,调用登录函数。注意init函数的参数,改成自己的appid
    var resp = RL_YTX.init('8a216da862fc745c01632005f49a0sdas');
    console.log("resp.code", resp.code);
    if (170002 == resp.code) {
    //缺少必要参数,详情见msg参数
    //用户逻辑处理
    } else if (174001 == resp.code) {
    //不支持HTML5,关闭页面//用户逻辑处理}
    } else if (200 == resp.code) {
    //初始化成功
    //用户逻辑处理
    //判断不支持的功能,屏蔽页面展示
    var unsupport = resp.unsupport;
    this.success();
    }

    4.登录函数,sig的计算方式 appid+userid+时间戳+token  appid和token一定要换成自己的

success() {
//账号登录参数设置
var loginBuilder = new RL_YTX.LoginBuilder();
//登录类型 1账号登录,3voip账号密码登录
loginBuilder.setType(1);
//设置用户名
loginBuilder.setUserName(this.userInfo.name);
//type值为1时,密码可以不赋值
loginBuilder.setPwd(this.userInfo.pass);
//设置sig
let timeType = this.getTimeStamp();
let need = '8a216da862fc745c01632005f49a0sdas' + this.userInfo.name + timeType + 'a65feab231a35caeb56a02253724807b';
let md5 = Md5.hashStr(need).toString();
loginBuilder.setSig(md5);
//设置时间戳
loginBuilder.setTimestamp(timeType);
//执行用户登录
let _this = this;
RL_YTX.login(loginBuilder, this.onMessageR, function (obj) {
console.log("错误码:", obj);
});
}

    5.时间戳计算函数

getTimeStamp() {
let now = new Date();
let timestamp = now.getFullYear() + '' + ((now.getMonth() + 1) >= 10 ? "" + (now.getMonth() + 1) : "0" + (now.getMonth() + 1)) + (now.getDate() >= 10 ? now.getDate() : "0" + now.getDate()) + (now.getHours() >= 10 ? now.getHours() : "0" + now.getHours()) + (now.getMinutes() >= 10 ? now.getMinutes() : "0" + now.getMinutes()) + (now.getSeconds() >= 10 ? now.getSeconds() : "0" + now.getSeconds());
return timestamp;
}

    6.注意引用md5 加密函数

    7.最终实现效果,发送接收消息可看官方文档实现。

Angular 5 整合容联云 即时通讯