jhim.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // #ifdef H5
  2. import './runtime'
  3. // #endif
  4. import TIM from './tim-wx';
  5. import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min";
  6. const EXPIRETIME = 604800;
  7. const LOGTAG = '--JHIM--:';
  8. class JhImGroup {
  9. constructor(jhim, groupId, groupType, name ,avatar) {
  10. this.jhim = jhim;
  11. this.groupId = groupId;
  12. this.groupType = groupType;
  13. this.name = name;
  14. this.avatar = avatar;
  15. this.groupReady = false;
  16. return this;
  17. }
  18. get ready() {
  19. return this.jhim.ready && this.groupReady;
  20. }
  21. join(isOwner) {
  22. console.log(LOGTAG + 'join', this.groupId, isOwner);
  23. let join = () => {
  24. console.log(LOGTAG + 'dojoin', this.groupId);
  25. return this.jhim.tim.joinGroup({
  26. groupID: this.groupId,
  27. type: TIM.TYPES.GRP_AVCHATROOM
  28. }).then(res => {
  29. console.log(LOGTAG + 'joinGrouped', res.data.status);
  30. switch (res.data.status) {
  31. case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
  32. break;
  33. case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
  34. case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
  35. this.groupReady = true;
  36. break;
  37. }
  38. });
  39. }
  40. return this.jhim.tim.searchGroupByID(this.groupId).then(() => {
  41. return join();
  42. }, () => {
  43. console.log(LOGTAG + 'createGroup');
  44. return this.jhim.tim.createGroup({
  45. groupID: this.groupId,
  46. name: this.groupId,
  47. type: this.groupType,
  48. }).then(() => {
  49. return join();
  50. });
  51. });
  52. }
  53. exit(dismiss) {
  54. console.log(LOGTAG + 'exit', this.groupId, dismiss);
  55. return this.jhim.tim.quitGroup(this.groupId).then(() => {
  56. this.groupReady = false;
  57. }).then(()=>{
  58. if(dismiss) {
  59. return this.jhim.tim.dismissGroup(this.groupId);;
  60. }else {
  61. return Promise.resolve();
  62. }
  63. });
  64. }
  65. getProfile() {
  66. return this.jhim.tim.getGroupProfile({
  67. groupID: this.groupId
  68. }).then(res => {
  69. return res.data.group;
  70. });
  71. // memberCount
  72. }
  73. on(cb, type) {
  74. if(!cb) return this;
  75. cb.jhimgroupcb = event => {
  76. let msgs = [];
  77. event.data.forEach(v => {
  78. if(v.conversationType==TIM.TYPES.CONV_GROUP && v.to==this.groupId && (!type || type==v.type)){
  79. msgs.push(v)
  80. }
  81. });
  82. if(msgs.length) cb(msgs);
  83. }
  84. this.jhim.tim.on(TIM.EVENT.MESSAGE_RECEIVED, cb.jhimgroupcb);
  85. return this;
  86. }
  87. off(cb) {
  88. if(!cb) return this;
  89. this.jhim.tim.off(TIM.EVENT.MESSAGE_RECEIVED, cb.jhimgroupcb);
  90. return this;
  91. }
  92. onReady(cb) {
  93. if(!cb) return this;
  94. if(this.jhim.ready) cb(true);//cb(this.ready);
  95. cb.jhimcbready = event => {
  96. cb(this.ready)
  97. }
  98. cb.jhimcbnotready = event => {
  99. cb(this.ready)
  100. }
  101. this.jhim.tim.on(TIM.EVENT.SDK_READY, cb.jhimcbready);
  102. this.jhim.tim.on(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
  103. return this;
  104. }
  105. offReady(cb) {
  106. if(!cb) return this;
  107. this.jhim.tim.off(TIM.EVENT.SDK_READY, cb.jhimcbready);
  108. this.jhim.tim.off(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
  109. return this;
  110. }
  111. sendText(text) {
  112. const message = this.jhim.tim.createTextMessage({
  113. to: this.groupId,
  114. conversationType: TIM.TYPES.CONV_GROUP,
  115. payload: {
  116. text
  117. },
  118. })
  119. message.nick = this.name;
  120. message.avatar = this.avatar;
  121. return this.jhim.tim.sendMessage(message).then(() => message);
  122. }
  123. sendCustomMessage(payload) {
  124. const message = this.tim.createCustomMessage({
  125. to: this.groupId,
  126. conversationType: TIM.TYPES.CONV_GROUP,
  127. payload,
  128. })
  129. message.nick = this.name;
  130. message.avatar = this.avatar;
  131. return this.tim.sendMessage(message).then(() => message);
  132. }
  133. }
  134. let instance = null;
  135. export default class JhIm {
  136. static getInstance(userId, sdkAppID, secretKey, userSig) {
  137. if (JhIm.instance) return JhIm.instance;
  138. return JhIm.instance = new JhIm(userId, sdkAppID, secretKey, userSig);
  139. }
  140. constructor(userId, sdkAppID, secretKey, userSig) {
  141. this.userId = userId;
  142. this.sdkAppID = sdkAppID;
  143. this.secretKey = secretKey;
  144. this.tim = TIM.create({
  145. SDKAppID: this.sdkAppID
  146. });
  147. const generator = new LibGenerateTestUserSig(this.sdkAppID, this.secretKey, EXPIRETIME);
  148. this.userSig = userSig || generator.genTestUserSig(this.userId);
  149. this.ready = false;
  150. this.roomId = null;
  151. this.groupReady = false;
  152. // 0 普通级别,日志量较多,接入时建议使用
  153. // 1 release级别,SDK 输出关键信息,生产环境时建议使用
  154. // 2 告警级别,SDK 只输出告警和错误级别的日志
  155. // 3 错误级别,SDK 只输出错误级别的日志
  156. // 4 无日志级别,SDK 将不打印任何日志
  157. this.tim.setLogLevel(3);
  158. this.TYPES = TIM.TYPES;
  159. this.EVENT = TIM.EVENT;
  160. this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event => {
  161. console.log(LOGTAG + 'recv', event);
  162. let msgs = [];
  163. event.data.forEach(v => {
  164. // https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html
  165. if (v.type == TIM.TYPES.MSG_TEXT) {
  166. msgs.push(v);
  167. msgs.push({
  168. type: v.type,
  169. name: v.nick,
  170. avatar: v.avatar,
  171. text: v.payload.text,
  172. conversationType: v.conversationType,
  173. time: v.time,
  174. })
  175. } else if (v.type == "TIMGroupTipElem") {
  176. this.onIMGroupTipElem && this.onIMGroupTipElem(v.payload);
  177. }
  178. })
  179. this.onIMMessageReceived && this.onIMMessageReceived(msgs);
  180. }, this);
  181. this.tim.on(TIM.EVENT.SDK_READY, event => {
  182. this.ready = true;
  183. }, this);
  184. this.tim.on(TIM.EVENT.SDK_NOT_READY, event => {
  185. this.ready = false;
  186. }, this);
  187. return this;
  188. }
  189. destory() {
  190. this.tim = null;
  191. }
  192. on(name, cb) {
  193. // 事件
  194. // https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/module-EVENT.html
  195. // 1. 收到消息 TIM.EVENT.MESSAGE_RECEIVED
  196. // https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html
  197. // -- type,
  198. // -- nick,
  199. // -- avatar,
  200. // -- payload
  201. // -- conversationType
  202. // -- time,
  203. // 2. 接入 TIM.EVENT.SDK_READY
  204. // 3. 掉线 TIM.EVENT.SDK_NOT_READY
  205. // 4. 收到被踢下线通知 TIM.EVENT.KICKED_OUT
  206. // TIM.EVENT.KICKED_OUT
  207. // 5. 收到 SDK 发生错误通知 TIM.EVENT.ERROR
  208. // - event.name - TIM.EVENT.ERROR
  209. // - event.data.code - 错误码
  210. // - event.data.message - 错误信息
  211. this.tim.on(name, cb);
  212. return this;
  213. }
  214. off(name, cb) {
  215. this.tim.off(name, cb);
  216. return this;
  217. }
  218. login(name, avatar) {
  219. if (this.ready) return Promise.resolve();
  220. console.log(LOGTAG + 'login');
  221. this.name = name;
  222. this.avatar = avatar;
  223. return new Promise(resolve => {
  224. let cb = event => {
  225. console.log(LOGTAG + 'ready');
  226. this.ready = true;
  227. this.tim.updateMyProfile({
  228. nick: name,
  229. avatar: avatar,
  230. allowType: TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
  231. });
  232. // this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event=>{
  233. // debugger
  234. // });
  235. resolve();
  236. this.tim.off(TIM.EVENT.SDK_READY, cb, this);
  237. }
  238. this.tim.on(TIM.EVENT.SDK_READY, cb, this);
  239. this.tim.login({
  240. userID: this.userId,
  241. userSig: this.userSig,
  242. })
  243. });
  244. }
  245. logout() {
  246. return this.tim.logout().then(() => {
  247. this.ready = false;
  248. });
  249. }
  250. onReady(cb) {
  251. if(!cb) return this;
  252. cb.jhimcbready = event => {
  253. this.ready = true;
  254. }
  255. cb.jhimcbnotready = event => {
  256. this.ready = false;
  257. }
  258. this.tim.on(TIM.EVENT.SDK_READY, cb.jhimcbready);
  259. this.tim.on(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
  260. }
  261. offReady(cb) {
  262. if(!cb) return this;
  263. this.tim.off(TIM.EVENT.SDK_READY, cb.jhimcbready);
  264. this.tim.off(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
  265. }
  266. createGroup(groupId, type=TIM.TYPES.GRP_AVCHATROOM) {
  267. return new JhImGroup(this, groupId, type, this.name ,this.avatar);
  268. }
  269. onMessageRecv(cb, from, type, conversationType=TIM.TIM.TYPES.CONV_C2C) {
  270. if(!cb) return;
  271. cb.jhimcb = event => {
  272. let msgs = event.data.filter( v=> {
  273. if(from && v.from != from) return false;
  274. if(conversationType && v.conversationType != conversationType) return false;
  275. if(type && v.type != type) return false;
  276. return true;
  277. });
  278. msgs.length && cb(msgs);
  279. }
  280. this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, cb.jhimcb);
  281. }
  282. offMessageRecv(cb) {
  283. if(!cb) return;
  284. this.time.off(TIM.EVENT.MESSAGE_RECEIVED,cb.jhimcb);
  285. }
  286. sendText(text, to) {
  287. const message = this.tim.createTextMessage({
  288. to,
  289. conversationType: TIM.TIM.TYPES.CONV_C2C,
  290. payload: {
  291. text
  292. },
  293. })
  294. message.nick = this.name;
  295. message.avatar = this.avatar;
  296. return this.tim.sendMessage(message).then(() => message);
  297. }
  298. sendCustomMessage(payload, to) {
  299. const message = this.tim.createCustomMessage({
  300. to: to,
  301. conversationType: TIM.TYPES.CONV_C2C,
  302. payload,
  303. })
  304. message.nick = this.name;
  305. message.avatar = this.avatar;
  306. return this.tim.sendMessage(message).then(() => message);
  307. }
  308. }