username@email.com
2023-02-28 67a0042c5f29e4bb0e0b82f6190f2bc51480b45c
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*连接服务端操作*/
(function ($, zou) {
    "use strict";
    var userinfo;
    var imChat;
    var isLoaded = 0;
 
    $.imServer = {
        init: function () {
            /* 首先需要获取用户的登录信息 */
            $.imServer.getUserInfo(function () {
                // 注册服务端方法
                //$.imServer.registerServer();
 
                // 初始化客户端界面代码
                //$.lrIM.init();
                // 连接服务端
                //$.imServer.connect();
            });
        }
 
        // 连接服务端
        , connect: function () {
            $.ajax({
                url: userinfo.imUrl + "/hubs",
                type: "get",
                dataType: "text",
                success: function (data) {
                    eval(data);
                    console.log(userinfo);
                    //Set the hubs URL for the connection
                    $.connection.hub.url = userinfo.imUrl;
                    $.connection.hub.qs = { "userId": userinfo.userId };
                    // Declare a proxy to reference the hub.
                    imChat = $.connection.ChatsHub;
                    $.imServer.registerClient();
                    // 连接成功后注册服务器方法
                    $.connection.hub.start().done(function () {
                        $.imServer.afterSuccess();
                    });
                    //断开连接后
                    $.connection.hub.disconnected(function () {
                        $.imServer.disconnected();
                    });
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    isLoaded = -1;
                },
            });
        }
        // 连接成功后执行方法
        , afterSuccess: function () {
            isLoaded = 1;
        }
        // 断开连接后执行
        , disconnected: function () {
 
        }
 
        // 注册客户端方法
        , registerClient: function () {
            if (imChat) {
                //接收消息
                imChat.client.revMsg = function (formUser, msg, dateTime) {
 
                }
            }
        }
        // 注册服务端方法
        , registerServer: function () {
            // 获取
            $.imServer.getUserList = function (departmentId, callback) {
                console.log(isLoaded);
                if (isLoaded == 1) {
                    imChat.server.getUserList(departmentId).done(function (list) {
                        if (!!callback) {
                            callback(list);
                        }
                    });
                }
                else if (isLoaded == 0) {
                    setTimeout(function () {
                        $.imServer.getUserList(departmentId, callback);
                    }, 500);
                }
            };
        }
 
        // 获取用户登录信息
        , getUserInfo: function (callback) {
            userinfo = zou.clientdata.get(['userinfo']);
            if (!!userinfo) {
                callback();
            }
            else {
                setTimeout(function () {
                    $.imServer.getUserInfo(callback);
                }, 100);
            }
        }
    };
})(jQuery, top.zou);
 
