郑州第一人民医院UI

chat.js 11KB

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