修复切换页面nan问题

This commit is contained in:
YeMingfei666 2025-05-08 16:24:39 +08:00
parent 1f59082bcb
commit ac28ee855f
6 changed files with 115 additions and 69 deletions

View File

@ -72,7 +72,11 @@ const useWebSocket = (options = {}) => {
} }
// 关闭现有连接并清理资源 // 关闭现有连接并清理资源
closeExistingConnection(); try{
closeExistingConnection();
}catch(err){
}
socketTask.value = uni.connectSocket({ socketTask.value = uni.connectSocket({
url: uni.conf.baseUrlwws, url: uni.conf.baseUrlwws,
success: (res) => { success: (res) => {
@ -353,7 +357,7 @@ const useWebSocket = (options = {}) => {
connect, connect,
onShowconnect, onShowconnect,
initNetworkListener, initNetworkListener,
connect connect,allowReconnect
}; };
}; };

View File

@ -97,7 +97,7 @@
</view> </view>
<view class="total-wrap" v-if="listinfo.status == 'unpaid'||!listinfo.id"> <view class="total-wrap u-m-t-30" v-if="listinfo.status == 'unpaid'||!listinfo.id">
<view>总计</view> <view>总计</view>
<view class="price"> {{listinfo.totalCost||''}} </view> <view class="price"> {{listinfo.totalCost||''}} </view>
</view> </view>

View File

@ -1,6 +1,6 @@
<template> <template>
<view class="container"> <view class="container">
<up-navbar :title="navTitle" @leftClick="back" placeholder></up-navbar> <up-navbar :title="navTitle" @leftClick="onback" placeholder></up-navbar>
<view class="headStatus"> <view class="headStatus">
<view class="status"> <view class="status">
@ -106,6 +106,11 @@
import { import {
back back
} from '@/utils/uniapp.js' } from '@/utils/uniapp.js'
function onback() {
closeSocket()
back()
}
const customStyle = { const customStyle = {
'width': '180rpx', 'width': '180rpx',
'height': '70rpx', 'height': '70rpx',
@ -121,7 +126,9 @@
onUnmounted, onUnmounted,
watchEffect, watchEffect,
nextTick, nextTick,
watch watch,
onBeforeMount,
onBeforeUnmount
} from 'vue'; } from 'vue';
import { import {
getTableInfo getTableInfo
@ -143,26 +150,8 @@
} from '@/stores/carts.js'; } from '@/stores/carts.js';
import useWebSocket from '@/common/js/carts-websocket.js'; import useWebSocket from '@/common/js/carts-websocket.js';
const cartStore = useCartsStore() const cartStore = useCartsStore()
let cartsSocket = null console.log(cartStore.goodsIsloading);
watch(() => cartStore.goodsIsloading, (newValue) => { console.log(cartStore.goodsMap);
console.log('cartStore.goodsIsloading', cartStore.goodsIsloading);
if (!newValue) {
cartsSocket = useWebSocket(socketInitOptions);
cartsSocket.connect();
}
})
if (cartStore.goodsIsloading) {
cartStore.goodsInit()
} else {
cartsSocket = useWebSocket(socketInitOptions);
cartsSocket.connect();
}
const noPayStatus = {
cancelled: "订单已取消",
done: "订单已关闭",
};
async function onMessage(Message) { async function onMessage(Message) {
console.log('onMessage'); console.log('onMessage');
cartStore.onMessage(Message, cartsSocket) cartStore.onMessage(Message, cartsSocket)
@ -171,6 +160,7 @@
youhuiReset() youhuiReset()
} }
if (Message.operate_type == "cleanup") { if (Message.operate_type == "cleanup") {
listinfo.totalCost = 0;
youhuiReset() youhuiReset()
if (listinfo.id) { if (listinfo.id) {
let res = await APIgetOrderById({ let res = await APIgetOrderById({
@ -203,6 +193,38 @@
youhuiReset() youhuiReset()
} }
} }
let cartsSocket = null
const socketInitOptions = {
initMessage: {
type: 'shopping',
operate_type: 'init',
table_code: uni.cache.get('tableCode'),
shop_id: uni.cache.get('shopId'),
},
onMessage
}
watch(() => cartStore.goodsIsloading, (newValue) => {
console.log('cartStore.goodsIsloading', cartStore.goodsIsloading);
if (!newValue) {
cartsSocket = useWebSocket(socketInitOptions);
cartsSocket.allowReconnect = true;
cartsSocket.connect();
}
})
if (cartStore.goodsIsloading) {
cartStore.goodsInit()
} else {
cartsSocket = useWebSocket(socketInitOptions);
cartsSocket.allowReconnect = true;
cartsSocket.connect();
}
const noPayStatus = {
cancelled: "订单已取消",
done: "订单已关闭",
};
function youhuiReset() { function youhuiReset() {
@ -213,15 +235,7 @@
uniqueIds.value = [] uniqueIds.value = []
orderInfoAfterRef.value.IntegralInputclose(); orderInfoAfterRef.value.IntegralInputclose();
} }
const socketInitOptions = {
initMessage: {
type: 'shopping',
operate_type: 'init',
table_code: uni.cache.get('tableCode'),
shop_id: uni.cache.get('shopId'),
},
onMessage
}
// //
import { import {
@ -743,19 +757,19 @@
orderorderInfo() orderorderInfo()
} }
onUnmounted(() => { onBeforeUnmount(() => {
uni.$off('returnData', handleReturnData); uni.$off('returnData', handleReturnData);
clearTimeout(backtimer) clearTimeout(backtimer)
if (cartsSocket) { closeSocket()
cartsSocket.closeExistingConnection()
}
}); });
onHide(() => { onHide(() => {
if (cartsSocket) { closeSocket()
cartsSocket.closeExistingConnection()
}
}) })
function closeSocket() {
// cartsSocket.closeExistingConnection()
}
onShow(() => { onShow(() => {
if (cartsSocket) { if (cartsSocket) {
cartsSocket.connect() cartsSocket.connect()

View File

@ -471,7 +471,8 @@
} from "@/common/api/order/index.js"; } from "@/common/api/order/index.js";
// websocket // websocket
import useWebSocket from '@/common/js/websocket.js'; // import useWebSocket from '@/common/js/websocket.js';
import useWebSocket from '@/common/js/carts-websocket.js';
// pinia // pinia
import { import {

View File

@ -440,6 +440,7 @@ export const useCartsStore = defineStore('cart',
carts, carts,
isEmpty, isEmpty,
setGoodsMap, setGoodsMap,
goodsMap,
goodsIsloading, goodsIsloading,
goodsInit, goodsInit,
onMessage, onMessage,

View File

@ -9,7 +9,8 @@ import {
APIuser APIuser
} from '@/common/api/api.js' } from '@/common/api/api.js'
import { import {
APIproductqueryShop,APIusershopInfodetail, APIproductqueryShop,
APIusershopInfodetail,
APIshopUserInfo APIshopUserInfo
} from '@/common/api/member.js' } from '@/common/api/member.js'
export const Storelogin = defineStore('login', { export const Storelogin = defineStore('login', {
@ -17,11 +18,13 @@ export const Storelogin = defineStore('login', {
token: '', token: '',
miniAppOpenId: '', miniAppOpenId: '',
userInfo: '', userInfo: '',
shopInfo:{} shopInfo: {}
}), }),
actions: { actions: {
async getShopInfo(shopId){ async getShopInfo(shopId) {
const shopRes=await APIusershopInfodetail({shopId}) const shopRes = await APIusershopInfodetail({
shopId
})
console.log(shopRes); console.log(shopRes);
}, },
actionslogin() { actionslogin() {
@ -104,15 +107,15 @@ export const productStore = defineStore('product', {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
if (q) { if (q) {
console.log(q) console.log(q)
let tableCode ="" let tableCode = ""
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
tableCode = this.getQueryString(decodeURIComponent(q), 'code') tableCode = this.getQueryString(decodeURIComponent(q), 'code')
// #endif // #endif
// #ifdef MP-ALIPAY // #ifdef MP-ALIPAY
tableCode = q tableCode = q
// #endif // #endif
// #ifdef H5 // #ifdef H5
tableCode= q.tableCode tableCode = q.tableCode
// #endif // #endif
console.log(tableCode); console.log(tableCode);
// 储存卓玛 // 储存卓玛
@ -120,6 +123,8 @@ export const productStore = defineStore('product', {
if (tableCode) { if (tableCode) {
console.log(uni.cache.get('tableCode')); console.log(uni.cache.get('tableCode'));
let data = await this.actionsproductqueryShop(tableCode) let data = await this.actionsproductqueryShop(tableCode)
console.log('data', data)
// -4请求登录 // -4请求登录
if (data.code == '500') { if (data.code == '500') {
if (await this.actionslogin()) { if (await this.actionslogin()) {
@ -149,6 +154,13 @@ export const productStore = defineStore('product', {
uni.cache.set('tableCode', tableCode) uni.cache.set('tableCode', tableCode)
if (tableCode) { if (tableCode) {
let data = await this.actionsproductqueryShop() let data = await this.actionsproductqueryShop()
if (!data) {
uni.showToast({
title: '店铺已过期或其他问题,请联系商家',
icon:'none'
})
return
}
// -4请求登录 // -4请求登录
const store = Storelogin() const store = Storelogin()
if (data.code == '-4') { if (data.code == '-4') {
@ -176,7 +188,7 @@ export const productStore = defineStore('product', {
}); });
// #endif // #endif
} }
}) })
}, },
@ -184,21 +196,33 @@ export const productStore = defineStore('product', {
actionsproductqueryShop(tableCode) { actionsproductqueryShop(tableCode) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
// try { // try {
try {
let res = await APIproductqueryShop({ let res = await APIproductqueryShop({
tableCode: tableCode?tableCode:uni.cache.get('tableCode'), tableCode: tableCode ? tableCode : uni.cache.get('tableCode'),
}) })
res.shopInfo.isVip = res.vip ? '1' : '0' if (res) {
res.shopTable.shopExtendMap = res.shopExtendMap res.shopInfo.isVip = res.vip ? '1' : '0'
// 店铺信息 res.shopTable.shopExtendMap = res.shopExtendMap
uni.cache.set('shopTable', res.shopTable) // 店铺信息
// 台桌信息 uni.cache.set('shopTable', res.shopTable)
uni.cache.set('shopInfo', res.shopInfo) // 台桌信息
uni.cache.set('shopId', res.shopTable.shopId, 30) uni.cache.set('shopInfo', res.shopInfo)
// 当前用户距离店铺的米数 uni.cache.set('shopId', res.shopTable.shopId, 30)
uni.cache.set('distance', res.distance) // 当前用户距离店铺的米数
uni.cache.set('distance', res.distance)
resolve(res)
} else {
console.log(res)
reject()
}
} catch (error) {
console.log(error);
reject()
//TODO handle the exception
}
resolve(res)
// } catch (e) { // } catch (e) {
// reject(false) // reject(false)
// } // }
@ -214,7 +238,7 @@ export const productStore = defineStore('product', {
uni.cache.set('shopUserInfo', res); uni.cache.set('shopUserInfo', res);
uni.cache.set('orderVIP', res) uni.cache.set('orderVIP', res)
uni.cache.set('ordershopUserInfo', res.shopInfo) uni.cache.set('ordershopUserInfo', res.shopInfo)
resolve(true) resolve(res)
} catch (e) { } catch (e) {
reject(false) reject(false)
} }
@ -226,14 +250,16 @@ export const productStore = defineStore('product', {
actionsAPIuser() { actionsAPIuser() {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
let res = null
// 获取店铺用户会员信息 // 获取店铺用户会员信息
if (uni.cache.get('shopId')) { if (uni.cache.get('shopId')) {
this.actionsproductqueryProduct() res = await this.actionsproductqueryProduct()
} else { } else {
let res = await APIuser() res = await APIuser()
uni.cache.set('userInfo', res); uni.cache.set('userInfo', res);
} }
resolve(true) console.log('actionsAPIuser res', res);
resolve(res)
} catch (e) { } catch (e) {
reject(false) reject(false)
} }