webim.js 19 KB

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