jhim.js 5.0 KB

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