问题修复

This commit is contained in:
2025-12-05 19:22:28 +08:00
parent 2c2a87ab19
commit 7fdabf1507
10 changed files with 442 additions and 345 deletions

View File

@@ -1,157 +1,241 @@
import {
reactive,
ref
} from 'vue';
import { reactive, ref } from 'vue';
// WebSocket 工具类,封装了 WebSocket 的连接、发送、接收、心跳、重连和关闭等操作
class WebsocketUtil {
// 构造函数,初始化 WebSocket 连接
constructor(url, time, params) {
this.url = url; // WebSocket 服务器的 URL
this.params = params; // WebSocket 服务器的 URL
this.time = time; // 心跳发送的间隔时间(秒)
this.socketTask = null; // WebSocket 任务对象
this.isOpen = false; // WebSocket 连接是否打开
this.reconnectTimeout = null; // 重连定时器
this.heartbeatInterval = null; // 心跳定时器
this.messageCallbacks = []; // 存储外部注册的消息回调函数的数组
this.messageCallbacks = []; // 存储外部注册的消息回调函数的数组
// 构造函数,初始化 WebSocket 连接
constructor(url, time, params) {
this.url = url; // WebSocket 服务器的 URL
this.params = params; // WebSocket 连接参数
this.time = time; // 心跳发送的间隔时间(秒)
this.socketTask = null; // WebSocket 任务对象
this.isOpen = false; // WebSocket 连接是否打开
// 初始化 WebSocket 连接
this.initializeWebSocket();
}
// 定时器相关
this.reconnectTimeout = null; // 重连定时器
this.heartbeatInterval = null; // 心跳定时器
this.heartbeatTimeout = null; // 心跳超时定时器(检测 pong 响应)
this.checkConnectionInterval = null; // 连接状态主动检查定时器
// 初始化 WebSocket 连接
initializeWebSocket() {
console.log('初始化WebSocket连接');
if (this.isOpen) {
return
}
this.socketTask = uni.connectSocket({
url: this.url,
success: () => {
console.log('WebSocket连接成功');
return this.socketTask;
},
fail: (error) => {
console.error('WebSocket连接失败', error);
uni.$emit('is-socket-open', true)
this.reconnect();
}
});
// 消息回调数组
this.messageCallbacks = []; // 存储外部注册的消息回调函数的数组
this.socketTask.onOpen((res) => {
console.log('WebSocket连接正常==', res);
this.isOpen = true;
// 连接成功后启动心跳和消息监听
this.startHeartbeat();
this.listenForMessages();
uni.$emit('is-socket-open', true)
// 重连策略配置
this.reconnectAttempts = 0; // 当前重连次数
this.reconnectMaxAttempts = 5; // 最大重连次数(设为 Infinity 表示无限重试)
this.reconnectDelay = 3000; // 基础重连延迟(毫秒)
this.maxReconnectDelay = 30000; // 最大重连延迟(毫秒)
});
this.socketTask.onError((res) => {
console.log('WebSocket连接失败==', res);
uni.$emit('is-socket-open', false)
this.reconnect();
});
// 注意:这里的 onClose 监听器应该放在 uni.connectSocket 调用之后
this.socketTask.onClose((result) => {
this.isOpen = false;
// if( this.isOpen ){
this.reconnect();
// }
});
// 初始化 WebSocket 连接
this.initializeWebSocket();
}
// 监听设备唤醒事件(浏览器环境)
if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
// 页面可见时,主动检查连接
this.checkConnection();
}
});
}
}
// 启动心跳检测
startHeartbeat() {
if (this.heartbeatInterval) {
clearInterval(this.heartbeatInterval);
}
this.heartbeatInterval = setInterval(() => {
if (this.isOpen) {
this.send(JSON.stringify({"type": "ping_interval","set": "pad"}));
}
}, this.time);
}
// 初始化 WebSocket 连接
initializeWebSocket() {
console.log('初始化WebSocket连接');
if (this.isOpen) {
return;
}
// 发送消息
send(data, type) {
if (this.socketTask && this.isOpen) {
this.socketTask.send({
data: data,
success: (res) => {
// console.log('消息发送成功', res);
this.startHeartbeat();
},
fail: (error) => {
console.error('消息发送失败', error);
this.reconnect(); // 这里可能需要根据实际情况判断是否重连
}
});
}
}
this.socketTask = uni.connectSocket({
url: this.url,
success: () => {
console.log('WebSocket连接请求发送成功');
},
fail: (error) => {
console.error('WebSocket连接失败', error);
uni.$emit('is-socket-open', false);
this.reconnect();
}
});
// 监听 WebSocket 消息
listenForMessages() {
if (this.socketTask) {
this.socketTask.onMessage((res) => {
const {
data
} = res;
this.messageCallbacks.forEach(callback => callback(data
.toString())); // 假设 data 是字符串或可转换为字符串
});
// this.send("WebSocket连接正常");
} else {
console.error('WebSocket 连接尚未建立,无法监听消息');
}
}
// 连接打开事件
this.socketTask.onOpen((res) => {
console.log('WebSocket连接正常==', res);
this.isOpen = true;
// 重置重连状态
this.reconnectAttempts = 0;
this.reconnectDelay = 3000;
// 启动心跳和消息监听
this.startHeartbeat();
this.listenForMessages();
uni.$emit('is-socket-open', true);
});
// 重连 WebSocket
reconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
}
this.reconnectTimeout = setTimeout(() => {
this.initializeWebSocket();
}, 3000);
}
// 连接错误事件
this.socketTask.onError((res) => {
console.log('WebSocket连接错误==', res);
uni.$emit('is-socket-open', false);
this.reconnect();
});
// 关闭 WebSocket 连接
closeSocket() {
if (this.socketTask) {
uni.closeSocket({
success: () => {
console.log('WebSocket连接已关闭');
this.isOpen = false;
},
fail: (error) => {
console.error('关闭WebSocket连接失败', error);
}
});
this.socketTask = null;
}
}
// 连接关闭事件
this.socketTask.onClose((result) => {
console.log('WebSocket连接已关闭', result);
this.isOpen = false;
this.reconnect();
});
// 外部注册消息回调函数
onMessage(callback) {
this.messageCallbacks.push(callback);
}
// 启动连接状态主动检查30秒一次
this.startConnectionCheck();
}
// 外部注销消息回调函数
offMessage(callback) {
this.messageCallbacks = []
}
// 启动心跳检测
startHeartbeat() {
// 清除现有定时器
if (this.heartbeatInterval) clearInterval(this.heartbeatInterval);
if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
// 销毁 WebSocket 连接,清理资源
destroy() {
this.closeSocket();
clearInterval(this.heartbeatInterval);
clearTimeout(this.reconnectTimeout);
this.messageCallbacks = [];
}
this.heartbeatInterval = setInterval(() => {
if (this.isOpen) {
// 发送心跳包
this.send(JSON.stringify({ "type": "ping_interval", "set": "pad" }));
// 设置5秒超时若未收到pong则重连
this.heartbeatTimeout = setTimeout(() => {
console.log('心跳超时,主动断开并重连');
this.closeSocket();
this.reconnect();
}, 5000);
}
}, this.time);
}
// 启动连接状态主动检查30秒一次
startConnectionCheck() {
if (this.checkConnectionInterval) clearInterval(this.checkConnectionInterval);
this.checkConnectionInterval = setInterval(() => {
if (!this.isOpen && !this.reconnectTimeout) {
console.log('主动检查到连接断开,触发重连');
this.reconnect();
}
}, 30000);
}
// 发送消息
send(data, type) {
if (this.socketTask && this.isOpen) {
this.socketTask.send({
data: data,
success: (res) => {
// 发送成功,重置心跳(可选,根据业务需求)
this.startHeartbeat();
},
fail: (error) => {
console.error('消息发送失败', error);
this.reconnect();
}
});
} else {
console.warn('WebSocket未连接无法发送消息');
}
}
// 监听 WebSocket 消息
listenForMessages() {
if (this.socketTask) {
this.socketTask.onMessage((res) => {
const data = res.data.toString();
try {
// 尝试解析JSON处理pong响应
const message = JSON.parse(data);
if (message.type === 'pong') {
// 收到pong清除心跳超时
if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
}
} catch (e) {
console.warn('消息解析失败非JSON格式:', e);
}
// 触发外部注册的回调函数
this.messageCallbacks.forEach(callback => callback(data));
});
} else {
console.error('WebSocket 连接尚未建立,无法监听消息');
}
}
// 重连 WebSocket指数退避策略
reconnect() {
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
// 达到最大重连次数,停止重试(可根据需求调整为无限重试)
if (this.reconnectAttempts >= this.reconnectMaxAttempts) {
console.log(`已达最大重连次数 ${this.reconnectMaxAttempts},停止重连`);
return;
}
// 指数退避:延迟时间 = 基础延迟 * 2^重连次数最大不超过maxReconnectDelay
const delay = Math.min(
this.reconnectDelay * Math.pow(2, this.reconnectAttempts),
this.maxReconnectDelay
);
this.reconnectTimeout = setTimeout(() => {
this.reconnectAttempts++;
console.log(`重连尝试 ${this.reconnectAttempts}/${this.reconnectMaxAttempts},延迟 ${delay}ms`);
this.initializeWebSocket();
}, delay);
}
// 主动检查连接状态
checkConnection() {
if (!this.isOpen && !this.reconnectTimeout) {
this.reconnect();
}
}
// 关闭 WebSocket 连接
closeSocket() {
if (this.socketTask) {
uni.closeSocket({
success: () => {
console.log('WebSocket连接已关闭');
this.isOpen = false;
},
fail: (error) => {
console.error('关闭WebSocket连接失败', error);
}
});
this.socketTask = null;
}
}
// 外部注册消息回调函数
onMessage(callback) {
this.messageCallbacks.push(callback);
}
// 外部注销消息回调函数
offMessage(callback) {
// 若传入callback则移除指定回调否则清空所有回调
if (callback) {
this.messageCallbacks = this.messageCallbacks.filter(cb => cb !== callback);
} else {
this.messageCallbacks = [];
}
}
// 销毁 WebSocket 连接,清理资源
destroy() {
this.closeSocket();
// 清除所有定时器
clearInterval(this.heartbeatInterval);
clearTimeout(this.heartbeatTimeout);
clearTimeout(this.reconnectTimeout);
clearInterval(this.checkConnectionInterval);
// 清空回调数组
this.messageCallbacks = [];
console.log('WebSocket资源已销毁');
}
}
export default WebsocketUtil;

