|
@@ -1,9 +1,11 @@
|
|
|
+// #ifdef H5
|
|
|
import './runtime'
|
|
|
-import TIM from './tim-wx';
|
|
|
-import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min";
|
|
|
-
|
|
|
-const EXPIRETIME = 604800;
|
|
|
-const LOGTAG = '--JHIM--:';
|
|
|
+// #endif
|
|
|
+import TIM from './tim-wx';
|
|
|
+import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min";
|
|
|
+
|
|
|
+const EXPIRETIME = 604800;
|
|
|
+const LOGTAG = '--JHIM--:';
|
|
|
|
|
|
class JhImGroup {
|
|
|
constructor(jhim, groupId, groupType, name ,avatar) {
|
|
@@ -67,7 +69,9 @@ class JhImGroup {
|
|
|
getProfile() {
|
|
|
return this.jhim.tim.getGroupProfile({
|
|
|
groupID: this.groupId
|
|
|
- }).then(res => res.data.group);
|
|
|
+ }).then(res => {
|
|
|
+ return res.data.group;
|
|
|
+ });
|
|
|
// memberCount
|
|
|
}
|
|
|
on(cb, type) {
|
|
@@ -81,6 +85,7 @@ class JhImGroup {
|
|
|
});
|
|
|
if(msgs.length) cb(msgs);
|
|
|
}
|
|
|
+ this.jhim.tim.on(TIM.EVENT.MESSAGE_RECEIVED, cb.jhimgroupcb);
|
|
|
return this;
|
|
|
}
|
|
|
off(cb) {
|
|
@@ -90,6 +95,7 @@ class JhImGroup {
|
|
|
}
|
|
|
onReady(cb) {
|
|
|
if(!cb) return this;
|
|
|
+ if(this.jhim.ready) cb(true);//cb(this.ready);
|
|
|
cb.jhimcbready = event => {
|
|
|
cb(this.ready)
|
|
|
}
|
|
@@ -114,6 +120,8 @@ class JhImGroup {
|
|
|
text
|
|
|
},
|
|
|
})
|
|
|
+ message.nick = this.name;
|
|
|
+ message.avatar = this.avatar;
|
|
|
return this.jhim.tim.sendMessage(message).then(() => message);
|
|
|
}
|
|
|
sendCustomMessage(payload) {
|
|
@@ -122,85 +130,87 @@ class JhImGroup {
|
|
|
conversationType: TIM.TYPES.CONV_GROUP,
|
|
|
payload,
|
|
|
})
|
|
|
+ message.nick = this.name;
|
|
|
+ message.avatar = this.avatar;
|
|
|
return this.tim.sendMessage(message).then(() => message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-let instance = null;
|
|
|
-export default class JhIm {
|
|
|
- static getInstance(userId, sdkAppID, secretKey, userSig) {
|
|
|
- if (JhIm.instance) return JhIm.instance;
|
|
|
- return JhIm.instance = new JhIm(userId, sdkAppID, secretKey, userSig);
|
|
|
- }
|
|
|
-
|
|
|
- constructor(userId, sdkAppID, secretKey, userSig) {
|
|
|
- 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 = userSig || generator.genTestUserSig(this.userId);
|
|
|
+let instance = null;
|
|
|
+export default class JhIm {
|
|
|
+ static getInstance(userId, sdkAppID, secretKey, userSig) {
|
|
|
+ if (JhIm.instance) return JhIm.instance;
|
|
|
+ return JhIm.instance = new JhIm(userId, sdkAppID, secretKey, userSig);
|
|
|
+ }
|
|
|
+
|
|
|
+ constructor(userId, sdkAppID, secretKey, userSig) {
|
|
|
+ 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 = userSig || generator.genTestUserSig(this.userId);
|
|
|
this.ready = false;
|
|
|
- this.roomId = null;
|
|
|
- this.groupReady = false;
|
|
|
-
|
|
|
- // 0 普通级别,日志量较多,接入时建议使用
|
|
|
- // 1 release级别,SDK 输出关键信息,生产环境时建议使用
|
|
|
- // 2 告警级别,SDK 只输出告警和错误级别的日志
|
|
|
- // 3 错误级别,SDK 只输出错误级别的日志
|
|
|
- // 4 无日志级别,SDK 将不打印任何日志
|
|
|
+ this.roomId = null;
|
|
|
+ this.groupReady = false;
|
|
|
+
|
|
|
+ // 0 普通级别,日志量较多,接入时建议使用
|
|
|
+ // 1 release级别,SDK 输出关键信息,生产环境时建议使用
|
|
|
+ // 2 告警级别,SDK 只输出告警和错误级别的日志
|
|
|
+ // 3 错误级别,SDK 只输出错误级别的日志
|
|
|
+ // 4 无日志级别,SDK 将不打印任何日志
|
|
|
this.tim.setLogLevel(3);
|
|
|
|
|
|
this.TYPES = TIM.TYPES;
|
|
|
- this.EVENT = TIM.EVENT;
|
|
|
-
|
|
|
- this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event => {
|
|
|
- console.log(LOGTAG + 'recv', event);
|
|
|
- let msgs = [];
|
|
|
+ this.EVENT = TIM.EVENT;
|
|
|
+
|
|
|
+ this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event => {
|
|
|
+ console.log(LOGTAG + 'recv', event);
|
|
|
+ let msgs = [];
|
|
|
event.data.forEach(v => {
|
|
|
- // https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html
|
|
|
+ // https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html
|
|
|
if (v.type == TIM.TYPES.MSG_TEXT) {
|
|
|
- msgs.push(v);
|
|
|
- msgs.push({
|
|
|
- type: v.type,
|
|
|
- name: v.nick,
|
|
|
- avatar: v.avatar,
|
|
|
- text: v.payload.text,
|
|
|
- conversationType: v.conversationType,
|
|
|
- time: v.time,
|
|
|
- })
|
|
|
- } else if (v.type == "TIMGroupTipElem") {
|
|
|
- this.onIMGroupTipElem && this.onIMGroupTipElem(v.payload);
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
- this.onIMMessageReceived && this.onIMMessageReceived(msgs);
|
|
|
- }, this);
|
|
|
-
|
|
|
+ msgs.push(v);
|
|
|
+ msgs.push({
|
|
|
+ type: v.type,
|
|
|
+ name: v.nick,
|
|
|
+ avatar: v.avatar,
|
|
|
+ text: v.payload.text,
|
|
|
+ conversationType: v.conversationType,
|
|
|
+ time: v.time,
|
|
|
+ })
|
|
|
+ } 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.ready = true;
|
|
|
- }, this);
|
|
|
- this.tim.on(TIM.EVENT.SDK_NOT_READY, event => {
|
|
|
- this.ready = false;
|
|
|
- }, this);
|
|
|
-
|
|
|
- return this;
|
|
|
+ }, this);
|
|
|
+ this.tim.on(TIM.EVENT.SDK_NOT_READY, event => {
|
|
|
+ this.ready = false;
|
|
|
+ }, this);
|
|
|
+
|
|
|
+ return this;
|
|
|
}
|
|
|
destory() {
|
|
|
this.tim = null;
|
|
|
- }
|
|
|
+ }
|
|
|
on(name, cb) {
|
|
|
// 事件
|
|
|
// https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/module-EVENT.html
|
|
|
// 1. 收到消息 TIM.EVENT.MESSAGE_RECEIVED
|
|
|
// https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html
|
|
|
- // -- type,
|
|
|
- // -- nick,
|
|
|
- // -- avatar,
|
|
|
- // -- payload
|
|
|
- // -- conversationType
|
|
|
+ // -- type,
|
|
|
+ // -- nick,
|
|
|
+ // -- avatar,
|
|
|
+ // -- payload
|
|
|
+ // -- conversationType
|
|
|
// -- time,
|
|
|
// 2. 接入 TIM.EVENT.SDK_READY
|
|
|
// 3. 掉线 TIM.EVENT.SDK_NOT_READY
|
|
@@ -209,13 +219,13 @@ export default class JhIm {
|
|
|
// 5. 收到 SDK 发生错误通知 TIM.EVENT.ERROR
|
|
|
// - event.name - TIM.EVENT.ERROR
|
|
|
// - event.data.code - 错误码
|
|
|
- // - event.data.message - 错误信息
|
|
|
+ // - event.data.message - 错误信息
|
|
|
this.tim.on(name, cb);
|
|
|
- return this;
|
|
|
- }
|
|
|
- off(name, cb) {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ off(name, cb) {
|
|
|
this.tim.off(name, cb);
|
|
|
- return this;
|
|
|
+ return this;
|
|
|
}
|
|
|
login(name, avatar) {
|
|
|
if (this.ready) return Promise.resolve();
|
|
@@ -233,6 +243,9 @@ export default class JhIm {
|
|
|
avatar: avatar,
|
|
|
allowType: TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
|
|
|
});
|
|
|
+ // this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event=>{
|
|
|
+ // debugger
|
|
|
+ // });
|
|
|
resolve();
|
|
|
this.tim.off(TIM.EVENT.SDK_READY, cb, this);
|
|
|
}
|
|
@@ -283,23 +296,27 @@ export default class JhIm {
|
|
|
offMessageRecv(cb) {
|
|
|
if(!cb) return;
|
|
|
this.time.off(TIM.EVENT.MESSAGE_RECEIVED,cb.jhimcb);
|
|
|
- }
|
|
|
- sendText(text, to) {
|
|
|
- const message = this.tim.createTextMessage({
|
|
|
- to,
|
|
|
- conversationType: TIM.TIM.TYPES.CONV_C2C,
|
|
|
- payload: {
|
|
|
- text
|
|
|
- },
|
|
|
- })
|
|
|
- return this.tim.sendMessage(message).then(() => message);
|
|
|
- }
|
|
|
- sendCustomMessage(payload, to) {
|
|
|
- const message = this.tim.createCustomMessage({
|
|
|
- to: to,
|
|
|
- conversationType: TIM.TYPES.CONV_C2C,
|
|
|
- payload,
|
|
|
- })
|
|
|
- return this.tim.sendMessage(message).then(() => message);
|
|
|
- }
|
|
|
+ }
|
|
|
+ sendText(text, to) {
|
|
|
+ const message = this.tim.createTextMessage({
|
|
|
+ to,
|
|
|
+ conversationType: TIM.TIM.TYPES.CONV_C2C,
|
|
|
+ payload: {
|
|
|
+ text
|
|
|
+ },
|
|
|
+ })
|
|
|
+ message.nick = this.name;
|
|
|
+ message.avatar = this.avatar;
|
|
|
+ return this.tim.sendMessage(message).then(() => message);
|
|
|
+ }
|
|
|
+ sendCustomMessage(payload, to) {
|
|
|
+ const message = this.tim.createCustomMessage({
|
|
|
+ to: to,
|
|
|
+ conversationType: TIM.TYPES.CONV_C2C,
|
|
|
+ payload,
|
|
|
+ })
|
|
|
+ message.nick = this.name;
|
|
|
+ message.avatar = this.avatar;
|
|
|
+ return this.tim.sendMessage(message).then(() => message);
|
|
|
+ }
|
|
|
}
|