Authored by 李奇

聊天记录接口对应完成

... ... @@ -22,15 +22,12 @@ const getClientData = (type, uid, imgSize) => {
return Promise.all(apiMethod)
.then(res => {
let msgHistory = res[1].data;
for (let msg of msgHistory) {
msg.isCs = msg.type === 100003; // 客服消息type
}
let history = res[1].data;
let hasHistory = history.length > 0;
return {
csSetting: res[0].data.config,
msgHistory: msgHistory,
hasHistory: hasHistory,
orders: res[2].data,
qas: []
};
... ...
<div class="panel-main">
<div class="main-body">
<div class="msg-list">
{{#if msgHistory}}
{{#each msgHistory}}
{{#if isCs}}
<div class="list-item guest his-msg">
<img src="{{csHead}}" class="avatar">
<div class="item-detail">
<span class="time">{{csName}} {{sendTimeShort}}</span>
<div class="msg-bubble">
<span>{{content}}</span>
</div>
</div>
</div>
{{else}}
<div class="list-item host his-msg">
<img src="{{userHead}}" class="avatar">
<div class="item-detail">
<span class="time">{{userName}} {{sendTimeShort}}</span>
<div class="msg-bubble">
<span>{{content}}</span>
</div>
</div>
</div>
{{/if}}
{{/each}}
{{#if hasHistory}}
<div class="list-item">
<span class="about-his has-his">最近聊天记录</span>
</div>
... ...
... ... @@ -15,6 +15,8 @@ var $ = require('yoho-jquery'),
// 记录鼠标位置
var uid,
cmEntity,
endTime,
hasMore = true,
originConf,
encryptionUid,
cursorPosition,
... ... @@ -40,7 +42,8 @@ var key,
urls = {
makeEval: '/evalute/saveEvalute',
leaveMsg: '/leavemessage/saveLeavemessage',
evalReason: '/evalute/queryEvaluteReason'
evalReason: '/evalute/queryEvaluteReason',
msgHistory: '/conversationMessage/pageList'
};
require('bootstrap');
... ... @@ -544,6 +547,92 @@ function getMessage(rec) {
$panelMainBody.scrollTop($panelMainBody[0].scrollHeight);
}
/**
* 消息解析
* @param msgList
*/
function msgResolve(msgList) {
var item,
heightBefore,
heightAfter,
msgTpl = '';
msgList = msgList.reverse();
if (endTime === msgList[0].sendTime) {
hasMore = false;
}
endTime = msgList[0].sendTime;
if (hasMore) {
for (item of msgList) {
item.message.newContent = _emojiPrefix(item.message.newContent);
if (item.message.type === 2) {
item.message.newContent =
['<img class="msg-img" src="', item.message.newContent, '">'].join('');
}
// 客户自己的消息
if (item.type === 3) {
msgTpl +=
`<div class="list-item host">
<img src="${item.userHead}" class="avatar">
<div class="item-detail">
<span class="time">${item.userName} ${item.sendTimeShort}</span>
<div class="msg-bubble">
<div>${item.message.newContent}</div>
</div>
</div>
</div>`;
// 客服消息
} else {
msgTpl +=
`<div class="list-item guest">
<img src="${item.csHead}" class="avatar">
<div class="item-detail">
<span class="time">${item.csName} ${item.sendTimeShort}</span>
<div class="msg-bubble">
<div>${item.message.newContent}</div>
</div>
</div>
</div>`;
}
}
heightBefore = $msgList.height();
$msgList.prepend(msgTpl);
heightAfter = $msgList.height();
$panelMainBody.scrollTop(heightAfter - heightBefore);
}
}
/**
* 获取历史聊天记录
*/
function fetchHistoryMsg() {
var data = {
uid: uid
};
if (endTime) {
data.endTime = endTime;
}
$.ajax({
type: 'GET',
url: urls.msgHistory,
data: data,
success: function(res) {
if (res && res.code === 200) {
hasMore && msgResolve(res.data);
$('.about-his.has-his').hide();
}
}
});
}
// 初始化socket连接
socketChat.init(Object.assign(socketConf, {
... ... @@ -890,11 +979,13 @@ $('.img-zoom-in').on('click', function(e) {
});
// 显示历史记录
$('.about-his.has-his').click(function(e) {
$(e.currentTarget).hide();
$('.list-item.his-msg').show();
$('.about-his.has-his').click(fetchHistoryMsg);
$panelMainBody.scrollTop($panelMainBody[0].scrollHeight);
// 滚动加载更多
$panelMainBody.scroll(function() {
var atTop = $panelMainBody.scrollTop() === 0;
atTop && hasMore && fetchHistoryMsg();
});
... ... @@ -907,3 +998,4 @@ $('.about-his.has-his').click(function(e) {
... ...