whisper-cli.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // 客服的id
  2. var kf_id = 0;
  3. var kf_name = '';
  4. // 是否点击显示表情的标志
  5. var flag = 1;
  6. // 发送锁 标识
  7. var sendLock = 0;
  8. // 窗口大小标识
  9. var size = 1;
  10. // 是否显示默认的聊天记录
  11. var commChat = 1;
  12. // 连接服务器
  13. if(config != undefined && config.socket != undefined){
  14. // 创建一个Socket实例
  15. var socket = new WebSocket('ws://' + config.socket+'?apiToken=70b0a7d015ef9a5268b2bc7e3e96af24');
  16. // 加锁
  17. lockTextarea();
  18. //showSystem({content: '连接中...'});
  19. document.getElementById('title').innerText = '连接中...';
  20. // 打开Socket
  21. socket.onopen = function(res) {
  22. console.log('握手成功');
  23. // 登录
  24. var login_data = '{"type":"userInit", "uid": 1, "name" : "' + config.name +
  25. '", "avatar" : "' + config.avatar + '", "group" : ' + config.group + '}';
  26. socket.send(login_data);
  27. // 解锁
  28. unlockTextarea();
  29. };
  30. // 监听消息
  31. socket.onmessage = function(res) {
  32. var data = eval("("+res.data+")");
  33. switch(data['message_type']){
  34. // 服务端ping客户端
  35. case 'ping':
  36. socket.send('{"type":"ping"}');
  37. break;
  38. // 已经被分配了客服
  39. case 'connect':
  40. kf_id = data.data.kf_id;
  41. kf_name = data.data.kf_name;
  42. showSystem({content: '客服 ' + data.data.kf_name + ' 为您服务'});
  43. document.getElementById('title').innerHTML = '与 ' + kf_name + ' 交流中';
  44. if(1 == commChat){
  45. showChatLog();
  46. }
  47. unlockTextarea();
  48. break;
  49. // 排队等待
  50. case 'wait':
  51. lockTextarea();
  52. if('暂时没有客服上班,请稍后再咨询。' == data.data.content){
  53. socket.close();
  54. }
  55. document.getElementById('title').innerHTML = '请稍后再来';
  56. showSystem(data.data);
  57. break;
  58. // 监测聊天数据
  59. case 'chatMessage':
  60. showMsg(data.data);
  61. break;
  62. // 问候语
  63. case 'helloMessage':
  64. showMsg(data.data, 1);
  65. break;
  66. // 转接
  67. case 'relinkMessage':
  68. commChat = 2;
  69. document.getElementById('title').innerHTML = '正在转接中...';
  70. break;
  71. // 评价
  72. case 'evaluate':
  73. if (data.data.status ==1) {
  74. alert('成功')
  75. } else {
  76. alert('失败')
  77. }
  78. showMsg(aa);
  79. break;
  80. }
  81. };
  82. // 监听错误
  83. socket.onerror = function(err){
  84. showSystem({content: '连接失败'});
  85. document.getElementById('title').innerText = '连接失败';
  86. };
  87. }
  88. // 点击表情
  89. document.getElementById('up-face').onclick = function(e){
  90. e.stopPropagation();
  91. if(1 == flag){
  92. showFaces();
  93. document.getElementById('face-box').style.display = 'block';
  94. flag = 2;
  95. }else{
  96. document.getElementById('face-box').style.display = 'none';
  97. flag = 1;
  98. }
  99. };
  100. // 监听点击旁边关闭表情
  101. document.addEventListener("click",function(){
  102. if(2 == flag){
  103. document.getElementById('face-box').style.display = 'none';
  104. flag = 1;
  105. }
  106. });
  107. // 点击发送消息
  108. document.getElementById('send').onclick = function(){
  109. sendMsg();
  110. document.getElementById('msg').value = '';
  111. // 滚动条自动定位到最底端
  112. wordBottom();
  113. };
  114. // 改变窗体事件
  115. window.onresize = function(){
  116. if(1 == size){
  117. size = 2;
  118. document.getElementById('face-box').style.top = '58%';
  119. document.getElementById('chat-content-box').style.height = '75%';
  120. }else if(2 == size){
  121. size = 1;
  122. document.getElementById('face-box').style.top = '190px';
  123. document.getElementById('chat-content-box').style.height = '60%';
  124. }
  125. };
  126. // 图片 文件上传
  127. layui.use(['upload', 'layer'], function () {
  128. var upload = layui.upload;
  129. var layer = layui.layer;
  130. // 执行实例
  131. var uploadInstImg = upload.render({
  132. elem: '#up-image' // 绑定元素
  133. , accept: 'images'
  134. , exts: 'jpg|jpeg|png|gif'
  135. , url: '/index/upload/uploadImg' // 上传接口
  136. , done: function (res) {
  137. sendMsg('img[' + res.data.src + ']');
  138. showBigPic();
  139. }
  140. , error: function () {
  141. // 请求异常回调
  142. }
  143. });
  144. });
  145. // 获取时间
  146. function getTime(){
  147. var myDate = new Date();
  148. var hour = myDate.getHours();
  149. var minute = myDate.getMinutes();
  150. if(hour < 10) hour = '0' + hour;
  151. if(minute < 10) minute = '0' + minute;
  152. return hour + ':' + minute;
  153. }
  154. // 展示系统消息
  155. function showSystem(msg){
  156. var _html = document.getElementById('chat-list').innerHTML;
  157. _html += '<div class="whisper-chat-system"><span>' + msg.content + '</span></div>';
  158. document.getElementById('chat-list').innerHTML = _html;
  159. }
  160. //机器人
  161. function aa() {
  162. socket.send(JSON.stringify({
  163. type: 'toRobot',
  164. data: {groups_id: 1, robot_name: '范德萨1', robotgroups_id: 1,
  165. from_id: config.uid}
  166. }));
  167. }
  168. //评价
  169. function bb() {
  170. socket.send(JSON.stringify({
  171. type: 'evaluate',
  172. data: {
  173. evaluate_id: 1, from_id: config.uid,
  174. }
  175. }));
  176. }
  177. // 发送信息
  178. function sendMsg(sendMsg){
  179. if(1 == sendLock){
  180. return false;
  181. }
  182. var msg = (typeof(sendMsg) == 'undefined') ? document.getElementById('msg').value : sendMsg;
  183. if('' == msg){
  184. return false;
  185. }
  186. var _html = document.getElementById('chat-list').innerHTML;
  187. var time = getTime();
  188. var content = replaceContent(msg);
  189. _html += '<div class="chat-mine">';
  190. _html += '<div class="author-name">我 ';
  191. _html += '<small class="chat-date">' + time + '</small>';
  192. _html += '</div><div class="chat-text">' + content + '</div></div>';
  193. // 发送消息
  194. socket.send(JSON.stringify({
  195. type: 'chatMessage',
  196. data: {to_id: kf_id, content: msg, from_id: config.uid}
  197. }));
  198. // 储存我发出的信息
  199. var key = kf_id + '-' + config.uid;
  200. if(typeof(Storage) !== "undefined"){
  201. var localMsg = getCache(key);
  202. if(localMsg == null || localMsg.length == 0){
  203. localMsg = [];
  204. }
  205. localMsg.push({type: 'mine', name: '我', time: time, content: content});
  206. cacheChat({key: key, data: localMsg});
  207. }
  208. document.getElementById('chat-list').innerHTML = _html;
  209. // 滚动条自动定位到最底端
  210. wordBottom();
  211. showBigPic();
  212. }
  213. // 展示发送来的消息
  214. function showMsg(info, flag){
  215. // 清除系统消息
  216. document.getElementsByClassName('whisper-chat-system').innerHTML = '';
  217. var _html = document.getElementById('chat-list').innerHTML;
  218. var content = replaceContent(info.content);
  219. _html += '<div class="chat-other">';
  220. _html += '<div class="author-name">' + info.name + ' ';
  221. _html += '<small class="chat-date">' + info.time + '</small>';
  222. _html += '</div><div class="chat-text">' + content + '</div></div>';
  223. document.getElementById('chat-list').innerHTML = _html;
  224. showBigPic();
  225. // 滚动条自动定位到最底端
  226. wordBottom();
  227. // 储存我收到的信息
  228. var key = kf_id + '-' + config.uid;
  229. if(typeof(Storage) !== "undefined" && typeof(flag) == "undefined"){
  230. var localMsg = getCache(key);
  231. if(localMsg == null || localMsg.length == 0){
  232. localMsg = [];
  233. }
  234. localMsg.push({type: 'other', name: info.name, time: info.time, content: content});
  235. cacheChat({key: key, data: localMsg});
  236. }
  237. }
  238. // 展示表情数据
  239. function showFaces(){
  240. var alt = getFacesIcon();
  241. var _html = '<ul>';
  242. var len = alt.length;
  243. for(var index = 0; index < len; index++){
  244. _html += '<li title="' + alt[index] + '" onclick="checkFace(this)"><img src="/static/customer/images/face/'+ index + '.gif" /></li>';
  245. }
  246. _html += '</ul>';
  247. document.getElementById('face-box').innerHTML = _html;
  248. }
  249. // 选择表情
  250. function checkFace(obj){
  251. var msg = document.getElementById('msg').value;
  252. document.getElementById('msg').value = msg + ' face' + obj.title + ' ';
  253. document.getElementById('msg').focus();
  254. document.getElementById('face-box').style.display = 'none';
  255. flag = 1;
  256. }
  257. // 缓存聊天数据 [本地存储策略]
  258. function cacheChat(obj){
  259. if(typeof(Storage) !== "undefined"){
  260. localStorage.setItem(obj.key, JSON.stringify(obj.data));
  261. }
  262. }
  263. // 从本地缓存中,拿出数据
  264. function getCache(key){
  265. return JSON.parse(localStorage.getItem(key));
  266. }
  267. // 展示本地聊天缓存
  268. function showChatLog(){
  269. var chatLog = getCache(kf_id + '-' + config.uid);
  270. if(chatLog == null || chatLog.length == 0){
  271. return ;
  272. }
  273. var _html = '';
  274. var len = chatLog.length;
  275. for(var i = 0; i < len; i++){
  276. var item = chatLog[i];
  277. if('mine' == item.type){
  278. _html += '<div class="chat-mine">';
  279. _html += '<div class="author-name">' + item.name + ' ';
  280. _html += '<small class="chat-date">' + item.time + '</small>';
  281. _html += '</div><div class="chat-text">' + item.content + '</div></div>';
  282. }else if('other' == item.type){
  283. _html += '<div class="chat-other">';
  284. _html += '<div class="author-name">' + item.name + ' ';
  285. _html += '<small class="chat-date">' + item.time + '</small>';
  286. _html += '</div><div class="chat-text">' + item.content + '</div></div>';
  287. }
  288. }
  289. document.getElementById('chat-list').innerHTML = _html;
  290. showBigPic();
  291. // 滚动条自动定位到最底端
  292. wordBottom();
  293. }
  294. // 锁住输入框
  295. function lockTextarea(){
  296. sendLock = 1;
  297. document.getElementById('msg').setAttribute('readonly', 'readonly');
  298. }
  299. // 解锁输入框
  300. function unlockTextarea(){
  301. sendLock = 0;
  302. document.getElementById('msg').removeAttribute('readonly');
  303. }
  304. // 双击图片
  305. function showBigPic(){
  306. layui.use('jquery', function(){
  307. var $ = layui.jquery;
  308. $(".layui-whisper-photos").on('click', function () {
  309. var src = this.src;
  310. layer.photos({
  311. photos: {
  312. data: [{
  313. "alt": "大图模式",
  314. "src": src
  315. }]
  316. }
  317. , shade: 0.5
  318. , closeBtn: 2
  319. , anim: 0
  320. , resize: false
  321. , success: function (layero, index) {
  322. }
  323. });
  324. });
  325. });
  326. }
  327. // 对话框定位到最底端
  328. function wordBottom(){
  329. var ex = document.getElementById("chat-list");
  330. ex.scrollTop = ex.scrollHeight;
  331. }