伊川12345

main.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. 
  2. var ws, n = 0, timer;
  3. var obj = {};
  4. var cls = 0;
  5. var lockReconnect = false;
  6. //话务日志参数
  7. var errorState=false;
  8. var reconnecTion;
  9. var recon;
  10. var loginOr=false;
  11. var directType;
  12. var sendType;
  13. var mesCont;
  14. var eorrorConnect;
  15. //创建scoket连接
  16. function createWebSocket() {
  17. try {
  18. $("#top-search li i").removeClass("active");
  19. $(".hw_show i").removeClass("Turn110_c");
  20. $(".hw_show i").addClass("Turn110");
  21. Connect();
  22. } catch (e) {
  23. //打印日志
  24. directType="lianjie";
  25. sendType="异常错误"; //JSON.stringify(data)
  26. mesCont=e.message;
  27. debuggerLog();
  28. reconnect();
  29. }
  30. }
  31. //连接
  32. function Connect() {
  33. try {
  34. ws = new WebSocket("ws://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  35. ws.onopen = function () {
  36. console.log(new Date() + " " + "建立连接");
  37. //心跳检测重置
  38. heartCheck.reset().start();
  39. cls = 0;
  40. $("#top-search li i").removeClass("active");
  41. $(".hw_show i").removeClass("Turn110_c");
  42. $(".hw_show i").addClass("Turn110");
  43. $(".Login").addClass("active");
  44. };
  45. //接收到消息的回调方法
  46. ws.onmessage = function (evt) {
  47. heartCheck.reset().start();
  48. var myDate = new Date();
  49. console.log(myDate + " receive " + evt.data);
  50. var data = JSON.parse(evt.data)[0];
  51. //打印日志
  52. directType="recive";
  53. sendType=data.Type; //JSON.stringify(data)
  54. mesCont=JSON.stringify(evt);
  55. debuggerLog()
  56. if (data) {
  57. var rlt = data.Result;
  58. if (rlt == true) {
  59. var type = data.Type;
  60. switch (type.toLowerCase()) {
  61. case "login": LoginBack(); break;//签入
  62. case "logout": LogoutBack(); break;//签出
  63. case "dropcall": DropCallBack(); break;//挂断
  64. case "makecall": MakeCallBack(); break;//外呼
  65. case "setstate": SetState(data); break;//置忙置闲
  66. //case "saybusy": SayBusyBack(data); break;
  67. //case "sayfree": SayFreeBack(data); break;
  68. case "meeting": MeetingBack(); break;//多方通话
  69. case "transfer": TransferBack(); break;//转移
  70. case "hold": HoldBack(); break;//保持
  71. case "retrieve": RetrieveBack(); break;//接回
  72. case "incoming": IncomingBack(data); break;//来电
  73. case "subscribe": SubScribeBack(); break;//监测
  74. case "subscribecancel": SubScribeCancelBack(); break;//停止监测
  75. case "agentstate": AgentStateBack(data); break;//坐席状态
  76. case "linestate": LineStateBack(data); break;//线路状态
  77. case "motorsetstate": SayFreeBack(); break;//班长置闲
  78. case "linestateagent": LineStateAgentBack(data); break;//线路状态通知
  79. case "callid": CallIDBack(data); break;//获取callid
  80. case "recordpath": RecordPathBack(data); break;//录音返回
  81. case "LineOutState": LineOutState(data); break;//录音返回
  82. }
  83. }
  84. else {
  85. if (rlt == false) {
  86. //layer.confirm('操作失败!', {
  87. // btn: ['确定']
  88. //});
  89. $(".hwzt").text('操作失败!');
  90. }
  91. else {
  92. $(".hwzt").text(rlt);
  93. //layer.confirm(rlt, {
  94. // btn: ['确定']
  95. //});
  96. }
  97. }
  98. }
  99. };
  100. //连接关闭的回调方法
  101. ws.onclose = function (evt) {
  102. directType="recive";
  103. sendType="close"; //JSON.stringify(data)
  104. mesCont=JSON.stringify(evt);
  105. debuggerLog();
  106. if (cls == 0) {
  107. cls = 1;
  108. $(".hwzt").text('连接关闭!');
  109. $("#top-search li i").removeClass("active");
  110. $(".hw_show i").removeClass("Turn110_c");
  111. $(".hw_show i").addClass("Turn110");
  112. console.log(ws);
  113. if (ws == null || ws.readyState != ws.OPEN) {
  114. console.log(new Date() + "开始重连");
  115. reconnect();
  116. }
  117. }
  118. };
  119. //连接发生错误的回调方法
  120. ws.onerror = function (evt) {
  121. directType="recive";
  122. sendType="连接错误 "; //JSON.stringify(data)
  123. mesCont=JSON.stringify(evt);
  124. debuggerLog();
  125. //产生异常
  126. $(".hwzt").text('连接异常!');
  127. $("#top-search li i").removeClass("active");
  128. $(".hw_show i").removeClass("Turn110_c");
  129. $(".hw_show i").addClass("Turn110");
  130. console.log(ws);
  131. if (ws == null || ws.readyState != ws.OPEN) {
  132. console.log(new Date() + "开始重连");
  133. reconnect();
  134. }
  135. };
  136. }
  137. catch (ex) {
  138. //layer.confirm('连接失败!', {
  139. // btn: ['确定']
  140. //});
  141. $(".hwzt").text('连接失败!');
  142. $("#top-search li i").removeClass("active");
  143. $(".hw_show i").removeClass("Turn110_c");
  144. $(".hw_show i").addClass("Turn110");
  145. }
  146. }
  147. //重连
  148. function reconnect() {
  149. if (lockReconnect) return;
  150. lockReconnect = true;
  151. //没连接上会一直重连,设置延迟避免请求过多
  152. setTimeout(function () {
  153. directType="send";
  154. sendType="重连中 "; //JSON.stringify(data)
  155. mesCont="重连中";
  156. debuggerLog();
  157. console.log(new Date() + " " + "重连中……");
  158. createWebSocket();
  159. lockReconnect = false;
  160. }, 2000);
  161. }
  162. function debuggerLog(){
  163. $.post(huayi.config.callcenter_url + 'Log/CallOpt', {
  164. direct: directType,
  165. type: sendType,
  166. cont: mesCont,
  167. "token": $.cookie("token")
  168. }, function(result) {
  169. result = $.parseJSON(result);
  170. if(result.state.toLowerCase() == "warning") {
  171. //layer.msg("创建工单成功");
  172. }
  173. })
  174. }
  175. //异常重连
  176. function errorFun(){
  177. clearInterval(timer);
  178. var errorNum=0
  179. timer = setInterval(function() {
  180. errorNum++;
  181. if (errorNum>2) {
  182. $(".hwzt").text('连接异常!');
  183. }else{
  184. $("#top-search li i").removeClass("active");
  185. $(".Login").addClass("active");
  186. $(".zxzt").removeClass("bl").addClass("br");
  187. $(".fwzt").removeClass("bl").addClass("br");
  188. $(".hwzt").text('');
  189. }
  190. reconnect();
  191. $(".hwzt").text('连接异常!');
  192. }, 15000);
  193. }
  194. noLine();
  195. clearInterval(recon);
  196. recon = setInterval(function () {
  197. if(loginOr){
  198. noLine();
  199. setTimeout(function () {
  200. lineState();
  201. }, 2000);
  202. }
  203. }, 1000*10);
  204. function lineState(){
  205. if (reconnecTion) {
  206. reconnecTion=false;
  207. }else{
  208. directType="send";
  209. sendType="断网 "; //JSON.stringify(data)
  210. mesCont="断网";
  211. debuggerLog();
  212. }
  213. }
  214. function noLine(){
  215. $.ajax({
  216. type: "get",
  217. url: huayi.config.callcenter_url + 'UserAccount/GetNowUser',
  218. async: true,
  219. dataType: 'json',
  220. data: {
  221. "token": $.cookie("token")
  222. },
  223. success: function(data) {
  224. reconnecTion = data.data.user.F_UserCode;
  225. }
  226. });
  227. }
  228. //发送
  229. function Send() {
  230. if (ws.readyState != ws.OPEN) {
  231. directType="send";
  232. sendType=ws.readyState; //JSON.stringify(data)
  233. mesCont="异常了";
  234. debuggerLog();
  235. Connect();
  236. }
  237. if (ws.readyState == ws.OPEN) {
  238. if (obj.Type) {
  239. directType="send";
  240. sendType=obj.Type;
  241. mesCont=JSON.stringify(obj);
  242. debuggerLog()
  243. }else{
  244. directType="send";
  245. sendType="建立连接";
  246. mesCont=JSON.stringify(obj);
  247. debuggerLog()
  248. }
  249. console.log(new Date() + " send " + JSON.stringify(obj));
  250. ws.send(JSON.stringify(obj));
  251. }
  252. }
  253. //心跳检测
  254. var heartCheck = {
  255. timeout: 25000, //25秒
  256. timeoutObj: null,
  257. serverTimeoutObj: null,
  258. reset: function () {
  259. clearTimeout(this.timeoutObj);
  260. clearTimeout(this.serverTimeoutObj);
  261. return this;
  262. },
  263. start: function () {
  264. var self = this;
  265. this.timeoutObj = setTimeout(function () {
  266. //这里发送一个心跳,后端收到后,返回一个心跳消息,
  267. //onmessage拿到返回的心跳就说明连接正常
  268. obj.Type = "Heart";
  269. Send();
  270. self.serverTimeoutObj = setTimeout(function () { //如果超过一定时间还没重置,说明后端主动断开了
  271. ws.close(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
  272. //打印日志
  273. directType="heart";
  274. sendType="心跳"; //JSON.stringify(data)
  275. mesCont="未收到";
  276. debuggerLog();
  277. }, self.timeout)
  278. }, this.timeout)
  279. }
  280. }
  281. //签入
  282. function LoginBack() {
  283. $("#top-search li i").removeClass("active");
  284. $(".hw_show i").removeClass("Turn110_c");
  285. $(".hw_show i").addClass("Turn110");
  286. $(".Logout").addClass("active");
  287. $(".SayBusy").addClass("active");
  288. $(".MakeCall").addClass("active");
  289. $(".zx").removeClass("br").addClass("bl");
  290. $(".xl").removeClass("br").addClass("bl");
  291. $(".hwzt").text('');
  292. }
  293. //签出
  294. function LogoutBack() {
  295. $("#top-search li i").removeClass("active");
  296. $(".hw_show i").removeClass("Turn110_c");
  297. $(".hw_show i").addClass("Turn110");
  298. $(".Login").addClass("active");
  299. $(".zx").removeClass("bl").addClass("br");
  300. $(".xl").removeClass("bl").addClass("br");
  301. }
  302. //来电
  303. function IncomingBack(data) {
  304. clearworkorder();
  305. $(".ldhm").val(data.Number);
  306. $(".hidTel").val(data.Number);
  307. $(".tel").text(data.Number);
  308. $(".lxr").text(data.Number);
  309. $(".ldtime").text(getNowFormatDate());
  310. $(".thsc").text("00:00");
  311. //获取callid
  312. $.ajaxSettings.async = false;
  313. $.getJSON(huayi.config.callcenter_url + 'CallInScreen/GetCallIdByPhone', { "tel": $(".hidTel").val(), "token": $.cookie("token") }, function (result) {
  314. $.ajaxSettings.async = true;
  315. if (result.state.toLowerCase() == "success") {
  316. $(".hidCallID").val(result.data.CallId);
  317. }
  318. })
  319. //获取电话所属地
  320. $.getJSON(huayi.config.callcenter_url + 'CallInScreen/GetPhoneLocation', { "tel": $(".hidTel").val(), "token": $.cookie("token") }, function (result) {
  321. if (result.state.toLowerCase() == "success") {
  322. $(".ldlocation").text(result.data[0].F_Name);
  323. $(".khgsd").text(result.data[0].F_Name);
  324. }
  325. })
  326. //获取客户信息
  327. $(".ttsdh").text($(".hidTel").val());
  328. $(".tsdh").val($(".hidTel").val());
  329. $.getJSON(huayi.config.callcenter_url + 'CallInScreen/GetCustomerByTel', { "tel": $(".hidTel").val(), "token": $.cookie("token") }, function (result) {
  330. if (result.state.toLowerCase() == "success") {
  331. if (result.data.length > 0) {
  332. $(".khmc").val(result.data[0].F_CustomerName);
  333. $(".tkhmc").text(result.data[0].F_CustomerName);
  334. $("#khid").val(result.data[0].F_CustomerId);
  335. $(".lxdh").val(result.data[0].F_Telephone);
  336. $(".tsdh").val(result.data[0].F_Mobile);
  337. $(".ttsdh").text(result.data[0].F_Mobile);
  338. $(".lxr").val(result.data[0].F_CustomerEName);
  339. $(".postcode").val(result.data[0].F_PostCode);
  340. $(".email").val(result.data[0].F_Email);
  341. }
  342. }
  343. })
  344. $('.maxOpen').trigger("click");
  345. $('.head-pic .lahei .la-before').show();
  346. $('.head-pic .lahei .la-after').hide();
  347. $('.head-pic .lahei .retur').hide();
  348. $(".Bacha").hide();
  349. }
  350. function CallIDBack(data) {
  351. //$(".hidCallID").val(data.CurrID);
  352. }
  353. //挂断
  354. function DropCallBack() {
  355. $("#top-search li i").removeClass("active");
  356. $(".hw_show i").removeClass("Turn110_c");
  357. $(".hw_show i").addClass("Turn110");
  358. $(".Logout").addClass("active");
  359. $(".SayBusy").addClass("active");
  360. $(".MakeCall").addClass("active");
  361. $(".td-call").hide();
  362. //$('.ldtp-con').css("display", 'none');
  363. }
  364. function LineStateAgentBack(data) {
  365. if (data.State == '1') {
  366. $(".xl").removeClass("br").addClass("bl");
  367. clearInterval(timer);
  368. if ($(".hidTel").val() && $(".hidCallID").val()) {
  369. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateGJ', { callid: $(".hidCallID").val(), "token": $.cookie("token") }, function (result) {
  370. result = JSON.parse(result);
  371. if (result.state.toLowerCase() == "success") {
  372. }
  373. })
  374. }
  375. $("#top-search li i").removeClass("active");
  376. $(".hw_show i").removeClass("Turn110_c");
  377. $(".hw_show i").addClass("Turn110");
  378. $(".Logout").addClass("active");
  379. $(".SayBusy").addClass("active");
  380. $(".MakeCall").addClass("active");
  381. $(".td-call").hide();
  382. $(".Bacha").show();
  383. //$(".hidTel").val("");
  384. //$(".hidCallID").val("");
  385. }
  386. if (data.State == '5') {
  387. $(".xl").removeClass("bl").addClass("br");
  388. if ($(".hidTel").val() && $(".hidCallID").val()) {
  389. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZL', { callid: $(".hidCallID").val(), "token": $.cookie("token") }, function (result) {
  390. result = JSON.parse(result);
  391. if (result.state.toLowerCase() == "success") {
  392. }
  393. })
  394. }
  395. }
  396. if (data.State == '6') {
  397. $(".xl").removeClass("bl").addClass("br");
  398. if ($(".hidTel").val() && $(".hidCallID").val()) {
  399. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZJ', { callid: $(".hidCallID").val(), "token": $.cookie("token") }, function (result) {
  400. result = JSON.parse(result);
  401. if (result.state.toLowerCase() == "success") {
  402. }
  403. })
  404. }
  405. $(".td-call").show();
  406. n = 0;
  407. $("#top-search li i").removeClass("active");
  408. $(".hw_show i").addClass("Turn110_c");
  409. $(".hw_show i").removeClass("Turn110");
  410. $(".DropCall").addClass("active");
  411. $(".Hold").addClass("active");
  412. $(".Transfer").addClass("active");
  413. $(".Meeting").addClass("active");
  414. clearInterval(timer);
  415. timer = setInterval(function () {
  416. n++;
  417. var m = parseInt(n / 60 % 60);
  418. var s = parseInt(n % 60);
  419. $(".thsc").text(toDub(m) + ":" + toDub(s));
  420. }, 1000);
  421. }
  422. }
  423. function toDub(i) {
  424. return i < 10 ? "0" + i : "" + i;
  425. }
  426. //外呼
  427. function MakeCallBack() {
  428. $("#top-search li i").removeClass("active");
  429. $(".hw_show i").addClass("Turn110_c");
  430. $(".hw_show i").removeClass("Turn110");
  431. $(".DropCall").addClass("active");
  432. }
  433. //置忙置闲
  434. function SetState(obj) {
  435. if (obj.State == '5') {
  436. $(".SayBusy").removeClass("active");
  437. $(".SayFree").addClass("active");
  438. $(".zx").removeClass("bl").addClass("br");
  439. }
  440. if (obj.State == '2') {
  441. $(".SayBusy").addClass("active");
  442. $(".SayFree").removeClass("active");
  443. $(".zx").removeClass("br").addClass("bl");
  444. }
  445. $.cookie("socket_state", obj.State);
  446. }
  447. //置忙
  448. function SayBusyBack() {
  449. $(".SayBusy").removeClass("active");
  450. $(".SayFree").addClass("active");
  451. }
  452. //置闲
  453. function SayFreeBack() {
  454. $(".SayBusy").addClass("active");
  455. $(".SayFree").removeClass("active");
  456. }
  457. //多方通话
  458. function MeetingBack() {
  459. }
  460. //转移
  461. function TransferBack() {
  462. $("#top-search li i").removeClass("active");
  463. $(".hw_show i").removeClass("Turn110_c");
  464. $(".hw_show i").addClass("Turn110");
  465. $(".Logout").addClass("active");
  466. $(".SayBusy").addClass("active");
  467. $(".MakeCall").addClass("active");
  468. }
  469. //保持
  470. function HoldBack() {
  471. $(".Hold").removeClass("active");
  472. $(".Retrieve").addClass("active");
  473. }
  474. //接回
  475. function RetrieveBack() {
  476. $(".Hold").addClass("active");
  477. $(".Retrieve").removeClass("active");
  478. }
  479. //监测
  480. function SubScribeBack() {
  481. var obj = $("iframe:visible")
  482. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  483. window.frames[obj.attr("name")].Start();
  484. }
  485. }
  486. //取消监测
  487. function SubScribeCancelBack() {
  488. var obj = $("iframe:visible")
  489. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  490. window.frames[obj.attr("name")].Stop();
  491. }
  492. }
  493. //班长监测返回状态
  494. //坐席状态
  495. function AgentStateBack(data) {
  496. var obj = $("iframe:visible")
  497. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  498. window.frames[obj.attr("name")].UpdateAgentState(data.AgentID, data.State);
  499. }
  500. }
  501. //线路状态
  502. function LineStateBack(data) {
  503. var obj = $("iframe:visible")
  504. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  505. window.frames[obj.attr("name")].UpdateLineState(data.AgentID, data.State);
  506. }
  507. }
  508. //录音返回
  509. function RecordPathBack(data) {
  510. if ($(".hidTel").val() && $(".hidCallID").val()) {
  511. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateLY', { callid: $(".hidCallID").val(),path:data.RecPath, "token": $.cookie("token") }, function (result) {
  512. result = JSON.parse(result);
  513. if (result.state.toLowerCase() == "success") {
  514. }
  515. })
  516. }
  517. }
  518. //录音返回
  519. function LineOutState(data) {
  520. if (lineOutHang) {
  521. layer.confirm('外线已挂断!', {
  522. btn: ['确定']
  523. });
  524. lineOutHang=false;
  525. }
  526. }
  527. //获取当前的日期时间 格式“yyyy-MM-dd HH:mm:ss”
  528. function getNowFormatDate() {
  529. var date = new Date();
  530. var seperator1 = "-";
  531. var seperator2 = ":";
  532. var month = date.getMonth() + 1;
  533. var strDate = date.getDate();
  534. if (month >= 1 && month <= 9) {
  535. month = "0" + month;
  536. }
  537. if (strDate >= 0 && strDate <= 9) {
  538. strDate = "0" + strDate;
  539. }
  540. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
  541. + " " + date.getHours() + seperator2 + date.getMinutes()
  542. + seperator2 + date.getSeconds();
  543. return currentdate;
  544. }