var newMsgBodyCount = 0; //新联系人个数
//替换表情
var regPlace = /(\[jie\]|\[kun\]|\[xia\]|\[shx\]|\[wx\]|\[kl\]|\[bq\]|\[lengh\]|\[pp\]|\[sa\]|\[mg\]|\[xu\]|\[lq\]|\[kk\]|\[shl\]|\[bp\]|\[yb\]|\[ruo\]|\[aini\]|\[gd\]|\[dk\]|\[xin\]|\[bangbangt\]|\[xiayu\]|\[lh\]|\[fn\]|\[fendou\]|\[qt\]|\[yhh\]|\[hq\]|\[kb\]|\[qiang\]|\[xs\]|\[db\]|\[lw\]|\[zhh\]|\[zhm\]|\[cd\]|\[fan\]|\[xig\]|\[huaix\]|\[hx\]|\[ka\]|\[yx\]|\[qiao\]|\[kel\]|\[gz\]|\[dy\]|\[jk\]|\[shuai\]|\[fd\]|\[duoyun\]|\[bu\]|\[baiy\]|\[se\]|\[gy\]|\[dx\]|\[yun\]|\[cj\]|\[ws\]|\[zt\]|\[qq\]|\[hn\]|\[jy\]|\[hanx\]|\[dg\]|\[ll\]|\[cy\]|\[kuk\]|\[ty\]|\[zk\]|\[tuu\]|\[ng\]|\[kf\]|\[bs\]|\[zhem\]|\[pz\]|\[shui\]|\[pj\]|\[gg\]|\[am\]|\[yao\]|\[cp\]|\[ch\]|\[tx\]|\[tp\]|\[wq\]|\[zj\]|\[bz\]|\[hd\]|\[yl\]|\[yiw\]|\[ic_launcher\]|\[ic_launcher_an\]|\[ic_launcher_huan\])/g;
//发送图片默认大小
var imgSize = "50%";
//上次显示的时间
var oldDatetime;
//历史记录时间;
var hisDateTime;
var unReadCount = 0;
var regCallback = function(data) {
return "";
};
var regCallback2 = function(data) {
return "
";
};
function loadErrorImg() {
$(".a-user-avatar > img, .a-myinfo-avatar > img, .a-myavatar > img,.user-avatar>img")
.error(function() {
$(this).attr("src", "images/face.png");
});
}
//发送人信息
var myInfo = {
id : "",
cName : "",
photoUri : "",
sex : 0,
orgInfo : "",
clientId : "",
loginId : "",
phone : "",
email : "",
birthday : "",
provinceName : "",
provinceID : "",
cityName : "",
cityID : "",
orgName : "",
departName : "",
dutyName : ""
};
//消息条数
var messagesize = {
total : 0
};
//操作字典
var OperaCode = {
CODE_WNGG : "10000", // 院内公告
CODE_DOC_TO_DOC : "11000", // 医生发送信息
CODE_GROUP : "11001", // 医生对群组发送信息
CODE_DOC_TO_PATIENT : "11002", // 医生对患者发送信息
CODE_FRIENDS : "11003", // 好友申请
CODE_SYS_GROUP : "10010", // 群组动态,退群
CODE_SYS_GROUP_DEL : "10011", // 群组动态,群解散
CODE_SYS_FRIENDS : "10020", // 医生同意好友申请后系统发起互相打招呼
CODE_PATIENT_TO_DOC : "12000", // 患者对医生发送信息
CODE_PHONE : "12001", // 预约回拨
CODE_FREEBACK : "13000", // 意见反馈
CODE_HEALTH_NEWS : "10001", // 健康报
CODE_REFERRAL : "13004", // 转诊助手
CODE_SYS_MESSAGE : "10002", // 系统公告
CODE_LOVE_WEEK_NEWS : "13001", // 爱心值周排名
CODE_LOVE_AWARD : "13002", // 爱心奖励
CODE_PHONE_WEEK_NEWS : "13003", // 电话诊室周报
CODE_HELP : "-9999" // 新手指导
};
//发送类型
var ContentType = {
CONTENT_TEXT : 0,
CONTENT_MP3 : 1,
CONTENT_IMG : 2,
CONTENT_IMG_TEXT : 3,
CONTENT_RECOMMEND : 4
};
//通信类
function AnImClass() {
this.appKey = 'drFnr1YojVjHrTfupuLDhppYLco7h6Ax';
this.AnImObj = null;
}
//初始化通信
AnImClass.prototype.initEvent = function() {
AnImClass.prototype.AnImObj = new AnIM(
{
key : this.appKey,
onStatusChanged : function(isConnected, error) {//通信状态改变
if (!isConnected) {
ifConnecte();
}
},
onReceivedMessage : function(msg_id, message, from,
custome_data, timestamp) {//接收消息回调方法
if (Structure.tarClientId == from
&& $("#talking_box").css('display') == 'block') {
loadMessage(custome_data, msg_id, message, false,
timestampFormat(timestamp), "text", from);
} else {
showNewMessage(custome_data, message, from,
timestampFormat(timestamp));
}
},
onReceivedBinary : function(msg_id, fileType, from,
custome_data, timestamp) {//接收二进制消息回调方法
var url = "";
var message = "";
if (Structure.tarClientId == from&& $("#talking_box").css('display') == 'block') {
if (fileType == 1) {
url = AnImClass.prototype.AnImObj
.getBinaryResourceURL(msg_id, "acc",
"application/vnd.americandynamics.acc");
message = '';
} else {
url = AnImClass.prototype.AnImObj
.getBinaryResourceURL(msg_id, "jpg",
"image/jpg");
message = "
";
}
loadMessage(custome_data, msg_id, message, false,
timestampFormat(timestamp), "", from);
} else {
if (fileType == 1) {
message = "[语音]";
} else {
message = "[图片]";
}
showNewMessage(custome_data, message, from,
timestampFormat(timestamp));
}
},
onReceivedTopicMessage : function(msg_id, message, from,
topicId, cdata, timestamp) {//接收群消息回调方法
if (Structure.tarClientId == topicId
&& $("#talking_box").css('display') == 'block') {
loadMessage(cdata, msg_id, message, false,
timestampFormat(timestamp), "", topicId);
} else {
showNewMessage(cdata, message, topicId,
timestampFormat(timestamp));
}
memberInfos[topicId] = {
"data-id" : cdata.groupId,
"data-name" : cdata.groupName,
"data-clientId" : cdata.groupClientId,
"data-photoUri" : cdata.groupPhoto,
"data-sex" : "0"
};
},
onReceivedTopicBinary : function(msg_id, fileType, from,
topicId, cdata, timestamp) {//接收二进制群消息回调方法
var url = "";
var message = "";
if (Structure.tarClientId == topicId&& $("#talking_box").css('display') == 'block') {
if (fileType == 1) {
url = AnImClass.prototype.AnImObj
.getBinaryResourceURL(msg_id, "acc",
"application/vnd.americandynamics.acc");
message = '';
} else {
url = AnImClass.prototype.AnImObj
.getBinaryResourceURL(msg_id, "jpg",
"image/jpg");
message = "
";
}
loadMessage(cdata, msg_id, message, false,
timestampFormat(timestamp), "", topicId);
} else {
if (fileType == 1) {
message = "[语音]";
} else {
message = "[图片]";
}
showNewMessage(cdata, message, topicId,
timestampFormat(timestamp));
}
memberInfos[topicId] = {
"data-id" : cdata.groupId,
"data-name" : cdata.groupName,
"data-clientId" : cdata.groupClientId,
"data-photoUri" : cdata.groupPhoto,
"data-sex" : "0"
};
},
onMessageSent : function(error, msg_id) {//发送回调方法
$("#msg_" + msg_id).hide();
if (error) {
$("#msg_" + msg_id).empty();
$("#msg_" + msg_id).addClass("tipico tipico-err");
}
},
onReceivedReceiveACK : function(msg_id, from) {//发送确认回调方法
},
onReceivedReadACK : function(msg_id, from) {
},
onClientsRemovedFromTopic : function(event_id, topic_id,
parties, timestamp) {
},
onTopicRemoved : function(event_id, topic_id, timestamp) {//群组解散回调方法
},
onReceivedNotice : function(msg_id, message, from, topicId,
custome_data, timestamp) {//接收通知回调方法
//alert('接收通知回调方法');
}
});
};
//连接通信
AnImClass.prototype.connect = function(clientId) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
AnImClass.prototype.AnImObj.connect(clientId);
}
};
//发送文本消息
AnImClass.prototype.sendMessage = function(clientId, message, customData,
receiveAck) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
var msgId = "";
msgId = AnImClass.prototype.AnImObj.sendMessage(clientId, message,
customData, receiveAck);
var customeDataObj = $.parseJSON(customData);
if (msgId) {
$("#text_msg").val("");
}
loadMessage(customeDataObj, msgId, message, true, (new Date())
.format("yyyy-MM-dd hh:mm:ss"), "text", clientId);
}
};
//发送群组消息
AnImClass.prototype.sendMessageToTopic = function(topicId, message, customData,
receiveAck) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
var msgId = AnImClass.prototype.AnImObj.sendMessageToTopic(topicId,
message, customData, receiveAck);
var customeDataObj = $.parseJSON(customData);
if (msgId) {
$("#text_msg").val("");
}
loadMessage(customeDataObj, msgId, message, true, (new Date())
.format("yyyy-MM-dd hh:mm:ss"), "text", topicId);
}
};
//获取私聊离线消息记录
AnImClass.prototype.getOfflineHistory = function(limit) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
AnImClass.prototype.AnImObj.getOfflineHistory(myInfo.clientId, limit,
function(error, messages, leftCount) {
if (error) {
console.log("获取私聊离线消息记录出错:"+error);
} else {
for ( var i in messages) {
var message = messages[i];
var messageStr = message.message;
if (message.content_type != "text"
&& message.customData
&& message.customData.msgType == 1) {
messageStr = '[语音]';
} else if (message.content_type != "text"
&& message.customData
&& message.customData.msgType == 2) {
messageStr = "[图片]";
}
showNewMessage(message.customData, messageStr,
message.from,
timestampFormat(message.timestamp),
message.content_type, message.from);
}
}
});
}
};
//获取群聊离线消息记录
AnImClass.prototype.getOfflineTopicHistory = function(limit) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
AnImClass.prototype.AnImObj.getOfflineTopicHistory(myInfo.clientId,
limit, "", function(error, messages, leftCount) {
if (error) {
console.log("获取群聊离线消息记录出错:"+error);
} else {
for ( var i in messages) {
var message = messages[i];
var messageStr = message.message;
if (message.content_type != "text"
&& message.customData
&& message.customData.msgType == 1) {
messageStr = '[语音]';
} else if (message.content_type != "text"
&& message.customData
&& message.customData.msgType == 2) {
messageStr = "[图片]";
}
showNewMessage(message.customData, messageStr,
message.topic_id,
timestampFormat(message.timestamp),
message.content_type, message.topic_id);
}
}
});
}
};
// 获取私聊历史消息记录
AnImClass.prototype.getHistory = function(tarClientId, clientId, limit,
timestamp) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
AnImClass.prototype.AnImObj
.getHistory(
tarClientId,
clientId,
limit,
timestamp,
function(error, messages) {
if (error) {
console.log("获取私聊历史消息记录出错:"+error);
} else {
$(".loading_box").show();
$(".sec-main-con").html("");
for ( var i in messages) {
var message = messages[i];
var url = "";
var messageStr = message.message;
if (message.content_type != "text"
&& message.customData
&& message.customData.msgType == 1) {
url = AnImClass.prototype.AnImObj
.getBinaryResourceURL(
message.msg_id, "acc",
"application/vnd.americandynamics.acc");
messageStr = '';
} else if (message.content_type != "text"
&& message.customData
&& message.customData.msgType == 2) {
url = AnImClass.prototype.AnImObj
.getBinaryResourceURL(
message.msg_id, "jpg",
"image/jpg");
messageStr = "
";
}
if (i == 0) {
showMsgListMessage(
message,
messageStr,
false,
timestampFormat(message.timestamp));
}
if (i == messages.length - 1) {
hisDateTime = message.timestamp;
}
loadHistoryMessage(message.customData,
message.msg_id, messageStr,
message.from == myInfo.clientId,
timestampFormat(message.timestamp),
message.content_type, message.from);
}
$(".dbimg").dblclick(function(){
$('#show-full-img > img').attr("src",$(this).attr("src"));
var contentImg=$('#show-full-img').get(0);
// var contentImg='
';
var d = art.dialog({
title: '图片',
padding: '1px 1px',
lock: true,
content: contentImg
});
$('.aui_content').niceScroll({cursorborder:"",cursorcolor:"#cccdd1"});
d.show();
});
setTimeout(scrollBottom, 100);
$(".loading_box").hide();
}
});
}
};
AnImClass.prototype.getHistory2 = function(tarClientId, clientId, limit,timestamp) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
AnImClass.prototype.AnImObj.getHistory(tarClientId,clientId,limit,timestamp,function(error, messages) {
if (error) {
console.log(error);
} else {
for ( var i in messages) {
var message = messages[i];
var url = "";
var messageStr = message.message;
if (message.content_type != "text" && message.customData
&& message.customData.msgType == 1) {
messageStr = '[语音]';
} else if (message.content_type != "text" && message.customData
&& message.customData.msgType == 2) {
messageStr = '[图片]';
}
if (message.customData.msgType == ContentType.CONTENT_IMG
&& message.content_type == "text") {
messageStr = "[图片]";
} else if (message.customData.msgType == 0) {
messageStr = YiHu.Util.htmlspecialchars(messageStr).replace(regPlace, regCallback2);
}
var NowTime=new Date().getTime();
if (NowTime-message.timestamp<=1000*60*60*24*7){
showOldMessage(message.customData, messageStr,tarClientId,timestampFormat(message.timestamp),message.content_type);
}
}
}
});
}
};
// 获取群聊历史消息记录
AnImClass.prototype.getTopicHistory2 = function(topicId, clientId, limit,timestamp) {
if (AnImClass.prototype.AnImObj == null) {
console.log("未注册通信");
} else {
AnImClass.prototype.AnImObj
.getTopicHistory(
topicId,
limit,
timestamp,
clientId,
function(error, messages) {
if (error) {
console.log(error);
} else {
for ( var i in messages) {
var message = messages[i];
var url = "";
var messageStr = message.message;
if (message.content_type != "text" && message.customData
&& message.customData.msgType == 1) {
messageStr = '[语音]';
} else if (message.content_type != "text" && message.customData
&& message.customData.msgType == 2) {
messageStr = '[图片]';
}
if (message.customData.msgType == ContentType.CONTENT_IMG
&& message.content_type == "text") {
messageStr = "[图片]";
} else if (message.customData.msgType == 0) {
messageStr = YiHu.Util.htmlspecialchars(messageStr).replace(regPlace, regCallback2);
}
var NowTime=new Date().getTime();
if (NowTime-message.timestamp<=1000*60*60*24*7){
showOldMessage(message.customData, messageStr,topicId,timestampFormat(message.timestamp),message.content_type);
}
}
}
});
}
};
//显示历史消息
function showOldMessage(customData, message, from, dateStr, dataType) {
var memberInfo = memberInfos[from];
if(!memberInfo){
return;
}
message = message.replace(/\n/g, "
");
var $linewMsg = $("#linewMsg_" + from);
if ($linewMsg.length > 0) {
return;
}
var isGroup = false;
if (customData.groupId && customData.groupId != "") {
isGroup = true;
}
var photoUri = memberInfo["data-photoUri"];
photoUri = (photoUri && photoUri != "") ? photoUri : "images/face.png";
var str = [
'
' + memberInfo["data-name"] + '
', '' + (isGroup ? customData.sourceName + ":" + message.replace(regPlace, regCallback2) : message .replace(regPlace, regCallback2)) + '
', '' + YiHu.Util.dataTimeToStr(dateStr) + '
' + message.replace(regPlace, regCallback) + '
', '', (isMy ? '' + YiHu.Util.dataTimeToStr(dateStr) + '
' + message.replace(regPlace, regCallback) + '
', '', '' + (isGroup ? customData.groupName : customData.sourceName) + '
', '' + (isGroup ? customData.sourceName + ":" + message.replace(regPlace, regCallback2) : message .replace(regPlace, regCallback2)) + '
', ''+appName+'-待办工作'+ '
', ''+titleApp+':'+ contentApp + '
', '' + '您有一条验证消息' + '
', '' + data[i].userName + '请求添加您为好友' + '
', '