SocketEvent.js 2.4 KB

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