You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/SoftMng/Content/signalR-2.2.0/js/MessageBox.js

240 lines
10 KiB
JavaScript

3 years ago
; !function (win, undefined) {
var config = {
aniTime: 200,
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;
},
unreadmsg: 0,
historymsg: [],
layerIndex: 0
},
dom = [$(window), $(document), $('html'), $('body')],
chatCore = {},
systemHub = $.connection.chatHub;
//构造弹出界面
chatCore.popchat = function (data) {
var strHtml = '<div class="ChatCore_chatbox" id="ChatCore_chatbox"><ul class="list-group ChatCore_chatview"></ul></div>';
if ($("#popchat_layer").length === 0) {
config.layerIndex = layer.open({
type: 1,
id: "popchat_layer",
skin: "dark_title",
title: ["消息提醒<i class='icon-refresh col-point'></i>", "height: 32px;line-height: 32px;"],
anim: -1,
move: false,
shade: false,
closeBtn: false,
area: ['230px', '320px'],
content: strHtml,
btn: false,
success: function (layero) {
$(".message-box-div button.action").find("i").addClass("icon-minus");
$(".message-box-div button.action").find("i").removeClass("icon-envelope");
$(layero).on('click',
'.icon-refresh',
function () {
chatCore.gethistorymsg(layero);
});
chatCore.chatbox = layero.find('#ChatCore_chatbox');
//填充历史消息
if (config.historymsg.length === 0)
chatCore.gethistorymsg(layero);
else {
chatCore.callback(layero, config.historymsg);
}
}
});
} else {
$("#popchat_layer").find("ul").prepend(chatCore.builditem(data));
}
};
//获取历史提醒
chatCore.gethistorymsg = function (layero) {
$(layero).find('.icon-refresh').addClass('icon-spin');
//等待窗口
CommonAjax.Post("/SoftMng/SignalR/GetHistoryMsg",
{ userid: userid },
function (data) {
setTimeout(function () {
$(layero).find('.icon-refresh').removeClass('icon-spin');
}, 3000);
config.historymsg = data;
chatCore.callback(layero, config.historymsg);
});
};
//回调
chatCore.callback = function (layero, data) {
$(layero).find("ul").html(chatCore.builditem(data));
config.historymsg = data;
config.unreadmsg = data.filter(function (item) {
return !item.ISREAD;
}).length;
chatCore.ChatCoreNode.find(".badge").show().text(config.unreadmsg);
//绑定事件
$('.list-group-item > span.icon-bell-alt').on('click',
function () {
var $othis = $(this);
var key = $othis.attr("data-key");
CommonAjax.Post("/SoftMng/SignalR/SetMsgStatus",
{ gid: key, status: 1 },
function (result) {
if (result) {
var index = config.historymsg.findIndex(function (item) {
return item.GID == key;
});
//减少未读
config.unreadmsg--;
console.log(config.unreadmsg)
chatCore.ChatCoreNode.find(".badge").show().text(config.unreadmsg);
if (config.unreadmsg === 0)
chatCore.ChatCoreNode.find(".badge").hide();
config.historymsg[index].ISREAD = 1;
chatCore.callback(layero, config.historymsg);
}
});
});
};
//构造提醒列表
chatCore.builditem = function (data) {
var str = "";
for (var i = 0; i < data.length; i++) {
str = str.concat(
'<li class="list-group-item list-group-item-' + (data[i].ISREAD == 0 ? 'light' : 'secondary') + '"><span>' + CommonJson.GetDate(data[i].CREATETIME).format('yyyy-mm-dd hh:ii') + '</span><span id="status_' + data[i].GID + '" data-key="' + data[i].GID + '" class="col-point ' + (data[i].ISREAD == 0 ? 'icon-bell-alt text-danger' : '') + '"></span><p class="card-text text-' + (data[i].ISREAD == 0 ? 'dark' : 'secondary') + '">' + data[i].MESSAGECONTENT + '</p></li>');
}
return str;
};
//所有事件
chatCore.event = function () {
$('.message-box-div button.action').on('click', function () {
if (chatCore.moved != undefined && chatCore.moved) {
chatCore.moved = false;
return;
}
if ($(this).find("i").hasClass("icon-envelope")) {
$(this).find("i").addClass("icon-minus");
$(this).find("i").removeClass("icon-envelope");
chatCore.popchat();
} else {
$(this).find("i").addClass("icon-envelope");
$(this).find("i").removeClass("icon-minus");
layer.close(config.layerIndex);
}
});
$('.message-box-div').on('mousedown', function (e) {
chatCore.downY = e.clientY;
chatCore.srcBottom = parseInt($('.message-box-div').css("bottom").replace("px", ""));
console.debug("srcBottom:" + chatCore.srcBottom);
})
.on('mousemove', function (e) {
if (e.buttons == 1) {
chatCore.moved = true;
var chgY = chatCore.downY - e.clientY;
var newBottom = chatCore.srcBottom + chgY;
console.debug("newBottom:" + newBottom);
$('.message-box-div').css("bottom", newBottom + "px")
console.debug($('.message-box-div').css("bottom"));
}
});
//连接IM服务器
systemHub.client.onConnected = function (id, userName, allUsers) {
//获取未读消息总数
CommonAjax.Post("/SoftMng/SignalR/GetHistoryMsg",
{ userid: userid },
function (data) {
config.historymsg = data;
config.unreadmsg = data.filter(function (item) {
return !item.ISREAD;
}).length;
chatCore.ChatCoreNode.find(".badge").show().text(config.unreadmsg);
});
};
//发送消息时,对方已不在线
systemHub.client.absentSubscriber = function () {
console.warn("消息已被阻止,对方设置拒绝接收!");
};
//接收消息
systemHub.client.receivePrivateMessage = function (connnectId, userName, data) {
console.debug("rec");
data.LASTREADTIME = "/Date(" + new Date(data.LASTREADTIME).valueOf() + ")/";
data.CREATETIME = "/Date(" + new Date(data.CREATETIME).valueOf() + ")/";
config.unreadmsg++;
chatCore.ChatCoreNode.find(".badge").show().text(config.unreadmsg);
config.historymsg.unshift(data);
var arr = new Array();
arr.push(data);
//右下角提醒
layer.msg(data.DESCRIPTION,
{
icon: 1,
time: 5000,
offset: "rb",
skin: "col-point",
closeBtn: 1,
success: function (layero, index) {
$(layero).on("click",
".layui-layer-content",
function () {
chatCore.popchat(arr);
layer.close(index);
});
}
});
//var log = {};
//log.imarea.scrollTop(log.imarea[0].scrollHeight);
};
//接收公告
systemHub.client.receiveNotice = function (model, action) {
var notice = $("#notice", document.getElementsByTagName("iframe").main.contentWindow.document);
if (action != "delete") {//新增或修改
var html = '<li class="success-element" data-id="' + model.GID + '" onclick="showNotice(this)">';
html += '<a href="#!" >' + model.Title + '</a>';
html += '<div class="agile-detail">';
html += '<i class="fa fa-clock-o mr-2"></i>';
html += model.CreateTime;
html += '</div></li>'
if (action == "update")
notice.find("[data-id='" + model.GID + "']").remove();
notice.prepend(html);
}
else {//删除操作
var delItem = model.split(',');
for (var i = 0; i < delItem.length; i++) {
notice.find("[data-id=" + delItem[i] + "]").remove();
}
}
};
};
//渲染骨架
chatCore.view = (function () {
var ChatCoreNode = chatCore.ChatCoreNode = $('<div class="message-box-div"><span class="badge badge-warning">' + config.unreadmsg + '</span><button type="button" class="btn btn-info action"><i class="icon-envelope"></i></button></div>');
dom[3].append(ChatCoreNode);
if (config.unreadmsg === 0) chatCore.ChatCoreNode.find(".badge").hide();
chatCore.event();
//启动连接
$.connection.hub.start().done(function () {
systemHub.server.connect(userid, username, userface);
}).fail(function (res) {
layer.msg(res);
});
}());
}(window);