; !function (win, undefined) {
var config = {
msgurl: 'Message',
chatlogurl: 'javascript:alert(\'暂未开放此功能,敬请期待\')',//聊天记录路径
aniTime: 200,
right: -232,
api: {
friend: 'friend.json', //好友列表接口
group: '', //群组列表接口
chatlog: 'chatlog.json', //聊天记录接口
groups: 'groups.json', //群组成员接口
sendurl: '' //发送消息接口
},
user: { //当前用户信息
name: username,
face: userface,//头像
avatar: ""//个性背景图
},
alluser: [],
//自动回复内置文案,也可动态读取数据库配置
autoReplay: [
'你没发错吧?'
],
chating: {},
hosts: (function ()
{
var dk = location.href.match(/\:\d+/);
dk = dk ? dk[0] : '';
return 'http://' + document.domain + dk + '/';
})(),
json: function (url, data, callback, error)
{
return $.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
success: callback,
error: error
});
},
stopMP: function (e)
{
e ? e.stopPropagation() : e.cancelBubble = true;
},
latechat: "",
latenum:0
},
dom = [$(window), $(document), $('html'), $('body')],
chatCore = {},
systemHub = $.connection.chatHub,
onlinenum = 0;//在线人数
//主界面tab
chatCore.tabs = function (index) {
var node = chatCore.node;
node.tabs.eq(index).addClass('ChatCore_tabnow').siblings().removeClass('ChatCore_tabnow');
node.list.eq(index).show().siblings('.ChatCore_list').hide();
if (node.list.eq(index).find('li').length === 0||index===1) {
chatCore.getDates(index);
}
};
//节点
chatCore.renode = function () {
var node = chatCore.node = {
tabs: $('#ChatCore_tabs>span'),
list: $('.ChatCore_list'),
online: $('.ChatCore_online'),
setonline: $('.ChatCore_setonline'),
onlinetex: $('#ChatCore_onlinetex'),
ChatCoreon: $('#ChatCore_on'),
ChatCoreFooter: $('#ChatCore_bottom'),
ChatCoreHide: $('#ChatCore_hide'),
ChatCoreSearch: $('#ChatCore_searchkey'),
searchMian: $('#ChatCore_searchmain'),
closeSearch: $('#ChatCore_closesearch'),
ChatCoreMin: $('#ChatCore_min')
};
};
//主界面缩放
chatCore.expend = function () {
var node = chatCore.node;
if (chatCore.ChatCoreNode.attr('state') !== '1') {
chatCore.ChatCoreNode.stop().animate({ right: config.right }, config.aniTime, function () {
node.ChatCoreon.addClass('ChatCore_off');
try {
localStorage.ChatCoreState = 1;
} catch (e) { }
chatCore.ChatCoreNode.attr({ state: 1 });
node.ChatCoreFooter.addClass('ChatCore_expend').stop().animate({ marginLeft: config.right }, config.aniTime / 2);
node.ChatCoreHide.addClass('ChatCore_show');
});
} else {
chatCore.ChatCoreNode.stop().animate({ right: 1 }, config.aniTime, function () {
node.ChatCoreon.removeClass('ChatCore_off');
try {
localStorage.ChatCoreState = 2;
} catch (e) { }
chatCore.ChatCoreNode.removeAttr('state');
node.ChatCoreFooter.removeClass('ChatCore_expend');
node.ChatCoreHide.removeClass('ChatCore_show');
});
node.ChatCoreFooter.stop().animate({ marginLeft: 0 }, config.aniTime);
}
};
//初始化窗口格局
chatCore.FangsiInit = function () {
var node = chatCore.node;
//主界面
try {
if(!localStorage.ChatCoreState){
config.aniTime = 0;
localStorage.ChatCoreState = 1;
}
if (localStorage.ChatCoreState === '1') {
chatCore.ChatCoreNode.attr({ state: 1 }).css({ right: config.right });
node.ChatCoreon.addClass('ChatCore_off');
node.ChatCoreFooter.addClass('ChatCore_expend').css({ marginLeft: config.right });
node.ChatCoreHide.addClass('ChatCore_show');
}
} catch (e) {
//layer.msg(e.message, 5, -1);
}
};
//聊天窗口
chatCore.popchat = function (param) {
var node = chatCore.node, log = {};
log.success = function (layero) {
chatCore.chatbox = layero.find('#ChatCore_chatbox');
log.chatlist = chatCore.chatbox.find('.ChatCore_chatmore>ul');
log.chatlist.html('
' + param.name + '×')
chatCore.tabchat(param, chatCore.chatbox);
//最小化聊天窗
chatCore.chatbox.find('.layer_setmin').on('click', function () {
var indexs = layero.attr('times');
layero.hide();
node.ChatCoreMin.text(chatCore.nowchat.name).show();
});
//关闭窗口
chatCore.chatbox.find('.ChatCore_close').on('click', function () {
//执行关闭
var indexs = layero.attr('times');
layer.close(indexs);
chatCore.chatbox = null;
config.chating = {};
config.chatings = 0;
});
//关闭某个聊天
log.chatlist.on('mouseenter', 'li', function () {
$(this).find('em').show();
}).on('mouseleave', 'li', function () {
$(this).find('em').hide();
});
log.chatlist.on('click', 'li em', function (e) {
var parents = $(this).parent(), dataType = parents.attr('type');
var dataId = parents.attr('data-id'), index = parents.index();
var chatlist = log.chatlist.find('li'), indexs;
config.stopMP(e);
delete config.chating[dataType + dataId];
config.chatings--;
parents.remove();
$('#ChatCore_area' + dataType + dataId).remove();
if (dataType === 'group') {
$('#ChatCore_group' + dataType + dataId).remove();
}
if (parents.hasClass('ChatCore_chatnow')) {
if (index === config.chatings) {
indexs = index - 1;
} else {
indexs = index + 1;
}
chatCore.tabchat(config.chating[chatlist.eq(indexs).attr('type') + chatlist.eq(indexs).attr('data-id')]);
}
if (log.chatlist.find('li').length === 1) {
log.chatlist.parent().hide();
}
});
//聊天选项卡
log.chatlist.on('click', 'li', function () {
var othis = $(this), dataType = othis.attr('type'), dataId = othis.attr('data-id');
chatCore.tabchat(config.chating[dataType + dataId]);
});
//发送热键切换
log.sendType = $('#ChatCore_sendtype'), log.sendTypes = log.sendType.find('span');
$('#ChatCore_enter').on('click', function (e) {
config.stopMP(e);
log.sendType.show();
});
log.sendTypes.on('click', function () {
log.sendTypes.find('i').text('');
$(this).find('i').text('√');
});
chatCore.transmit();
};
log.html = ''
+ '
'
+ ''
+ ' '
+ ' ' + param.name + ''
+ ' '
+ ' '
+ ' '
+ ' '
+ '
'
+ '
'
+ '
'
+ '
'
+ '
'
+ '
注:只能在该用户在线时发送消息!
'
+ '
'
+ '
'
+ '
发送
'
+ '
'
+ ' √按Enter键发送'
+ ' 按Ctrl+Enter键发送'
+ '
'
+ '
'
+ '
'
+ '
';
if (config.chatings < 1) {
layer.open({
type: 1,
title: false,
shade: [0],
area: ['620px', '520px'],
move: '.ChatCore_chatbox .ChatCore_move',
moveType: 1,
scrollbar: false,
closeBtn: false,
content: log.html,
success: function (layero) {
log.success(layero);
}
})
} else {
log.chatmore = chatCore.chatbox.find('#ChatCore_chatmore');
log.chatarea = chatCore.chatbox.find('#ChatCore_chatarea');
log.chatmore.show();
log.chatmore.find('ul>li').removeClass('ChatCore_chatnow');
log.chatmore.find('ul').append('' + param.name + '×');
log.chatarea.find('.ChatCore_chatview').removeClass('ChatCore_chatthis');
log.chatarea.append('');
chatCore.tabchat(param);
}
//群组
log.chatgroup = chatCore.chatbox.find('#ChatCore_groups');
if (param.type === 'group') {
log.chatgroup.find('ul').removeClass('ChatCore_groupthis');
log.chatgroup.append('');
chatCore.getGroups(param);
}
//点击群员切换聊天窗
log.chatgroup.on('click', 'ul>li', function () {
chatCore.popchatbox($(this));
});
};
//定位到某个聊天队列
chatCore.tabchat = function (param) {
var node = chatCore.node, log = {}, keys = param.type + param.id;
chatCore.nowchat = param;
chatCore.chatbox.find('#ChatCore_user' + keys).addClass('ChatCore_chatnow').siblings().removeClass('ChatCore_chatnow');
chatCore.chatbox.find('#ChatCore_area' + keys).addClass('ChatCore_chatthis').siblings().removeClass('ChatCore_chatthis');
chatCore.chatbox.find('#ChatCore_group' + keys).addClass('ChatCore_groupthis').siblings().removeClass('ChatCore_groupthis');
chatCore.chatbox.find('.ChatCore_face>img').attr('src', param.face);
chatCore.chatbox.find('.ChatCore_face, .ChatCore_names').attr('href', param.href);
chatCore.chatbox.find('.ChatCore_names').text(param.name);
chatCore.chatbox.find('.ChatCore_seechatlog').attr('href', config.chatlogurl);
log.groups = chatCore.chatbox.find('.ChatCore_groups');
if (param.type === 'group') {
log.groups.show();
} else {
log.groups.hide();
}
$('#ChatCore_write').focus();
};
//弹出聊天窗
chatCore.popchatbox = function (othis) {
var node = chatCore.node, userId = othis.attr('id'), dataId = othis.attr('data-id'), param = {
id: dataId, //用户ID
userid: userId,
type: othis.attr('type'),
name: othis.find('.ChatCore_onename').text(), //用户名
face: othis.find('.ChatCore_oneface').attr('src'), //用户头像
href: ''//config.hosts + 'user/' + dataId //用户主页
}, key = param.type + dataId;
if (!config.chating[key]) {
chatCore.popchat(param);
config.chatings++;
} else {
chatCore.tabchat(param);
}
config.chating[key] = param;
var chatbox = $('#ChatCore_chatbox');
if (chatbox[0]) {
node.ChatCoreMin.hide();
chatbox.parents('.xubox_layer').show();
}
};
//请求群员
chatCore.getGroups = function (param) {
};
//消息传输
chatCore.transmit = function () {
var node = chatCore.node, log = {};
node.sendbtn = $('#ChatCore_sendbtn');
node.imwrite = $('#ChatCore_write');
//发送
log.send = function ()
{
if (!(config.latechat.search(chatCore.nowchat.id) > -1))
{
config.latenum += 1;
config.latechat += '' + chatCore.nowchat.name + ''
}
var data = {
content: node.imwrite.val(),
id: chatCore.nowchat.id,
sign_key: '', //密匙
_: +new Date
};
if (data.content.replace(/\s/g, '') === '') {
layer.tips('说点啥呗!', '#ChatCore_write', 2);
node.imwrite.focus();
} else {
//此处皆为模拟
var keys = chatCore.nowchat.type + chatCore.nowchat.id;
//聊天模版
log.html = function (param, type) {
return ''
+ ''
+ function ()
{
if (type === 'me') {
return '
' + param.time + ''
+ '
' + param.name + ''
+ '
';
} else
{
return '
'
+ '
' + param.name + ''
+ '
' + param.time + '';
}
}()
+ '
'
+ '' + param.content + '
'
+ '';
};
log.imarea = chatCore.chatbox.find('#ChatCore_area' + keys);
log.imarea.append(log.html({
time: new Date().toLocaleString(),
name: config.user.name,
face: config.user.face,
content: data.content
}, 'me'));
node.imwrite.val('').focus();
log.imarea.scrollTop(log.imarea[0].scrollHeight);
// 调用服务端sendPrivateMessage方法来转发消息
systemHub.server.sendPrivateMessage(chatCore.nowchat.id, data.content);
}
};
node.sendbtn.on('click', log.send);
node.imwrite.keyup(function (e) {
if (e.keyCode === 13) {
log.send();
}
});
};
//所有触发事件
chatCore.event = function () {
var node = chatCore.node;
//主界面tab
node.tabs.eq(0).addClass('ChatCore_tabnow');
node.tabs.on('click', function () {
var othis = $(this), index = othis.index();
chatCore.tabs(index);
});
//列表展收
node.list.on('click', 'h5', function () {
var othis = $(this), chat = othis.siblings('.ChatCore_chatlist'), parentss = othis.parent();
if (parentss.hasClass('ChatCore_liston')) {
chat.hide();
parentss.removeClass('ChatCore_liston');
} else {
chat.show();
parentss.addClass('ChatCore_liston');
}
});
//设置在线隐身
node.online.on('click', function (e) {
config.stopMP(e);
node.setonline.show();
});
node.setonline.find('span').on('click', function (e) {
var index = $(this).index();
config.stopMP(e);
if (index === 0) {
node.onlinetex.html('在线');
node.online.removeClass('ChatCore_offline');
} else if (index === 1) {
node.onlinetex.html('隐身');
node.online.addClass('ChatCore_offline');
}
node.setonline.hide();
});
node.ChatCoreon.on('click', chatCore.expend);
node.ChatCoreHide.on('click', chatCore.expend);
//搜索
node.ChatCoreSearch.keyup(function () {
var val = $(this).val().replace(/\s/g , '');
if (val !== '') {
node.searchMian.show();
node.closeSearch.show();
//获取所有在线用户
var str = '';
$.post("/JsonHandler/GetOnlineUser", function (data) {
if (data.state)
{
var num = 0;
for (var i = 0; i < data.data.length; i++) {
for (var h = 0; h < config.alluser.length; h++) {
if (data.data[i].UserId == config.alluser[h].UserId) {
delete data.data[i];
num++;
}
}
}
var sda = "[";
for (var i = 0 + num; i < data.data.length; i++) {
if (data.data[i].UserOrg == null || data.data[i].UserOrg == "") {
if (data.data[i].Sex == 1) //男
{
data.data[i].UserOrg = "/images/defaultuser.jpg";
}
if (data.data[i].Sex == 2) //女
{
data.data[i].UserOrg = "/images/defaultuser1.jpg";
}
}
sda += "{\"ConnectionId\":\"\",\"LastLoginTime\":\"\",\"UserFace\":\"" + data.data[i].UserOrg + "\",\"UserId\":\"" + data.data[i].UserId + "\",\"UserName\":\"" + data.data[i].UserName + "\"},";
}
sda = sda.substring(0, sda.length - 1);
sda += "]";
var ss = eval('(' + sda + ')');
for (i = 0; i < config.alluser.length; i++) {
if (config.alluser[i].UserName.search(val) > -1) {
if (str == '') {
str += '';
}
str += '- ' + config.alluser[i].UserName + '' + config.alluser[i].LastLoginTime + '
'
}
}
for (i = 0; i < ss.length; i++) {
if (ss[i].UserName.search(val) > -1) {
if (str == '') {
str += '';
}
str += '- ' + ss[i].UserName + '' + ss[i].LastLoginTime + '
'
}
}
if (str != '') {
str += '
';
node.list.eq(3).html(str);
}
if (str == '') {
node.list.eq(3).html('- 没有符合条件的结果
');
}
}
},"json")
} else {
node.searchMian.hide();
node.closeSearch.hide();
}
});
node.closeSearch.on('click', function () {
$(this).hide();
node.searchMian.hide();
node.ChatCoreSearch.val('').focus();
});
//弹出聊天窗
config.chatings = 0;
node.list.on('click', '.ChatCore_childnode', function () {
chatCore.popchatbox($(this));
});
//点击最小化栏
node.ChatCoreMin.on('click', function () {
$(this).hide();
$('#ChatCore_chatbox').parents('.xubox_layer').show();
});
//document事件
dom[1].on('click', function () {
node.setonline.hide();
$('#ChatCore_sendtype').hide();
});
// 连接IM服务器成功
// 主要是更新在线用户
systemHub.client.onConnected = function (id, userName, allUsers) {
var node = chatCore.node, myf = node.list.eq(0), str = '', i = 0;
myf.addClass('loading');
onlinenum = allUsers.length;
config.alluser = allUsers;
if (onlinenum > 0) {
str += '- '
+ '
在线用户(' + onlinenum + ')
'
+ '';
for (; i < onlinenum; i++) {
str += '- ' + allUsers[i].UserName + '' + allUsers[i].LoginTime + '
';
}
str += '
';
myf.html(str);
} else {
myf.html('- 没有任何数据
');
}
myf.removeClass('loading');
};
//新用户上线
systemHub.client.onNewUserConnected = function (connnectId, userId, userName, loginTime)
{
onlinenum = onlinenum + 1;
$(".ChatCore_nums").html("(" + onlinenum + ")");
var myf = $('#ChatCore_friend_list'), str = '';
str += '- ' + userName + '' + loginTime + '
';
myf.append(str);
};
//用户离线
systemHub.client.onUserDisconnected = function (id, userName) {
onlinenum = onlinenum - 1;
$(".ChatCore_nums").html("(" + onlinenum + ")");
$("#ChatCore_friend_list li[data-id=" + id + "]").remove();
};
//发送消息时,对方已不在线
systemHub.client.absentSubscriber = function () {
layer.open({
title: "系统提醒",
text: "对方已不在线,请采用其它方式沟通!",
skin: "layui-layer-lan"
});
};
//接收消息
systemHub.client.receivePrivateMessage = function (connnectId, userName, message)
{
var node = chatCore.node, log = {}, othis = $("#ChatCore_friend_list li[data-id=" + connnectId + "]");
//聊天模版
log.html = function (param, type) {
return '- '
+ '
'
+ function () {
if (type === 'me') {
return '
' + param.time + ''
+ '
' + param.name + ''
+ '
';
} else {
return '
'
+ '
' + param.name + ''
+ '
' + param.time + '';
}
}()
+ '
'
+ ''+param.content+'
'
+ ' ';
};
chatCore.popchatbox(othis);
var keys = chatCore.nowchat.type + chatCore.nowchat.id;
log.imarea = chatCore.chatbox.find('#ChatCore_area' + keys);
log.imarea.append(log.html({
time: new Date().toLocaleString(),
name: othis.find('.ChatCore_onename').text(),
face: othis.find('.ChatCore_oneface').attr('src'),
content: message
}));
log.imarea.scrollTop(log.imarea[0].scrollHeight);
};
};
//请求列表数据
chatCore.getDates = function (index)
{
var node = chatCore.node, myf = node.list.eq(index);
myf.addClass('loading');
var str = ''
switch (index)
{
case 0:
break;
case 1:
str = '最近聊天 (' + config.latenum + ')
' + '';
str += config.latechat;
str += "
";
myf.html(str);
myf.removeClass('loading');
break;
default:
break;
}
}
//渲染骨架
chatCore.view = (function () {
var ChatCoreNode = chatCore.ChatCoreNode = $(''
+ '
' + config.user.name + '
'
+ '
×
'
+ '
'
+ '
'
+ '
'
+ '
'
+ '
'
+ '
'
+ '
'
+ '
');
dom[3].append(ChatCoreNode);
chatCore.renode();
chatCore.event();
// 启动连接
$.connection.hub.start().done(function () {
systemHub.server.connect(userid, username,userface);
}).fail(function (res) {
alert(res);
});
chatCore.FangsiInit();
}());
}(window);