whisper-cli.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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=72fb0848e7bf20e052362bac4a86164c');
  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. function aa() {
  152. socket.send(JSON.stringify({
  153. type: 'toRobot',
  154. data: {groups_id: 1, robot_name: '范德萨1', robotgroups_id: 1,
  155. from_id: config.uid, from_avatar: 333}
  156. }));
  157. }
  158. // 发送信息
  159. function sendMsg(sendMsg){
  160. if(1 == sendLock){
  161. return false;
  162. }
  163. var msg = (typeof(sendMsg) == 'undefined') ? document.getElementById('msg').value : sendMsg;
  164. if('' == msg){
  165. return false;
  166. }
  167. var _html = document.getElementById('chat-list').innerHTML;
  168. var time = getTime();
  169. var content = replaceContent(msg);
  170. _html += '<div class="chat-mine">';
  171. _html += '<div class="author-name">我 ';
  172. _html += '<small class="chat-date">' + time + '</small>';
  173. _html += '</div><div class="chat-text">' + content + '</div></div>';
  174. // 发送消息
  175. socket.send(JSON.stringify({
  176. type: 'chatMessage',
  177. data: {to_id: kf_id, to_name: kf_name, content: msg, from_name: config.name,
  178. from_id: config.uid, from_avatar: config.avatar}
  179. }));
  180. // 储存我发出的信息
  181. var key = kf_id + '-' + config.uid;
  182. if(typeof(Storage) !== "undefined"){
  183. var localMsg = getCache(key);
  184. if(localMsg == null || localMsg.length == 0){
  185. localMsg = [];
  186. }
  187. localMsg.push({type: 'mine', name: '我', time: time, content: content});
  188. cacheChat({key: key, data: localMsg});
  189. }
  190. document.getElementById('chat-list').innerHTML = _html;
  191. // 滚动条自动定位到最底端
  192. wordBottom();
  193. showBigPic();
  194. }
  195. // 展示发送来的消息
  196. function showMsg(info, flag){
  197. // 清除系统消息
  198. document.getElementsByClassName('whisper-chat-system').innerHTML = '';
  199. var _html = document.getElementById('chat-list').innerHTML;
  200. var content = replaceContent(info.content);
  201. _html += '<div class="chat-other">';
  202. _html += '<div class="author-name">' + info.name + ' ';
  203. _html += '<small class="chat-date">' + info.time + '</small>';
  204. _html += '</div><div class="chat-text">' + content + '</div></div>';
  205. document.getElementById('chat-list').innerHTML = _html;
  206. showBigPic();
  207. // 滚动条自动定位到最底端
  208. wordBottom();
  209. // 储存我收到的信息
  210. var key = kf_id + '-' + config.uid;
  211. if(typeof(Storage) !== "undefined" && typeof(flag) == "undefined"){
  212. var localMsg = getCache(key);
  213. if(localMsg == null || localMsg.length == 0){
  214. localMsg = [];
  215. }
  216. localMsg.push({type: 'other', name: info.name, time: info.time, content: content});
  217. cacheChat({key: key, data: localMsg});
  218. }
  219. }
  220. // 展示表情数据
  221. function showFaces(){
  222. var alt = getFacesIcon();
  223. var _html = '<ul>';
  224. var len = alt.length;
  225. for(var index = 0; index < len; index++){
  226. _html += '<li title="' + alt[index] + '" onclick="checkFace(this)"><img src="/static/customer/images/face/'+ index + '.gif" /></li>';
  227. }
  228. _html += '</ul>';
  229. document.getElementById('face-box').innerHTML = _html;
  230. }
  231. // 选择表情
  232. function checkFace(obj){
  233. var msg = document.getElementById('msg').value;
  234. document.getElementById('msg').value = msg + ' face' + obj.title + ' ';
  235. document.getElementById('msg').focus();
  236. document.getElementById('face-box').style.display = 'none';
  237. flag = 1;
  238. }
  239. // 缓存聊天数据 [本地存储策略]
  240. function cacheChat(obj){
  241. if(typeof(Storage) !== "undefined"){
  242. localStorage.setItem(obj.key, JSON.stringify(obj.data));
  243. }
  244. }
  245. // 从本地缓存中,拿出数据
  246. function getCache(key){
  247. return JSON.parse(localStorage.getItem(key));
  248. }
  249. // 展示本地聊天缓存
  250. function showChatLog(){
  251. var chatLog = getCache(kf_id + '-' + config.uid);
  252. if(chatLog == null || chatLog.length == 0){
  253. return ;
  254. }
  255. var _html = '';
  256. var len = chatLog.length;
  257. for(var i = 0; i < len; i++){
  258. var item = chatLog[i];
  259. if('mine' == item.type){
  260. _html += '<div class="chat-mine">';
  261. _html += '<div class="author-name">' + item.name + ' ';
  262. _html += '<small class="chat-date">' + item.time + '</small>';
  263. _html += '</div><div class="chat-text">' + item.content + '</div></div>';
  264. }else if('other' == item.type){
  265. _html += '<div class="chat-other">';
  266. _html += '<div class="author-name">' + item.name + ' ';
  267. _html += '<small class="chat-date">' + item.time + '</small>';
  268. _html += '</div><div class="chat-text">' + item.content + '</div></div>';
  269. }
  270. }
  271. document.getElementById('chat-list').innerHTML = _html;
  272. showBigPic();
  273. // 滚动条自动定位到最底端
  274. wordBottom();
  275. }
  276. // 锁住输入框
  277. function lockTextarea(){
  278. sendLock = 1;
  279. document.getElementById('msg').setAttribute('readonly', 'readonly');
  280. }
  281. // 解锁输入框
  282. function unlockTextarea(){
  283. sendLock = 0;
  284. document.getElementById('msg').removeAttribute('readonly');
  285. }
  286. // 双击图片
  287. function showBigPic(){
  288. layui.use('jquery', function(){
  289. var $ = layui.jquery;
  290. $(".layui-whisper-photos").on('click', function () {
  291. var src = this.src;
  292. layer.photos({
  293. photos: {
  294. data: [{
  295. "alt": "大图模式",
  296. "src": src
  297. }]
  298. }
  299. , shade: 0.5
  300. , closeBtn: 2
  301. , anim: 0
  302. , resize: false
  303. , success: function (layero, index) {
  304. }
  305. });
  306. });
  307. });
  308. }
  309. // 对话框定位到最底端
  310. function wordBottom(){
  311. var ex = document.getElementById("chat-list");
  312. ex.scrollTop = ex.scrollHeight;
  313. }