webim.js 19 KB

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