Нет описания

chat.js 9.7KB

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