index.js
7.03 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
/*
yoho客服 im: 首页
@author acgpiano
*/
'use strict';
import { time } from './time';
import { tip } from './tip';
import { Customer, Employee} from './role';
import { RatingView, LeaveMSGView, OrderListView } from './view';
require('../../home/jquery.upload');
var saveImage = require('../../home/save-image');
var socket = require('./socket-chat'),
socketConf = require('./socket-config'),
cmEntity = socketConf.conversationMessage;
const chatBox = $('#chat-window'),
encryptedUid = $('#encrypteduid').val();
const append = (html) => {
chatBox.append(html);
};
let gg, mm;
// 聊天调整footer位置
const resizeFooter = () => {
let bottom = $('#chat-footer').height();
chatBox.css('bottom', `${bottom}px`);
chatBox[0].scrollTop = chatBox[0].scrollHeight;
};
var chat = {
// 初始化websocket
init: function() {
const self = this;
socketConf.conversationMessage.encryptedUid = encryptedUid;
$.ajax({
type: 'GET',
url: '/service/userinfo',
}).then(function(result) {
// 获取用户信息
gg = new Customer(result.head || cmEntity.userHead);
mm = new Employee(cmEntity.userHead);
self.fetchHistoryMsg(function() {
socket.init(Object.assign(socketConf, {
onMessage: function(e) {
var received = JSON.parse(e.data);
console.log(received);
cmEntity.conversationId = received.newConversationId > 0 ?
received.newConversationId :
received.conversationId;
self.getMessage(received);
}
}));
});
});
},
// 获取10条历史记录
fetchHistoryMsg: function() {
const self = this;
var data = {
encryptedUid: encryptedUid,
};
$.ajax({
type: 'GET',
url: '/service/msghistory',
data: data,
success: function(result) {
if (result && result.code === 200) {
for (let item of result.data) {
self.getMessage(item);
}
}
}
});
},
// 处理收到的消息
getMessage: function(rec) {
var recType = rec.type,
message = rec.message,
msgType = message.type,
allTypes = socketConf.recType;
switch (recType) {
case allTypes.ENTER:
this.enterSuccess(message);
break;
case allTypes.LINK_SUCCESS:
this.linkSuccess(msgType, message);
break;
case allTypes.CU_SEND:
this.handleCusMsg(rec, msgType, message);
break;
// case allTypes.BREAK_TIME:
// breakCountdown(message);
// break;
case allTypes.ROBOT_SEND:
rec.csName = rec.csName || '客服机器人';
append(mm.sendMsg(message.content));
resizeFooter();
break;
case allTypes.CS_SEND:
// 处理客服消息
this.handleCsMsg(rec, msgType, message);
break;
// case allTypes.EVAL_INVITE:
// // 客服发起
// processInfo.promoter = 2;
// showEvalModal();
// break;
// case allTypes.CS_CHATTING:
// // 正在人工会话
// csChatting(message);
// break;
case allTypes.OFFLINE:
append(time(Date.now()).show());
append(tip().offLine());
break;
default:
break;
}
},
// 刚进入提示
enterSuccess: function(message) {
append(time(Date.now()).show());
append(tip().offLine());
append(tip(message.content).onLine());
},
// 连线人工客服
linkSuccess: function(type, message) {
var status = $('.chat-status'),
name = $('.service-name'),
comment = $('.chat-comment');
if (type === 2 || type === 3) {
status.css('background', '#85be4a');
name.text('YOHO!客服');
comment.css('display', 'inline-block');
append(tip(message.content).onLine());
} else {
name.text('YOHO!客服');
}
},
// 用户发消息
handleCusMsg: function(rec, msgType, message) {
if (msgType === 2) {
append(gg.sendImg(message.content, true));
} else {
append(gg.sendMsg(message.content, true));
}
resizeFooter();
},
// 客服发消息
handleCsMsg: function(rec, msgType, message) {
if (msgType === 2) {
append(mm.sendImg(message.content));
} else {
append(mm.sendMsg(message.content));
}
resizeFooter();
}
};
chat.init();
// 用户发送消息
const sendMsg = function(type, content) {
if (!content) {
return;
} else {
cmEntity.type = 3;
cmEntity.message.content = content;
cmEntity.message.type = type || 1;
socket.send(JSON.stringify(cmEntity));
}
};
// 聊天区发送信息
$('.text-in').on('keydown', function(e) {
if (e.keyCode === 13) {
sendMsg(1, $(this).val());
$(this).val('');
}
});
// 触发菜单
$('.menu-trigger').on('touchend', function() {
let $menu = $('.menu');
if ($menu.css('display') === 'none') {
$menu.show();
resizeFooter();
} else {
$menu.hide();
resizeFooter();
}
$('.upload-img').upload({
auto: true,
fileType: 'image/*',
uploadScript: '/api/upload/image',
fileObjName: 'filename',
fileSizeLimit: 999999,
height: '100%',
width: '100%',
multi: false,
formData: {
bucket: 'suggest'
},
onUploadComplete: function(file, data) {
let url;
data = saveImage.saveImage(data);
url = data.imgList[0].imgRelUrl + '?imageView2/2/w/640/q/90';
url = 'http://img11.static.yhbimg.com/suggest' + url;
sendMsg(2, url);
}
});
$('.uploadifive-button').css('position', 'absolute');
});
// 评论
$('.chat-comment').on('touchend', function() {
let $comment = $('.comment'),
box = new RatingView($comment);
if ($comment.css('display') === 'none') {
$comment.show();
} else {
$comment.hide();
}
});
// 留言
let leaveMsg = new LeaveMSGView();
leaveMsg.init();
// 订单
let orderList = new OrderListView();
// 图片放大
$('#chat-window').on('click', '.chat-image', function() {
let img = $(this),
fake = $('.fake-img-wrap');
fake.find('img').attr('src', img.attr('src'));
fake.fadeIn();
fake.on('click', function() {
fake.fadeOut();
});
});