chat-page.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. KEFU_ID=KEFU_ID!=""? KEFU_ID:"kefu2";
  2. new Vue({
  3. el: '#app',
  4. delimiters:["<{","}>"],
  5. data: {
  6. window:window,
  7. server:getWsBaseUrl()+"/chat_server",
  8. socket:null,
  9. msgList:[],
  10. messageContent:"",
  11. chatTitle:"正在连接...",
  12. visitor:{},
  13. face:[
  14. "em-smile",
  15. "em-laughing",
  16. "em-blush",
  17. "em-smiley",
  18. "em-relaxed",
  19. "em-smirk",
  20. "em-heart_eyes",
  21. "em-kissing_heart",
  22. "em-kissing_closed_eyes",
  23. "em-flushed",
  24. "em-relieved",
  25. "em-satisfied",
  26. "em-grin",
  27. "em-wink",
  28. "em-stuck_out_tongue_winking_eye",
  29. "em-stuck_out_tongue_closed_eyes",
  30. "em-grinning",
  31. "em-kissing",
  32. "em-kissing_smiling_eyes",
  33. "em-stuck_out_tongue",
  34. "em-sleeping",
  35. "em-worried",
  36. "em-frowning",
  37. "em-anguished",
  38. "em-open_mouth",
  39. "em-grimacing",
  40. "em-confused",
  41. "em-hushed",
  42. "em-expressionless",
  43. "em-unamused",
  44. "em-sweat_smile",
  45. "em-sweat",
  46. "em-disappointed_relieved",
  47. "em-weary",
  48. "em-pensive",
  49. "em-disappointed",
  50. "em-confounded",
  51. "em-fearful",
  52. "em-cold_sweat",
  53. "em-persevere",
  54. "em-cry",
  55. "em-sob",
  56. "em-joy",
  57. "em-astonished",
  58. "em-scream",
  59. "em-tired_face",
  60. "em-angry",
  61. "em-rage",
  62. "em-triumph",
  63. "em-sleepy",
  64. "em-yum",
  65. "em-mask",
  66. "em-sunglasses",
  67. "em-dizzy_face",
  68. "em-imp",
  69. "em-smiling_imp",
  70. "em-neutral_face",
  71. "em-no_mouth",
  72. "em-innocent",
  73. "em-alien"
  74. ],
  75. showKfonline:false,
  76. socketClosed:false,
  77. },
  78. methods: {
  79. //初始化websocket
  80. initConn() {
  81. let socket = new ReconnectingWebSocket(this.server);//创建Socket实例
  82. socket.maxReconnectAttempts = 30;
  83. this.socket = socket
  84. this.socket.onmessage = this.OnMessage;
  85. this.socket.onopen = this.OnOpen;
  86. this.socket.onclose = this.OnClose;
  87. //心跳
  88. this.ping();
  89. },
  90. OnOpen() {
  91. this.chatTitle="连接成功!";
  92. let mes = {};
  93. mes.type = "userInit";
  94. this.visitor.refer=REFER;
  95. mes.data = this.visitor;
  96. this.socket.send(JSON.stringify(mes));
  97. },
  98. OnMessage(e) {
  99. const redata = JSON.parse(e.data);
  100. if (redata.type == "kfOnline") {
  101. let msg = redata.data
  102. if(this.showKfonline && this.visitor.to_id==msg.id){
  103. return;
  104. }
  105. this.visitor.to_id=msg.id;
  106. this.chatTitle=msg.name+",正在与您沟通!"
  107. $(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>");
  108. this.scrollBottom();
  109. this.showKfonline=true;
  110. }
  111. if (redata.type == "notice") {
  112. let msg = redata.data
  113. if(!msg){
  114. return;
  115. }
  116. this.chatTitle=msg
  117. $(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>");
  118. this.scrollBottom();
  119. }
  120. if (redata.type == "message") {
  121. let msg = redata.data
  122. this.visitor.to_id=msg.id;
  123. let content = {};
  124. content.avator = msg.avator;
  125. content.name = msg.name;
  126. content.content =replaceContent(msg.content,true);
  127. content.is_kefu = false;
  128. content.time = msg.time;
  129. this.msgList.push(content);
  130. //this.saveHistory(content);
  131. this.scrollBottom();
  132. flashTitle();//标题闪烁
  133. this.alertSound();//提示音
  134. }
  135. if (redata.type == "close") {
  136. this.chatTitle="连接关闭!请重新打开页面";
  137. $(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>");
  138. this.scrollBottom();
  139. this.socket.close();
  140. this.socketClosed=true;
  141. }
  142. window.parent.postMessage(redata);
  143. },
  144. //发送给客户
  145. chatToUser() {
  146. this.messageContent=this.messageContent.trim("\r\n");
  147. if(this.messageContent==""||this.messageContent=="\r\n"){
  148. this.$message({
  149. message: '不能发送空白信息',
  150. type: 'warning'
  151. });
  152. return;
  153. }
  154. if(this.socketClosed){
  155. this.$message({
  156. message: '连接关闭!请重新打开页面',
  157. type: 'warning'
  158. });
  159. return;
  160. }
  161. let _this=this;
  162. let mes = {};
  163. mes.type = "visitor";
  164. mes.from_id = this.visitor.visitor_id;
  165. mes.to_id = this.visitor.to_id;
  166. mes.content = this.messageContent;
  167. //发送消息
  168. $.post("/message",mes,function(res){
  169. if(res.code!=200){
  170. _this.$message({
  171. message: res.msg,
  172. type: 'error'
  173. });
  174. return;
  175. }
  176. let content = {}
  177. content.avator=_this.visitor.avator;
  178. content.content = replaceContent(_this.messageContent);
  179. content.name = _this.visitor.name;
  180. content.is_kefu = true;
  181. content.time = _this.getNowDate();
  182. _this.msgList.push(content);
  183. //_this.saveHistory(content);
  184. _this.scrollBottom();
  185. _this.messageContent = "";
  186. });
  187. },
  188. OnClose() {
  189. this.chatTitle="连接关闭!请重新打开页面";
  190. $(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>");
  191. },
  192. //获取当前用户信息
  193. getUserInfo(){
  194. let obj=this.getCache("visitor");
  195. if(!obj){
  196. let _this=this;
  197. //发送消息
  198. $.post("/visitor_login",{uid:1,refer:REFER,to_id:KEFU_ID,client_ip:returnCitySN["cip"],},function(res){
  199. if(res.code!=200){
  200. _this.$message({
  201. message: res.msg,
  202. type: 'error'
  203. });
  204. return;
  205. }
  206. _this.visitor=res.result;
  207. _this.setCache("visitor",res.result);
  208. _this.initConn();
  209. });
  210. }else{
  211. this.visitor=obj;
  212. this.initConn();
  213. }
  214. },
  215. //获取信息列表
  216. getMesssagesByVisitorId(){
  217. let _this=this;
  218. $.ajax({
  219. type:"get",
  220. url:"/messages?visitorId="+this.visitor.visitor_id,
  221. success: function(data) {
  222. if(data.code==200 && data.result!=null&&data.result.length!=0){
  223. let msgList=data.result;
  224. _this.msgList=[];
  225. for(let i=0;i<msgList.length;i++){
  226. let visitorMes=msgList[i];
  227. let content = {}
  228. if(visitorMes["mes_type"]=="kefu"){
  229. content.is_kefu = false;
  230. content.avator = visitorMes["kefu_avator"];
  231. content.name = visitorMes["kefu_name"];
  232. }else{
  233. content.is_kefu = true;
  234. content.avator = visitorMes["visitor_avator"];
  235. content.name = visitorMes["visitor_name"];
  236. }
  237. content.content = replaceContent(visitorMes["content"]);
  238. content.time = visitorMes["time"];
  239. _this.msgList.push(content);
  240. _this.scrollBottom();
  241. }
  242. _this.$nextTick(() => {
  243. $(".chatBox").append("<div class=\"chatTime\">—— 以上是历史消息 ——</div>");
  244. });
  245. }
  246. if(data.code!=200){
  247. _this.$message({
  248. message: data.msg,
  249. type: 'error'
  250. });
  251. }
  252. }
  253. });
  254. },
  255. //滚动到底部
  256. scrollBottom:function(){
  257. this.$nextTick(() => {
  258. //debugger;
  259. $('body').scrollTop($("body")[0].scrollHeight);
  260. });
  261. },
  262. //获取日期
  263. getNowDate : function() {// 获取日期
  264. var d = new Date(new Date());
  265. return d.getFullYear() + '-' + this.digit(d.getMonth() + 1) + '-' + this.digit(d.getDate())
  266. + ' ' + this.digit(d.getHours()) + ':' + this.digit(d.getMinutes()) + ':' + this.digit(d.getSeconds());
  267. },
  268. //补齐数位
  269. digit : function (num) {
  270. return num < 10 ? '0' + (num | 0) : num;
  271. },
  272. setCache : function (key,obj){
  273. if(typeof(Storage) !== "undefined"){
  274. localStorage.setItem(key, JSON.stringify(obj));
  275. }
  276. },getCache : function (key){
  277. return JSON.parse(localStorage.getItem(key));
  278. },
  279. //获取自动欢迎语句
  280. getNotice : function (){
  281. let _this=this;
  282. $.get("/notice?kefu_id="+KEFU_ID,function(res) {
  283. //debugger;
  284. if (res.result != null) {
  285. let msg = res.result;
  286. for(let i=0;i<msg.length;i++){
  287. let content = msg[i];
  288. content.content = replaceContent(content.content);
  289. setTimeout(function () {
  290. _this.msgList.push(content);
  291. _this.scrollBottom();
  292. }, 5000*(i+1));
  293. }
  294. }
  295. });
  296. },
  297. initCss(){
  298. var _this=this;
  299. $(function () {
  300. //$(".chatContext").css("max-height",$(window).height());
  301. // if (top.location != location){
  302. // $(".chatContext").css("max-height",$(window).height()-65);
  303. // }
  304. //展示表情
  305. var faces=placeFace();
  306. $.each(faceTitles, function (index, item) {
  307. // _this.face.push({"name":item,"path":faces[item]});
  308. });
  309. $(".faceBtn").click(function(e){
  310. var status=$('.faceBox').css("display");
  311. if(status=="block"){
  312. $('.faceBox').hide();
  313. }else{
  314. $('.faceBox').show();
  315. }
  316. return false;
  317. });
  318. });
  319. },
  320. //心跳
  321. ping(){
  322. let _this=this;
  323. let mes = {};
  324. mes.type = "ping";
  325. mes.data = "visitor:"+_this.visitor.visitor_id;
  326. setInterval(function () {
  327. if(_this.socket!=null){
  328. _this.socket.send(JSON.stringify(mes));
  329. }
  330. },10000);
  331. },
  332. //初始化
  333. init(){
  334. this.initCss();
  335. $("#app").click(function(){
  336. clearTimeout(titleTimer);
  337. document.title = originTitle;
  338. });
  339. $('body').click(function(){
  340. clearTimeout(titleTimer);
  341. document.title = originTitle;
  342. $('.faceBox').hide();
  343. });
  344. },
  345. //表情点击事件
  346. faceIconClick(index){
  347. $('.faceBox').hide();
  348. this.messageContent+="face["+this.face[index]+"]";
  349. },
  350. //上传图片
  351. uploadImg (url){
  352. let _this=this;
  353. $('#uploadImg').after('<input type="file" accept="image/gif,image/jpeg,image/jpg,image/png" id="uploadImgFile" name="file" style="display:none" >');
  354. $("#uploadImgFile").click();
  355. $("#uploadImgFile").change(function (e) {
  356. var formData = new FormData();
  357. var file = $("#uploadImgFile")[0].files[0];
  358. formData.append("imgfile",file); //传给后台的file的key值是可以自己定义的
  359. filter(file) && $.ajax({
  360. url: url || '',
  361. type: "post",
  362. data: formData,
  363. contentType: false,
  364. processData: false,
  365. dataType: 'JSON',
  366. mimeType: "multipart/form-data",
  367. success: function (res) {
  368. if(res.code!=200){
  369. _this.$message({
  370. message: res.msg,
  371. type: 'error'
  372. });
  373. }else{
  374. _this.messageContent+='img[' + res.result.path + ']';
  375. _this.chatToUser();
  376. }
  377. },
  378. error: function (data) {
  379. console.log(data);
  380. }
  381. });
  382. });
  383. },
  384. //粘贴上传图片
  385. onPasteUpload(event){
  386. let items = event.clipboardData && event.clipboardData.items;
  387. let file = null;
  388. if (items && items.length) {
  389. // 检索剪切板items
  390. for (var i = 0; i < items.length; i++) {
  391. if (items[i].type.indexOf('image') !== -1) {
  392. file = items[i].getAsFile()
  393. }
  394. }
  395. }
  396. if (!file) {
  397. return;
  398. }
  399. let _this=this;
  400. var formData = new FormData();
  401. formData.append('imgfile', file);
  402. $.ajax({
  403. url: '/uploadimg',
  404. type: "post",
  405. data: formData,
  406. contentType: false,
  407. processData: false,
  408. dataType: 'JSON',
  409. mimeType: "multipart/form-data",
  410. success: function (res) {
  411. if(res.code!=200){
  412. _this.$message({
  413. message: res.msg,
  414. type: 'error'
  415. });
  416. }else{
  417. _this.messageContent+='img[' + res.result.path + ']';
  418. _this.chatToUser();
  419. }
  420. },
  421. error: function (data) {
  422. console.log(data);
  423. }
  424. });
  425. },
  426. //提示音
  427. alertSound(){
  428. var b = document.getElementById("chatMessageAudio");
  429. var p = b.play();
  430. p && p.then(function(){}).catch(function(e){});
  431. }
  432. },
  433. mounted() {
  434. document.addEventListener('paste', this.onPasteUpload)
  435. },
  436. created: function () {
  437. this.init();
  438. this.getUserInfo();
  439. //加载历史记录
  440. this.getMesssagesByVisitorId();
  441. //this.msgList=this.getHistory();
  442. //滚动底部
  443. //this.scrollBottom();
  444. //获取欢迎
  445. this.getNotice();
  446. }
  447. })