|
@@ -2,6 +2,7 @@ import TIM from './tim-wx';
|
|
import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min";
|
|
import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min";
|
|
|
|
|
|
const EXPIRETIME = 604800;
|
|
const EXPIRETIME = 604800;
|
|
|
|
+const LOGTAG = '--JHIM--:';
|
|
|
|
|
|
export default class JhIm {
|
|
export default class JhIm {
|
|
constructor(userId, sdkAppID, secretKey) {
|
|
constructor(userId, sdkAppID, secretKey) {
|
|
@@ -23,9 +24,21 @@ export default class JhIm {
|
|
this.tim.setLogLevel(1);
|
|
this.tim.setLogLevel(1);
|
|
|
|
|
|
this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event=>{
|
|
this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event=>{
|
|
- event = event.data.map(v=>v);
|
|
|
|
- console.log(11111111111,event);
|
|
|
|
- this.onIMMessageReceived&&this.onIMMessageReceived(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);
|
|
|
|
|
|
this.tim.on(TIM.EVENT.SDK_READY, event=>this.onIMSDKReady&&this.onIMSDKReady(event), this);
|
|
this.tim.on(TIM.EVENT.SDK_READY, event=>this.onIMSDKReady&&this.onIMSDKReady(event), this);
|
|
@@ -68,11 +81,13 @@ export default class JhIm {
|
|
avatar: avatar,
|
|
avatar: avatar,
|
|
allowType: TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
|
|
allowType: TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
|
|
});
|
|
});
|
|
|
|
+ console.log(LOGTAG+'ready');
|
|
this.onIMReady&&this.onIMReady(event);
|
|
this.onIMReady&&this.onIMReady(event);
|
|
resolve();
|
|
resolve();
|
|
this.tim.off(TIM.EVENT.SDK_READY, cb, this);
|
|
this.tim.off(TIM.EVENT.SDK_READY, cb, this);
|
|
}
|
|
}
|
|
this.tim.on(TIM.EVENT.SDK_READY, cb, this);
|
|
this.tim.on(TIM.EVENT.SDK_READY, cb, this);
|
|
|
|
+ console.log(LOGTAG+'login');
|
|
this.tim.login({
|
|
this.tim.login({
|
|
userID: this.userId,
|
|
userID: this.userId,
|
|
userSig: this.userSig,
|
|
userSig: this.userSig,
|
|
@@ -85,10 +100,12 @@ export default class JhIm {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
createGroup(roomId) {
|
|
createGroup(roomId) {
|
|
- this.roomId = roomId;
|
|
|
|
|
|
+ console.log(LOGTAG+'search for createGroup', roomId);
|
|
|
|
+ this.roomId = String(roomId);
|
|
return this.tim.searchGroupByID(this.roomId).then(()=>{
|
|
return this.tim.searchGroupByID(this.roomId).then(()=>{
|
|
return this.joinGroup(roomId);
|
|
return this.joinGroup(roomId);
|
|
},()=>{
|
|
},()=>{
|
|
|
|
+ console.log(LOGTAG+'createGroup');
|
|
return this.tim.createGroup({
|
|
return this.tim.createGroup({
|
|
groupID: this.roomId,
|
|
groupID: this.roomId,
|
|
name: this.roomId,
|
|
name: this.roomId,
|
|
@@ -99,43 +116,36 @@ export default class JhIm {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
dismissGroup() {
|
|
dismissGroup() {
|
|
- return this.tim.dismissGroup(this._createGroup.roomId).then(() => {
|
|
|
|
|
|
+ console.log(LOGTAG+'dismissGroup');
|
|
|
|
+ return this.tim.dismissGroup(this.roomId).then(() => {
|
|
this.groupReady = false;
|
|
this.groupReady = false;
|
|
this.roomId = null;
|
|
this.roomId = null;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
joinGroup(roomId) {
|
|
joinGroup(roomId) {
|
|
- this.roomId = roomId;
|
|
|
|
|
|
+ console.log(LOGTAG+'joinGroup',roomId);
|
|
|
|
+ this.roomId = String(roomId);
|
|
return this.tim.joinGroup({
|
|
return this.tim.joinGroup({
|
|
groupID: this.roomId,
|
|
groupID: this.roomId,
|
|
type: TIM.TYPES.GRP_AVCHATROOM
|
|
type: TIM.TYPES.GRP_AVCHATROOM
|
|
}).then((imResponse) => {
|
|
}).then((imResponse) => {
|
|
|
|
+ console.log(LOGTAG+'joinGrouped',imResponse.data.status);
|
|
switch (imResponse.data.status) {
|
|
switch (imResponse.data.status) {
|
|
case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
|
|
case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
|
|
break;
|
|
break;
|
|
case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
|
|
case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
|
|
case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
|
|
case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
|
|
this.groupReady = true;
|
|
this.groupReady = true;
|
|
- this.sendGroupMessage({
|
|
|
|
- data: this.userId,
|
|
|
|
- description: 'userJoinin',
|
|
|
|
- extension: '',
|
|
|
|
- });
|
|
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
exitGroup() {
|
|
exitGroup() {
|
|
- return this.sendGroupMessage({
|
|
|
|
- data: this.userId,
|
|
|
|
- description: 'userJoinin',
|
|
|
|
- extension: '',
|
|
|
|
- }).then(()=>{
|
|
|
|
- return this.tim.quitGroup(this.roomId).then(()=>{
|
|
|
|
- this.groupReady = false;
|
|
|
|
- this.roomId = null;
|
|
|
|
- });
|
|
|
|
- })
|
|
|
|
|
|
+ console.log(LOGTAG+'exitGroup');
|
|
|
|
+ return this.tim.quitGroup(this.roomId).then(()=>{
|
|
|
|
+ this.groupReady = false;
|
|
|
|
+ this.roomId = null;
|
|
|
|
+ });
|
|
}
|
|
}
|
|
sendGroupText(text) {
|
|
sendGroupText(text) {
|
|
const message = this.tim.createTextMessage({
|
|
const message = this.tim.createTextMessage({
|
|
@@ -146,7 +156,7 @@ export default class JhIm {
|
|
return this.tim.sendMessage(message).then(()=>({
|
|
return this.tim.sendMessage(message).then(()=>({
|
|
name: this.name,
|
|
name: this.name,
|
|
avatar: this.avatar,
|
|
avatar: this.avatar,
|
|
- message: text,
|
|
|
|
|
|
+ text: text,
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
sendGroupMessage(payload) {
|
|
sendGroupMessage(payload) {
|