Переглянути джерело

reactor:呼叫中心底层方法重构;

zhangchong 10 місяців тому
батько
коміт
67f4b8c2e3
2 змінених файлів з 4 додано та 177 видалено
  1. 4 4
      src/layout/navBars/breadcrumb/telControl.vue
  2. 0 173
      src/utils/websocket.ts

+ 4 - 4
src/layout/navBars/breadcrumb/telControl.vue

@@ -573,7 +573,7 @@ import { submitLog } from '@/api/public/log';
 import { getDataByCode } from '@/api/system/dict';
 import { useTransition, useDocumentVisibility } from '@vueuse/core';
 import { useWebSocket } from '@/hooks/useWebsocket';
-import { olaFn,olaWs } from '@/utils/olaFn';
+import { olaFn, olaWs } from '@/utils/olaFn';
 // 引入组件
 const CommonAdvice = defineAsyncComponent(() => import('@/components/CommonAdvice/index.vue')); // 常用意见
 const AnnexList = defineAsyncComponent(() => import('@/components/AnnexList/index.vue'));
@@ -867,7 +867,7 @@ const onConnected = () => {
 		}
 	}
 	olaRef.value.login(array_ola_queue, currentTel.value.telNo, { type: 'onhook' });
-  olaWs.value = olaRef.value;
+	olaWs.value = olaRef.value;
 	if (currentTel.value.telModel != 2) {
 		// 普通模式才链接语音助手
 		connectVoiceAssistant(currentTel.value.telNo); // 坐席助手开启
@@ -885,7 +885,7 @@ const onConnected = () => {
 // 呼叫中心链接关闭
 const onDisconnected = (event: any) => {
 	console.log('呼叫中心断开链接:', event);
-  olaWs.value = null;
+	olaWs.value = null;
 	resetState();
 	const currentTel = Local.get('currentTel');
 	const name: string = `天润分机号:${currentTel}的websocket断开链接`;
@@ -1242,7 +1242,7 @@ const onMessage = async (event: any) => {
 		// 其他消息
 		// console.log('command/reply', data);
 	}
-};;
+};
 // 呼叫中心链接错误
 const onError = (ws: any, e: any) => {
 	console.log(`呼叫中心链接错误:${e}`);

+ 0 - 173
src/utils/websocket.ts

@@ -1,173 +0,0 @@
-interface SocketOptions {
-	heartbeatInterval?: number;
-	reconnectInterval?: number;
-	maxReconnectAttempts?: number;
-	isReconnect?: boolean;
-	uid?: string;
-	trans?: string;
-	notice?:string;
-}
-
-class Socket {
-	url: string;
-	ws: WebSocket | null = null;
-	opts: SocketOptions;
-	reconnectAttempts: number = 0;
-	listeners: { [key: string]: Function[] } = {};
-	heartbeatInterval: number | null = null;
-
-	constructor(url: string, opts: SocketOptions = {}) {
-		this.url = url;
-		this.opts = {
-			heartbeatInterval: 2 * 1000, // 心跳间隔
-			reconnectInterval: 4 * 1000, // 重连间隔
-			maxReconnectAttempts: 99, // 最大重连次数
-			isReconnect: true, // 是否需要重连
-			uid: '', // 用户id
-			trans: '', // 订阅的频道
-			notice: '', // 订阅的频道
-			...opts,
-		};
-
-		this.init();
-	}
-
-	init() {
-		this.ws = new WebSocket(this.url);
-
-		this.ws.onopen = this.onOpen.bind(this);
-		this.ws.onmessage = this.onMessage.bind(this);
-		this.ws.onerror = this.onError.bind(this);
-		this.ws.onclose = this.onClose.bind(this);
-	}
-
-	onOpen(event: Event) {
-		// this.reconnectAttempts=0;
-		this.startSubscribe();
-		this.startHeartbeat();
-		this.emit('open', event);
-	}
-
-	onMessage(event: MessageEvent) {
-		this.emit('message', event);
-	}
-
-	onError(event: Event) {
-		console.error('WebSocket error:', event);
-		this.emit('error', event);
-	}
-
-	onClose(event: CloseEvent) {
-		this.stopHeartbeat();
-		this.emit('close', event);
-
-		// @ts-ignore
-		if (this.opts.isReconnect) {
-			this.reConnect();
-		}
-	}
-
-	reConnect() {
-		// @ts-ignore
-		if (this.reconnectAttempts < this.opts.maxReconnectAttempts) {
-			setTimeout(() => {
-				this.reconnectAttempts++;
-				this.init();
-			}, this.opts.reconnectInterval);
-		} else {
-			console.error('已到达重连次数最高,请手动刷新重连');
-		}
-	}
-
-	startHeartbeat() {
-		if (!this.opts.heartbeatInterval) return;
-		this.heartbeatInterval = window.setInterval(() => {
-			if (this.ws?.readyState === WebSocket.OPEN) {
-				this.send({
-					id: '',
-					type: 2,
-					from: this.opts.uid,
-					to: 'sys',
-					timestamps: new Date().getTime(),
-					body: 'PING',
-				});
-			}
-		}, this.opts.heartbeatInterval);
-	}
-
-	startSubscribe() {
-		// @ts-ignore
-		const moreTime = this.opts.heartbeatInterval + 100;
-		setTimeout(() => {
-			if (!this.opts.trans) return;
-			this.send({
-				id: '',
-				type: 8,
-				from: this.opts.uid,
-				to: this.opts.trans,
-				timestamps: new Date().getTime(),
-				body: 'subscribe',
-			});
-			if (this.opts.notice) {
-				this.send({
-					id: '',
-					type: 8,
-					from: this.opts.uid,
-					to: this.opts.notice,
-					timestamps: new Date().getTime(),
-					body: 'subscribe',
-				});
-			}
-		}, moreTime);
-	}
-	stopHeartbeat() {
-		if (this.heartbeatInterval) {
-			clearInterval(this.heartbeatInterval);
-			this.heartbeatInterval = null;
-		}
-	}
-
-	send(data: any) {
-		if (this.ws?.readyState === WebSocket.OPEN) {
-			this.ws.send(JSON.stringify(data));
-		} else {
-			console.error('WebSocket is not open. Cannot send:', data);
-		}
-	}
-
-	on(event: string, callback: Function) {
-		if (!this.listeners[event]) {
-			this.listeners[event] = [];
-		}
-		this.listeners[event].push(callback);
-	}
-
-	off(event: string) {
-		if (this.listeners[event]) {
-			delete this.listeners[event];
-		}
-	}
-
-	emit(event: string, data: any) {
-		this.listeners[event]?.forEach((callback) => callback(data));
-	}
-
-	close() {
-		this.opts.isReconnect = false;
-		this.ws?.close();
-		this.ws = null;
-		this.stopHeartbeat();
-		this.listeners = {};
-	}
-}
-
-export function useSocket(url: string, opts?: SocketOptions) {
-	const socket = new Socket(url, opts);
-	return {
-		socket,
-		close: socket.close.bind(socket),
-		send: socket.send.bind(socket),
-		on: socket.on.bind(socket),
-		off: socket.off.bind(socket),
-	};
-}