whisper-cli.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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);
  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": ' + config.uid + ', "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. };
  73. // 监听错误
  74. socket.onerror = function(err){
  75. showSystem({content: '连接失败'});
  76. document.getElementById('title').innerText = '连接失败';
  77. };
  78. }
  79. // 点击表情
  80. document.getElementById('up-face').onclick = function(e){
  81. e.stopPropagation();
  82. if(1 == flag){
  83. showFaces();
  84. document.getElementById('face-box').style.display = 'block';
  85. flag = 2;
  86. }else{
  87. document.getElementById('face-box').style.display = 'none';
  88. flag = 1;
  89. }
  90. };
  91. // 监听点击旁边关闭表情
  92. document.addEventListener("click",function(){
  93. if(2 == flag){
  94. document.getElementById('face-box').style.display = 'none';
  95. flag = 1;
  96. }
  97. });
  98. // 点击发送消息
  99. document.getElementById('send').onclick = function(){
  100. sendMsg();
  101. document.getElementById('msg').value = '';
  102. // 滚动条自动定位到最底端
  103. wordBottom();
  104. };
  105. // 改变窗体事件
  106. window.onresize = function(){
  107. if(1 == size){
  108. size = 2;
  109. document.getElementById('face-box').style.top = '58%';
  110. document.getElementById('chat-content-box').style.height = '75%';
  111. }else if(2 == size){
  112. size = 1;
  113. document.getElementById('face-box').style.top = '190px';
  114. document.getElementById('chat-content-box').style.height = '60%';
  115. }
  116. };
  117. // 图片 文件上传
  118. layui.use(['upload', 'layer'], function () {
  119. var upload = layui.upload;
  120. var layer = layui.layer;
  121. // 执行实例
  122. var uploadInstImg = upload.render({
  123. elem: '#up-image' // 绑定元素
  124. , accept: 'images'
  125. , exts: 'jpg|jpeg|png|gif'
  126. , url: '/index/upload/uploadImg' // 上传接口
  127. , done: function (res) {
  128. sendMsg('img[' + res.data.src + ']');
  129. showBigPic();
  130. }
  131. , error: function () {
  132. // 请求异常回调
  133. }
  134. });
  135. });
  136. // 获取时间
  137. function getTime(){
  138. var myDate = new Date();
  139. var hour = myDate.getHours();
  140. var minute = myDate.getMinutes();
  141. if(hour < 10) hour = '0' + hour;
  142. if(minute < 10) minute = '0' + minute;
  143. return hour + ':' + minute;
  144. }
  145. // 展示系统消息
  146. function showSystem(msg){
  147. var _html = document.getElementById('chat-list').innerHTML;
  148. _html += '<div class="whisper-chat-system"><span>' + msg.content + '</span></div>';
  149. document.getElementById('chat-list').innerHTML = _html;
  150. }
  151. // 发送信息
  152. function sendMsg(sendMsg){
  153. if(1 == sendLock){
  154. return false;
  155. }
  156. var msg = (typeof(sendMsg) == 'undefined') ? document.getElementById('msg').value : sendMsg;
  157. if('' == msg){
  158. return false;
  159. }
  160. var _html = document.getElementById('chat-list').innerHTML;
  161. var time = getTime();
  162. var content = replaceContent(msg);
  163. _html += '<div class="chat-mine">';
  164. _html += '<div class="author-name">我 ';
  165. _html += '<small class="chat-date">' + time + '</small>';
  166. _html += '</div><div class="chat-text">' + content + '</div></div>';
  167. // 发送消息
  168. socket.send(JSON.stringify({
  169. type: 'chatMessage',
  170. data: {to_id: kf_id, to_name: kf_name, content: msg, from_name: config.name,
  171. from_id: config.uid, from_avatar: config.avatar}
  172. }));
  173. // 储存我发出的信息
  174. var key = kf_id + '-' + config.uid;
  175. if(typeof(Storage) !== "undefined"){
  176. var localMsg = getCache(key);
  177. if(localMsg == null || localMsg.length == 0){
  178. localMsg = [];
  179. }
  180. localMsg.push({type: 'mine', name: '我', time: time, content: content});
  181. cacheChat({key: key, data: localMsg});
  182. }
  183. document.getElementById('chat-list').innerHTML = _html;
  184. // 滚动条自动定位到最底端
  185. wordBottom();
  186. showBigPic();
  187. }
  188. // 展示发送来的消息
  189. function showMsg(info, flag){
  190. // 清除系统消息
  191. document.getElementsByClassName('whisper-chat-system').innerHTML = '';
  192. var _html = document.getElementById('chat-list').innerHTML;
  193. var content = replaceContent(info.content);
  194. _html += '<div class="chat-other">';
  195. _html += '<div class="author-name">' + info.name + ' ';
  196. _html += '<small class="chat-date">' + info.time + '</small>';
  197. _html += '</div><div class="chat-text">' + content + '</div></div>';
  198. document.getElementById('chat-list').innerHTML = _html;
  199. showBigPic();
  200. // 滚动条自动定位到最底端
  201. wordBottom();
  202. // 储存我收到的信息
  203. var key = kf_id + '-' + config.uid;
  204. if(typeof(Storage) !== "undefined" && typeof(flag) == "undefined"){
  205. var localMsg = getCache(key);
  206. if(localMsg == null || localMsg.length == 0){
  207. localMsg = [];
  208. }
  209. localMsg.push({type: 'other', name: info.name, time: info.time, content: content});
  210. cacheChat({key: key, data: localMsg});
  211. }
  212. }
  213. // 展示表情数据
  214. function showFaces(){
  215. var alt = getFacesIcon();
  216. var _html = '<ul>';
  217. var len = alt.length;
  218. for(var index = 0; index < len; index++){
  219. _html += '<li title="' + alt[index] + '" onclick="checkFace(this)"><img src="/static/customer/images/face/'+ index + '.gif" /></li>';
  220. }
  221. _html += '</ul>';
  222. document.getElementById('face-box').innerHTML = _html;
  223. }
  224. // 选择表情
  225. function checkFace(obj){
  226. var msg = document.getElementById('msg').value;
  227. document.getElementById('msg').value = msg + ' face' + obj.title + ' ';
  228. document.getElementById('msg').focus();
  229. document.getElementById('face-box').style.display = 'none';
  230. flag = 1;
  231. }
  232. // 缓存聊天数据 [本地存储策略]
  233. function cacheChat(obj){
  234. if(typeof(Storage) !== "undefined"){
  235. localStorage.setItem(obj.key, JSON.stringify(obj.data));
  236. }
  237. }
  238. // 从本地缓存中,拿出数据
  239. function getCache(key){
  240. return JSON.parse(localStorage.getItem(key));
  241. }
  242. // 展示本地聊天缓存
  243. function showChatLog(){
  244. var chatLog = getCache(kf_id + '-' + config.uid);
  245. if(chatLog == null || chatLog.length == 0){
  246. return ;
  247. }
  248. var _html = '';
  249. var len = chatLog.length;
  250. for(var i = 0; i < len; i++){
  251. var item = chatLog[i];
  252. if('mine' == item.type){
  253. _html += '<div class="chat-mine">';
  254. _html += '<div class="author-name">' + item.name + ' ';
  255. _html += '<small class="chat-date">' + item.time + '</small>';
  256. _html += '</div><div class="chat-text">' + item.content + '</div></div>';
  257. }else if('other' == item.type){
  258. _html += '<div class="chat-other">';
  259. _html += '<div class="author-name">' + item.name + ' ';
  260. _html += '<small class="chat-date">' + item.time + '</small>';
  261. _html += '</div><div class="chat-text">' + item.content + '</div></div>';
  262. }
  263. }
  264. document.getElementById('chat-list').innerHTML = _html;
  265. showBigPic();
  266. // 滚动条自动定位到最底端
  267. wordBottom();
  268. }
  269. // 锁住输入框
  270. function lockTextarea(){
  271. sendLock = 1;
  272. document.getElementById('msg').setAttribute('readonly', 'readonly');
  273. }
  274. // 解锁输入框
  275. function unlockTextarea(){
  276. sendLock = 0;
  277. document.getElementById('msg').removeAttribute('readonly');
  278. }
  279. // 双击图片
  280. function showBigPic(){
  281. layui.use('jquery', function(){
  282. var $ = layui.jquery;
  283. $(".layui-whisper-photos").on('click', function () {
  284. var src = this.src;
  285. layer.photos({
  286. photos: {
  287. data: [{
  288. "alt": "大图模式",
  289. "src": src
  290. }]
  291. }
  292. , shade: 0.5
  293. , closeBtn: 2
  294. , anim: 0
  295. , resize: false
  296. , success: function (layero, index) {
  297. }
  298. });
  299. });
  300. });
  301. }
  302. // 对话框定位到最底端
  303. function wordBottom(){
  304. var ex = document.getElementById("chat-list");
  305. ex.scrollTop = ex.scrollHeight;
  306. }