Nav apraksta

chat.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. var bDrag = false;
  2. var disX = disY = 0;
  3. var chat = document.getElementById('chat')
  4. var chart_name = document.getElementsByClassName('name')[0];
  5. var userCode = window.localStorage.getItem('userCode') //当前登录人
  6. var touristCode = window.localStorage.getItem('touristCode') //聊天人
  7. var seatFlag = window.localStorage.getItem('seatFlag')
  8. var roleName = window.localStorage.getItem('roleName')
  9. var touserid;
  10. var aa, fq;
  11. var flag;
  12. var flagAct;
  13. var modelState=0;
  14. var video = 0;
  15. var id = parseInt(Math.random() * 100000);
  16. if(roleName != '回访办'){
  17. console.log(111)
  18. $('.open_chat').hide()
  19. // return
  20. }
  21. if(userCode == null && touristCode == null) {
  22. var id = parseInt(Math.random() * 100000)
  23. userCode = id;
  24. window.localStorage.setItem('touristCode', id);
  25. } else if(userCode == null && touristCode != null) {
  26. userCode = touristCode;
  27. }
  28. if(seatFlag == 'true') {
  29. $('.replyType_btn').show()
  30. } else {
  31. $('.replyType_btn').hide()
  32. }
  33. getInforNotice()
  34. fq = setInterval(function() {
  35. getInforNotice()
  36. }, 5000)
  37. //console.log(document.cookie.getItem('touristCode'))
  38. //};
  39. $('.open_chat').click(function() {
  40. if($('#chat').css('display') == 'block') {
  41. $('#chat').hide();
  42. } else {
  43. $('#chat').show();
  44. }
  45. //获取客服工号
  46. // getTouserid();
  47. //获取聊天列表
  48. getchatinglist();
  49. clearInterval(fq)
  50. getNoneInfor()
  51. $('.name').text(userCode);
  52. GetOnlineservicetinfo()
  53. aa = setInterval(function() {
  54. //获取聊天列表
  55. // getchatinglist();
  56. GetOnlineservicetinfo()
  57. }, 5000)
  58. //移动拖拽弹框
  59. chart_name.onmousedown = function(event) {
  60. var event = event || window.event;
  61. bDrag = true;
  62. disX = event.clientX - chat.offsetLeft;
  63. disY = event.clientY - chat.offsetTop;
  64. this.setCapture && this.setCapture();
  65. return false
  66. };
  67. document.onmousemove = function(event) {
  68. if(!bDrag) return;
  69. var event = event || window.event;
  70. var iL = event.clientX - disX;
  71. var iT = event.clientY - disY;
  72. var maxL = document.documentElement.clientWidth - chat.offsetWidth;
  73. var maxT = document.documentElement.clientHeight - chat.offsetHeight;
  74. iL = iL < 0 ? 0 : iL;
  75. iL = iL > maxL ? maxL : iL;
  76. iT = iT < 0 ? 0 : iT;
  77. iT = iT > maxT ? maxT : iT;
  78. chat.style.marginTop = chat.style.marginLeft = 0;
  79. chat.style.left = iL + "px";
  80. chat.style.top = iT + "px";
  81. return false
  82. };
  83. document.onmouseup = window.onblur = chart_name.onlosecapture = function() {
  84. bDrag = false;
  85. chart_name.releaseCapture && chart_name.releaseCapture();
  86. };
  87. window.onkeydown = function(e) {
  88. if(e.keyCode == 13) {
  89. sendinfo();
  90. flag = 0
  91. }
  92. }
  93. if(flag = 0) {
  94. $('.chatlist_con').scrollTop($('.chatlist_con')[0].scrollHeight);
  95. }
  96. })
  97. $('.close_icon').click(function() {
  98. modelState=0
  99. $('.replyType_cont').hide()
  100. $('.con').hide()
  101. $('#chat').hide();
  102. clearInterval(aa);
  103. fq = setInterval(function() {
  104. getInforNotice()
  105. }, 5000)
  106. })
  107. clearInterval(aa)
  108. //列表点击样式
  109. $('.sidebar_list').click(function(e) {
  110. if(e.target.tagName == 'LI' || e.target.tagName == 'P') {
  111. if(e.target.tagName == 'LI') {
  112. flag = 0;
  113. if(flag == 0) {
  114. $('.chatlist_con').scrollTop($('.chatlist_con')[0].scrollHeight);
  115. }
  116. $(e.target).addClass('actives');
  117. $(e.target).siblings().removeClass('actives')
  118. touserid = $(e.target).children('p').eq(0).text()
  119. $('.name').text(touserid);
  120. GetOnlineservicetinfo()
  121. }
  122. }
  123. })
  124. //快捷消息回复模板
  125. $('.reply_btn').click(function() {
  126. if($('.con').css('display') == 'block') {
  127. $('.con').hide()
  128. } else {
  129. $('.con').show()
  130. }
  131. $('.replyType_cont').hide()
  132. getMessage();
  133. })
  134. //回话类型
  135. $('.replyType_btn').click(function() {
  136. if(modelState==1){
  137. layer.msg("已选过", {
  138. time: 1500,
  139. icon: 2
  140. });
  141. return
  142. }
  143. if($('.replyType_cont').css('display') == 'block') {
  144. $('.replyType_cont').hide()
  145. } else {
  146. $('.replyType_cont').show()
  147. }
  148. $('.con').hide()
  149. getReplyType()
  150. })
  151. //发送图片
  152. $("#scwj").click(function() {
  153. $("#upFile").trigger("click");
  154. })
  155. $("#upFile").change(function() {
  156. upload();
  157. });
  158. $('.con').click(function(e) {
  159. if(e.target.tagName == 'UL') {
  160. return false;
  161. }
  162. let messgaeCont = $(e.target).html();
  163. const arr = messgaeCont.split('、');
  164. $('.send_content').val(arr[1]);
  165. $('.con').hide()
  166. })
  167. //通知
  168. function getInforNotice() {
  169. // $.ajax({
  170. // type: "get",
  171. // url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/getonlineserviceifhave",
  172. // async: true,
  173. // dataType: 'json',
  174. // data: {
  175. // userCode: userCode
  176. // },
  177. // success: function(res) {
  178. // console.log(res)
  179. // if(res.data == 0) {
  180. // // debugger
  181. // $('audio').remove();
  182. // audioElementHovertree = document.createElement('audio');
  183. // audioElementHovertree.setAttribute('src', 'js/video/info.mp3');
  184. // audioElementHovertree.setAttribute('autoplay', 'autoplay'); //打开自动播放
  185. // $('.mark').css('display', 'block');
  186. // }
  187. // }
  188. // });
  189. }
  190. //判断消息状态
  191. function getNoneInfor() {
  192. $.ajax({
  193. type: "get",
  194. url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/uponlineserviceifhave",
  195. async: true,
  196. dataType: 'json',
  197. data: {
  198. userCode: userCode
  199. },
  200. success: function(res) {
  201. if(res.state == 'success') {
  202. $('.mark').css('display', 'none');
  203. }
  204. }
  205. });
  206. }
  207. //回话类型
  208. function getReplyType() {
  209. $('.replyType_cont').unbind("click").click(function(e) {
  210. if(e.target.tagName == 'LI') {
  211. $.ajax({
  212. type: "get",
  213. url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/addonlinesertype",
  214. async: true,
  215. dataType: 'json',
  216. data: {
  217. zxusercode: userCode,
  218. fwusercode: touserid,
  219. fwtype: $(e.target).text()
  220. },
  221. success: function(res) {
  222. if(res.state == 'success') {
  223. modelState=1
  224. layer.msg("提交成功", {
  225. time: 1500,
  226. icon: 1
  227. });
  228. $('.replyType_cont').hide()
  229. }
  230. }
  231. });
  232. }
  233. e.stopPropagation()
  234. })
  235. }
  236. //图片上传预览 IE是用了滤镜。
  237. var file = document.getElementById("upFile");
  238. function upload() {
  239. flagAct = 1;
  240. var MAXWIDTH = 170;
  241. var MAXHEIGHT = 120;
  242. var div = document.getElementById('preview');
  243. if(file.files && file.files[0]) {
  244. div.innerHTML = '<img id=imghead>';
  245. var img = document.getElementById('imghead');
  246. img.onload = function() {
  247. var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
  248. img.width = rect.width;
  249. img.height = rect.height;
  250. // img.style.marginLeft = rect.left+'px';
  251. img.style.marginTop = rect.top + 'px';
  252. }
  253. var reader = new FileReader();
  254. reader.onload = function(evt) {
  255. img.src = evt.target.result;
  256. }
  257. reader.readAsDataURL(file.files[0]);
  258. }
  259. }
  260. function clacImgZoomParam(maxWidth, maxHeight, width, height) {
  261. var param = {
  262. top: 0,
  263. left: 0,
  264. width: width,
  265. height: height
  266. };
  267. if(width > maxWidth || height > maxHeight) {
  268. rateWidth = width / maxWidth;
  269. rateHeight = height / maxHeight;
  270. if(rateWidth > rateHeight) {
  271. param.width = maxWidth;
  272. param.height = Math.round(height / rateWidth);
  273. } else {
  274. param.width = Math.round(width / rateHeight);
  275. param.height = maxHeight;
  276. }
  277. }
  278. param.left = Math.round((maxWidth - param.width) / 2);
  279. param.top = Math.round((maxHeight - param.height) / 2);
  280. return param;
  281. }
  282. function getMessage() {
  283. $.ajax({
  284. type: "get",
  285. url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/getmassmodellistall",
  286. async: true,
  287. dataType: 'json',
  288. data: {},
  289. success: function(res) {
  290. let messageStr = ''
  291. res.data.forEach(function(v, n) {
  292. messageStr += '<li>' + (n + 1) + '、' + v.massmodelcenter + '</li>'
  293. })
  294. $('.con').html(messageStr);
  295. }
  296. });
  297. }
  298. //发送消息
  299. function sendinfo() {
  300. flag = 0
  301. console.log(flagAct)
  302. //发送聊天消息
  303. if(flagAct == 1) {
  304. if(file.files.length > 0) {
  305. var formData = new FormData();
  306. for(var i = 0; i < file.files.length; i++) {
  307. formData.append('file' + i, file.files[i]);
  308. }
  309. formData.append("fromuserid", userCode)
  310. formData.append("touserid", touserid)
  311. $.ajax({
  312. url: huayi.config.callcenter_url + 'testusertypeapi/api/TestUserType/addonlineserviceimg',
  313. type: "POST",
  314. data: formData,
  315. /**
  316. *必须false才会自动加上正确的Content-Type
  317. */
  318. contentType: false,
  319. /**
  320. * 必须false才会避开jQuery对 formdata 的默认处理
  321. * XMLHttpRequest会对 formdata 进行正确的处理
  322. */
  323. processData: false,
  324. success: function(result) {
  325. $('#imghead').css('display', 'none');
  326. flagAct = 0
  327. }
  328. });
  329. }
  330. } else {
  331. if($('.send_content').val() == '') {
  332. return false;
  333. }
  334. $.ajax({
  335. type: "get",
  336. url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/AddOnlineservice",
  337. async: true,
  338. dataType: 'json',
  339. data: {
  340. Msgcontent: $('.send_content').val(),
  341. fromuserid: userCode,
  342. touserid: touserid
  343. },
  344. success: function(res) {
  345. $('.send_content').val('')
  346. }
  347. });
  348. }
  349. }
  350. ////消息
  351. //$.ajax({
  352. // type: "get",
  353. // url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/GetListOnlineservicemap",
  354. // async: true,
  355. // dataType: 'json',
  356. // data: {
  357. // PageIndex: 1,
  358. // PageSize: 10,
  359. // isread:0,
  360. // fromid:userCode
  361. // },
  362. // success: function(res) {
  363. // console.log(res)
  364. // }
  365. // });
  366. //获取聊天记录
  367. function GetOnlineservicetinfo() {
  368. $.ajax({
  369. type: "get",
  370. url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/GetOnlineservicetinfo",
  371. async: true,
  372. dataType: 'json',
  373. data: {
  374. fromuserid: userCode,
  375. touserid: touserid
  376. },
  377. success: function(res) {
  378. let str1 = ''
  379. res.data.forEach(function(v, n) {
  380. if(v.fromuserid == userCode) {
  381. if(v.imgurlinfo != null && v.imgurlinfo != '') {
  382. str1 += '<li><p class="message_time "><span>' + v.createtime +
  383. '</span></p><div class="message_mincon self"><div class="message_img "><img onclick="imgEnlarge()" src="' + huayi.config.img_url +
  384. v.imgurlinfo + '"></div></div></li>'
  385. } else {
  386. str1 += '<li><p class="message_time "><span>' + v.createtime +
  387. '</span></p><div class="message_mincon self"><div class="message_text ">' +
  388. v.Msgcontent + '</div></div></li>'
  389. }
  390. } else {
  391. if(v.imgurlinfo != null && v.imgurlinfo != '') {
  392. str1 += '<li><p class="message_time "><span>' + v.createtime +
  393. '</span></p><div class="message_mincon"><div class="message_img"><img onclick="imgEnlarge()" src="' + huayi.config.img_url +
  394. v.imgurlinfo + '"></div></div></li>'
  395. } else {
  396. str1 += '<li><p class="message_time "><span>' + v.createtime +
  397. '</span></p><div class="message_mincon"><div class="message_text ">' +
  398. v.Msgcontent + '</div></div></li>'
  399. }
  400. }
  401. })
  402. $('.chatlist_con').html(str1);
  403. //图片放大
  404. if(flag == 0) {
  405. $('.chatlist_con').scrollTop($('.chatlist_con')[0].scrollHeight);
  406. flag = 1
  407. }
  408. }
  409. });
  410. }
  411. function imgEnlarge(str) {
  412. layer.photos({
  413. photos: '.message_img',
  414. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  415. });
  416. }
  417. //获取客服工号
  418. function getTouserid() {
  419. $.ajax({
  420. type: "get",
  421. url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/getzxuserinfo",
  422. async: false,
  423. dataType: 'json',
  424. data: {
  425. fromuserid: userCode
  426. },
  427. success: function(res) {
  428. touserid = res.data;
  429. }
  430. });
  431. }
  432. //获取聊天列表
  433. function getchatinglist() {
  434. $.ajax({
  435. type: "get",
  436. url: huayi.config.callcenter_url + "testusertypeapi/api/TestUserType/getchatinglist",
  437. async: false,
  438. dataType: 'json',
  439. data: {
  440. usercode: userCode
  441. },
  442. success: function(res) {
  443. let str = '';
  444. let num;
  445. res.data.forEach(function(v, n) {
  446. str += '<li><p class="chat_name personName">' + v.usercode + '</p></li>'
  447. })
  448. $('.sidebar_list').html(str);
  449. $(".sidebar_list li").eq(0).addClass('actives')
  450. touserid = $(".sidebar_list li").eq(0).text()
  451. // console.log('6666',)
  452. // let className = document.getElementsByClassName('personName');
  453. // console.log(touserid)
  454. // // debugger
  455. // for(let i = 0; i < className.length; i++) {
  456. // if(className[i].innerText == String(touserid)) {
  457. // num = i
  458. // $(".sidebar_list li").eq(num).addClass('actives');
  459. // }
  460. // }
  461. }
  462. });
  463. }
  464. //查看更多
  465. // function getmore(ele) {
  466. // console.log("你点击了查看更多")
  467. // }
  468. // var wrap = document.querySelector(".wrap");
  469. // var next = document.querySelector(".arrow_right");
  470. // var prev = document.querySelector(".arrow_left");
  471. // next.onclick = function () {
  472. // next_pic();
  473. // }
  474. // prev.onclick = function () {
  475. // prev_pic();
  476. // }
  477. // function next_pic () {
  478. // var newLeft = parseInt(wrap.style.left)-600;
  479. // wrap.style.left = newLeft + "px";
  480. // }
  481. // function prev_pic () {
  482. // var newLeft = parseInt(wrap.style.left)+600;
  483. // wrap.style.left = newLeft + "px";
  484. // }