/*网页端操作*/
(function ($, zou) {
    "use strict";
    var userinfo;
    var isWindowOpen = false;
 
    $.lrIM = {
        init: function () {
            $._lrIM.render();
 
            
        },
 
        // 添加一条信息
        // id:消息主键;name:消息名称;msg:消息内容;img:消息头像
        addMsgTolist: function (id, name, msg, img) {
            var $list = $('#zou_im_last_list');
            var $item = $list.find('[data-value="' + id + '"]');
            if ($item.length > 0) {
 
 
            }
            else {
                var _html = '<li data-value="' + id + '">';
                _html += '<img src="' + top.$.rootUrl + '/Content/images/zouim/' + img + '">';
                _html += '';
                _html += '<div class="z-im-onemsg">';
                _html += '<p class="z-im-onemsg-title">' + name + '</p>';
                _html += '<p class="z-im-onemsg-content">' + msg + '</p>';
                _html += '</div></li>';
            }
        }
        // 更新某一个聊天对象消息数量
        , updateMsgNum: function (id, num) {
 
        }
    };
 
    $._lrIM = {
        render: function () {
            var _html = '<div class="z-im-icon"  ><a href="javascript:;" id="lr_imicon_btn" title="企业内部通讯"><i class="fa fa-commenting"></i><span class="label label-success"></span></a></div>';
            _html += '<div class="z-im-wrap" >';
            /*联系人列表*/
            _html += '<div class="z-im-user-list" style="display:none;" id="zou_im_list" >';
            _html += '<div class="z-im-header">企业内部通讯<div class="z-im-close"><a id="zou_im_close" href="javascript:;">×</a></div></div>';
            _html += '<div class="z-im-search"><input type="text" placeholder="搜索:同事名称、讨论组名称"><i class="fa fa-search"></i></div>';
 
            _html += '<div class="z-im-body">';
 
            _html += '<div class="z-im-body-nav" id="zou_im_list_nav" ><ul>';
            _html += '<li class="active nav_tab" data-value="last"><a title="最近回话"><i class="fa fa-comment"></i></a></li>';
            _html += '<li class="nav_tab" data-value="user"><a title="联系人"><i class="fa fa-user"></i></a></li>';
            _html += '<li class="nav_tab" data-value="group"><a title="讨论组"><i class="fa fa-users" style="font-size: 20px;"></i></a></li>';
            _html += '</ul></div>';
 
            _html += '<div class="z-im-body-list" id="zou_im_body_list">';
 
            _html += '<div id="zou_im_last_list" class="zou_im_body_ul active" ></div>';
            _html += '<div id="zou_im_user_list" class="zou_im_body_user" style="display:none;" ><div class="z-top-department" > <div id="im_department"></div> </div><div class="z-userlist-content" id="lr_userlist_content" > <ul id="lr_userlist" class="z-im-chatlist"></ul></div></div>';
            _html += '<div id="zou_im_group_list" class="zou_im_body_ul" style="display:none;" ></div>';
 
            _html += '</div>';
 
            _html += '</div>';//<div class="z-im-body">
            _html += '</div>';//<div class="z-im-user-list">
 
            /*聊天窗口*/
            _html += '<div class="z-im-window" style="display:none;" id="zou_im_window">';
 
            _html += '<div class="z-im-window-header"><span class="text"></span><div class="close"><a href="javascript:;">×</a></div></div>';
 
            _html += '<div class="z-im-window-chat">';
            _html += '<div class="z-im-window-content">';
            _html += '</div></div>';
 
            _html += '<div class="z-im-window-tool"><a class="z-im-window-tool-chatlogbtn "><i class="fa fa-clock-o"></i>沟通记录</a></div>';//<a class="z-im-window-tool-face " title="选择表情"><i class="fa fa-meh-o"></i></a>
            _html += '<div class="z-im-window-send"><textarea autofocus placeholder="按回车发送消息,shift+回车换行"></textarea></div>'
 
            _html += '</div>';
 
            _html += '</div>';
 
            $('body').append(_html);
 
 
 
            /*注册事件*/
            // 外部触发按钮
            $('#lr_imicon_btn').on('click', function () {
                var $im = $('#zou_im_list');
                if ($im.is(':hidden')) {
                    $im.show();
                }
            });
 
            // 关闭按钮
            $('#zou_im_close').on('click', function () {
                var $im = $('#zou_im_list');
                var $im_message_window = $('#zou_im_window');
                $im.hide();
                $im_message_window.hide();
            });
 
            // 联系人列表切换
            $('#zou_im_list_nav li').on('click', function () {
                var $this = $(this);
                if (!$this.hasClass('active')) {
                    var $parent = $this.parent();
                    $parent.find('.active').removeClass('active');
                    $this.addClass('active');
 
                    var id = '#zou_im_' + $this.attr('data-value') + '_list';
                    var $list = $(id);
 
                    $('#zou_im_body_list>div.active').removeClass('active').hide();
                    $list.addClass('active').show();
                }
            });
            // 打开聊天窗口
            $('#zou_im_body_list').delegate(".z-user-item", "click", function (e) {
                var $this = $(this);
                var userId = $this.attr('data-value');
                var userName = $this.find('a').text();
                console.log(userId);
 
                $._lrIM.openWindow(userId, userName);
 
               
            });
            // 发送消息
            var $textarea = $('#zou_im_window .z-im-window-send');
            $textarea.delegate("textarea", 'keypress', function (e) {
                var keyCode = e.keyCode || e.which || e.charCode;
                var shiftKey = e.shiftKey || e.metaKey;
                if (shiftKey && keyCode == "13") {
                }
                else if (keyCode == "13" && isWindowOpen) {
                    var sendText = $(this).val();
                    if (sendText) {
                        $._lrIM.addRightMsg(userinfo.realName, zou.getDate('yyyy-MM-dd hh:mm'), $._lrIM.getUserImg(), sendText);
                    }
                    $('#zou_im_window .z-im-window-send').html('<textarea autofocus placeholder="按回车发送消息,shift+回车换行"></textarea>');
                    setTimeout(function () {
                        $('#zou_im_window .z-im-window-send>textarea').focus();
                    }, 100);
                    e.preventDefault();
                    return false;
                }
            });
 
 
            // 加载部门
            $('#im_department').lrselect({
                type: 'tree',
                // 展开最大高度
                maxHeight: 343,
                // 是否允许搜索
                allowSearch: true,
                // 访问数据接口地址
                url: top.$.rootUrl + '/LR_OrganizationModule/Department/GetTree',
                // 访问数据接口参数
                param: { companyId: '', parentId: '' },
                placeholder: '请选择部门',
                select: function (department) {
                    var $list = $('#lr_userlist');
                    $list.html("");
                    // 获取本部门员工信息列表,如果为空就隐藏
                    $.imServer.getUserList(department.value, function (list) {
                        $.each(list, function (id, item) {
                            $list.append($._lrIM.getUserHtml(item));
                        });
                    });
                }
            });
            userinfo = zou.clientdata.get(['userinfo']);
            $('#im_department').lrselectSet(userinfo.departmentId);
 
            // 优化滚动条
            $('#lr_userlist_content').mCustomScrollbar({ // 优化滚动条
                theme: "minimal-dark"
            });
            $('#zou_im_window .z-im-window-chat').mCustomScrollbar({ // 优化滚动条
                theme: "minimal-dark"
            });
            //$('#zou_im_body_list').mCustomScrollbar({ // 优化滚动条
            //    theme: "minimal-dark"
            //});
        }
        , initData: function () { // 初始化数据
            
        }
        , getUserHtml: function (userItem) {
            if (userItem.F_UserId != userinfo.userId) {
                var _li = '';
                if (zou.isExistImg(top.$.rootUrl + userItem.F_HeadIcon)) {
                    headimg = top.$.rootUrl + userItem.F_HeadIcon;
                }
                else if (userItem.F_Gender != 0) {
                    headimg = top.$.rootUrl + '/Content/images/head/on-boy.jpg';
                }
                else {
                    headimg = top.$.rootUrl + '/Content/images/head/on-girl.jpg';
                }
                _li = '<li class="z-user-item" data-value="' + userItem.F_UserId + '"  ><img class="headimg" src="' + headimg + '"><a>' + userItem.F_RealName + '</a></li>';
                return _li;
            }
        },
        getUserImg: function () {
            var headimg = '';
            if (zou.isExistImg(top.$.rootUrl + userinfo.headIcon)) {
                headimg = top.$.rootUrl + userinfo.headIcon;
            }
            else if (userinfo.gender != 0) {
                headimg = top.$.rootUrl + '/Content/images/head/on-boy.jpg';
            }
            else {
                headimg = top.$.rootUrl + '/Content/images/head/on-girl.jpg';
            }
            return headimg;
 
        },
        openWindow: function (userId, userName) {
            var $window = $('#zou_im_window');
            $window.attr('data-value', userId);
            $window.find('.z-im-window-header>.text').text(userName);
            $('#zou_im_window .z-im-window-content').html("");
            $window.show();
            isWindowOpen = true;
        },
        addRightMsg: function (userName, datatime, img, msg) {
            var html = '<div class="right"><div class="author-name">';
            html += '<small class="chat-date">' + datatime + '</small>';
            html += '<small class="chat-text">' + userName + '</small>';
            html += '<img src="' + img + '" />';
            html += '</div>';
            html += '<div class="chat-message"><em></em>' + msg + '</div></div>';
            $('#zou_im_window .z-im-window-content').append(html);
            $("#zou_im_window .z-im-window-chat").mCustomScrollbar("scrollTo", 'bottom');
        }
    }
})(jQuery, top.zou);