View File

@@ -2,12 +2,18 @@
<view class="page-gray u-font-28">
<up-sticky offsetTop="0" customNavHeight="0">
<view class="navbar">
<image
src="/static/iconImg/exit.svg"
class="exit"
mode=""
@click="toDiancan"
></image>
<view class="u-flex"
@click="loginOut()"
>
<image
src="/static/iconImg/exit.svg"
class="exit"
mode=""
></image>
<text class="u-font-32 color-333 u-m-l-16">退出</text>
</view>
<text class="font-700 u-font-40 color-333">台桌列表</text>
<view class="u-flex navbar-right" style="gap: 134rpx">
<up-icon name="scan" size="64rpx" color="#999"></up-icon>
@@ -215,6 +221,13 @@ onShow(async () => {
getTable();
});
function loginOut() {
uni.clearStorage();
uni.redirectTo({
url: "/pages/login/index",
});
}
/**
* 扫码上传
*/

View File

@@ -293,20 +293,7 @@ const vdata = reactive({
loginType: "merchant",
},
});
vdata.formData.merchantName = "19107220837";
vdata.formData.username = "19111112222";
vdata.formData.pwd = "123456";
vdata.formData.code = "666666";
// #ifdef H5
// vdata.formData.merchantName = "19191703856";
// vdata.formData.username = "18888888888";
// vdata.formData.pwd = "123456";
// vdata.formData.code = "666666";
// #endif
// #ifdef MP-WEIXIN
// vdata.formData.username = '15699991111'
// vdata.formData.pwd = 'qwer1234'
// #endif
function accountTypeChange(e) {
// #ifdef H5

View File

@@ -125,7 +125,7 @@
class="u-p-t-32 u-p-b-32 u-p-r-28 u-p-l-28 u-m-t-40 bg-fff u-flex u-row-between"
>
<view class="color-333" style="font-weight: bold">历史订单</view>
<view class="color-666">
<!-- <view class="color-666">
<uni-icons color="#666" type="trash"></uni-icons>
<text
class="u-m-l-10"
@@ -134,7 +134,7 @@
"
>清空历史订单</text
>
</view>
</view> -->
</view>
<view
v-for="(item, index) in historyOrder"
@@ -581,7 +581,7 @@ function getshopsInfo() {
async function workerConfirm() {
//debugger
form.workerAccount=accountStore.staffUserName
// form.workerAccount=accountStore.staffUserName
if (!form.workerAccount) {
return infoBox.showToast("请输入服务员账号");
}
@@ -603,7 +603,7 @@ async function workerConfirm() {
placeNum: 1, //当前订单下单次数
waitCall: 0, //是否等叫 0 否 1 等叫
vipPrice: false, //是否使用会员价
limitRate: null,
limitRate: props.limitTimeDiscount,
subStatus: subStatus.value,
orderId: props.orderInfo.id?props.orderInfo.id:"",
});

View File

@@ -523,10 +523,7 @@ function onMessage() {
);
}
if (
msg.status == 0 &&
msg.type != "time_discount"
) {
if (msg.status == 0 && msg.type != "time_discount") {
console.log("msg", msg);
infoBox.showToast(msg.msg || "添加失败");
data.isGoodsAdd = true;
@@ -646,7 +643,7 @@ async function getHistoryOrderDetail() {
if (shopInfo.registerType == "before") {
return;
}
if(!data.table.tableCode){
if (!data.table.tableCode) {
return;
}
data.historyOrder = [];
@@ -1962,10 +1959,6 @@ onShow(() => {
data.userInfo = shopif;
}
}
// 判断是否token过期,是否有名字
if (!data.userInfo.shopName) {
loginShowOut();
}
getHistoryOrderDetail();
nextTick(() => {
@@ -2018,7 +2011,7 @@ async function getLimit() {
}
async function getTableDetail() {
if(!data.table.tableCode){
if (!data.table.tableCode) {
return;
}
let res = await $returnTableDetail({

View File

@@ -180,6 +180,10 @@ function returnLimitPrice(data) {
shopUserInfo: cartStore.shopUserInfo,
idKey: "productId",
});
console.log("cartStore.shopInfo", cartStore.shopInfo);
console.log("cartStore.limitTimeDiscount", cartStore.limitTimeDiscount);
console.log("cartStore.shopUserInfo", cartStore.shopUserInfo);
return price;
}

View File

@@ -515,6 +515,7 @@ async function discountconfirm(form) {
product_id: modelData.data.product_id,
sku_id: modelData.data.sku_id,
discount_sale_amount: discount_sale_amount,
is_time_discount: discount_sale_amount>0 ? 0 : (modelData.data.is_time_discount||modelData.data.isTimeDiscount)
// discount_sale_note: str + form.note,
};
updateCart(par);

View File

@@ -1,195 +1,203 @@
<template>
<my-model ref="model" :title="title" iconColor="#000" @close="resetForm">
<template #desc>
<view class="u-text-left u-p-30 color-666 u-font-28">
<view class="u-m-t-32 u-flex ">
<view class="" v-if="accountPoints.calcRes.usable">
<text class="color-red">*</text>
<text class=""
v-if="accountPoints.calcRes.equivalentPoints">100积分等于{{to2(accountPoints.calcRes.equivalentPoints*100)}},</text>
<text>
最大抵扣积分{{accountPoints.calcRes.maxUsablePoints}}
</text>
<text>,
最小抵扣积分0
</text>
</view>
</view>
<view class="u-m-t-40 u-flex ">
<view>积分</view>
<view class="u-m-l-32 border u-p-l-10 u-p-r-10 u-flex-1">
<uni-easyinput type="number" @input="pointsInput" @change="pointsChange" paddingNone
:inputBorder="false" v-model="form.points" placeholder="输入积分抵扣数量"></uni-easyinput>
</view>
</view>
</view>
</template>
<template #btn>
<view class="u-p-30">
<view class="u-m-t-10">
<my-button @tap="confirm" shape="circle" fontWeight="700">修改</my-button>
<view class="">
<my-button @tap="close" type="cancel" bgColor="#fff">取消</my-button>
</view>
</view>
</view>
</template>
</my-model>
<my-model ref="model" :title="title" iconColor="#000" @close="resetForm">
<template #desc>
<view class="u-text-left u-p-30 color-666 u-font-28">
<view class="u-m-t-32 u-flex">
<view class="" v-if="accountPoints.calcRes.usable">
<text class="color-red">*</text>
<text class="" v-if="accountPoints.calcRes.equivalentPoints"
>{{ accountPoints.calcRes.equivalentPoints }}积分等于1,</text
>
<text>
最大抵扣积分{{ accountPoints.calcRes.maxUsablePoints }}
</text>
<text>, 最小抵扣积分0 </text>
</view>
</view>
<view class="u-m-t-40 u-flex">
<view>积分</view>
<view class="u-m-l-32 border u-p-l-10 u-p-r-10 u-flex-1">
<uni-easyinput
type="number"
@input="pointsInput"
@change="pointsChange"
paddingNone
:inputBorder="false"
v-model="form.points"
placeholder="输入积分抵扣数量"
></uni-easyinput>
</view>
</view>
</view>
</template>
<template #btn>
<view class="u-p-30">
<view class="u-m-t-10">
<my-button @tap="confirm" shape="circle" fontWeight="700"
>修改</my-button
>
<view class="">
<my-button @tap="close" type="cancel" bgColor="#fff"
>取消</my-button
>
</view>
</view>
</view>
</template>
</my-model>
</template>
<script setup>
import {
reactive,
nextTick,
ref,
watch
} from 'vue';
import myModel from '@/components/my-components/my-model.vue'
import myButton from '@/components/my-components/my-button.vue'
import myTabs from '@/components/my-components/my-tabs.vue'
import infoBox from '@/commons/utils/infoBox.js'
const props = defineProps({
title: {
type: String,
default: '积分抵扣'
},
accountPoints:{
type:Object,
default:()=>{
return {
calcRes:{
usable: false,
unusableReason: '',
minDeductionPoints: 0,
maxUsablePoints: 0
}
}
}
},
price: {
type: [Number, String],
default: 0
}
})
import { reactive, nextTick, ref, watch } from "vue";
import myModel from "@/components/my-components/my-model.vue";
import myButton from "@/components/my-components/my-button.vue";
import myTabs from "@/components/my-components/my-tabs.vue";
import infoBox from "@/commons/utils/infoBox.js";
const props = defineProps({
title: {
type: String,
default: "积分抵扣",
},
accountPoints: {
type: Object,
default: () => {
return {
calcRes: {
usable: false,
unusableReason: "",
minDeductionPoints: 0,
maxUsablePoints: 0,
},
};
},
},
price: {
type: [Number, String],
default: 0,
},
});
function to2(n) {
if (!n) {
return ''
}
return n.toFixed(2)
}
function to2(n) {
if (!n) {
return "";
}
return n.toFixed(2);
}
function pointsInput(e){
setTimeout(()=>{
form.points=Math.floor(e)
},100)
}
function pointsInput(e) {
setTimeout(() => {
form.points = Math.floor(e);
}, 100);
}
function pointsChange(newval) {
form.points = Math.floor(newval);
if (newval < 0) {
form.points = 0;
return infoBox.showToast("积分抵扣不能小于0");
}
if (newval > props.accountPoints.calcRes.maxUsablePoints) {
form.points = props.price;
return infoBox.showToast(
"积分抵扣不能大于" + props.accountPoints.calcRes.maxUsablePoints
);
}
}
function pointsChange(newval) {
form.points=Math.floor(newval)
if (newval < 0) {
form.points = 0
return infoBox.showToast('积分抵扣不能小于0')
}
if (newval > props.accountPoints.calcRes.maxUsablePoints) {
form.points = props.price
return infoBox.showToast('积分抵扣不能大于'+props.accountPoints.calcRes.maxUsablePoints)
}
}
const form = reactive({
points: props.price,
});
watch(
() => props.price,
(newval) => {
form.points = newval;
}
);
function resetForm() {
form.points = 0;
}
const form = reactive({
points: props.price,
})
watch(() => props.price, (newval) => {
form.points = newval
})
const model = ref(null);
function resetForm() {
form.points=0
}
function open() {
model.value.open();
form.points = props.price;
}
const model = ref(null)
function close() {
model.value.close();
}
const emits = defineEmits(["confirm"]);
function open() {
model.value.open()
form.points = props.price
}
function close() {
model.value.close()
}
const emits = defineEmits(['confirm'])
function confirm() {
emits('confirm',Math.floor(form.points) )
close()
}
defineExpose({
open,
close
})
function confirm() {
emits("confirm", Math.floor(form.points));
close();
}
defineExpose({
open,
close,
});
</script>
<style lang="scss" scoped>
.border {
border-radius: 8rpx;
overflow: hidden;
border-color: #999;
}
.border {
border-radius: 8rpx;
overflow: hidden;
border-color: #999;
}
.lh34 {
line-height: 34rpx;
}
.lh34 {
line-height: 34rpx;
}
.tag {
background-color: #fff;
border: 1px solid #E5E5E5;
line-height: inherit;
font-size: 24rpx;
color: #666666;
padding: 6rpx 20rpx;
border-radius: 8rpx;
.tag {
background-color: #fff;
border: 1px solid #e5e5e5;
line-height: inherit;
font-size: 24rpx;
color: #666666;
padding: 6rpx 20rpx;
border-radius: 8rpx;
&.active {
border-color: #E6F0FF;
color: $my-main-color;
}
}
&.active {
border-color: #e6f0ff;
color: $my-main-color;
}
}
.hover-class {
background-color: #E5E5E5;
}
.hover-class {
background-color: #e5e5e5;
}
.discount {
.u-absolute {
top: 0;
bottom: 0;
right: 0;
}
}
.discount {
.u-absolute {
top: 0;
bottom: 0;
right: 0;
}
}
.bg1 {
background: #F7F7FA;
}
.bg1 {
background: #f7f7fa;
}
.tab {
padding: 0 80rpx;
}
.tab {
padding: 0 80rpx;
}
.border {
border: 1px solid #E5E5E5;
border-radius: 4rpx;
}
.border {
border: 1px solid #e5e5e5;
border-radius: 4rpx;
}
.input-box {
padding: 22rpx 32rpx;
font-size: 28rpx;
color: #666;
}
.input-box {
padding: 22rpx 32rpx;
font-size: 28rpx;
color: #666;
}
.placeholder-class {
font-size: 28rpx;
}
.placeholder-class {
font-size: 28rpx;
}
</style>

View File

@@ -998,6 +998,9 @@ watch(
if (newval) {
getNewUserDiscount();
// getShopUserDetail();
}else{
newUserDiscount.value =0
newUserDiscountRes.value =null
}
}
);

View File

@@ -188,7 +188,7 @@ export const useCartStore = defineStore("cart", {
})
);
}
if (msg.status == 0&&msg.type!="time_discount") {
if (msg.status == 0 && msg.type != "time_discount") {
uni.showToast({
title: "添加失败",
icon: "none",
@@ -204,6 +204,9 @@ export const useCartStore = defineStore("cart", {
this.limitTimeDiscount = msg.time_dis_info;
this.getOrder();
break;
case "reload":
this.getOrder();
break;
case "pad_add":
case "add":
break;
@@ -224,7 +227,7 @@ export const useCartStore = defineStore("cart", {
break;
case "pad_cleanup":
case "cleanup":
this.goodsList=[]
this.goodsList = [];
break;
case "product_update":
break;
@@ -277,6 +280,7 @@ export const useCartStore = defineStore("cart", {
*/
setOrder(order) {
this.order = order;
this.limitTimeDiscount = order.limitRate;
this.oldCartList = Object.values(order.detailMap).reduce((pre, cur) => {
pre.push(
...cur.map((v) => {