client.page.js
32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
/**
* 在线客服客户页
*
* @author: liqi <qi.li@yoho.cn>
* @date: 2016/10/21
*/
'use strict';
var $ = require('yoho-jquery'),
uuid = require('uuid'),
emojiMap = require('./emoji-map'),
tab = require('./tab-hidden'),
socketChat = require('./socket-chat'),
socketConf = require('./socket-config');
var allRTs,
endTime,
originConf,
encryptedUid,
socketConfCM,
assetsPrefix,
cursorPosition,
configDomains,
hasMore = true,
titleInterval,
$html = $('html'),
$window = $(window),
$document = $(document),
$msgList = $('.msg-list'),
docTitle = document.title,
$msgEdit = $('.msg-edit'),
$leaveMsg = $('#leaveMsg'),
$close = $('.header .close'),
$sendOrder = $('.send-order'),
$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]'),
$panelMainBody = $('.panel-main .main-body');
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');
var processInfo = {
scrollLoad: false,
manual: false,
promoter: 1, // 评论发起者 1:客户自己 2:客服
savedEval: false // 是否保存过评论
};
var key,
urls = {
makeEval: '/evalute/saveEvalute',
leaveMsg: '/leavemessage/saveLeavemessage',
msgHistory: '/conversationMessage/pageList',
evalReason: '/evalute/queryReasonByConversationId'
};
require('bootstrap');
require('../common');
require('blueimp-file-upload/js/jquery.iframe-transport');
require('blueimp-file-upload/js/jquery.fileupload');
require('blueimp-file-upload/js/jquery.fileupload-process');
require('blueimp-file-upload/js/jquery.fileupload-validate');
allRTs = socketConf.recType;
encryptedUid = $encryptedUid.val();
assetsPrefix = $assetsPrefix.val();
socketConfCM = socketConf.conversationMessage;
socketConfCM.encryptedUid = encryptedUid;
// 页面初始化
function pageInit() {
// 原始配置信息用于重新连线
originConf = JSON.parse(JSON.stringify(socketConf));
/**
* 设置光标位置
* @param el
* @param pos
*/
function setCurPos(el, pos) {
var range;
if (el.setSelectionRange) {
// chrome and firefox support
el.setSelectionRange(pos, pos);
el.focus();
} else if (el.createTextRange) {
// IE support
range = el.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
/**
* 添加新的消息
*/
function msgAppend(dom) {
$msgList.append(dom);
$panelMainBody.scrollTop($panelMainBody[0].scrollHeight);
}
/**
* 表情设置
* @param e
*/
function setEmoji(e) {
var i,
pos,
start,
end,
newVal,
textDom,
emojiText,
tag = $(e.target),
area = $('.msg-area'),
val = area.val(),
len = emojiMap.length,
emojiId = tag.data('id'),
comp = $('.emoji-component');
for (i = 0; i < len; i++) {
if (emojiMap[i].file === emojiId.toString()) {
emojiText = emojiMap[i].text;
}
}
if (cursorPosition) {
start = val.substring(0, cursorPosition);
end = val.substring(cursorPosition);
newVal = [start, emojiText, end].join('');
} else {
newVal = [val, emojiText].join('');
}
comp.hide();
area.val(newVal);
cursorPosition += emojiText.length;
textDom = area[0];
pos = cursorPosition ? cursorPosition : newVal.length;
setCurPos(textDom, pos);
}
/**
* 系统通知
* @param tip 具体消息
*/
function systemTip(tip) {
var dom;
dom = tipTpl({
content: tip
});
// 添加消息
msgAppend(dom);
}
/**
* 断线重连提醒
*/
function offlineTip(message) {
var tip;
if (!message) {
tip = '您已断线,请重新<a class="red-tip reconnect">连线客服</a>';
return systemTip(tip);
}
message.content = message.content +
'<a class="red-tip reconnect">连线客服</a>';
// 系统通知
systemTip(message.content);
}
/**
* 发送消息前的共通处理
*/
function beforeSendMsg() {
var $countdown = $('.countdown');
$countdown.parents('.list-item').remove();
if (titleInterval) {
clearInterval(titleInterval);
document.title = docTitle;
}
}
/**
* 处理发送消息
* @param e
* @param msgType
* @param msgContent
*/
function sendMessage(e, msgType, msgContent) {
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);
}
}
/**
* enter提交
* enter+ctrl换行
*/
function msgEnterAction(e) {
var code = e.keyCode,
tag = $(e.target),
val = tag.val(),
pos = tag.getCursorPosition(),
start = val.substring(0, pos),
end = val.substring(pos),
newVal = [start, '\n', end].join(''),
ctrlLike = e.ctrlKey || e.metaKey;
if (code !== 13) {
return;
}
if (!ctrlLike) {
e.preventDefault();
sendMessage(e);
} else {
tag.val(newVal);
setCurPos(tag[0], pos + 1);
}
}
/**
* 常见问题
*/
function toggleAnswer(e) {
var cTag = $(e.currentTarget),
parent = cTag.parent(),
next = cTag.next();
next.length && next.toggle();
next.length && parent.toggleClass('has-a');
}
/**
* 人工客服
*/
function manualService() {
socketConfCM.type = allRTs.MANUAL_SERVICE;
socketChat.send(socketConfCM);
}
/**
* 正在进行人工会话
*/
function csChatting(message) {
// 系统通知
systemTip(message.newContent);
}
/**
* 留言
*/
function leaveMsg() {
var $lm = $('#leaveMsg'),
$ta = $lm.find('textarea');
$lm.find('.leave-msg-tip').addClass('hide');
$ta.val('');
$lm.modal({
show: true
});
}
/**
* 成功进入在线客服
* @param message 消息对象
*/
function enterSuccess(message) {
var $ms = $('.icon.manual-service');
// 系统通知
systemTip(message.content);
// 显示人工客服
$ms.show();
}
/**
* 人工链接
* @param msgType 内部消息类型
* @param message 内部消息对象
* @private
*/
function linkSuccess(msgType, message) {
var OUT_SERVICE = 0,
LINE_UP = 1,
MANUAL_SERVICE = 2,
ADMIN_MANUAL_SERVICE = 3;
var dom,
tipText,
$iconEval = $('.icon.evaluate'),
$iconMs = $('.icon.manual-service');
switch (msgType) {
case OUT_SERVICE: // 0是没上班
tipText = message.content +
'您也可以选择<a class="leave-msg">留言</a>';
dom = tipTpl({
content: tipText
});
break;
case LINE_UP: // 1是需要排队
tipText = message.content +
'您也可以选择<a class="leave-msg">留言</a>';
dom = tipTpl({
content: tipText
});
// 隐藏人工
$iconMs.hide();
break;
case MANUAL_SERVICE: // 2是接入人工成功
tipText = message.content;
dom = tipTpl({
content: tipText
});
// 接入人工客服需要评价
processInfo.manual = true;
// 显示评价&隐藏人工
$iconEval.show();
$iconMs.hide();
break;
case ADMIN_MANUAL_SERVICE: // 3是管理员分配客服成功
tipText = message.content;
dom = tipTpl({
content: tipText
});
// 接入人工客服需要评价
processInfo.manual = true;
// 显示评价&隐藏人工
$iconEval.show();
$iconMs.hide();
break;
default:
break;
}
// 添加消息
msgAppend(dom);
}
/**
* 返回表情路径处理
* @param text 文本
* @private
*/
function emojiPrefix(text) {
if (typeof text === 'string') {
return text.replace(/src="(\d{3}).gif"/g, 'src="' + assetsPrefix + '/img/service/emoji/$1.gif"');
}
return text;
}
/**
* 处理客户消息
* @private
*/
function handleCusMsg(rec, msgType, message) {
var dom;
message.newContent = emojiPrefix(message.newContent);
// 用户头像处理
if (!rec.userHead) {
rec.userHead = assetsPrefix + socketConf.defaultUserHead;
}
// 图片添加标签
if (msgType === 2) {
message.newContent = '<img class="img-msg" src="' + message.content + '">';
dom = cusTpl({
userHead: rec.userHead,
userName: rec.userName,
sendTimeShort: rec.sendTimeShort,
newContent: message.newContent
});
var image = new Image();
image.src = message.content;
image.onload = function() {
msgAppend(dom);
};
} else {
dom = cusTpl({
userHead: rec.userHead,
userName: rec.userName,
sendTimeShort: rec.sendTimeShort,
newContent: message.newContent
});
msgAppend(dom);
}
}
/**
* 链接终端倒计时
* @private
*/
function breakCountdown(message) {
var dom,
tip;
tip = message.content +
'<span class="countdown"></span>';
dom = tipTpl({
content: tip
});
// 消息添加
msgAppend(dom);
}
/**
* 处理客服消息
* @private
*/
function handleCsMsg(rec, msgType, message) {
var dom;
message.newContent = emojiPrefix(message.newContent);
if (msgType === 2) {
message.newContent = '<img class="img-msg" src="' + message.content + '">';
dom = cusTpl({
userHead: rec.userHead,
userName: rec.userName,
sendTimeShort: rec.sendTimeShort,
newContent: message.newContent
});
var image = new Image();
image.src = message.content;
image.onload = function() {
msgAppend(dom);
};
} else {
dom = csTpl({
csHead: rec.csHead,
csName: rec.csName,
sendTimeShort: rec.sendTimeShort,
newContent: message.newContent
});
msgAppend(dom);
}
}
/**
* 显示评价弹框
*/
function showEvalModal(isclose) {
var $evalModal = $('#makeEvaluation');
// 没有接入人工
if (!processInfo.manual) {
return;
}
// 评价原因
function discontentHtml(len, data) {
var i,
dom = '';
for (i = 0; i < len; i = i + 2) {
dom += disTpl({
id1: data[i].id,
id1Content: data[i].content,
id2: data[i + 1] && data[i + 1].id,
id2Content: data[i + 1] && data[i + 1].content
});
}
$evalModal.find('.discontent').empty().append(dom);
}
// 拉取评价原因
$.ajax({
type: 'GET',
url: urls.evalReason,
data: {
conversationId: socketConfCM.conversationId
},
success: function success(res) {
var data = res.data,
len = data.length;
if (res && res.code === 200) {
// 评价原因
len && discontentHtml(len, data);
if (isclose) {
// 评价完成后,是否关闭窗口
window.close();
}
}
$evalModal.modal('show');
}
});
}
/**
* 处理收到消息
*/
function getMessage(rec) {
var dom,
recType = rec.type,
message = rec.chatMessage,
msgType = message ? message.type : -1,
isHidden = tab.tabIsHidden(),
allTypes = socketConf.recType;
console.log('客户收到消息!', rec);
// 删除上个定时器
if (titleInterval) {
clearInterval(titleInterval);
}
if (isHidden) {
titleInterval = setInterval(function() {
document.title = '您有新消息!';
setTimeout(function() {
document.title = docTitle;
}, 300);
}, 600);
}
switch (recType) {
case allTypes.ENTER:
enterSuccess(message);
break;
case allTypes.LINK_SUCCESS:
linkSuccess(msgType, message);
break;
case allTypes.CU_SEND:
handleCusMsg(rec, msgType, message);
break;
case allTypes.BREAK_TIME:
breakCountdown(message);
break;
case allTypes.ROBOT_SEND:
rec.csName = rec.csName || '客服机器人';
dom = robotTpl({
assetsPrefix: assetsPrefix,
csName: rec.csName,
sendTimeShort: rec.sendTimeShort,
newContent: message.newContent
});
break;
case allTypes.CS_SEND:
handleCsMsg(rec, msgType, message);
break;
case allTypes.EVAL_INVITE:
// 客服发起
if (!processInfo.savedEval) {
processInfo.promoter = 2;
showEvalModal();
}
break;
case allTypes.CS_CHATTING:
csChatting(message);
break;
case allTypes.OP_LEAVE:
offlineTip(message);
break;
case allTypes.OFFLINE:
offlineTip(message);
break;
default:
break;
}
// 添加新消息到消息列表
dom && msgAppend(dom);
}
/**
* 连接socket
*/
function connectSocket() {
var $reconnect,
$iconEval = $('.icon.evaluate'),
$iconMs = $('.icon.manual-service'),
$connectFail = $('.connect-fail'),
// 获取上次会话id
lastSessionId = window.cookie('sessionId');
originConf.conversationMessage.sessionId = lastSessionId || 0;
socketChat.init($.extend(originConf, {
onMessage: function onMessage(e) {
var jsonString = e.data,
received = JSON.parse(jsonString);
socketConfCM.conversationId = received.newConversationId !== 0 ?
received.newConversationId :
received.conversationId;
// 保存会话id
window.setCookie('conversationId', socketConfCM.conversationId, {expires: 60 * 15});
window.setCookie('sessionId', received.sessionId, {expires: 60 * 15});
getMessage(received);
},
onOpen: function() {
$iconMs.hide();
$iconEval.hide();
},
onClose: function() {
$iconEval.hide();
$reconnect = $msgList.find('.list-item')
.last().find('.reconnect');
if(!$reconnect.length) {
offlineTip();
}
},
connectFailCb: function() {
$connectFail.fadeIn();
},
socketClosedCb: function() {
offlineTip();
}
}));
}
/**
* 消息解析
* @param msgList
*/
function msgResolve(msgList) {
var i,
len,
item,
dom = '',
heightBefore,
heightAfter;
msgList = msgList.reverse();
len = msgList.length;
if (endTime === msgList[0].sendTime) {
hasMore = false;
}
endTime = msgList[0].sendTime;
if (hasMore) {
for (i = 0; i < len; i++) {
item = msgList[i];
item.chatMessage.newContent = emojiPrefix(item.chatMessage.newContent);
if (item.chatMessage.type === 2) {
item.chatMessage.newContent =
['<img class="img-msg" src="', item.chatMessage.newContent, '">'].join('');
}
switch (item.type) {
case allRTs.CU_SEND:
if (!item.userHead) {
item.userHead = assetsPrefix + socketConf.defaultUserHead;
}
dom = cusTpl({
userHead: item.userHead,
userName: item.userName,
sendTimeShort: item.sendTimeShort,
newContent: item.chatMessage.newContent
});
break;
case allRTs.ROBOT_SEND:
item.csName = item.csName || '客服机器人';
dom = robotTpl({
assetsPrefix: assetsPrefix,
csName: item.csName,
sendTimeShort: item.sendTimeShort,
newContent: item.chatMessage.newContent
});
break;
case allRTs.CS_SEND:
dom = csTpl({
csHead: item.csHead,
csName: item.csName,
sendTimeShort: item.sendTimeShort,
newContent: item.chatMessage.newContent
});
break;
default:
break;
}
}
heightBefore = $msgList.height();
$msgList.prepend(dom);
heightAfter = $msgList.height();
$panelMainBody.scrollTop(heightAfter - heightBefore);
}
}
/**
* 获取历史聊天记录
*/
function fetchHistoryMsg() {
var msgList = [];
var data = {
encryptedUid: encryptedUid
};
if (endTime) {
data.endTime = endTime;
}
$.ajax({
type: 'GET',
url: urls.msgHistory,
data: data,
success: function(res) {
if (res && res.code === 200) {
if (hasMore) {
msgList = res.data && res.data.records || [];
msgResolve(msgList);
}
$history.hide();
processInfo.scrollLoad = true;
}
}
});
}
// 用户信息获取并初始化socket连接
(function() {
var param = {
return_type: 'jsonp',
method: 'open.passport.get'
};
$.getJSON('//www.yohobuy.com/common/passport/?callback=?', param, function(jsonData) {
if (jsonData && jsonData.data && jsonData.data.result !== -1) {
socketConfCM.userHead = jsonData.data.headIco || '';
socketConfCM.userName = decodeURIComponent(window.getUser()[0]);
// 原始配置信息用于重新连线
originConf = JSON.parse(JSON.stringify(socketConf));
// 连接
connectSocket();
}
});
}());
// tab页title重置
tab.tabVisible(function() {
document.title = docTitle;
clearInterval(titleInterval);
});
// 校验留言输入内容
$leaveMsg.find('textarea').bind('change', function() {
var $lm = $('#leaveMsg'),
val = $(this).val();
if (val === '') {
$lm.find('.leave-msg-tip').removeClass('hide');
} else {
$lm.find('.leave-msg-tip').addClass('hide');
}
});
// 提交留言
$leaveMsg.find('.submit').click(function() {
var lMsg = $('#leaveMsg'),
val = lMsg.find('textarea').val().trim();
if (val === '') {
lMsg.find('.leave-msg-tip').removeClass('hide');
return false;
}
$.ajax({
type: 'POST',
url: urls.leaveMsg,
data: {
content: val,
encryptedUid: encryptedUid,
conversationId: socketConfCM.newConversationId || socketConfCM.conversationId
},
success: function(res) {
if (res && res.code === 200) {
lMsg.modal('hide');
}
},
error: function() {
lMsg.modal('hide');
}
});
});
// 提交评价
$makeEvalModal.find('.submit').click(function() {
var $btnEval = $('.icon.evaluate'),
mEval = $('#makeEvaluation'),
reason = mEval.find('textarea').val().trim(),
star = mEval.find('.star.positive').length,
reasonTypes = mEval.find('.dis-row .type.chosen');
var data = {
conversationId: socketConfCM.conversationId,
encryptedUid: encryptedUid,
promoter: processInfo.promoter,
stars: star
};
var reasonIds,
idArray = [];
if (star < 4) {
$.each(reasonTypes, function(index, item) {
idArray.push($(item).data('id'));
});
reasonIds = idArray.join(':');
data.reasonIds = reasonIds;
data.reasonMsg = reason;
}
$.ajax({
type: 'POST',
url: urls.makeEval,
data: data,
success: function(res) {
// 提交评价过后,不管是否成功都应该关闭弹框
processInfo.promoter = 1;
mEval.modal('hide');
$btnEval.hide();
processInfo.savedEval = true;
if (res && res.code === 200) {
// 评价后通知客服
socketConfCM.type = allRTs.EVAL_NOTICE;
socketConfCM.uuid = uuid.v4();
socketChat.send(socketConfCM);
}
},
error: function() {
mEval.modal('hide');
}
});
});
// 根节点高度设置
$html.css({
height: '100%'
});
// 获取鼠标位置
$.fn.getCursorPosition = function() {
var sel,
pos = 0,
selLength,
el = $(this).get(0);
if ('selectionStart' in el) {
pos = el.selectionStart;
} else if ('selection' in document) {
sel = document.selection.createRange();
selLength = document.selection.createRange().text.length;
el.focus();
sel.moveStart('character', -el.value.length);
pos = sel.text.length - selLength;
}
return pos;
};
// enter提交 ctrl+enter换行
$msgArea.on('keydown', msgEnterAction);
// 常见问题
$('.qa-list .q').on('click', toggleAnswer);
// 发送订单信息
$sendOrder.on('click', function(e) {
var tag = $(this),
no = tag.data('no'),
nm = tag.data('nm'),
time = tag.data('time'),
status = tag.data('status');
var msgContent = ['单号:', no, '金额:', '¥' + nm, '下单时间:', time, '状态:', status];
sendMessage(e, 10, msgContent);
});
// 发送图片
$sendImgInput.fileupload({
dataType: 'json',
acceptFileTypes: /(\.|\/)(bmp|gif|jpe?g|png)$/i,
maxFileSize: 5000000, // 5M
url: configDomains.imServer + '/fileManage/uploadFile',
progressall: function(e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('.progress-bar').css('width', progress + '%');
},
done: function(e, res) {
res = res.result;
if (res && res.code === 200) {
// 共通处理
beforeSendMsg();
// 上传成功后发送图片消息
socketConfCM.type = allRTs.CU_SEND;
socketConfCM.chatMessage.content = res.data.filePath;
socketConfCM.chatMessage.type = 2;
socketConfCM.uuid = uuid.v4();
socketChat.send(socketConfCM);
}
setTimeout(function() {
$('.progress-bar').fadeOut();
}, 500);
},
error: function() {
$('.progress-bar').css('width', 0);
$('.upload-tip').text('上传失败,请稍后重试').fadeIn();
setTimeout(function() {
$('.upload-tip').fadeOut().text('');
}, 2000);
}
}).on('fileuploadprocessalways', function(e, data) {
var $tip = $('.upload-tip'),
currentFile = data.files[data.index];
if (data.files.error && currentFile.error) {
switch (currentFile.error) {
case 'File type not allowed':
$tip.text('您上传的图片格式不正确').fadeIn();
break;
case 'File is too large':
$tip.text('请上传5M以内的图片').fadeIn();
break;
default:
break;
}
setTimeout(function() {
$tip.fadeOut().text('');
}, 2000);
}
});
// 订单信息、常见问题
$rightHeadTab.on('click', function() {
var $el = $(this),
$siblings = $el.siblings(),
$panRight = $('.panel-right'),
order = $el.hasClass('order'),
active = $el.hasClass('active'),
$qaUse = $panRight.find('.qa-use'),
$orderUse = $panRight.find('.order-use');
if (active) {
return;
}
$el.addClass('active');
$siblings.removeClass('active');
if (order) {
$qaUse.hide();
$orderUse.show();
} else {
$qaUse.show();
$orderUse.hide();
}
});
// 星评
$document.on('click', '.make-eval .star', function() {
var el,
i,
len,
tag = $(this),
index = tag.index(),
$detailReason = $('.detail-reason'),
textEl = $('.make-eval .star-text'),
startList = $('.make-eval .stars .star');
var starText = {
0: '非常不满意',
1: '不满意',
2: '一般',
3: '满意',
4: '非常满意'
};
for (i = 0, len = startList.length; i < len; i++) {
el = $(startList[i]);
el.index() <= index && el.addClass('positive');
el.index() > index && el.removeClass('positive');
}
index < 3 && $detailReason.show();
index >= 3 && $detailReason.hide();
textEl.text(starText[index]);
});
// 选择不满意类型
$document.on('click', '.dis-row .type', function(e) {
$(e.target).toggleClass('chosen');
});
// 留言
$msgList.on('click', '.leave-msg', leaveMsg);
// 消息编辑框功能区
$msgEdit.find('.util .icon').on('click', function(e) {
var cTa = $(this),
index = cTa.index(),
comp = $('.emoji-component');
var utilMap = {
0: function() {
var isNone,
compHeight,
topPo = cTa.offset().top,
leftPo = cTa.offset().left,
cTaHeight = cTa.height();
isNone = comp.css('display') === 'none';
comp.css({display: 'inline-block'});
compHeight = comp.height();
isNone && comp.css({
top: topPo - cTaHeight - compHeight - 5,
left: leftPo
});
!isNone && comp.hide();
},
2: function() {
showEvalModal();
},
3: function() {
manualService();
}
};
if (index === 1) {
return;
}
utilMap[index](e);
});
// 设置表情
$document.on('click', '.emoji-component .emoji', setEmoji);
// 表情组件隐藏
$document.on('click', function(e) {
var $t = $(e.target),
comp = $('.emoji-component');
if (!$t.hasClass('emoji') && !$t.hasClass('emoji-component')) {
comp.hide();
}
});
// 重新连线
$document.on('click', '.reconnect', function() {
$('.connect-fail').hide();
// 共通处理
beforeSendMsg();
connectSocket();
});
// 发送
$msgEdit.find('.send').on('click', sendMessage);
// 关闭聊天窗口
$close.click(function() {
if (processInfo.manual && !processInfo.savedEval) {
// 没有保存过评论
showEvalModal(true);
} else {
window.close();
}
});
// 失去焦点更新鼠标位置
$msgArea.on('blur', function() {
cursorPosition = $(this).getCursorPosition();
});
// 消息图片放大显示
$msgList.on('click', '.msg-bubble .img-msg', function(e) {
var $et = $(e.target),
$zoom = $('.img-zoom-in'),
$img = $zoom.find('img'),
maxH = $window.height() - 100;
$img.css('max-height', maxH);
$img.attr('src', $et.attr('src'));
$zoom.fadeIn();
});
$document.on('click', '.img-zoom-in', function() {
$(this).fadeOut();
});
// 显示历史记录
$history.click(fetchHistoryMsg);
// 滚动加载更多
$panelMainBody.scroll(function() {
var loading = $panelMainBody.scrollTop() === 0;
processInfo.scrollLoad && loading && hasMore && fetchHistoryMsg();
});
}
// 拉取域名信息
(function() {
$.ajax({
type: 'GET',
url: '/service/domains',
success: function(domains) {
configDomains = domains;
// url前缀添加
for (key in urls) {
if (urls.hasOwnProperty(key)) {
urls[key] = configDomains.imCs + urls[key];
}
}
// 检查支持性
(function() {
if (!window.WebSocket) {
$('.un-support').show();
return false;
} else {
socketConf.servers.push(configDomains.imSocket);
pageInit();
}
}());
}
});
}());
// 点击刷新提示
window.onbeforeunload = function() {
return '';
};