From d990ce5abb7f4e72d94c99d05565f08f3a34c72b Mon Sep 17 00:00:00 2001 From: zhangdaiscott Date: Wed, 28 Jul 2021 17:20:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9F=A5=E6=89=BE=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=EF=BC=9A'@/common/js-sdk/socket/socket.js'=20at=20pag?= =?UTF-8?q?es/home/home.vue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js-sdk/socket/socket.js | 110 ++++++++++++++++++++++++++ components/my-componets/my-select.vue | 17 ++-- 2 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 common/js-sdk/socket/socket.js diff --git a/common/js-sdk/socket/socket.js b/common/js-sdk/socket/socket.js new file mode 100644 index 0000000..cde6016 --- /dev/null +++ b/common/js-sdk/socket/socket.js @@ -0,0 +1,110 @@ +import configService from '@/common/service/config.service.js'; +import store from '@/store/index.js'; +class socket { + constructor(options) { + this.socketUrl = configService.apiUrl; + this.socketStart = false; + this.monitorSocketError(); + this.monitorSocketClose(); + this.socketReceive(); + } + init(socket_type,callback) { + const _this = this; + if (configService.apiUrl) { + if(this.socketStart){ + console.log('webSocket已经启动了'); + }else{ + let userid=store.state.userid?store.state.userid:store.getters.userid; + let url=this.socketUrl.replace("https://","wss://").replace("http://","ws://")+"/"+socket_type+"/"+userid+"_app"; + console.log("启动this.socketUrl连接地址:",url); + + uni.connectSocket({ + url: url, + method: 'GET' + }); + uni.onSocketOpen((res) => { + this.socketStart = true; + callback && callback(); + console.log('WebSocket连接已打开!'); + }); + /*setTimeout(() => { + _this.getHeartbeat(); + }, 5000);*/ + } + }else{ + console.log('config/baseUrl socketUrl为空'); + } + } + //Socket给服务器发送消息 + send(data, callback) { + const _this = this; + if (store.state.userid) { + data.userUid =store.state.userid; + } + console.log(data); + uni.sendSocketMessage({ + data: JSON.stringify(data), + success: () => { + callback && callback(true); + }, + fail: () => { + callback && callback(false); + } + }); + } + //Socket接收服务器发送过来的消息 + socketReceive() { + const _this = this; + uni.onSocketMessage(function(res) { + console.log("APP:----》收到服务器内容:",res); + let data = JSON.parse(res.data); + //console.log('收到服务器内容:', data); + _this.acceptMessage && _this.acceptMessage(data); + }); + } + //关闭Socket + closeSocket() { + const _this = this; + uni.closeSocket(); + _this.socketStart = false; + } + //监听Socket关闭 + monitorSocketClose() { + const _this = this; + uni.onSocketClose(function(res) { + console.log('WebSocket 已关闭!'); + _this.socketStart = false; + setTimeout(function() { + //_this.init(); + }, 3000); + }); + } + //监听Socket错误 + monitorSocketError() { + const _this = this; + uni.onSocketError(function(res) { + _this.socketStart = false; + console.log('WebSocket连接打开失败,请检查!'); + }); + } + //心跳 + getHeartbeat() { + const _this = this; + this.send({ + type: "心跳", + userUid: store.state.userid + }, (val) => { + setTimeout(() => { + if (val) { + //_this.getHeartbeat(); + } else { + if(!_this.socketStart){ + //_this.init(); + } + } + }, 10000); + }); + } +}; +const mySocket = new socket(); +export default mySocket; diff --git a/components/my-componets/my-select.vue b/components/my-componets/my-select.vue index a0e3c5a..201b5a7 100644 --- a/components/my-componets/my-select.vue +++ b/components/my-componets/my-select.vue @@ -42,14 +42,19 @@ initDictData() { //根据字典Code, 初始化字典数组 if (this.searchUrl){ - this.$api.get(this.searchUrl,{"code":this.dictCode}).then(res=>{ - this.dictOptions=res; - this.getIndex() + this.$http.get(this.searchUrl,{"code":this.dictCode}).then(res=>{ + if(res.data.success){ + this.dictOptions = res; + this.getIndex() + } }) }else{ - this.$api.getDict(this.dictCode).then(res => { - this.dictOptions = res; - this.getIndex() + let code = this.dictCode; + this.$http.get(`/sys/dict/getDictItems/${code}`).then(res=>{ + if(res.data.success){ + this.dictOptions = res.data.result; + this.getIndex() + } }) } },