This commit is contained in:
魏啾
2024-06-07 17:21:52 +08:00
parent c8633827cc
commit 5a16a5199e
2 changed files with 166 additions and 192 deletions

View File

@@ -24,9 +24,8 @@ class webSocketUtils {
} }
// 进入这个页面的时候创建websocket连接【整个页面随时使用】 // 进入这个页面的时候创建websocket连接【整个页面随时使用】
connectSocketInit(data) { connectSocketInit(data) {
console.log(data,"初始化")
this.data = data; this.data = data;
console.log('this.url==', this.url); this.socketTask ? this.socketTask.Close() : null
this.socketTask = uni.connectSocket({ this.socketTask = uni.connectSocket({
url: this.url, url: this.url,
success: () => { success: () => {
@@ -36,7 +35,6 @@ class webSocketUtils {
return this.socketTask; return this.socketTask;
}, },
}); });
console.log('this.params==', this.params);
this.socketTask.onOpen((res) => { this.socketTask.onOpen((res) => {
this.connectNum = 1; this.connectNum = 1;
console.log('WebSocket连接正常'); console.log('WebSocket连接正常');
@@ -138,7 +136,7 @@ class webSocketUtils {
//如果不是人为关闭的话,进行重连 //如果不是人为关闭的话,进行重连
if (!this.is_open_socket) { if (!this.is_open_socket) {
console.log('进行重连'); console.log('进行重连');
// this.canReconnect = true; this.canReconnect = true;
this.reconnectTimeOut = setInterval(() => { this.reconnectTimeOut = setInterval(() => {
this.connectSocketInit(this.data); this.connectSocketInit(this.data);
}, this.timeout); }, this.timeout);

View File

@@ -283,42 +283,32 @@
uni.reLaunch({ uni.reLaunch({
url: '/pages/login/login?types=' + 0 url: '/pages/login/login?types=' + 0
}); });
return false
} }
uni.$on('message', this.getMessage) uni.$on('message', this.getMessage)
setTimeout(() => {
if (uni.cache.get('token') && uni.cache.get('tableCode')) {
this.productqueryShopIdByTableCode() //获取shop User id
}
}, 500)
this.$nextTick(() => { this.$nextTick(() => {
this.countTitleTopNum(); //导航栏 this.countTitleTopNum(); //导航栏
}); });
}, },
onUnload() { onUnload() {
try {
this.socketSendMsg({ //定义socket数据传参 this.socketSendMsg({ //定义socket数据传参
"type": "close", //“addcart:添加购物车create0rder:生成订单clearCart:庆康购物车”, "type": "close", //“addcart:添加购物车create0rder:生成订单clearCart:庆康购物车”,
}) })
console.log(this.socketTicket, '关闭长连接') } catch (e) {
//TODO handle the exception
}
this.socketTicket.Close() this.socketTicket.Close()
uni.$off('message') uni.$off('message')
}, },
onShow() { onShow() {
// console.log(this.socketTicket,'进入页面')
// if (this.socketTicket) {
// this.socketTicket.Close()
// uni.$off('message')
// }
// uni.onNetworkStatusChange((res) => { //监听网络状态变化
// if (!res.isConnected) {
// // 检查网络是否连接
// } else {
// console.log(res, 111);
// // ...这里写你的业务逻辑
// }
// });
// setTimeout()
this.handlemessage()
setTimeout(() => {
console.log('调试1')
if (uni.cache.get('token') && uni.cache.get('tableCode')) {
this.productqueryShopIdByTableCode() //获取shop User id
}
}, 500)
}, },
methods: { methods: {
// 单独获取他的shopUserid // 单独获取他的shopUserid
@@ -337,16 +327,6 @@
uni.pro.switchTab('index/index') uni.pro.switchTab('index/index')
}, 1000) }, 1000)
} }
// try {
// if (res.data) {
// let time = new Date
// console.log(time, '时间戳')
// uni.cache.set('shopUser', res.data)
// this.handlemessage()
// }
// } catch (e) {
// //TODO handle the exception
// }
}, },
getMessage(msg) { //wss 回显数据 getMessage(msg) { //wss 回显数据
if (msg.status != 'success') { if (msg.status != 'success') {
@@ -432,12 +412,7 @@
}, },
handlemessage() { handlemessage() {
this.socketTicket ? this.socketTicket.Close() : null //调用前先判断是否有socket正在进行 先关闭后链接 this.socketTicket ? this.socketTicket.Close() : null //调用前先判断是否有socket正在进行 先关闭后链接
// this.socketTicket = new webSocketUtils( this.socketTicket = new webSocketUtils(`${uni.conf.baseUrlwws}`, 5000, {
// `${uni.conf.baseUrlwws}/websocket/table?tableId=${uni.cache.get('tableCode')}&shopId=${uni.cache.get('shopUser')}&userId=${uni.cache.get('userInfo').id}`,
// 5000)
this.socketTicket = new webSocketUtils(
`${uni.conf.baseUrlwws}`,
5000, {
tableId: uni.cache.get('tableCode'), tableId: uni.cache.get('tableCode'),
shopId: uni.cache.get('shopUser'), shopId: uni.cache.get('shopUser'),
userId: uni.cache.get('userInfo').id, userId: uni.cache.get('userInfo').id,
@@ -665,6 +640,7 @@
switch (t) { switch (t) {
case 1: case 1:
// 返回 // 返回
this.socketTicket.Close()
uni.switchTab({ uni.switchTab({
url: '/pages/index/index' url: '/pages/index/index'
}) })