fix: 代客下单优化离开代客下单页面时主动断开scoket,添加优惠券修改,修改获取优惠券记录请求方式改为get
This commit is contained in:
@@ -13,12 +13,13 @@ export interface ApifoxModel {
|
||||
[property: string]: string | number | undefined; // 限制额外属性类型
|
||||
}
|
||||
|
||||
export type msgType = 'add' | 'reduce' | 'remove' | 'edit' | 'init' | 'cleanup' | 'del' | 'rottable' | 'batch';
|
||||
export type msgType = 'add' | 'reduce' | 'remove' | 'edit' | 'init' | 'cleanup' | 'del' | 'rottable' | 'batch' | 'disconnect';
|
||||
|
||||
class WebSocketManager {
|
||||
private client: WebSocket | null = null;
|
||||
private connected: boolean = false;
|
||||
private shop_id = user.userInfo.shopId ? String(user.userInfo.shopId) : '';
|
||||
private autoConnect: boolean = true;
|
||||
private initParams: ApifoxModel = {
|
||||
type: 'manage',
|
||||
account: this.shop_id || '', // 提供默认值
|
||||
@@ -65,7 +66,9 @@ class WebSocketManager {
|
||||
this.clearTimer();
|
||||
this.connected = false;
|
||||
console.log("WebSocket 连接已断开");
|
||||
this.reconnect(); // 自动重连
|
||||
if (this.autoConnect) {
|
||||
this.reconnect(); // 自动重连
|
||||
}
|
||||
};
|
||||
|
||||
this.client.onerror = (error: Event) => {
|
||||
@@ -101,6 +104,7 @@ class WebSocketManager {
|
||||
this.disconnect();
|
||||
this.setupWebSocket();
|
||||
this.onMessage = onMessage;
|
||||
this.autoConnect = true
|
||||
}
|
||||
|
||||
public sendMessage(message: any) {
|
||||
@@ -109,7 +113,10 @@ class WebSocketManager {
|
||||
this.reconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.operate_type == 'disconnect') {
|
||||
this.disconnect();
|
||||
return
|
||||
}
|
||||
const msg = JSON.stringify({ ...this.initParams, ...message });
|
||||
try {
|
||||
this.client?.send(msg);
|
||||
@@ -132,11 +139,15 @@ class WebSocketManager {
|
||||
this.client.close();
|
||||
this.client = null;
|
||||
this.connected = false;
|
||||
this.autoConnect = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 自动重连机制
|
||||
private reconnect() {
|
||||
if (!this.autoConnect) {
|
||||
return;
|
||||
}
|
||||
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||
this.reconnectAttempts++;
|
||||
console.log(`尝试第 ${this.reconnectAttempts} 次重连...`);
|
||||
|
||||
Reference in New Issue
Block a user