SocketEvent.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /// +----------------------------------------------------------------------
  2. // | HTTP 全局监听口[监听远程执行之后]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016-2017 ,Lioc All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // +----------------------------------------------------------------------
  8. // | Author: Mr Table <admin@hjf.pw>
  9. // +----------------------------------------------------------------------
  10. import md5 from "../vendor/md5.js";
  11. import Message from "../socket/Message.js";
  12. import utils from "@/library/utils/Comm.js";
  13. // #ifndef APP-PLUS
  14. import store from "@/store/index.js";
  15. //#endif
  16. import Request from "@/library/Request.js";
  17. class SocketEvent {
  18. app = null;
  19. constructor(app) {
  20. this.app = app;
  21. }
  22. /**
  23. * 打开项目
  24. * @param {Object} res
  25. */
  26. async onOpen(res) {
  27. console.log(res, 'res打开')
  28. uni.$emit('socketOpen', res);
  29. // #ifdef APP-PLUS
  30. const allApp = await utils.getAppStore()
  31. // console.log(allApp)
  32. let user = allApp.$store.state.user;
  33. //#endif
  34. // #ifndef APP-PLUS
  35. let user = store.state.user;
  36. //#endif
  37. if (user != null) {
  38. // #ifdef APP-PLUS
  39. const webSocket = allApp.globalData.webSocket;
  40. //#endif
  41. // #ifndef APP-PLUS
  42. const webSocket =this.app.webSocket
  43. //#endif
  44. webSocket.send(new Message(user.token, "auth", true),
  45. (res) => {
  46. let data = JSON.parse(res);
  47. if (data.type == 'auth_yes') {
  48. }
  49. if (data.type == 'auth_error') {
  50. //store.commit('loginOut');
  51. // uni.showToast({ 'title' : data.msg,duration : 3000,icon:'none'});
  52. }
  53. Request.get("chatBagenum").then(res => {
  54. if (res.code == 200) {
  55. // #ifdef APP-PLUS
  56. allApp.$store.commit('setTxbagenum', res.data);
  57. //#endif
  58. // #ifndef APP-PLUS
  59. store.commit('setTxbagenum', res.data);
  60. //#endif
  61. }
  62. }).catch((err) => {
  63. });
  64. }, () => {
  65. console.log('测试失败了');
  66. });
  67. }
  68. }
  69. async onMessage(res) {
  70. // #ifdef APP-PLUS
  71. const allApp = await utils.getAppStore()
  72. //#endif
  73. if (res.code == 'chat' || res.code == 'group_chat') {
  74. // #ifdef APP-PLUS
  75. let bagenum = allApp.$store.state.txbagenum;
  76. //bagenum.count ++;
  77. bagenum.chatCount++;
  78. allApp.$store.commit('setTxbagenum', bagenum);
  79. //#endif
  80. // #ifndef APP-PLUS
  81. let bagenum = store.state.txbagenum;
  82. //bagenum.count ++;
  83. bagenum.chatCount++;
  84. store.commit('setTxbagenum', bagenum);
  85. //#endif
  86. }
  87. //对方添加好友
  88. if (res.code == 'friend_add') {
  89. if (utils.isJSON(res.data)) {
  90. var data = JSON.parse(res.data);
  91. // #ifdef APP-PLUS
  92. //消息提示
  93. let bagenumAr = allApp.$store.state.txbagenum;
  94. bagenumAr.applyCount++;
  95. allApp.$store.commit('setTxbagenum', bagenumAr);
  96. //#endif
  97. // #ifndef APP-PLUS
  98. //消息提示
  99. let bagenumAr = store.state.txbagenum;
  100. bagenumAr.applyCount++;
  101. store.commit('setTxbagenum', bagenumAr);
  102. //#endif
  103. }
  104. }
  105. //申请进群
  106. if (res.code == 'group_add') {
  107. }
  108. uni.$emit('socketMessage', res);
  109. return '';
  110. }
  111. onError(res) {
  112. uni.$emit('socketClose', res);
  113. }
  114. onStop(res) {
  115. uni.$emit('socketClose', res);
  116. }
  117. onClose(res) {
  118. uni.$emit('socketClose', res);
  119. }
  120. onSendError(res) {
  121. uni.$emit('socketSendError', res);
  122. }
  123. onSendSuccess(res) {
  124. uni.$emit('socketSendSuccess', res);
  125. }
  126. onHeartBeat(res) {
  127. uni.$emit('socketHeartBeat', res);
  128. }
  129. }
  130. export default SocketEvent;