webim.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. import packetCode from './PacketCodeC.js'
  2. import store from './store/index.js'
  3. import SKIMSDK from './websocket_sdk.js'
  4. const EventDispatcher = function() {
  5. this.listeners = {}
  6. }
  7. let eventDispatcher
  8. const WEBIM = {
  9. resource: "web",
  10. /*单聊标识*/
  11. CHAT: "chat",
  12. /*群聊标识*/
  13. GROUPCHAT: "groupchat",
  14. token: null,
  15. userId: null,
  16. nickName: "",
  17. isReadDel: 0,
  18. /*用户 jid 10004541/web */
  19. userIdStr: null,
  20. /*服务器连接地址 ws://localhost:5260 */
  21. serverUrl: null,
  22. server: null,
  23. /*消息超时 时间 默认 15 秒*/
  24. sendTimeOut: 15,
  25. /*等待消息回执的 消息Id 数组*/
  26. waitReceiptMessageIds: {},
  27. heartCheck: false,
  28. isReconnection: false,
  29. options: null,
  30. /*初始化*/
  31. initWebIM: function(serverUrl, heartCheck, isReconnection) {
  32. WEBIM.heartCheck = heartCheck;
  33. WEBIM.isReconnection = isReconnection;
  34. WEBIM.serverUrl = serverUrl;
  35. WEBIM.options = {
  36. url: serverUrl,
  37. success(res) {},
  38. fail() {}
  39. }
  40. eventDispatcher = new EventDispatcher();
  41. WEBIM.server = new SKIMSDK({
  42. heartCheck: heartCheck,
  43. isReconnection: isReconnection,
  44. });
  45. WEBIM.server.initWebSocket(WEBIM.options);
  46. WEBIM.server.onReceivedMsg(event => {
  47. let packet = packetCode.decode(event.data);
  48. let command = packet.command;
  49. eventDispatcher.dispatchEvent(command, toJSON(packet))
  50. eventDispatcher.removeListener(command, toJSON(packet))
  51. let name = 'pushRes';
  52. let value = packet;
  53. if (command === -10) {
  54. store.commit('$uStore', {
  55. name,
  56. value
  57. });
  58. }
  59. });
  60. WEBIM.server.onNetworkChange(WEBIM.options);
  61. WEBIM.server.onSocketClosed(WEBIM.options)
  62. },
  63. disconnect: function(e) {
  64. WEBIM.server.closeWebSocket()
  65. },
  66. isConnect: function() {
  67. return WEBIM.server._isLogin;
  68. },
  69. login: (username, password, code, func) => {
  70. let requestPacket = {
  71. username,
  72. password,
  73. code: null,
  74. version: 1,
  75. command: 1
  76. }
  77. send(requestPacket)
  78. eventDispatcher.addListener('2', func)
  79. },
  80. register: (phone, password, nickname, func) => {
  81. let p = {
  82. phone,
  83. password,
  84. nickname,
  85. version:1,
  86. command:49
  87. }
  88. send(p)
  89. eventDispatcher.addListener('50', func)
  90. },
  91. heartTest: (userId, func) => {
  92. let packet = {
  93. userId,
  94. version: 1,
  95. command: 17
  96. }
  97. send(packet)
  98. eventDispatcher.addListener('18', func)
  99. },
  100. send2Group: (toGroupId, userId, message, msgType, func) => {
  101. let requestPacket = {
  102. toGroupId,
  103. userId,
  104. msgType,
  105. message,
  106. version: 1,
  107. command: 15
  108. }
  109. send(requestPacket)
  110. eventDispatcher.addListener('16', func)
  111. },
  112. send2Friend: (toUserId, userId, message, msgType, func) => {
  113. let requestPacket = {
  114. userId,
  115. toUserId,
  116. msgType,
  117. message,
  118. version: 1,
  119. command: 3
  120. }
  121. send(requestPacket)
  122. eventDispatcher.addListener('4', func)
  123. },
  124. logout: (userId, func) => {
  125. let requestPacket = {
  126. userId,
  127. version: 1,
  128. command: 5
  129. }
  130. send(requestPacket)
  131. eventDispatcher.addListener('6', func)
  132. },
  133. createGroup: (userIdList, defaultGroupName, userId, func) => {
  134. let requestPacket = {
  135. userId,
  136. userIdList,
  137. defaultGroupName,
  138. version: 1,
  139. command: 7
  140. }
  141. send(requestPacket)
  142. eventDispatcher.addListener('8', func)
  143. },
  144. queryMembers: (groupId, userId, func) => {
  145. let requestPacket = {
  146. groupId,
  147. userId,
  148. version: 1,
  149. command: 9
  150. }
  151. send(requestPacket)
  152. eventDispatcher.addListener('10', func)
  153. },
  154. getLastMessage: (id, chatId, userId, chatType, func) => {
  155. let requestPacket = {
  156. id: id,
  157. chatId,
  158. userId,
  159. chatType,
  160. version: 1,
  161. command: 45
  162. }
  163. send(requestPacket)
  164. eventDispatcher.addListener('46', func)
  165. },
  166. joinGroup: (groupId, ids, userName, func) => {
  167. let requestPacket = {
  168. groupId,
  169. userIds: ids,
  170. currentUsername: userName,
  171. version: 1,
  172. command: 11
  173. }
  174. send(requestPacket)
  175. eventDispatcher.addListener('12', func)
  176. },
  177. delGroupMember: (groupId, ids, func) => {
  178. let requestPacket = {
  179. groupId,
  180. userIds: ids,
  181. version: 1,
  182. command: 13
  183. }
  184. send(requestPacket)
  185. eventDispatcher.addListener('14', func)
  186. },
  187. transferGroup: (groupId, userId, func) => {
  188. let requestPacket = {
  189. groupId,
  190. userId,
  191. version: 1,
  192. command: 53
  193. }
  194. send(requestPacket)
  195. eventDispatcher.addListener('54', func)
  196. },
  197. getGroups: (condition, userId, func) => {
  198. let requestPacket = {
  199. condition,
  200. userId,
  201. version: 1,
  202. command: 25
  203. }
  204. send(requestPacket)
  205. eventDispatcher.addListener('26', func)
  206. },
  207. listGuests: (userId, func) => {
  208. let packet = {
  209. userId,
  210. version: 1,
  211. command: 47
  212. }
  213. send(packet)
  214. eventDispatcher.addListener('48', func)
  215. },
  216. queryFriendMessages: (toUserId, userId, pageNum, func) => {
  217. let requestPacket = {
  218. userId,
  219. pageNum,
  220. toUserId,
  221. pageSize: 10,
  222. version: 1,
  223. command: 27
  224. }
  225. send(requestPacket)
  226. eventDispatcher.addListener('28', func)
  227. },
  228. getFriendMessageByCondition: (toUserId, userId, pageNum, condition, func) => {
  229. let requestPacket = {
  230. userId,
  231. condition,
  232. pageNum,
  233. toUserId,
  234. pageSize: 5,
  235. version: 1,
  236. command: 27
  237. }
  238. send(requestPacket)
  239. eventDispatcher.addListener('28', func)
  240. },
  241. queryGroupMessages: (toGroupId, userId, pageNum, func) => {
  242. let requestPacket = {
  243. toGroupId,
  244. pageNum,
  245. pageSize: 10,
  246. userId,
  247. version: 1,
  248. command: 23
  249. }
  250. send(requestPacket)
  251. eventDispatcher.addListener('24', func)
  252. },
  253. getGroupMessageByCondition: (toGroupId, userId, pageNum, condition, func) => {
  254. let requestPacket = {
  255. toGroupId,
  256. pageNum,
  257. condition,
  258. pageSize: 5,
  259. userId,
  260. version: 1,
  261. command: 23
  262. }
  263. send(requestPacket)
  264. eventDispatcher.addListener('24', func)
  265. },
  266. queryChats: (condition, userId, func) => {
  267. let requestPacket = {
  268. condition,
  269. userId,
  270. pageNum: -1,
  271. pageSize: -1,
  272. version: 1,
  273. command: 19
  274. }
  275. send(requestPacket)
  276. eventDispatcher.addListener('20', func)
  277. },
  278. updateNickName: (userId, nickName, func) => {
  279. let requestPacket = {
  280. userId,
  281. nickName,
  282. version: 1,
  283. command: 79
  284. }
  285. send(requestPacket)
  286. eventDispatcher.addListener('80', func)
  287. },
  288. getBusinessCard: (userId, func) => {
  289. let requestPacket = {
  290. userId,
  291. version: 1,
  292. command: 65
  293. }
  294. send(requestPacket)
  295. eventDispatcher.addListener('66', func)
  296. },
  297. createChatList: (userId, groupId, message, msgType, func) => {
  298. let requestPacket = {
  299. userId,
  300. groupId,
  301. msgType,
  302. message,
  303. version: 1,
  304. command: 61
  305. }
  306. send(requestPacket)
  307. eventDispatcher.addListener('62', func)
  308. },
  309. randomSmsCode: (phone, func) => {
  310. let req = {
  311. userId: '5f6d9d98',
  312. phone,
  313. version: 1,
  314. command: 63
  315. }
  316. send(req)
  317. eventDispatcher.addListener('64', func)
  318. },
  319. getCustomerServiceList: (userId, func) => {
  320. let requestPacket = {
  321. userId,
  322. version: 1,
  323. command: 93
  324. }
  325. send(requestPacket)
  326. eventDispatcher.addListener('94', func)
  327. },
  328. deleteEmontion: (userId, motionId, func) => {
  329. let requestPacket = {
  330. userId,
  331. motionId,
  332. version: 1,
  333. command: 95
  334. }
  335. send(requestPacket)
  336. eventDispatcher.addListener('96', func)
  337. },
  338. stopAudio: (userId, chatId, chatType, func) => {
  339. let requestPacket = {
  340. userId,
  341. chatId,
  342. chatType,
  343. method: 21,
  344. version: 1,
  345. command: 21
  346. }
  347. send(requestPacket)
  348. eventDispatcher.addListener('22', func)
  349. },
  350. queryGroupUser: (userId, groupId, func) => {
  351. let requestPacket = {
  352. userId,
  353. groupId,
  354. method: 17,
  355. version: 1,
  356. command: 21
  357. }
  358. send(requestPacket)
  359. eventDispatcher.addListener('22', func)
  360. },
  361. getUserRemark: (userId, friendUserId, func) => {
  362. let requestPacket = {
  363. friendUserId,
  364. userId,
  365. method: 10,
  366. version: 1,
  367. command: 21
  368. }
  369. send(requestPacket)
  370. eventDispatcher.addListener('22', func)
  371. },
  372. updateAvatar: (userId, avatar, func) => {
  373. let requestPacket = {
  374. userId,
  375. avatar,
  376. version: 1,
  377. command: 97
  378. }
  379. send(requestPacket)
  380. eventDispatcher.addListener('98', func)
  381. },
  382. getUserById: (userId, func) => {
  383. let requestPacket = {
  384. userId,
  385. version: 1,
  386. command: 65
  387. }
  388. send(requestPacket)
  389. eventDispatcher.addListener('66', func)
  390. },
  391. deleteGroupMsg: (userId, msgId, groupId, func) => {
  392. let requestPacket = {
  393. userId,
  394. msgId,
  395. groupId,
  396. method: 5,
  397. version: 1,
  398. command: 81
  399. }
  400. send(requestPacket)
  401. eventDispatcher.addListener('82', func)
  402. },
  403. deleteFriendMsg: (userId, msgId, friendId, func) => {
  404. let requestPacket = {
  405. userId,
  406. msgId,
  407. friendId,
  408. version: 1,
  409. command: 73
  410. }
  411. send(requestPacket)
  412. eventDispatcher.addListener('74', func)
  413. },
  414. updateRemarkName: (userId, friendUserId, remarkName, func) => {
  415. let requestPacket = {
  416. userId,
  417. friendUserId,
  418. remarkName,
  419. method: 7,
  420. version: 1,
  421. command: 21
  422. }
  423. send(requestPacket)
  424. eventDispatcher.addListener('22', func)
  425. },
  426. updatePassword: (userId, username, password, func) => {
  427. let requestPacket = {
  428. userId,
  429. username,
  430. password,
  431. version: 1,
  432. command: 77
  433. }
  434. send(requestPacket)
  435. eventDispatcher.addListener('78', func)
  436. },
  437. uploadContact: (name, phone, userId, func) => {
  438. let requestPacket = {
  439. name,
  440. phone,
  441. userId,
  442. method: 9,
  443. version: 1,
  444. command: 21
  445. }
  446. send(requestPacket)
  447. eventDispatcher.addListener('22', func)
  448. },
  449. freshCode: (func) => {
  450. let requestPacket = {
  451. method: 4,
  452. version: 1,
  453. command: 21
  454. }
  455. send(requestPacket)
  456. eventDispatcher.addListener('22', func)
  457. },
  458. delChat: (userId, chatId, func) => {
  459. let requestPacket = {
  460. userId,
  461. chatId,
  462. version: 1,
  463. command: 83
  464. }
  465. send(requestPacket)
  466. eventDispatcher.addListener('84', func)
  467. },
  468. updateGroupNick: (userId, groupId, nickName, func) => {
  469. let requestPacket = {
  470. userId,
  471. groupId,
  472. nickName,
  473. version: 1,
  474. command: 79
  475. }
  476. send(requestPacket)
  477. eventDispatcher.addListener('80', func)
  478. },
  479. attend: (chat, userId, func) => {
  480. let requestPacket = {
  481. userId,
  482. chat,
  483. version: 1,
  484. command: 75
  485. }
  486. send(requestPacket)
  487. eventDispatcher.addListener('76', func)
  488. },
  489. queryMsgByChatId: (msgId, groupId, userId, func) => {
  490. let requestPacket = {
  491. userId,
  492. msgId,
  493. groupId,
  494. version: 1,
  495. command: 69
  496. }
  497. send(requestPacket)
  498. eventDispatcher.addListener('70', func)
  499. },
  500. addEmoticon(userId, avatar, func) {
  501. let requestPacket = {
  502. userId,
  503. avatar,
  504. version: 1,
  505. command: 71
  506. }
  507. send(requestPacket)
  508. eventDispatcher.addListener('72', func)
  509. },
  510. getEmoticons(userId, func) {
  511. let requestPacket = {
  512. userId,
  513. version: 1,
  514. command: 85
  515. }
  516. send(requestPacket)
  517. eventDispatcher.addListener('86', func)
  518. },
  519. updateGroupName: (userId, groupId, groupName, func) => {
  520. let requestPacket = {
  521. userId,
  522. groupId,
  523. groupName,
  524. version: 1,
  525. command: 91
  526. }
  527. send(requestPacket)
  528. eventDispatcher.addListener('92', func)
  529. },
  530. clearGroupMsg: (userId, groupId, func) => {
  531. let requestPacket = {
  532. userId,
  533. groupId,
  534. version: 1,
  535. command: 87
  536. }
  537. send(requestPacket)
  538. eventDispatcher.addListener('88', func)
  539. },
  540. openChat: (chatId, userId, chatType, func) => {
  541. let requestPacket = {
  542. userId,
  543. chatId,
  544. chatType,
  545. version: 1,
  546. command: 39
  547. }
  548. send(requestPacket)
  549. eventDispatcher.addListener('40', func)
  550. },
  551. getMoneys(userId, pageNum, func) {
  552. let requestPacket = {
  553. pageNum,
  554. pageSiz: 5,
  555. userId,
  556. version: 1,
  557. command: 41
  558. }
  559. send(requestPacket)
  560. eventDispatcher.addListener('42', func)
  561. },
  562. queryNotice(userId, groupId, func) {
  563. let requestPacket = {
  564. userId,
  565. groupId,
  566. action: 'query',
  567. version: 1,
  568. command: 43
  569. }
  570. send(requestPacket)
  571. eventDispatcher.addListener('44', func)
  572. },
  573. updateNotice(userId, groupId, context, func) {
  574. let requestPacket = {
  575. userId,
  576. groupId,
  577. action: 'update',
  578. context,
  579. version: 1,
  580. command: 43
  581. }
  582. send(requestPacket)
  583. eventDispatcher.addListener('44', func)
  584. },
  585. removeGroupUser(userIds, groupId, func) {
  586. let requestPacket = {
  587. userIds,
  588. groupId,
  589. version: 1,
  590. command: 51
  591. }
  592. send(requestPacket)
  593. eventDispatcher.addListener('52', func)
  594. },
  595. insertFriend(userId, friendId, func) {
  596. let requestPacket = {
  597. userId,
  598. friendId,
  599. version: 1,
  600. command: 55
  601. }
  602. send(requestPacket)
  603. eventDispatcher.addListener('56', func)
  604. },
  605. // 查找用户
  606. findFriendRequestList: (nickName, func) => {
  607. let requestPacket = {
  608. nickName,
  609. version: 1,
  610. command: 89
  611. }
  612. send(requestPacket)
  613. eventDispatcher.addListener('90', func)
  614. },
  615. // 查看新朋友列表
  616. queryFriendRequestList(userId, func) {
  617. let requestPacket = {
  618. userId,
  619. version: 1,
  620. command: 57
  621. }
  622. send(requestPacket)
  623. eventDispatcher.addListener('58', func)
  624. },
  625. // 同意好友请求
  626. AcceptFriendRequest(friendId, userId, func) {
  627. let requestPacket = {
  628. friendId,
  629. userId,
  630. version: 1,
  631. command: 59
  632. }
  633. send(requestPacket)
  634. eventDispatcher.addListener('60', func)
  635. },
  636. queryPostsReq: (userId, pageNum, func) => {
  637. let requestPacket = {
  638. pageNum,
  639. pageSize: 10,
  640. userId,
  641. version: 1,
  642. command: 106
  643. }
  644. send(requestPacket)
  645. eventDispatcher.addListener('107', func)
  646. },
  647. createPostReq: (userId, postContext, urls, func) => {
  648. let requestPacket = {
  649. postContext,
  650. urls,
  651. pageSize: 10,
  652. userId,
  653. version: 1,
  654. command: 102
  655. }
  656. send(requestPacket)
  657. eventDispatcher.addListener('103', func)
  658. },
  659. toFabulousRes: (id, userId, postId, func) => {
  660. let requestPacket = {
  661. id,
  662. postId,
  663. pageSize: 10,
  664. userId,
  665. version: 1,
  666. command: 108
  667. }
  668. send(requestPacket)
  669. eventDispatcher.addListener('109', func)
  670. },
  671. toCommentReqPacket: (id, userId, postId, comment, func) => {
  672. let requestPacket = {
  673. id,
  674. postId,
  675. comment,
  676. pageSize: 10,
  677. userId,
  678. version: 1,
  679. command: 104
  680. }
  681. send(requestPacket)
  682. eventDispatcher.addListener('105', func)
  683. },
  684. getLinks: (userId, func) => {
  685. let packet = {
  686. userId,
  687. link: {
  688. title:'看一看'
  689. },
  690. version: 1,
  691. command: 110
  692. }
  693. send(packet)
  694. eventDispatcher.addListener('111', func)
  695. },
  696. createMessage: (message, msgType, toGroupId, func) => {
  697. let p ={
  698. toGroupId,
  699. msgType,
  700. message,
  701. version: 1,
  702. command: 120
  703. }
  704. send(p)
  705. eventDispatcher.addListener('121', func)
  706. },
  707. robRedPacket: (func) =>{
  708. let p = {
  709. version: 1,
  710. command:118
  711. }
  712. send(p)
  713. eventDispatcher.addListener('119', func)
  714. },
  715. createRedPacket: (func) =>{
  716. let p ={
  717. version: 1,
  718. command:116
  719. }
  720. send(p)
  721. eventDispatcher.addListener('117', func)
  722. },
  723. addFaceUser: (userId, faceId, func)=> {
  724. let p = {
  725. userId,
  726. faceId,
  727. version: 1,
  728. command: 121
  729. }
  730. send(p)
  731. eventDispatcher.addListener('122', func)
  732. },
  733. listFaces: (keyword, func)=> {
  734. let p = {
  735. keyword,
  736. version: 1,
  737. command: 123
  738. }
  739. send(p)
  740. eventDispatcher.addListener('124', func)
  741. },
  742. listBanner:(func)=>{
  743. let p = {
  744. version: 1,
  745. command: 125
  746. }
  747. send(p)
  748. eventDispatcher.addListener('126', func)
  749. },
  750. joinRoom:(groupIds, func) =>{
  751. let p={
  752. groupIds,
  753. version: 1,
  754. command: -1
  755. }
  756. send(p)
  757. eventDispatcher.addListener('-2', func)
  758. },
  759. quitRoom:(groupIds, func) =>{
  760. let p={
  761. groupIds,
  762. version: 1,
  763. command: -3
  764. }
  765. send(p)
  766. eventDispatcher.addListener('-4', func)
  767. },
  768. queryOnlineMessage(userId, func){
  769. let p={
  770. userId,
  771. version: 1,
  772. command: -7
  773. }
  774. send(p)
  775. eventDispatcher.addListener('-6', func)
  776. }
  777. }
  778. function toJSON(packet) {
  779. return JSON.parse(JSON.stringify(packet))
  780. }
  781. EventDispatcher.prototype.addListener = function(eventKey, fun, context) {
  782. let list = []
  783. this.listeners[eventKey] = list
  784. let listener = {
  785. func: fun,
  786. context: context
  787. }
  788. list.push(listener)
  789. return listener
  790. }
  791. EventDispatcher.prototype.removeListener = function(eventKey, fun, context) {
  792. let list = this.listeners[eventKey]
  793. if (list !== undefined) {
  794. let size = list.length
  795. for (let i = 0; i < size; i++) {
  796. let listener = list[i]
  797. if (listener.func === fun && listener.context === context) {
  798. list.splice(i, 1)
  799. return
  800. }
  801. }
  802. }
  803. }
  804. EventDispatcher.prototype.dispatchEvent = function(eventKey, event) {
  805. let list = this.listeners[eventKey]
  806. if (list !== undefined) {
  807. let size = list.length
  808. for (let i = 0; i < size; i++) {
  809. let listener = list[i]
  810. let fun = listener.func
  811. let context = listener.context
  812. if (context != null) {
  813. fun.call(context, event)
  814. } else {
  815. fun(event)
  816. }
  817. }
  818. }
  819. }
  820. let send = (p) => {
  821. p.token = store.state.userData.token;
  822. WEBIM.server.sendWebSocketMsg({
  823. data: p,
  824. success(res) {
  825. //console.log('【websocket】发送成功')
  826. },
  827. fail(err) {
  828. console.log('【websocket】发送失败')
  829. console.log(err)
  830. }
  831. });
  832. }
  833. export default WEBIM