jhim.js 8.0 KB

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