SocketEvent.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // #ifndef APP-PLUS
  13. import store from "@/store/index.js";
  14. //#endif
  15. class SocketEvent {
  16. app = null;
  17. // #ifdef APP-PLUS-NVUE
  18. allApp = null;
  19. //#endif
  20. constructor(app) {
  21. this.app = app;
  22. // #ifdef APP-PLUS-NVUE
  23. this.allApp = getApp()
  24. //#endif
  25. }
  26. /**
  27. * 打开项目
  28. * @param {Object} res
  29. */
  30. onOpen(res) {
  31. uni.$emit('socketOpen',res);
  32. // #ifdef APP-PLUS-NVUE
  33. let user = allApp.$store.state.user;
  34. //#endif
  35. // #ifndef APP-PLUS-NVUE
  36. let user = store.state.user;
  37. //#endif
  38. if(user != null) {
  39. this.app.webSocket.send(new Message(user.token,"auth",true),
  40. (res)=>{
  41. let data = JSON.parse(res);
  42. if(data.type == 'auth_yes') {
  43. }
  44. if(data.type == 'auth_error') {
  45. //store.commit('loginOut');
  46. // uni.showToast({ 'title' : data.msg,duration : 3000,icon:'none'});
  47. }
  48. this.app.request.get("chatBagenum").then(res=>{
  49. if(res.code == 200) {
  50. // #ifdef APP-PLUS-NVUE
  51. allApp.$store.commit('setTxbagenum',res.data);
  52. //#endif
  53. // #ifndef APP-PLUS-NVUE
  54. store.commit('setTxbagenum',res.data);
  55. //#endif
  56. }
  57. }).catch((err)=>{
  58. });
  59. },()=>{
  60. console.log('测试失败了');
  61. });
  62. }
  63. }
  64. onMessage(res) {
  65. if(res.code == 'chat' || res.code == 'group_chat') {
  66. // #ifdef APP-PLUS-NVUE
  67. let bagenum = allApp.$store.state.txbagenum;
  68. //bagenum.count ++;
  69. bagenum.chatCount ++;
  70. allApp.$store.commit('setTxbagenum',bagenum);
  71. //#endif
  72. // #ifndef APP-PLUS-NVUE
  73. let bagenum = store.state.txbagenum;
  74. //bagenum.count ++;
  75. bagenum.chatCount ++;
  76. store.commit('setTxbagenum',bagenum);
  77. //#endif
  78. }
  79. //对方添加好友
  80. if(res.code == 'friend_add') {
  81. if(this.app.utils.isJSON(res.data)) {
  82. var data= JSON.parse(res.data);
  83. // #ifdef APP-PLUS-NVUE
  84. //消息提示
  85. let bagenumAr = allApp.$store.state.txbagenum;
  86. bagenumAr.applyCount ++;
  87. allApp.$store.commit('setTxbagenum',bagenumAr);
  88. //#endif
  89. // #ifndef APP-PLUS-NVUE
  90. //消息提示
  91. let bagenumAr = store.state.txbagenum;
  92. bagenumAr.applyCount ++;
  93. store.commit('setTxbagenum',bagenumAr);
  94. //#endif
  95. }
  96. }
  97. //申请进群
  98. if(res.code == 'group_add') {
  99. }
  100. uni.$emit('socketMessage',res);
  101. return '';
  102. }
  103. onError(res) {
  104. uni.$emit('socketClose',res);
  105. }
  106. onStop(res) {
  107. uni.$emit('socketClose',res);
  108. }
  109. onClose(res) {
  110. uni.$emit('socketClose',res);
  111. }
  112. onSendError(res){
  113. uni.$emit('socketSendError',res);
  114. }
  115. onSendSuccess(res) {
  116. uni.$emit('socketSendSuccess',res);
  117. }
  118. onHeartBeat(res){
  119. uni.$emit('socketHeartBeat',res);
  120. }
  121. }
  122. export default SocketEvent;