Authored by 李奇

消息调试

... ... @@ -33,76 +33,7 @@
</div>
<div class="panel-main">
<div class="main-body">
<div class="msg-list">
<div class="list-item">
<span class="his-msg">近一周没有历史消息</span>
</div>
<div class="list-item guest">
<img src="/img/service/agent-avatar.png" class="avatar">
<div class="item-detail">
<span class="time">客服助手 10:15:37</span>
<div class="msg-bubble">
<span>我的快递还没到,神马情况啊!我的快递还没到,神马情况啊!</span>
</div>
</div>
</div>
<div class="list-item host">
<img src="/img/service/cus-avatar.png" class="avatar">
<div class="item-detail">
<span class="time">18823562175 10:14:37</span>
<div class="msg-bubble">
<span>单号:8027398592<br>
金额:¥859<br>
下单时间:2016-09-20 10:01:01<br>
状态:待发货
</span>
</div>
</div>
</div>
<div class="list-item guest">
<img src="/img/service/agent-avatar.png" class="avatar">
<div class="item-detail">
<span class="time">客服助手 10:15:37</span>
<div class="msg-bubble">
<img class="msg-img" src="/img/service/adv.png">
</div>
</div>
</div>
<div class="list-item host">
<img src="/img/service/cus-avatar.png" class="avatar">
<div class="item-detail">
<span class="time">18823562175 10:14:37</span>
<div class="msg-bubble">
<img class="msg-img" src="/img/service/adv.png">
</div>
</div>
</div>
<div class="list-item">
<p class="push-tip">
<span class="tip">用户已经退出会话</span>
</p>
</div>
<div class="list-item">
<p class="push-tip">
<span class="tip">您目前排在第X位,请您先休息一下,客服MM马上就来!您也可以 <a class="red-tip" data-toggle="modal" data-target="#leaveMsg">选择留言</a></span>
</p>
</div>
<div class="list-item">
<p class="push-tip">
<span class="tip">用户已经退出会话</span>
</p>
</div>
<div class="list-item">
<p class="push-tip">
<span class="tip">您向用户推送了一个评价申请</span>
</p>
</div>
<div class="list-item">
<p class="push-tip">
<span class="tip">会话已断开,如果您还有问题需要咨询,请重新<a class="red-tip reconnect">连线客服</a></span>
</p>
</div>
</div>
<div class="msg-list"></div>
</div>
<div class="main-footer">
<div class="msg-edit">
... ... @@ -111,9 +42,9 @@
<span class="icon"></span>
<span class="icon"><label for="sendImg"></label></span>
<span class="icon"></span>
<span class="icon"><a title="客服"></a></span>
<span class="icon"></span>
</div>
<textarea class="text msg-area" placeholder="请描述您遇到的问题" spellcheck="false"></textarea>
<textarea class="text msg-area" placeholder="请描述您遇到的问题" spellcheck="false" maxlength="500"></textarea>
</div>
<span class="send">发送</span>
<input id="sendImg" type="file" name="files[]" accept="image/bmp,image/jpeg,image/png" multiple>
... ...
... ... @@ -13,77 +13,11 @@ var $ = require('yoho-jquery'),
require('bootstrap');
require('../common');
config.conversationMessage.userId = 15575902; // todo 暂时写死
config.conversationMessage.userId = 15575902; // todo userId 暂时写死
var conversationMessage = config.conversationMessage;
var cM = config.conversationMessage;
socketChat.init(Object.assign(config, {
onOpen: function (e) {
console.log('websocket opened!')
},
onMessage: function (e) {
var jsonString = e.data;
var received = JSON.parse(jsonString);
conversationMessage.conversationId = received.conversationId;
console.log('监听到消息时:received=', received);
},
onClose: function() {
console.log('websocket closed!')
}
}));
// 调整窗口位置
$(window).on('resize', function() {
var vh = $(this).height(),
vw = $(this).width();
$('.chat-panel').css({
width: vw - (vh - (70 + 950) / 1.7),
'margin-left': (vh - (70 + 950) / 1.7) / 2
}).show();
}).trigger('resize');
// 获取鼠标位置
$.fn.getCursorPosition = function() {
var el = $(this).get(0);
var pos = 0;
if ('selectionStart' in el) {
pos = el.selectionStart;
} else if ('selection' in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
};
// // 图片上传
// $('#sendImg').fileupload({
// dataType: 'json',
// url: 'test',
// done: function(e, data) {
// $.each(data.result.files, function(index, file) {
// $('<p/>').text(file.name).appendTo(document.body);
// });
// }
// });
// enter提交 ctrl+enter换行
$('.msg-edit .msg-area').on('keydown', msgEnterAction);
// 常见问题
$('.qa-list .q').on('click', toggleAnswer);
$('.send-order').on('click', sendOrderInfo);
/**
... ... @@ -180,6 +114,10 @@ function dispatchUtil(e) {
$('#makeEvaluation').modal({
show: true
});
},
3: function() {
connectService();
}
};
... ... @@ -369,7 +307,7 @@ function msgEnterAction(e) {
if (!ctrlLike) {
e.preventDefault();
sendMsg(e);
_sendMessage(e);
} else {
var tag = $(e.target),
pos = tag.getCursorPosition(),
... ... @@ -385,22 +323,6 @@ function msgEnterAction(e) {
}
/**
* 发送消息
* @param e
*/
function sendMsg(e) {
var tag = $(e.target),
msg = tag.val();
if (!msg) return;
conversationMessage.type = 3;
conversationMessage.message.content = msg;
// conversationMessage.uuid = uuid.randomUUID();
console.log('消息发送时conversationMessage=', conversationMessage);
socketChat.send(JSON.stringify(Object.assign(conversationMessage)));
}
/**
* 常见问题
* @param e
*/
... ... @@ -423,16 +345,199 @@ function sendOrderInfo(e) {
}
/**
* 连接客服
* @param e
* 连线客服
* @param
*/
function connectService() {
conversationMessage.type = 2;
console.log('连线客服时:', conversationMessage);
socketChat.send(JSON.stringify(conversationMessage));
cM.type = 2;
socketChat.send(JSON.stringify(cM));
}
/**
* 处理收到消息
* @private
*/
function _getMessage(received) {
console.log('客户收到消息!', received);
var recType = received.type,
message = received.message,
msgType = message.type,
allTypes = config.receivedType;
// 请求人工/接入人工成功/接入人工失败
var tipTpl;
if (recType === 2) {
switch(msgType) {
case 0: // 0是没上班
tipTpl = `<div class="list-item">
<p class="push-tip">
<span class="tip">${message.content}</span>
</p>
</div>`;
break;
case 1: // 1是需要排队
tipTpl = `<div class="list-item">
<p class="push-tip">
<span class="tip">${message.content}您也可以选择<a class="leave-msg">留言</a>!</span>
</p>
</div>`;
break;
case 2: // 2是接入人工成功
tipTpl = `<div class="list-item">
<p class="push-tip">
<span class="tip">${message.content}</span>
</p>
</div>`;
break;
}
}
// 待重构
if(recType === 3 || recType === 103 || recType === 10003) {
message.newContent = message.newContent.replace(/src="(\d{3}).gif"/g, 'src="/img/service/emoji/$1.gif"');
message.content = message.content.replace(/src="(\d{3}).gif"/g, 'src="/img/service/emoji/$1.gif"');
if(recType === 3) {
tipTpl = `<div class="list-item host">
<img src="/img/service/cus-avatar.png" class="avatar">
<div class="item-detail">
<span class="time">18823562175 10:14:37</span>
<div class="msg-bubble">
<div>${message.newContent}</div>
</div>
</div>
</div>`
}
if(recType === 103) {
tipTpl = `<div class="list-item guest">
<img src="/img/service/agent-avatar.png" class="avatar">
<div class="item-detail">
<span class="time">客服机器人 10:14:37</span>
<div class="msg-bubble">
<div>${message.newContent}</div>
</div>
</div>
</div>`
}
if(recType === 10003) {
tipTpl = `<div class="list-item guest">
<img src="/img/service/agent-avatar.png" class="avatar">
<div class="item-detail">
<span class="time">客服助手 10:14:37</span>
<div class="msg-bubble">
<div>${message.newContent}</div>
</div>
</div>
</div>`
}
}
if(recType === 999999999) {
tipTpl = `<div class="list-item">
<p class="push-tip">
<span class="tip">${message.content}<a class="red-tip reconnect">连线客服</a></span>
</p>
</div>`;
}
$('.msg-list').append(tipTpl);
$('.panel-main .main-body').scrollTop($('.panel-main .main-body')[0].scrollHeight);
}
/**
* 处理发送消息
* @private
*/
function _sendMessage(e, msgType) {
var tag = $('.msg-area'),
msg = tag.val().trim();
if (!msg) return;
cM.type = 3;
cM.message.content = msg;
cM.message.type = msgType || 1;
cM.uuid = uuid.v4(); // 用于标示当条消息
socketChat.send(JSON.stringify(Object.assign(cM)));
tag.val('');
}
socketChat.init(Object.assign(config, {
onOpen: function (e) {
console.log('websocket opened!')
},
onMessage: function (e) {
var jsonString = e.data;
var received = JSON.parse(jsonString);
cM.conversationId = received.conversationId;
_getMessage(received);
},
onClose: function() {
console.log('websocket closed!')
}
}));
// 调整窗口位置
$(window).on('resize', function() {
var vh = $(this).height(),
vw = $(this).width();
$('.chat-panel').css({
width: vw - (vh - (70 + 950) / 1.7),
'margin-left': (vh - (70 + 950) / 1.7) / 2
}).show();
}).trigger('resize');
// 获取鼠标位置
$.fn.getCursorPosition = function() {
var el = $(this).get(0);
var pos = 0;
if ('selectionStart' in el) {
pos = el.selectionStart;
} else if ('selection' in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
};
// // 图片上传
// $('#sendImg').fileupload({
// dataType: 'json',
// url: 'test',
// done: function(e, data) {
// $.each(data.result.files, function(index, file) {
// $('<p/>').text(file.name).appendTo(document.body);
// });
// }
// });
// enter提交 ctrl+enter换行
$('.msg-edit .msg-area').on('keydown', msgEnterAction);
// 常见问题
$('.qa-list .q').on('click', toggleAnswer);
$('.send-order').on('click', sendOrderInfo);
// 订单信息、常见问题
$('.right-head .tab').click('click', switchTab);
... ... @@ -451,6 +556,9 @@ $('.emoji-component .emoji').on('click', setEmoji);
// 连线客服
$('.push-tip .reconnect').on('click', connectService);
// 发送
$('.msg-edit .send').on('click', _sendMessage);
// 记录鼠标位置
var cursorPo;
$('.msg-area').on('blur', function() {
... ...
... ... @@ -10,40 +10,10 @@ var servers,
server,
conversationMessage;
function _randomServer() {
return servers[new Date().getTime() % servers.length];
}
function _getMsg(received) {
var msg = received.message;
switch (received.type) {
case 1: // 返回进入聊天成功
break;
case 2: // 返回时说明已经分配了
break;
case 3: // 用户发消息
break;
case 10003: // 客服发消息
// var content = received.message.newContent || received.message.content;
// if (received.sessionId === conversationMessage.sessionId) {
// user = '我';
// } else if (received.csId === 0) {
// user = '机器人';
// } else {
// user = received.csName; //客服名称
// }
break;
case 999999999: // 断线
break;
default:
break;
}
return msg;
}
function socketInit(options) {
var param;
... ...
... ... @@ -8,11 +8,12 @@
var config = {
servers: [{host: '192.168.102.18', port: 10240}], // socket.yohobuy.com // 192.168.102.18 // 127.0.0.1
receivedType: {
进入聊天成功: 1,
客服已分配: 2,
用户发消息: 3,
客服发消息: 10003,
断线: 999999999
1: '用户进入聊天',
2: '连线客服成功',
3: '用户发消息',
103: '机器人消息',
10003: '客服发送消息',
999999999: '断线'
},
conversationMessage: {
objectId: '',
... ...
... ... @@ -314,8 +314,8 @@ $color-3a3a3a: #3a3a3a;
background-image: url('/img/service/tip.png');
}
.red-tip {
color: #ff0000;
a {
color: #d0021b;
text-decoration: none;
cursor: pointer;
}
... ... @@ -348,6 +348,7 @@ $color-3a3a3a: #3a3a3a;
}
.msg-bubble {
float: right;
position: relative;
display: inline-block;
max-width: 235px;
... ... @@ -664,6 +665,10 @@ $color-3a3a3a: #3a3a3a;
.prd-desc {
overflow: hidden;
a {
color: #333333;
}
.prd-nm {
margin-bottom: 9px;
}
... ...