Authored by 李奇

代码优化

... ... @@ -9,15 +9,15 @@
var $ = require('yoho-jquery'),
uuid = require('uuid'),
serviceApi = require('./service-api'),
view = require('./view'),
broswer = require('./broswer'),
cursor = require('./cursor'),
view = require('./view'),
util = require('./utility'),
send = require('./socket/send'),
edit = require('./socket/edit'),
receive = require('./socket/receive'),
util = require('./utility'),
clipPaste = require('./clip-paste'),
serviceApi = require('./service-api'),
receive = require('./socket/receive'),
socketChat = require('./socket/chat'),
socketConf = require('./socket/config'),
processInfo = require('./view').processInfo;
... ... @@ -28,12 +28,10 @@ var allRTs,
encryptedUid,
socketConfCM,
assetsPrefix,
titleInterval,
$html = $('html'),
$window = $(window),
$document = $(document),
$msgList = $('.msg-list'),
docTitle = document.title,
$msgEdit = $('.msg-edit'),
$leaveMsg = $('#leaveMsg'),
$close = $('.header .close'),
... ... @@ -41,7 +39,6 @@ var allRTs,
$sendImgInput = $('#sendImg'),
$history = $('.about-his.has-his'),
$msgArea = $('.msg-edit .msg-area'),
$makeEvalModal = $('#makeEvaluation'),
$rightHeadTab = $('.right-head .tab'),
$encryptedUid = $('input[name=encryptedUid]'),
$assetsPrefix = $('input[name=assetsPrefix]'),
... ... @@ -50,8 +47,7 @@ var allRTs,
var tipTpl = require('hbs/service/tip.hbs'),
csTpl = require('hbs/service/cs-msg.hbs'),
cusTpl = require('hbs/service/cus-msg.hbs'),
robotTpl = require('hbs/service/robot-msg.hbs'),
disTpl = require('hbs/service/discontent-row.hbs');
robotTpl = require('hbs/service/robot-msg.hbs');
require('bootstrap');
require('../common');
... ... @@ -124,41 +120,24 @@ var _loadPage = function() {
var $countdown = $('.countdown');
$countdown.parents('.list-item').remove();
if (titleInterval) {
clearInterval(titleInterval);
document.title = docTitle;
}
util.removeBlinkAlert();
}
/**
* 处理发送消息
* @param e
* @param msgType
* @param msgContent
*/
function sendMessage(e, msgType, msgContent) {
function sendMessage() {
var $area = $('.msg-area'),
msg = $area.val().trim();
beforeSendMsg();
if (!msgContent) {
if (!msg) {
return;
}
socketConfCM.type = allRTs.CU_SEND;
socketConfCM.uuid = uuid.v4();
socketConfCM.chatMessage.content = msg;
socketConfCM.chatMessage.type = msgType || 1;
socketChat.send(socketConfCM);
$area.val('');
} else {
socketConfCM.type = allRTs.CU_SEND;
socketConfCM.uuid = uuid.v4();
socketConfCM.chatMessage.type = msgType || 1;
socketConfCM.chatMessage.content = msgContent;
socketChat.send(socketConfCM);
if (!msg) {
return;
}
send.text(msg);
$area.val('');
}
/**
... ... @@ -174,14 +153,6 @@ var _loadPage = function() {
}
/**
* 人工客服
*/
function manualService() {
socketConfCM.type = allRTs.MANUAL_SERVICE;
socketChat.send(socketConfCM);
}
/**
* 正在进行人工会话
*/
function csChatting(message) {
... ... @@ -405,19 +376,8 @@ var _loadPage = function() {
isHidden = broswer.tabIsHidden(),
allTypes = socketConf.recType;
// 删除上个定时器
if (titleInterval) {
clearInterval(titleInterval);
}
if (isHidden) {
titleInterval = setInterval(function() {
document.title = '您有新消息';
setTimeout(function() {
document.title = docTitle;
}, 300);
}, 600);
}
// 提醒
isHidden && util.addBlinkAlert();
// 预处理
receive.preProcess(rec);
... ... @@ -691,10 +651,7 @@ var _loadPage = function() {
});
// tab页title重置
broswer.tabVisible(function() {
document.title = docTitle;
clearInterval(titleInterval);
});
broswer.tabVisible(util.removeBlinkAlert);
// 校验留言输入内容
$leaveMsg.find('textarea').bind('change', function() {
... ... @@ -752,7 +709,7 @@ var _loadPage = function() {
var msgContent = ['单号:', no, '金额:', '¥' + nm, '下单时间:', time, '状态:', status];
sendMessage(e, 10, msgContent);
send.order(msgContent);
});
// 发送图片
... ... @@ -773,11 +730,7 @@ var _loadPage = function() {
beforeSendMsg();
// 上传成功后发送图片消息
socketConfCM.type = allRTs.CU_SEND;
socketConfCM.chatMessage.content = res.data.filePath;
socketConfCM.chatMessage.type = 2;
socketConfCM.uuid = uuid.v4();
socketChat.send(socketConfCM);
send.image(res.data.filePath);
}
setTimeout(function() {
$('.progress-bar').fadeOut();
... ... @@ -871,7 +824,7 @@ var _loadPage = function() {
},
3: function() {
manualService();
send.manual();
}
};
... ...
... ... @@ -14,18 +14,44 @@ var conMSG = sockConf.conversationMessage;
* @param {msg} 消息体
*/
function _send(msg) {
conMSG.uuid = uuid.v4();
chat.send(msg);
}
/**
* 文本发送
*/
exports.text = function(content) {
var TEXT_TYPE = 1; // 文本消息
conMSG.type = recTypes.CU_SEND;
conMSG.chatMessage.content = content;
conMSG.chatMessage.type = TEXT_TYPE;
_send(conMSG);
};
/**
* 图片发送
*/
exports.image = function(path) {
var IMG_TYPE = 2; // 图片消息
conMSG.type = recTypes.CU_SEND;
conMSG.chatMessage.content = path;
conMSG.chatMessage.type = IMG_TYPE;
conMSG.uuid = uuid.v4();
_send(conMSG);
};
/**
* 订单发送
*/
exports.order = function(content) {
var ORDER_TYPE = 10; // 订单消息
conMSG.type = recTypes.CU_SEND;
conMSG.chatMessage.type = ORDER_TYPE;
conMSG.chatMessage.content = content;
_send(conMSG);
};
... ... @@ -35,7 +61,14 @@ exports.image = function(path) {
exports.completeEval = function() {
if (chat.isOpen()) {
conMSG.type = recTypes.EVAL_NOTICE;
conMSG.uuid = uuid.v4();
_send(conMSG);
}
};
/**
* 人工客服
*/
exports.manual = function() {
conMSG.type = recTypes.MANUAL_SERVICE;
_send(conMSG);
};
... ...
var blinkInterval;
var docTitle = document.title;
/**
* 检查是否支持 websocket
* @returns {boolean}
... ... @@ -5,3 +8,31 @@
exports.isSupport = function() {
return !!window.WebSocket;
};
/**
* 添加闪烁提醒
* @returns {boolean}
*/
exports.addBlinkAlert = function() {
if (blinkInterval) {
clearInterval(blinkInterval);
}
blinkInterval = setInterval(function() {
document.title = '您有新消息';
setTimeout(function() {
document.title = docTitle;
}, 300);
}, 600);
};
/**
* 去除闪烁提醒
* @returns {boolean}
*/
exports.removeBlinkAlert = function() {
if (blinkInterval) {
clearInterval(blinkInterval);
document.title = docTitle;
}
};
... ...