123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /// +----------------------------------------------------------------------
- // | HTTP 全局监听口[监听远程执行之后]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016-2017 ,Lioc All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: Mr Table <admin@hjf.pw>
- // +----------------------------------------------------------------------
- import md5 from "../vendor/md5.js";
- import Message from "../socket/Message.js";
- import utils from "@/library/utils/Comm.js";
- // #ifndef APP-PLUS
- import store from "@/store/index.js";
- //#endif
- import Request from "@/library/Request.js";
- class SocketEvent {
- app = null;
- constructor(app) {
- this.app = app;
- }
- /**
- * 打开项目
- * @param {Object} res
- */
- async onOpen(res) {
- console.log(res, 'res打开')
- uni.$emit('socketOpen', res);
- // #ifdef APP-PLUS
- const allApp = await utils.getAppStore()
- // console.log(allApp)
- let user = allApp.$store.state.user;
- //#endif
- // #ifndef APP-PLUS
- let user = store.state.user;
- //#endif
- if (user != null) {
- // #ifdef APP-PLUS
- const webSocket = allApp.globalData.webSocket;
- //#endif
- // #ifndef APP-PLUS
- const webSocket =this.app.webSocket
- //#endif
- webSocket.send(new Message(user.token, "auth", true),
- (res) => {
- let data = JSON.parse(res);
- if (data.type == 'auth_yes') {
- }
- if (data.type == 'auth_error') {
- //store.commit('loginOut');
- // uni.showToast({ 'title' : data.msg,duration : 3000,icon:'none'});
- }
- Request.get("chatBagenum").then(res => {
- if (res.code == 200) {
- // #ifdef APP-PLUS
- allApp.$store.commit('setTxbagenum', res.data);
- //#endif
- // #ifndef APP-PLUS
- store.commit('setTxbagenum', res.data);
- //#endif
- }
- }).catch((err) => {
- });
- }, () => {
- console.log('测试失败了');
- });
- }
- }
- async onMessage(res) {
- // #ifdef APP-PLUS
- const allApp = await utils.getAppStore()
- //#endif
- if (res.code == 'chat' || res.code == 'group_chat') {
- // #ifdef APP-PLUS
- let bagenum = allApp.$store.state.txbagenum;
- //bagenum.count ++;
- bagenum.chatCount++;
- allApp.$store.commit('setTxbagenum', bagenum);
- //#endif
- // #ifndef APP-PLUS
- let bagenum = store.state.txbagenum;
- //bagenum.count ++;
- bagenum.chatCount++;
- store.commit('setTxbagenum', bagenum);
- //#endif
- }
- //对方添加好友
- if (res.code == 'friend_add') {
- if (utils.isJSON(res.data)) {
- var data = JSON.parse(res.data);
- // #ifdef APP-PLUS
- //消息提示
- let bagenumAr = allApp.$store.state.txbagenum;
- bagenumAr.applyCount++;
- allApp.$store.commit('setTxbagenum', bagenumAr);
- //#endif
- // #ifndef APP-PLUS
- //消息提示
- let bagenumAr = store.state.txbagenum;
- bagenumAr.applyCount++;
- store.commit('setTxbagenum', bagenumAr);
- //#endif
- }
- }
- //申请进群
- if (res.code == 'group_add') {
- }
- uni.$emit('socketMessage', res);
- return '';
- }
- onError(res) {
- uni.$emit('socketClose', res);
- }
- onStop(res) {
- uni.$emit('socketClose', res);
- }
- onClose(res) {
- uni.$emit('socketClose', res);
- }
- onSendError(res) {
- uni.$emit('socketSendError', res);
- }
- onSendSuccess(res) {
- uni.$emit('socketSendSuccess', res);
- }
- onHeartBeat(res) {
- uni.$emit('socketHeartBeat', res);
- }
- }
- export default SocketEvent;
|