jhim.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import TIM from './tim-wx';
  2. import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min";
  3. const EXPIRETIME = 604800;
  4. const LOGTAG = '--JHIM--:';
  5. export default class JhIm {
  6. constructor(userId, sdkAppID, secretKey) {
  7. this.userId = userId;
  8. this.sdkAppID = sdkAppID;
  9. this.secretKey = secretKey;
  10. this.tim = TIM.create({ SDKAppID: this.sdkAppID });
  11. const generator = new LibGenerateTestUserSig(this.sdkAppID, this.secretKey, EXPIRETIME);
  12. this.userSig = generator.genTestUserSig(this.userId);
  13. this.roomId = null;
  14. this.logined = false;
  15. this.groupReady = false;
  16. // 0 普通级别,日志量较多,接入时建议使用
  17. // 1 release级别,SDK 输出关键信息,生产环境时建议使用
  18. // 2 告警级别,SDK 只输出告警和错误级别的日志
  19. // 3 错误级别,SDK 只输出错误级别的日志
  20. // 4 无日志级别,SDK 将不打印任何日志
  21. this.tim.setLogLevel(1);
  22. this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event=>{
  23. console.log(LOGTAG+'recv',event);
  24. let msgs = [];
  25. event.data.forEach(v=>{
  26. if(v.type == "TIMTextElem") {
  27. msgs.push({
  28. name: v.nick,
  29. avatar: v.avatar,
  30. text: v.payload.text
  31. })
  32. }else if(v.type == "TIMGroupTipElem") {
  33. this.onIMGroupTipElem&&this.onIMGroupTipElem(v.payload);
  34. }
  35. })
  36. this.onIMMessageReceived&&this.onIMMessageReceived(msgs);
  37. }, this);
  38. this.tim.on(TIM.EVENT.SDK_READY, event=>this.onIMSDKReady&&this.onIMSDKReady(event), this);
  39. this.tim.on(TIM.EVENT.SDK_NOT_READY, event=>{
  40. this.logined = false;
  41. this.onIMSDKNotReady&&this.onIMSDKNotReady();
  42. }, this);
  43. // 收到被踢下线通知
  44. // event.name - TIM.EVENT.KICKED_OUT
  45. // event.data.type - 被踢下线的原因,例如 :
  46. // - TIM.TYPES.KICKED_OUT_MULT_ACCOUNT 多实例登录被踢
  47. // - TIM.TYPES.KICKED_OUT_MULT_DEVICE 多终端登录被踢
  48. // - TIM.TYPES.KICKED_OUT_USERSIG_EXPIRED 签名过期被踢。使用前需要将SDK版本升级至v2.4.0或以上。
  49. this.tim.on(TIM.EVENT.KICKED_OUT, event=>this.onIMKickedOut&&this.onIMKickedOut(event), this);
  50. // 收到 SDK 发生错误通知,可以获取错误码和错误信息
  51. // event.name - TIM.EVENT.ERROR
  52. // event.data.code - 错误码
  53. // event.data.message - 错误信息
  54. this.tim.on(TIM.EVENT.ERROR, event=>this.onIMError&&this.onIMError(event), this);
  55. return this;
  56. }
  57. on(name, cb) {
  58. this[name] = cb;
  59. return this;
  60. }
  61. off(name) {
  62. this[name] = null;
  63. return this;
  64. }
  65. login(name, avatar) {
  66. if(this.logined) return Promise.resolve();
  67. this.name = name;
  68. this.avatar = avatar;
  69. return new Promise(resolve=>{
  70. let cb = event=>{
  71. this.logined = true;
  72. this.tim.updateMyProfile({
  73. nick: name,
  74. avatar: avatar,
  75. allowType: TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
  76. });
  77. console.log(LOGTAG+'ready');
  78. this.onIMReady&&this.onIMReady(event);
  79. resolve();
  80. this.tim.off(TIM.EVENT.SDK_READY, cb, this);
  81. }
  82. this.tim.on(TIM.EVENT.SDK_READY, cb, this);
  83. console.log(LOGTAG+'login');
  84. this.tim.login({
  85. userID: this.userId,
  86. userSig: this.userSig,
  87. })
  88. });
  89. }
  90. logout() {
  91. if(!this.logined) return Promise.resolve();
  92. return this.tim.logout().then(()=>{
  93. this.logined = false;
  94. });
  95. }
  96. createGroup(roomId) {
  97. console.log(LOGTAG+'search for createGroup', roomId);
  98. this.roomId = String(roomId);
  99. return this.tim.searchGroupByID(this.roomId).then(()=>{
  100. return this.joinGroup(roomId);
  101. },()=>{
  102. console.log(LOGTAG+'createGroup');
  103. return this.tim.createGroup({
  104. groupID: this.roomId,
  105. name: this.roomId,
  106. type: TIM.TYPES.GRP_AVCHATROOM,
  107. }).then(() => {
  108. return this.joinGroup(roomId);
  109. });
  110. });
  111. }
  112. dismissGroup() {
  113. console.log(LOGTAG+'dismissGroup');
  114. return this.tim.dismissGroup(this.roomId).then(() => {
  115. this.groupReady = false;
  116. this.roomId = null;
  117. });
  118. }
  119. joinGroup(roomId) {
  120. console.log(LOGTAG+'joinGroup',roomId);
  121. this.roomId = String(roomId);
  122. return this.tim.joinGroup({
  123. groupID: this.roomId,
  124. type: TIM.TYPES.GRP_AVCHATROOM
  125. }).then((imResponse) => {
  126. console.log(LOGTAG+'joinGrouped',imResponse.data.status);
  127. switch (imResponse.data.status) {
  128. case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
  129. break;
  130. case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
  131. case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
  132. this.groupReady = true;
  133. break;
  134. }
  135. });
  136. }
  137. exitGroup() {
  138. console.log(LOGTAG+'exitGroup');
  139. return this.tim.quitGroup(this.roomId).then(()=>{
  140. this.groupReady = false;
  141. this.roomId = null;
  142. });
  143. }
  144. sendGroupText(text) {
  145. const message = this.tim.createTextMessage({
  146. to: this.roomId,
  147. conversationType: TIM.TYPES.CONV_GROUP,
  148. payload: {text},
  149. })
  150. return this.tim.sendMessage(message).then(()=>({
  151. name: this.name,
  152. avatar: this.avatar,
  153. text: text,
  154. }));
  155. }
  156. sendGroupMessage(payload) {
  157. const message = this.tim.createCustomMessage({
  158. to: this.roomId,
  159. conversationType: TIM.TYPES.CONV_GROUP,
  160. payload,
  161. })
  162. return this.tim.sendMessage(message);
  163. }
  164. getGroupProfile() {
  165. return this.tim.getGroupProfile({ groupID: this.roomId }).then(res => imResponse.data.group);
  166. }
  167. getGroupNum() {
  168. return this.getGroupProfile(res=>res.memberCount);
  169. }
  170. }