';
}
// 发送消息
sendMessage(jobhtml, 'job', id);
// 设置当前沟通职位
jid = id;
}
function nochat() {
layer.open({
content: '设定不感兴趣将会删除与TA的' + chat_name + '记录,并将TA加入' + chat_name + '黑名单,确定继续?',
btn: ['确定', '取消'],
yes: function(index) {
layer.close(index);
loadlayer();
$.post(weburl + "/index.php?m=chat&c=delChatLog", {
to: toid,
type: totype
}, function(res) {
layer.closeAll('loading');
if (res.errmsg) {
layer.msg(res.errmsg, 2, 8, function() {
location.href = weburl;
});
} else {
window.location.href = weburl + '/member/index.php?c=chat';
}
},'json');
}
});
}
// 发送消息
function sendMessage(content, type, id) {
var time = new Date().getTime();
if (socket && socket.readyState == 1 && canSend){
// 根据消息类型区分保存内容
if (type == 'job') {
var msgcontent = 'jobid=' + id;
showChatJob(id);
} else if (type == 'resume') {
var msgcontent = 'eid=' + id;
} else if (type == 'invite') {
var msgcontent = 'inviteid=' + id;
} else if (type == 'ask') {
var msgcontent = 'ask=' + id;
} else if (type == 'confirm') {
var msgcontent = 'confirm=' + id;
} else if (type == 'refuse') {
var msgcontent = 'refuse=' + id;
} else if (type == 'read') {
var msgcontent = 'read=' + id;
} else {
var msgcontent = content;
}
var mineData = {
id: mine.id,
mine: true,
ftype: mine.usertype,
avatar: mine.avatar,
username: mine.username,
linkman: mine.linkman,
timestamp: time,
content: msgcontent,
msgtype: type,
op: {},
};
if (type == 'confirm') {
mineData.op.confirmcon = content;
}
var sendData = {
mine: mineData,
to: {
id: toid,
utype: totype
}
};
var message = {
type: 'chatMessage',
data: {
content: sendData
}
};
// 保存聊天记录
var nolog = ['change', 'read'];
if (nolog.indexOf(type) == -1) {
var pdata = {
to: sendData.to.id,
totype: sendData.to.utype,
content: msgcontent,
timestamp: sendData.mine.timestamp,
msgtype: sendData.mine.msgtype,
nowid: sendData.mine.id,
nowtype: sendData.mine.ftype
};
$.post(weburl + "/index.php?m=chat&c=chatLog", pdata, function(data) {
if (data) {
var res = eval('(' + data + ')');
if (res.error != 0) {
if (res.error == 4) {
$('#chatid').val(id);
server_single('chat');
firstTab();
var msglayer = layer.open({
type: 1,
title: chat_name,
closeBtn: 1,
border: [10, 0.3, '#000', true],
area: ['810px', 'auto'],
content: $("#tcmsg"),
cancel: function() {
window.location.reload();
}
});
return false;
}else if (res.error == 3) {
layer.msg(res.errmsg, 2, 8);
} else if (res.errmsg) {
layer.msg(res.errmsg, 2, 8, function() {
location.href = weburl;
return;
});
}
} else {
message.data.content.mine.chatid = res.chatid;
socket.send(JSON.stringify(message));
// 渲染消息内容
mineData.chatid = res.chatid;
// 渲染消息内容
mineData.htmlcontent = content;
if(typeof chatStorage[toid]!='undifined' && chatStorage[toid]){
chatStorage[toid].history.unshift(mineData);
}
var html = chatRender(mineData, 'send');
}
}
});
} else {
socket.send(JSON.stringify(message));
// 渲染消息内容
mineData.htmlcontent = content;
var html = chatRender(mineData, 'send');
}
// 消息时间间隔超60秒,显示新时间
if (sendData.mine.timestamp - sendTime > 60 * 1000) {
$("#chat_content").find('ul').append('
' + timeFormat(sendData.mine.timestamp) +
'
');
sendTime = sendData.mine.timestamp;
}
} else {
layer.msg(chat_name + '功能加载中', 2, 8);
}
}
// 渲染消息内容
function chatRender(msg, type) {
if (type == 'send') {
// 发送
if (msg.msgtype == 'char') {
var content = rexContent(msg.htmlcontent, msg.msgtype,msg);
// 我说
var html = '
';
html += '
';
html += '' + content + '
';
} else if (msg.msgtype == 'change') {
var html = '
' + msg.htmlcontent +
'
';
} else if (msg.msgtype == 'ask') {
if (msg.content.indexOf('ask') != -1) {
var askid = msg.content.replace('ask=', '');
var html = getAskHtml(askid, msg);
}
} else if (msg.msgtype == 'confirm') {
if (msg.content.indexOf('confirm') != -1) {
var confirmid = msg.content.replace('confirm=', '');
var askvalue = '';
huhuanStatus(confirmid, 1);
if (confirmid == 'wx') {
askvalue = towx;
} else if (confirmid == 'tel') {
askvalue = totel;
}
var html = confirmAskHtml(askvalue, confirmid, msg);
}
} else if (msg.msgtype == 'refuse') {
if (msg.content.indexOf('refuse') != -1) {
var refuseid = msg.content.replace('refuse=', '');
huhuanStatus(refuseid, 2);
var html = getRefuseHtml(refuseid, msg);
}
} else {
var html = msg.htmlcontent;
}
} else {
// 接收
if (msg.msgtype == 'char' || msg.msgtype == 'voice') {
msg.content = rexContent(msg.content, msg.msgtype,msg);
// 他说
var html = '
';
html += '
';
html += '' + msg.content + '
';
} else if (msg.msgtype == 'ask') {
if (msg.content.indexOf('ask') != -1) {
var askid = msg.content.replace('ask=', '');
var html = getAskHtml(askid, msg);
}
} else if (msg.msgtype == 'confirm') {
if (msg.content.indexOf('confirm') != -1) {
var confirmid = msg.content.replace('confirm=', '');
huhuanStatus(confirmid, '1');
var html = confirmAskHtml(msg.confirmcon, confirmid, msg);
}
} else if (msg.msgtype == 'refuse') {
if (msg.content.indexOf('refuse') != -1) {
var refuseid = msg.content.replace('refuse=', '');
huhuanStatus(refuseid, '2');
var html = getRefuseHtml(refuseid, msg);
}
} else if (msg.msgtype == 'job') {
if (msg.content.indexOf('jobid') != -1) {
var jobid = msg.content.replace('jobid=', '');
var html = getJobHtml(joblist[jobid]);
}
} else if (msg.msgtype == 'resume') {
if (msg.content.indexOf('eid') != -1) {
var eid = msg.content.replace('eid=', '');
var html = getResumeHtml(eid);
}
} else if (msg.msgtype == 'change') {
if (msg.ftype == 1) {
var change = '求职者更换了与您沟通的职位';
} else {
var change = '招聘者更换了与您沟通的职位';
}
var html = '
' + change +
'
';
} else if (msg.msgtype == 'invite') {
if (msg.content.indexOf('inviteid') != -1) {
var inviteid = msg.content.replace('inviteid=', '');
var html = '
';
html += '
';
html +=
'发送了面试邀请
';
}
} else if (msg.msgtype == 'spview') {
if (msg.mine) {
var html = '
';
html += '
';
html += '
';
if (msg.content == 'closesp') {
var fuser = msg.fusertype != mine.usertype ? '对方' : '';
html += '
'+ fuser +'已取消
';
}else if (msg.content == 'refused') {
var fuser = msg.fusertype == mine.usertype ? '对方' : '';
html += '
'+ fuser +'已拒绝
';
}else{
html += '
'+ msg.content +'
';
}
html += '
';
} else {
var html = '
';
html += '
';
html += '
';
if (msg.content == 'closesp') {
var fuser = msg.fusertype != mine.usertype ? '对方' : '';
html += '
'+ fuser +'已取消
';
}else if (msg.content == 'refused') {
var fuser = msg.fusertype == mine.usertype ? '对方' : '';
html += '
'+ fuser +'已拒绝
';
}else{
html += '
'+ msg.content +'
';
}
html += '
';
}
}else if (msg.msgtype == 'read'){
return
}else if (msg.msgtype == 'map'){
let map = msg.content .split('|');
var staticmap = '';
if(map.length == 4){
staticmap = map[1];
}
if(staticmap!=''){
msg.content = '';
if (msg.mine) {
// 我说
var html = '
';
html += '
';
html += '' + msg.content + '
';
} else {
// 他说
var html = '
';
html += '
';
html += '' + msg.content + '
';
}
}
} else {
var html = msg.content;
}
// 消息时间间隔超60秒,显示新时间
if (msg.timestamp - sendTime > 60 * 1000) {
$("#chat_content").find('ul').append('
' + timeFormat(msg.timestamp) + '
');
sendTime = msg.timestamp;
}
}
$("#chat_content").find('ul').append(html);
picviewjs();
var scroll = document.getElementById('chat_content').scrollHeight;
chatScrollTo(scroll);
// 图片类型加载较慢,滚动到底部需要时间
if ($("#chat_content").find('ul').last().find('img')) {
// 上面滚动过之后,新的高度
lastscroll = document.getElementById('chat_content').scrollHeight;
var timer = setInterval(function() {
var imgscroll = document.getElementById('chat_content').scrollHeight;
if (imgscroll > lastscroll) {
chatScrollTo(imgscroll);
clearInterval(timer);
timer = null;
}
}, 200);
setTimeout(function() {
if (timer) {
clearInterval(timer);
}
}, 2000);
}
return html;
}
function getJobHtml(job, msg) {
var html = '';
if (job) {
html = '
';
html += '
' + job.name + '
';
html += '' + job.job_salary + '';
html += '
' + job.com_name + '
';
html += '
' + job.job_city_one;
if (job.job_city_two) {
html += '-' + job.job_city_two;
}
if (job.job_exp) {
html += ' . ' + job.job_exp + '经验';
}
if (job.job_edu) {
html += ' . ' + job.job_edu + '学历';
}
html += '
';
if (msg && msg.newjob) {
var msgtime = parseInt(msg.msgtime);
var gtuser;
if (msg.newjob == 1) {
gtuser = '你';
} else {
if (msg.fusertype == 1) {
if (mine.usertype == 1) {
gtuser = '你';
} else {
gtuser = '招聘者';
}
} else {
if (mine.usertype == 1) {
gtuser = '求职者';
} else {
gtuser = '你';
}
}
}
html += '
' + timeFormat(msgtime) + ' 由' + gtuser + '发起的沟通
';
}
html += '';
}
return html;
}
function getResumeHtml(eid) {
var html = '';
if (expect) {
html = '
';
}
return html;
}
// 转义内容
function rexContent(content, msgtype,msg) {
if (msgtype == 'voice') {
content = (content || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&')
.replace(//g, '>').replace(/'/g, ''').replace(/"/g, '"') //XSS
.replace(/voice\[([^\s]+?)\]/g, function(voice) { //转义图片
var voiceurl = voice.replace(/(^voice\[)|(\]$)/g, '');
var voicehtml = getVoiceHtml(voiceurl, msg);
var voicestatus = 0;
var html = '';
html += '
';
if (msg.mine) { //右侧
html += '
' + msg.voicelength +
'
';
} else { //左侧
html += '
' + msg.voicelength +
'
';
}
html += '
' + voicehtml.innerHTML + '
';
if (!msg.mine && msg.voicestatus != 1) { //未读消息红点
html += ' ';
voicestatus = 0;
} else {
voicestatus = 1;
}
html += '';
html += '
';
return html;
})
} else {
content = (content || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&')
.replace(//g, '>').replace(/'/g, ''').replace(/"/g, '"') //XSS
.replace(/img\[([^\s]+?)\]/g, function(img) { //转义图片
return '';
})
.replace(/face\[([^\s\[\]]+?)\]/g, function(face) { //转义表情
var alt = face.replace(/^face/g, '');
return '';
})
}
return content;
}
function upImage(obj) {
layer.load();
$('#imgform').submit();
var iframe = $('#chat_iframe'),
timer = setInterval(function() {
var res;
try {
res = iframe.contents().find('body').text();
} catch (e) {
layer.closeAll('loading');
clearInterval(timer);
timer = null;
}
if (res) {
layer.closeAll('loading');
clearInterval(timer);
timer = null;
iframe.contents().find('body').html('');
try {
res = JSON.parse(res);
} catch (e) {
res = {};
}
if (res.msg) {
layer.msg(res.msg, 2, 8);
} else {
focusInsert(document.getElementById("send_content"), 'img[' + (res.data.url || '') + ']', true);
send();
}
}
}, 30);
obj.value = '';
}
function timeFormat(nowtime) {
if (nowtime) {
nowtime = new Date(nowtime);
} else {
nowtime = new Date();
}
var month = nowtime.getMonth(),
day = nowtime.getDate(),
hour = nowtime.getHours(),
minutes = nowtime.getMinutes();
month = month + 1;
if (month < 10) {
month = '0' + month;
}
if (hour < 10 && hour > 0) {
hour = '0' + hour;
}
if (minutes < 10 && minutes > 0) {
minutes = '0' + minutes;
}
if (day < 10 && day > 0) {
day = '0' + day;
}
var time = month + '-' + day + ' ' + hour + ':' + minutes;
return time;
}
// 页面滚动
function chatScrollTo(ypos) {
var clientHeight = document.getElementById('chat_box').clientHeight;;
if (ypos > clientHeight) {
// 兼容各种浏览器
document.getElementById('chat_box').scrollTop = ypos;
}
}
//在焦点处插入内容
function focusInsert(obj, str, nofocus) {
var result, val = obj.value;
nofocus || obj.focus();
if (document.selection) { //ie
result = document.selection.createRange();
document.selection.empty();
result.text = str;
} else {
result = [val.substring(0, obj.selectionStart), str, val.substr(obj.selectionEnd)];
nofocus || obj.focus();
obj.value = result.join('');
}
}
function moreChat() {
$.ajax({
type: "post",
url: weburl + "/index.php?m=chat&c=getChatPage",
async: false,
data: {
'id': toid,
'totype': totype,
'page': chatpage,
'lastid': lastid
},
dataType: "json",
success: function(res) {
layer.closeAll('loading');
if (res) {
if(res.msg){
return layer.msg(res.msg, 2, 8, function(){
window.location.href = weburl + '/member/';
});
}
chatpage = chatpage + 1;
var scroll = null;
if (chatpage > 2) {
// 第二页开始,记录前一次滚动距离
scroll = document.getElementById('chat_content').scrollHeight;
}
$('#chat_more').remove();
if (res.code == 0) {
if(res.adminjob){
adminjob = res.adminjob;
}
var chatlog = res.data;
if (chatlog.length > 0) {
pastSend = chatlog[0].sendTime;
for (var i in chatlog) {
if(chatStorage[toid]){
chatStorage[toid].history.push(chatlog[i]);
}
renderPast(chatlog[i]);
// 最后一条时,如还有历史消息,增加更多消息
if (i == chatlog.length - 1 && res.ismore) {
$("#chat_content").find('ul').prepend(
'
查看更多消息
'
);
lastid = chatlog[i]['id'];
}
if (scroll) {
// 渲染后,滚动到上一页的距离
var newScroll = document.getElementById('chat_content').scrollHeight;
chatScrollTo(newScroll - scroll);
}
}
}
}
prepare();
moreReady = true;
}
}
});
}
function isArray (arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
// 深度克隆
function deepClone (obj) {
// 对常见的“非”值,直接返回原来值
if(!obj || typeof(obj) == 'undefined' || isNaN(obj)) return obj;
if(typeof obj !== "object" && typeof obj !== 'function') {
//原始类型直接返回
return obj;
}
var o = isArray(obj) ? [] : {};
for(let i in obj) {
if(obj.hasOwnProperty(i)){
o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
}
}
return o;
}
function chathistoryHtml(thischatStorage){
if(thischatStorage.history && thischatStorage.history.length>0){
chatpage = chatpage + 1;
$('#chat_more').remove();
var chatlog = thischatStorage.history;
if (chatlog.length > 0) {
pastSend = chatlog[0].sendTime;
for (var i in chatlog) {
renderPast(chatlog[i])
// 最后一条时,如还有历史消息,增加更多消息
if (i == chatlog.length - 1) {
$("#chat_content").find('ul').prepend(
'
查看更多消息
'
);
lastid = chatlog[i]['id'];
}
}
}
moreReady = true;
}
prepare(thischatStorage);
}
// 渲染历史消息
function renderPast(msg) {
// 接收
if (msg.msgtype == 'char' || msg.msgtype == 'voice') {
msg.content = rexContent(msg.content, msg.msgtype,msg);
if (msg.mine) {
// 我说
var html = '
';
html += '
';
html += '' + msg.content + '
';
} else {
// 他说
var html = '
';
html += '
';
html += '' + msg.content + '
';
}
} else if (msg.msgtype == 'ask') {
if (msg.content.indexOf('ask') != -1) {
var askid = msg.content.replace('ask=', '');
var html = getAskHtml(askid, msg);
}
} else if (msg.msgtype == 'confirm') {
if (msg.content.indexOf('confirm') != -1) {
var confirmid = msg.content.replace('confirm=', '');
var html = confirmAskHtml(msg.confirmcon, confirmid, msg);
}
} else if (msg.msgtype == 'refuse') {
if (msg.content.indexOf('refuse') != -1) {
var refuseid = msg.content.replace('refuse=', '');
var html = getRefuseHtml(refuseid, msg);
}
} else if (msg.msgtype == 'job') {
if (msg.content.indexOf('jobid') != -1) {
var jobid = msg.content.replace('jobid=', '');
var html = getJobHtml(joblist[jobid], msg);
if (jid == '') {
showChatJob(jobid);
}
jid = jobid;
jobShowed = false;
}
}else if (msg.msgtype == 'adminjob'){
if(msg.content.indexOf('jobid') != -1){
var jobid = msg.content.replace('jobid=', '');
var html = getJobHtml(adminjob[jobid], msg);
if (jid == '') {
showChatJob(jobid);
}
jid = jobid;
jobShowed = false;
}
} else if (msg.msgtype == 'resume') {
if (msg.content.indexOf('eid') != -1) {
var eid = msg.content.replace('eid=', '');
var html = getResumeHtml(eid);
}
} else if (msg.msgtype == 'change') {
if (mine.usertype == 1) {
var change = '求职者更换了与您沟通的职位';
} else {
var change = '招聘者更换了与您沟通的职位';
}
var html = '
' + change +
'
';
} else if (msg.msgtype == 'invite') {
if (msg.content.indexOf('inviteid') != -1) {
var qyid = msg.content.replace('inviteid=', '');
if (msg.mine) {
var html = '
';
html += '
';
html +=
'发送了面试邀请
';
} else {
var html = '
';
html += '
';
html +=
'发送了面试邀请
';
}
if (inviteid != '' && inviteid == qyid) {
inviteid = '';
}
}
} else if (msg.msgtype == 'spview') {
if (msg.mine) {
var html = '
';
html += '
';
html += '
';
if (msg.content == 'closesp') {
var fuser = msg.fusertype != mine.usertype ? '对方' : '';
html += '
'+ fuser +'已取消
';
}else if (msg.content == 'refused') {
var fuser = msg.fusertype == mine.usertype ? '对方' : '';
html += '
'+ fuser +'已拒绝
';
}else{
html += '
'+ msg.content +'
';
}
html += '
';
} else {
var html = '
';
html += '
';
html += '
';
if (msg.content == 'closesp') {
var fuser = msg.fusertype != mine.usertype ? '对方' : '';
html += '
'+ fuser +'已取消
';
}else if (msg.content == 'refused') {
var fuser = msg.fusertype == mine.usertype ? '对方' : '';
html += '
'+ fuser +'已拒绝
';
}else{
html += '
'+ msg.content +'
';
}
html += '
';
}
} else if (msg.msgtype == 'map') {
let map = msg.content .split('|');
var staticmap = '';
if(map.length == 4){
staticmap = map[1];
}
if(staticmap!=''){
msg.content = '';
if (msg.mine) {
// 我说
var html = '
';
html += '
';
html += '' + msg.content + '
';
} else {
// 他说
var html = '
';
html += '
';
html += '' + msg.content + '
';
}
}
} else {
var html = msg.content;
}
if(typeof html !== 'undefined'){
$("#chat_content").find('ul').prepend(html);
picviewjs();
// 消息时间间隔超60秒,显示新时间
if (pastSend - msg.sendTime > 60 * 1000) {
$("#chat_content").find('ul').prepend('