修改websocket为全局只有一个

This commit is contained in:
2025-05-19 09:07:35 +08:00
parent ac28ee855f
commit c7e4e5e26b
8 changed files with 3780 additions and 269 deletions

View File

@@ -63,7 +63,8 @@
<view class="u-m-r-30 u-flex u-flex-y-center">
<template v-if="ordershopUserInfo.registerType=='before'">
<up-button shape="circle" @click="toJiacai" plain :custom-style="customStyle">
<up-button shape="circle" v-if="!listinfo.id" @click="toJiacai" plain
:custom-style="customStyle">
<view class="u-flex u-flex-y-center">
<image style="width: 40rpx;height: 40rpx;" src="@/static/icon/add-goods.png"
mode=""></image>
@@ -106,6 +107,9 @@
import {
back
} from '@/utils/uniapp.js'
import {
onLoad
} from '@dcloudio/uni-app'
function onback() {
closeSocket()
@@ -148,12 +152,11 @@
import {
useCartsStore
} from '@/stores/carts.js';
import useWebSocket from '@/common/js/carts-websocket.js';
import {
useWebSocket
} from '@/stores/carts-websocket.js';
const cartStore = useCartsStore()
console.log(cartStore.goodsIsloading);
console.log(cartStore.goodsMap);
async function onMessage(Message) {
console.log('onMessage');
cartStore.onMessage(Message, cartsSocket)
listinfo.totalPrices = historyTotalPrices * 1 + cartStore.totalPrice * 1
if (Message.operate_type == "init") {
@@ -176,7 +179,6 @@
}, 1000);
return;
}
console.log(res);
}
}
if (Message.operate_type == "del") {
@@ -192,34 +194,21 @@
.operate_type == 'product_update') {
youhuiReset()
}
return Message
}
let cartsSocket = null
const socketInitOptions = {
initMessage: {
type: 'shopping',
operate_type: 'init',
table_code: uni.cache.get('tableCode'),
shop_id: uni.cache.get('shopId'),
},
onMessage
cartStore.goodsInit()
const socketInitPar = {
type: 'shopping',
operate_type: 'init',
table_code: uni.cache.get('tableCode'),
shop_id: uni.cache.get('shopId'),
}
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();
}
function socketInit() {
cartsSocket = useWebSocket();
cartsSocket.connect(socketInitPar, onMessage);
}
const noPayStatus = {
cancelled: "订单已取消",
done: "订单已关闭",
@@ -233,7 +222,11 @@
listinfo.pointsDiscountAmount = 0
listinfo.Productroll = 0
uniqueIds.value = []
orderInfoAfterRef.value.IntegralInputclose();
try {
orderInfoAfterRef.value?.IntegralInputclose();
} catch {
}
}
@@ -308,18 +301,30 @@
let historyTotalPrices = 0;
// * 获取订单详情接口
const orderorderInfo = async () => {
console.log('listinfo.id', listinfo.id);
let res = listinfo.id ? await APIgetOrderById({
orderId: listinfo.id
}) : await APIhistoryOrder({
tableCode: options.tableCode
})
if (options.tableCode) {
if (res && res.tableCode) {
socketInitPar.table_code = res.tableCode
let tableRes = await getTableInfo({
tableCode: options.tableCode
})
console.log(tableRes);
listinfo.tableName = tableRes.name;
} else {
if (options.tableCode) {
socketInitPar.table_code = options.tableCode
let tableRes = await getTableInfo({
tableCode: options.tableCode
})
console.log(tableRes);
listinfo.tableName = tableRes.name;
}
}
socketInit()
console.log(res);
getOrderInfoAfterCalcInit(res)
}
@@ -393,12 +398,9 @@
listinfo.originAmount = Math.round(sum * 100) / 100;
// 打包费packFeess 计算购物车商品费用totalPrices 餐位费Seatcharge 商品卷Productroll 优惠卷coupondiscountAmount 积分listinfo.pointsDiscountAmount
console.log('listinfo');
console.log(listinfo);
let sums = nowCartPackFee + historyOrderPackFee + listinfo.totalPrices + seatFee - (listinfo
.Productroll || 0) - (listinfo.coupondiscountAmount || 0) - (listinfo
.pointsDiscountAmount || 0);
console.log('listinfo', listinfo);
listinfo.totalCost = Math.round(sums * 100) / 100;
// 总价格
@@ -571,51 +573,92 @@
}
function isPayBefor() {
return ordershopUserInfo.value.registerType == 'before' ? true : false
}
const istoricalorders = async () => {
// 购物车有数据
if (!cartStore.isEmpty) {
await createOrder()
goToPay()
}
// 先调用历史订单
let APIhistoryOrderres = await APIhistoryOrder({
tableCode: listinfo.tableCode
function pay_lock() {
cartsSocket.sendMessage({
...socketInitPar,
operate_type: 'pay_lock'
})
// 判断是否有历史订单
if (APIhistoryOrderres.detailMap) {
let combinedArray = [];
for (const key in APIhistoryOrderres.detailMap) {
if (APIhistoryOrderres.detailMap.hasOwnProperty(key)) {
let subArray = APIhistoryOrderres.detailMap[key];
combinedArray = [...combinedArray, ...subArray]
}
}
// 判断支付之前是是否还有新加订单
if (listinfo.combinedArray.length === combinedArray.length) {
}
function pay_unlock() {
clearInterval(payStatusTimer)
cartsSocket.sendMessage({
...socketInitPar,
operate_type: 'pay_unlock'
})
}
async function search_pay_lock() {
cartsSocket.sendMessage({
...socketInitPar,
operate_type: 'search_pay_lock'
})
return new Promise((relove, reject) => {
cartsSocket.socketTask.onMessage(res => {
relove(JSON.parse(res.data))
})
})
}
let payStatusTimer = null
let orderIsLock = true;
const istoricalorders = async () => {
//先付
if (isPayBefor()) {
console.log('isPayBefor');
// 购物车有数据
if (!cartStore.isEmpty) {
const res = await createOrder()
goToPay()
}
} else {
//后付
// 先调用历史订单
let APIhistoryOrderres = await APIhistoryOrder({
tableCode: listinfo.tableCode
})
// 判断是否有历史订单
if (APIhistoryOrderres.detailMap) {
let combinedArray = [];
for (const key in APIhistoryOrderres.detailMap) {
if (APIhistoryOrderres.detailMap.hasOwnProperty(key)) {
let subArray = APIhistoryOrderres.detailMap[key];
combinedArray = [...combinedArray, ...subArray]
}
}
// 判断支付之前是是否还有新加订单
if (listinfo.combinedArray.length === combinedArray.length) {
goToPay()
} else {
uni.showToast({
title: '你的订单已更新!',
icon: 'none'
})
orderorderInfo()
}
} else {
uni.showToast({
title: '你的订单已更新!',
title: '你的小伙伴已提交订单~',
icon: 'none'
})
orderorderInfo()
setTimeout(() => {
orderorderInfo()
}, 1000)
}
} else {
uni.showToast({
title: '你的小伙伴已提交订单~',
icon: 'none'
})
setTimeout(() => {
orderorderInfo()
}, 1000)
}
}
// * 去支付
const goToPay = async () => {
const goToPay = async (isCreateOrder = false) => {
// 余额支付
if (paymentmethod.payType == 'accountPay') {
if (orderVIP.value.isVip == 0) {
@@ -659,6 +702,23 @@
}
return false
}
const canPayRes = await search_pay_lock()
console.log('canPayRes', canPayRes);
const canPay = canPayRes.status == 1 ? true : false;
console.log('canPay', canPay);
if (!canPay) {
uni.showToast({
title: '有人正在付款中!',
icon: 'none'
})
return
}
pay_lock()
payStatusTimer = setInterval(() => {
pay_lock()
}, 1000)
if (orderVIP.value.freeDineConfig.enable && isBwc.value) {
await storeMemberpay.actionspayltPayVip({
shopId: orderVIP.value.shopId,
@@ -670,6 +730,7 @@
buyerRemark: '',
seatNum: is_type.value == 0 ? listinfo.seatNum : 0, //用餐人数
})
orderorderInfo()
return false;
} else {
@@ -700,9 +761,11 @@
returnUrl: ''
})
} catch (error) {
pay_unlock()
//TODO handle the exception
}
}
pay_unlock()
orderorderInfo()
}
@@ -712,7 +775,7 @@
shopUserId: orderVIP.value.id,
orderAmount: listinfo.totalCost,
})
orderInfoAfterRef.value.getCalcUsablePoints(res)
orderInfoAfterRef.value?.getCalcUsablePoints(res)
}
//
const clickPointsamount = (Pointsamount) => {
@@ -760,13 +823,16 @@
onBeforeUnmount(() => {
uni.$off('returnData', handleReturnData);
clearTimeout(backtimer)
clearInterval(payStatusTimer)
closeSocket()
});
onHide(() => {
closeSocket()
clearInterval(payStatusTimer)
})
function closeSocket() {
// cartsSocket.closeExistingConnection()
}
@@ -777,10 +843,9 @@
try {
// 更新数据
if (orderVIP.value) {
console.log(uni.cache.get('orderVIP'))
orderVIP.value = uni.cache.get('orderVIP')
paymentMethodref.value.orderVIPfun(uni.cache.get('orderVIP'))
if (listinfo.id) {
if (listinfo.id && ordershopUserInfo.value.registerType != 'before') {
orderorderInfo()
}
}
@@ -789,28 +854,7 @@
}
});
let options = {}
// 监听页面返回事件
onMounted(async () => {
// 获取当前页面栈
const pages = getCurrentPages();
// 获取当前页面实例
const currentPage = pages[pages.length - 1];
// 获取页面参数
options = currentPage.options;
listinfo.id = options.orderId;
if (options.shopId) {
// 每次进来全局更新shopId
uni.cache.set('shopId', options.shopId, 30)
uni.$on('returnData', handleReturnData);
}
// * 获取会员信息
await storeuser.actionsproductqueryProduct()
await nextTick()
orderVIP.value = uni.cache.get('orderVIP')
// 积分信息
orderorderInfo()
})
// 跳转到加菜页面
function toJiacai() {
back()
@@ -845,12 +889,41 @@
'cancelled': '已取消'
}
const navTitle = computed(() => {
if (!listinfo.id && !cartStore.isEmpty) {
return '待确认'
}
if (cartStore.isEmpty && listinfo.id) {
return payStatus[listinfo.status] || '待确认'
} else {
return '待确认'
}
})
async function init(opt) {
Object.assign(options, opt)
listinfo.id = options.orderId;
console.log('init', listinfo);
if (options.shopId) {
// 每次进来全局更新shopId
uni.cache.set('shopId', options.shopId, 30)
uni.$on('returnData', handleReturnData);
}
// * 获取会员信息
await storeuser.actionsproductqueryProduct()
await nextTick()
orderVIP.value = uni.cache.get('orderVIP')
if (options.tableCode) {
socketInitPar.table_code = options.tableCode
socketInit()
} else {
orderorderInfo()
}
}
onLoad((opt) => {
init(opt)
})
</script>
<style lang="scss">
@@ -1270,6 +1343,7 @@
bottom: 0;
left: 0;
width: 100%;
z-index: 999;
// height: 80rpx;
.flex-between {

View File

@@ -218,6 +218,14 @@
}
const orderinfo = (e) => {
if(e.status=='unpaid'){
uni.pro.navigateTo('order/confirm-order', {
orderId: e.id,
shopId: e.shopId,
})
return
}
uni.pro.navigateTo('order/detail', {
orderId: e.id,
shopId: e.shopId