WXL (wul)
昨天 08881d6b6412822d5035f63a0775ca1f213c8668
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// 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;
}));