import TIM from './tim-wx'; import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min"; const EXPIRETIME = 604800; const LOGTAG = '--JHIM--:'; export default class JhIm { constructor(userId, sdkAppID, secretKey) { this.userId = userId; this.sdkAppID = sdkAppID; this.secretKey = secretKey; this.tim = TIM.create({ SDKAppID: this.sdkAppID }); const generator = new LibGenerateTestUserSig(this.sdkAppID, this.secretKey, EXPIRETIME); this.userSig = generator.genTestUserSig(this.userId); this.roomId = null; this.logined = false; this.groupReady = false; // 0 普通级别,日志量较多,接入时建议使用 // 1 release级别,SDK 输出关键信息,生产环境时建议使用 // 2 告警级别,SDK 只输出告警和错误级别的日志 // 3 错误级别,SDK 只输出错误级别的日志 // 4 无日志级别,SDK 将不打印任何日志 this.tim.setLogLevel(1); this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event=>{ console.log(LOGTAG+'recv',event); let msgs = []; event.data.forEach(v=>{ if(v.type == "TIMTextElem") { msgs.push({ name: v.nick, avatar: v.avatar, text: v.payload.text }) }else if(v.type == "TIMGroupTipElem") { this.onIMGroupTipElem&&this.onIMGroupTipElem(v.payload); } }) this.onIMMessageReceived&&this.onIMMessageReceived(msgs); }, this); this.tim.on(TIM.EVENT.SDK_READY, event=>this.onIMSDKReady&&this.onIMSDKReady(event), this); this.tim.on(TIM.EVENT.SDK_NOT_READY, event=>{ this.logined = false; this.onIMSDKNotReady&&this.onIMSDKNotReady(); }, this); // 收到被踢下线通知 // event.name - TIM.EVENT.KICKED_OUT // event.data.type - 被踢下线的原因,例如 : // - TIM.TYPES.KICKED_OUT_MULT_ACCOUNT 多实例登录被踢 // - TIM.TYPES.KICKED_OUT_MULT_DEVICE 多终端登录被踢 // - TIM.TYPES.KICKED_OUT_USERSIG_EXPIRED 签名过期被踢。使用前需要将SDK版本升级至v2.4.0或以上。 this.tim.on(TIM.EVENT.KICKED_OUT, event=>this.onIMKickedOut&&this.onIMKickedOut(event), this); // 收到 SDK 发生错误通知,可以获取错误码和错误信息 // event.name - TIM.EVENT.ERROR // event.data.code - 错误码 // event.data.message - 错误信息 this.tim.on(TIM.EVENT.ERROR, event=>this.onIMError&&this.onIMError(event), this); return this; } on(name, cb) { this[name] = cb; return this; } off(name) { this[name] = null; return this; } login(name, avatar) { if(this.logined) return Promise.resolve(); this.name = name; this.avatar = avatar; return new Promise(resolve=>{ let cb = event=>{ this.logined = true; this.tim.updateMyProfile({ nick: name, avatar: avatar, allowType: TIM.TYPES.ALLOW_TYPE_ALLOW_ANY }); console.log(LOGTAG+'ready'); this.onIMReady&&this.onIMReady(event); resolve(); this.tim.off(TIM.EVENT.SDK_READY, cb, this); } this.tim.on(TIM.EVENT.SDK_READY, cb, this); console.log(LOGTAG+'login'); this.tim.login({ userID: this.userId, userSig: this.userSig, }) }); } logout() { if(!this.logined) return Promise.resolve(); return this.tim.logout().then(()=>{ this.logined = false; }); } createGroup(roomId) { console.log(LOGTAG+'search for createGroup', roomId); this.roomId = String(roomId); return this.tim.searchGroupByID(this.roomId).then(()=>{ return this.joinGroup(roomId); },()=>{ console.log(LOGTAG+'createGroup'); return this.tim.createGroup({ groupID: this.roomId, name: this.roomId, type: TIM.TYPES.GRP_AVCHATROOM, }).then(() => { return this.joinGroup(roomId); }); }); } dismissGroup() { console.log(LOGTAG+'dismissGroup'); return this.tim.dismissGroup(this.roomId).then(() => { this.groupReady = false; this.roomId = null; }); } joinGroup(roomId) { console.log(LOGTAG+'joinGroup',roomId); this.roomId = String(roomId); return this.tim.joinGroup({ groupID: this.roomId, type: TIM.TYPES.GRP_AVCHATROOM }).then((imResponse) => { console.log(LOGTAG+'joinGrouped',imResponse.data.status); switch (imResponse.data.status) { case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意 break; case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功 case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中 this.groupReady = true; break; } }); } exitGroup() { console.log(LOGTAG+'exitGroup'); return this.tim.quitGroup(this.roomId).then(()=>{ this.groupReady = false; this.roomId = null; }); } sendGroupText(text) { const message = this.tim.createTextMessage({ to: this.roomId, conversationType: TIM.TYPES.CONV_GROUP, payload: {text}, }) return this.tim.sendMessage(message).then(()=>({ name: this.name, avatar: this.avatar, text: text, })); } sendGroupMessage(payload) { const message = this.tim.createCustomMessage({ to: this.roomId, conversationType: TIM.TYPES.CONV_GROUP, payload, }) return this.tim.sendMessage(message); } getGroupProfile() { return this.tim.getGroupProfile({ groupID: this.roomId }).then(res => imResponse.data.group); } getGroupNum() { return this.getGroupProfile(res=>res.memberCount); } }