|
|
@@ -2,9 +2,10 @@
|
|
|
import {useAppStore} from "@/store";
|
|
|
import website from "@/config/index";
|
|
|
import userApi from '~api/user/index';
|
|
|
- import {getStorage, setStorage, delStorage} from "@/utils/storage";
|
|
|
- import {calcDate, getObjValue, isNullES, isString} from "js-fast-way";
|
|
|
- import {getWssApiUrl} from "@/config/envApi";
|
|
|
+ import {getStorage, setStorage} from "@/utils/storage";
|
|
|
+ import {calcDate, getObjValue, isNullES} from "js-fast-way";
|
|
|
+ import HcSocket from "@/httpApi/request/socket";
|
|
|
+
|
|
|
export default {
|
|
|
data() {
|
|
|
return {
|
|
|
@@ -12,7 +13,7 @@
|
|
|
}
|
|
|
},
|
|
|
onLaunch() {
|
|
|
- this.setInitSocket()
|
|
|
+ HcSocket.initSocket()
|
|
|
},
|
|
|
onShow() {
|
|
|
//console.log('App Show')
|
|
|
@@ -67,66 +68,6 @@
|
|
|
return Promise.reject(res)
|
|
|
}
|
|
|
},
|
|
|
- //长连接
|
|
|
- setInitSocket() {
|
|
|
- this.timeId = setInterval(() => {
|
|
|
- if (this.isSocket) {
|
|
|
- clearInterval(this.timeId)
|
|
|
- } else {
|
|
|
- this.setAppSocket()
|
|
|
- }
|
|
|
- console.log('setInterval')
|
|
|
- }, 3000)
|
|
|
- },
|
|
|
- setAppSocket() {
|
|
|
- const {user_id} = this.userInfo
|
|
|
- if (user_id) {
|
|
|
- this.initWebSocket(user_id)
|
|
|
- }
|
|
|
- },
|
|
|
- initWebSocket(user_id) {
|
|
|
- const _this = this, store = useAppStore()
|
|
|
- let startTime = 0;
|
|
|
- uni.connectSocket({
|
|
|
- url: getWssApiUrl() + user_id,
|
|
|
- complete: ()=> {
|
|
|
- startTime = new Date()
|
|
|
- _this.isSocket = true
|
|
|
- console.log('websocket链接成功')
|
|
|
- }
|
|
|
- });
|
|
|
- uni.onSocketOpen((res) => {
|
|
|
- console.log('WebSocket连接已打开!');
|
|
|
- _this.sendSocketMessage();
|
|
|
- });
|
|
|
- uni.onSocketError((res) => {
|
|
|
- console.log('WebSocket连接失败,请检查!');
|
|
|
- });
|
|
|
- uni.onSocketClose((res) => {
|
|
|
- console.log('WebSocket 已关闭!');
|
|
|
- //关闭后在连接
|
|
|
- const endTime = new Date()
|
|
|
- const duration = endTime - startTime
|
|
|
- if(duration > 1000) {
|
|
|
- _this.isSocket = false
|
|
|
- //_this.setAppSocket()
|
|
|
- }
|
|
|
- });
|
|
|
- //收到的消息
|
|
|
- uni.onSocketMessage(({data}) => {
|
|
|
- const countData = isString(data) ? JSON.parse(data) : {}
|
|
|
- if (!isNullES(countData)) {
|
|
|
- console.log(countData)
|
|
|
- store.setMsgCountData(countData)
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
- sendSocketMessage() {
|
|
|
- const store = useAppStore()
|
|
|
- uni.sendSocketMessage({
|
|
|
- data: store.projectId + ',' + store.contractId
|
|
|
- });
|
|
|
- },
|
|
|
}
|
|
|
}
|
|
|
</script>
|