// cti-manager.js - 移除prototype依赖的版本
|
(function(root, factory) {
|
if (typeof define === 'function' && define.amd) {
|
define([], factory);
|
} else if (typeof module === 'object' && module.exports) {
|
module.exports = factory();
|
} else {
|
root.CTIManager = factory();
|
}
|
}(this, function() {
|
'use strict';
|
|
// 简单的对象扩展方法
|
function extend(target, source) {
|
if (!source) return target;
|
for (var key in source) {
|
if (source.hasOwnProperty(key)) {
|
target[key] = source[key];
|
}
|
}
|
return target;
|
}
|
|
function CTIManager(options) {
|
var defaultOptions = {
|
autoReconnect: true,
|
maxReconnectAttempts: 5,
|
debug: true
|
};
|
|
this.options = extend({}, defaultOptions);
|
this.options = extend(this.options, options || {});
|
|
this.websocket = null;
|
this.isLoggedIn = false;
|
this.currentCall = null;
|
this.seatInfo = {};
|
this.eventHandlers = {};
|
|
this.init();
|
}
|
|
CTIManager.prototype = {
|
init: function() {
|
this.setupEventHandlers();
|
},
|
|
setupEventHandlers: function() {
|
// 默认事件处理器
|
this.on('login_success', this.handleLoginSuccess.bind(this));
|
this.on('login_failed', this.handleLoginFailed.bind(this));
|
this.on('call_start', this.handleCallStart.bind(this));
|
this.on('call_end', this.handleCallEnd.bind(this));
|
this.on('seat_status_change', this.handleSeatStatusChange.bind(this));
|
},
|
|
connect: function(url) {
|
try {
|
// 确保CTIWebSocket已全局可用
|
if (typeof CTIWebSocket === 'undefined') {
|
this.trigger('error', { type: 'dependency_error', message: 'CTIWebSocket未定义' });
|
return false;
|
}
|
|
this.websocket = new CTIWebSocket({
|
url: url,
|
debug: this.options.debug,
|
reconnectInterval: 3000,
|
maxReconnectAttempts: this.options.maxReconnectAttempts
|
});
|
|
this.bindWebSocketEvents();
|
return true;
|
} catch (error) {
|
this.trigger('error', { type: 'connection_error', error: error });
|
return false;
|
}
|
},
|
|
bindWebSocketEvents: function() {
|
var self = this;
|
|
this.websocket.on('open', function() {
|
self.trigger('connected');
|
});
|
|
this.websocket.on('close', function() {
|
self.isLoggedIn = false;
|
self.trigger('disconnected');
|
});
|
|
this.websocket.on('error', function(error) {
|
self.trigger('error', error);
|
});
|
|
// 监听特定的CTI消息
|
this.websocket.on('cti:system:keepalive', function(data) {
|
self.trigger('keepalive', data);
|
});
|
|
this.websocket.on('cti:control:tp_callin', function(data) {
|
self.trigger('call_incoming', data);
|
if (data.uuid) {
|
self.trigger('uuid_received', data.uuid);
|
}
|
});
|
},
|
|
// 登录相关方法
|
login: function(seatname, seatnum, password) {
|
if (!this.websocket || !this.websocket.isConnected) {
|
this.trigger('error', { type: 'not_connected', message: 'WebSocket未连接' });
|
return false;
|
}
|
|
return this.websocket.seatlogin(seatname, seatnum, password);
|
},
|
|
logout: function() {
|
if (this.websocket && this.isLoggedIn) {
|
var result = this.websocket.seatlogout(this.seatInfo.seatname, this.seatInfo.seatnum);
|
if (result) {
|
this.isLoggedIn = false;
|
this.seatInfo = {};
|
}
|
return result;
|
}
|
return false;
|
},
|
|
// 呼叫控制方法
|
makeCall: function(phoneNumber) {
|
if (!this.isLoggedIn) {
|
this.trigger('error', { type: 'not_logged_in', message: '坐席未登录' });
|
return false;
|
}
|
|
return this.websocket.callout(this.seatInfo.seatname, this.seatInfo.seatnum, phoneNumber);
|
},
|
|
hangupCall: function(uuid) {
|
if (!this.isLoggedIn) {
|
return false;
|
}
|
|
var callUuid = uuid || (this.currentCall ? this.currentCall.uuid : '');
|
return this.websocket.hangup(this.seatInfo.seatname, this.seatInfo.seatnum, callUuid);
|
},
|
|
// 坐席状态控制
|
setAfk: function() {
|
if (this.isLoggedIn) {
|
return this.websocket.afk(this.seatInfo.seatname, this.seatInfo.seatnum);
|
}
|
return false;
|
},
|
|
setOnline: function() {
|
if (this.isLoggedIn) {
|
return this.websocket.online(this.seatInfo.seatname, this.seatInfo.seatnum);
|
}
|
return false;
|
},
|
|
// 事件管理
|
on: function(event, handler) {
|
if (!this.eventHandlers[event]) {
|
this.eventHandlers[event] = [];
|
}
|
this.eventHandlers[event].push(handler);
|
},
|
|
off: function(event, handler) {
|
if (this.eventHandlers[event]) {
|
var index = this.eventHandlers[event].indexOf(handler);
|
if (index > -1) {
|
this.eventHandlers[event].splice(index, 1);
|
}
|
}
|
},
|
|
trigger: function(event, data) {
|
if (this.eventHandlers[event]) {
|
this.eventHandlers[event].forEach(function(handler) {
|
if (typeof handler === 'function') {
|
handler(data);
|
}
|
});
|
}
|
},
|
|
destroy: function() {
|
if (this.websocket) {
|
this.websocket.close();
|
this.websocket = null;
|
}
|
this.eventHandlers = {};
|
}
|
};
|
|
return CTIManager;
|
}));
|