Aucune description

main.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. var ws, n = 0,
  2. timer,hidTel,hidCallID,hidActionID;
  3. var lockReconnect = false; //避免重复连接
  4. var obj = {};
  5. var directType;
  6. var sendType;
  7. var mesCont;
  8. var eorrorConnect;
  9. var cls = 0;
  10. var lasttime = new Date().getTime();
  11. var errorState;
  12. var reconnecTion;
  13. var recon;
  14. var loginOr;
  15. //创建scoket连接
  16. function createWebSocket() {
  17. try {
  18. $("#top-search li i").removeClass("active");
  19. Connect();
  20. } catch(e) {
  21. //打印日志
  22. reconnect();
  23. }
  24. }
  25. //连接
  26. function Connect() {
  27. ws = new WebSocket("ws://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  28. ws.onopen = function() {
  29. console.log(new Date() + " " + "建立连接");
  30. //心跳检测重置
  31. heartCheck.reset().start();
  32. cls = 0;
  33. $(".Login").addClass("active");
  34. if (errorState) {
  35. //自动签入
  36. lasttime = new Date().getTime();
  37. obj.Type = "Login";
  38. obj.AgentType = "0";
  39. Send();
  40. clearInterval(timer);
  41. //loginOr=false;
  42. errorState=false;
  43. }else{
  44. //签入
  45. Send();
  46. }
  47. };
  48. //接收到消息的回调方法
  49. ws.onmessage = function(evt) {
  50. //如果获取到消息,心跳检测重置
  51. //拿到任何消息都说明当前连接是正常的
  52. heartCheck.reset().start();
  53. var myDate = new Date();
  54. console.log(myDate + " receive " + evt.data);
  55. var data = JSON.parse(evt.data)[0];
  56. if(data) {
  57. var rlt = data.Result;
  58. if(rlt == true) {
  59. var type = data.Type;
  60. if (eorrorConnect) {
  61. LoginBack();
  62. obj.Type = "Login";
  63. Send();
  64. eorrorConnect=false;
  65. }
  66. switch(type.toLowerCase()) {
  67. //case "heart": HeartBack(); break;//心跳
  68. case "login":
  69. LoginBack();
  70. break; //签入
  71. case "logout":
  72. LogoutBack();
  73. break; //签出
  74. case "dropcall":
  75. DropCallBack();
  76. break; //挂断
  77. case "makecall":
  78. MakeCallBack();
  79. break; //外呼
  80. case "setstate":
  81. SetState(data);
  82. break; //置忙置闲
  83. //case "saybusy": SayBusyBack(data); break;
  84. //case "sayfree": SayFreeBack(data); break;
  85. case "meeting":
  86. MeetingBack();
  87. break; //多方通话
  88. case "transfer":
  89. TransferBack();
  90. break; //转移
  91. case "hold":
  92. HoldBack();
  93. break; //保持
  94. case "retrieve":
  95. RetrieveBack();
  96. break; //接回
  97. case "incoming":
  98. IncomingBack(data);
  99. break; //来电
  100. case "subscribe":
  101. SubScribeBack();
  102. break; //监测
  103. case "subscribecancel":
  104. SubScribeCancelBack();
  105. break; //停止监测
  106. case "agentstate":
  107. AgentStateBack(data);
  108. break; //坐席状态
  109. case "linestate":
  110. LineStateBack(data);
  111. break; //线路状态
  112. case "motorsetstate":
  113. SayFreeBack();
  114. break; //班长置闲
  115. case "linestateagent":
  116. LineStateAgentBack(data);
  117. break; //线路状态通知
  118. case "callid":
  119. CallIDBack(data);
  120. break; //获取callid
  121. case "recordpath":
  122. RecordPathBack(data);
  123. break; //录音返回
  124. }
  125. } else {
  126. if(rlt == false) {
  127. if (data.Type=="AcdConnectNotify") {
  128. //LogoutBack();
  129. $(".hwzt").text('连接异常!');
  130. setTimeout(function () {
  131. // 签入
  132. obj.Type = "login";
  133. Send();
  134. }, 5000);
  135. }
  136. $(".hwzt").text('操作失败!');
  137. } else {
  138. $(".hwzt").text(rlt);
  139. //layer.confirm(rlt, {
  140. // btn: ['确定']
  141. //});
  142. }
  143. }
  144. }
  145. };
  146. //连接关闭的回调方法
  147. ws.onclose = function(evt) {
  148. if(cls == 0) {
  149. cls = 1;
  150. //console.log("连接关闭!");
  151. //layer.confirm('连接关闭!', {
  152. // btn: ['确定']
  153. //});
  154. $(".hwzt").text('连接关闭!');
  155. $("#top-search li i").removeClass("active");
  156. reconnect();
  157. }
  158. };
  159. //连接发生错误的回调方法
  160. ws.onerror = function(evt) {
  161. //产生异常
  162. $(".hwzt").text('连接出现异常!');
  163. console.log(ws);
  164. if(ws == null || ws.readyState != ws.OPEN) {
  165. console.log(new Date() + "开始重连");
  166. reconnect();
  167. }
  168. };
  169. }
  170. //重连
  171. function reconnect() {
  172. if(lockReconnect) return;
  173. lockReconnect = true;
  174. //没连接上会一直重连,设置延迟避免请求过多
  175. setTimeout(function() {
  176. console.log(new Date() + " " + "重连中……");
  177. createWebSocket("ws://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  178. lockReconnect = false;
  179. }, 2000);
  180. }
  181. //异常重连
  182. function errorFun(){
  183. clearInterval(timer);
  184. var errorNum=0
  185. timer = setInterval(function() {
  186. errorNum++;
  187. if (errorNum>2) {
  188. //$("#top-search li i").removeClass("active");
  189. $(".hwzt").text('连接异常!');
  190. }else{
  191. LogoutBack();
  192. }
  193. reconnect();
  194. $(".hwzt").text('连接异常!');
  195. }, 15000);
  196. }
  197. noLine();
  198. clearInterval(recon);
  199. recon = setInterval(function () {
  200. if(loginOr){
  201. noLine();
  202. setTimeout(function () {
  203. lineState();
  204. }, 2000);
  205. }
  206. }, 1000*10);
  207. function lineState(){
  208. if (reconnecTion) {
  209. reconnecTion=false;
  210. }else{
  211. errorState=true;
  212. errorFun();
  213. var numEorr=0;
  214. numEorr++;
  215. if (numEorr>2) {
  216. $("#top-search li i").removeClass("active");
  217. }
  218. LogoutBack();
  219. $(".hwzt").text('网络异常!');
  220. }
  221. }
  222. function noLine(){
  223. $.ajax({
  224. type: "get",
  225. url: huayi.config.callcenter_url + 'UserAccount/GetNowUser',
  226. async: true,
  227. dataType: 'json',
  228. data: {
  229. "token": $.cookie("token")
  230. },
  231. success: function(data) {
  232. reconnecTion = data.data.user.F_UserCode;
  233. }
  234. });
  235. }
  236. //发送
  237. function Send() {
  238. if(ws.readyState != ws.OPEN) {
  239. errorState=true;
  240. LogoutBack();
  241. errorFun();
  242. $(".hwzt").text('连接异常!');
  243. reconnect();
  244. }
  245. if(ws.readyState == ws.OPEN) {
  246. console.log(new Date() + " send " + JSON.stringify(obj));
  247. if (obj.Type) {
  248. }else{
  249. }
  250. ws.send(JSON.stringify(obj));
  251. }
  252. }
  253. //心跳检测
  254. var heartCheck = {
  255. timeout:15000, //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. $("#top-search li i").removeClass("active");
  274. $(".Login").addClass("active");
  275. $(".zxzt").removeClass("bl").addClass("br");
  276. $(".fwzt").removeClass("bl").addClass("br");
  277. $(".hwzt").text('');
  278. }, self.timeout)
  279. }, this.timeout)
  280. }
  281. }
  282. //签入
  283. function LoginBack() {
  284. loginOr=true;
  285. $("#top-search li i").removeClass("active");
  286. $(".Logout").addClass("active");
  287. $(".SayBusy").addClass("active");
  288. $(".MakeCall").addClass("active");
  289. $(".zxzt").removeClass("br").addClass("bl");
  290. $(".fwzt").removeClass("br").addClass("bl");
  291. $(".hwzt").text('');
  292. }
  293. //签出
  294. function LogoutBack() {
  295. loginOr=false;
  296. $("#top-search li i").removeClass("active");
  297. $(".Login").addClass("active");
  298. $(".zxzt").removeClass("bl").addClass("br");
  299. $(".fwzt").removeClass("bl").addClass("br");
  300. $(".hwzt").text('');
  301. }
  302. //来电
  303. function IncomingBack(data) {
  304. debugger
  305. if(data.CallID!=0){
  306. var p = '<a href="javascript:;" class="active J_menuTab" data-id="./callScreen/callScreen.html?tel=' + data.Number + '&CallID=' + data.CallID + '&TrunkNumber=' + data.TrunkNumber +'">来电弹屏' + data.Number + ' <i class="fa fa-times-circle"></i></a>';
  307. $(".J_menuTab").removeClass("active");
  308. var nif = '<iframe class="J_iframe J_iframeNew" name="iframe'+ data.CallID +'" width="100%" height="100%" src="./callScreen/callScreen.html?Number=' + data.Number + '&CallID=' + data.CallID + '&ActionID=' + data.ActionID + '&TrunkNumber=' + data.TrunkNumber + '" frameborder="0" data-id="./callScreen/callScreen.html?tel=' + data.Number + '&CallID=' + data.CallID + '&TrunkNumber=' + data.TrunkNumber +'" seamless></iframe>';
  309. $(".J_mainContent").find("iframe.J_iframe").hide().parents(".J_mainContent").append(nif);
  310. $(".J_menuTabs .page-tabs-content").append(p);
  311. hidTel=data.Number;
  312. hidCallID=data.CallID;
  313. hidActionID=data.ActionID;
  314. }else{
  315. var timestamp = Date.parse(new Date());
  316. var p = '<a href="javascript:;" class="active J_menuTab" data-id="./callScreen/callScreen.html?tel=' + data.Number + '&CallID='+timestamp+ '&TrunkNumber=' + data.TrunkNumber +'">来电弹屏' + data.Number + ' <i class="fa fa-times-circle"></i></a>';
  317. $(".J_menuTab").removeClass("active");
  318. var nif = '<iframe class="J_iframe J_iframeNew" name="iframe'+timestamp+'" width="100%" height="100%" src="./callScreen/callScreen.html?Number=' + data.Number + '&CallID='+timestamp+ '&TrunkNumber=' + data.TrunkNumber +'" frameborder="0" data-id="./callScreen/callScreen.html?tel=' + data.Number + '&CallID='+timestamp+ '&TrunkNumber=' + data.TrunkNumber +'" seamless></iframe>';
  319. $(".J_mainContent").find("iframe.J_iframe").hide().parents(".J_mainContent").append(nif);
  320. $(".J_menuTabs .page-tabs-content").append(p);
  321. hidTel=data.Number;
  322. hidCallID=timestamp;
  323. }
  324. }
  325. //***************************************************
  326. function CallIDBack(data) {
  327. //$(".hidCallID").val(data.CurrID);
  328. }
  329. //挂断
  330. function DropCallBack() {
  331. $("#top-search li i").removeClass("active");
  332. $(".Logout").addClass("active");
  333. $(".SayBusy").addClass("active");
  334. $(".MakeCall").addClass("active");
  335. var obj = $("iframe:visible")
  336. // if (obj.attr("data-id") == './callScreen/callScreen.html?tel=' + hidTel + '&CallID=' + hidCallID + '') {
  337. // window.frames[obj.attr("name")].$(".td-call").hide();
  338. // window.frames['iframe'+ hidCallID +''].$(".td-call").hide();
  339. // }
  340. if(window.frames['iframe'+ hidCallID +'']){
  341. window.frames['iframe'+ hidCallID +''].$(".td-call").hide();
  342. }
  343. }
  344. //线路状态通知
  345. function LineStateAgentBack(data) {
  346. var objiframe = $("iframe:visible")
  347. //0分机不可用,1空闲,2摘机等待拨号,3正在拨号,4呼出振铃,5来电振铃,6通话中,7播放忙音中,8移除IP分机,9通话保持中
  348. if(data.State == '0') {
  349. $(".hwzt").text('分机不可用'); //左下角状态显示
  350. //$(".fwzt").removeClass("br").removeClass("bl");
  351. $(".fwzt").removeClass("bl").addClass("br");//20180509 by fanlongfei 增加话机状态灯
  352. $(".zxzt").removeClass("bl").addClass("br");//20180509 by fanlongfei 增加坐席状态灯
  353. }
  354. if(data.State == '1') {
  355. $(".hwzt").text('空闲'); //左下角状态显示
  356. $(".fwzt").removeClass("br").addClass("bl");
  357. $(".zxzt").removeClass("br").addClass("bl");//20180509 by fanlongfei 增加坐席状态灯
  358. if(window.frames['iframe'+ hidCallID +'']){
  359. window.frames['iframe'+ hidCallID +''].clearInter();
  360. }
  361. if (hidTel && hidCallID) {
  362. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateGJ', {
  363. callid: hidCallID,
  364. "token": $.cookie("token")
  365. }, function (result) {
  366. result = JSON.parse(result);
  367. if (result.state.toLowerCase() == "success") { }
  368. })
  369. }
  370. $("#top-search li i").removeClass("active");
  371. $(".Logout").addClass("active");
  372. $(".SayBusy").addClass("active");
  373. $(".MakeCall").addClass("active");
  374. if(window.frames['iframe'+ hidCallID +'']){
  375. window.frames['iframe'+ hidCallID +''].$(".td-call").hide();
  376. }
  377. }
  378. if(data.State == '2') {
  379. $(".hwzt").text('摘机等待拨号'); //左下角状态显示
  380. }
  381. if(data.State == '3') {
  382. $(".hwzt").text('正在拨号'); //左下角状态显示
  383. }
  384. if(data.State == '4') {
  385. $(".hwzt").text('呼出振铃'); //左下角状态显示
  386. }
  387. if(data.State == '5') {
  388. $(".hwzt").text('来电振铃'); //左下角状态显示
  389. //$(".fwzt").removeClass("bl").addClass("br");
  390. $(".fwzt").removeClass("br").addClass("bl");//20180509 by fanlongfei 增加话机状态灯
  391. $(".zxzt").removeClass("bl").addClass("br");//20180509 by fanlongfei 增加坐席状态灯
  392. if (hidTel && hidCallID) {
  393. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZL', {
  394. callid: hidCallID,
  395. "token": $.cookie("token")
  396. }, function (result) {
  397. result = JSON.parse(result);
  398. if (result.state.toLowerCase() == "success") { }
  399. })
  400. }
  401. }
  402. if(data.State == '6') {
  403. $(".hwzt").text('通话中'); //左下角状态显示
  404. $(".fwzt").removeClass("bl").addClass("br");
  405. $(".zxzt").removeClass("bl").addClass("br");//20180509 by fanlongfei 增加坐席状态灯
  406. if(hidTel && hidCallID) {
  407. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZJ', {
  408. callid: hidCallID,
  409. "token": $.cookie("token")
  410. }, function(result) {
  411. result = JSON.parse(result);
  412. if(result.state.toLowerCase() == "success") {}
  413. })
  414. }
  415. $("#top-search li i").removeClass("active");
  416. $(".DropCall").addClass("active");
  417. $(".Hold").addClass("active");
  418. $(".Transfer").addClass("active");
  419. $(".Meeting").addClass("active");
  420. var aaa=data.CallDirection;
  421. if(data.CallDirection=="in"){
  422. // if (objiframe.attr("data-id") == './callScreen/callScreen.html?tel=' + hidTel + '&CallID=' + hidCallID + '') {
  423. setTimeout(window.frames['iframe'+ hidCallID +''].calling(),500);/*2018-05-19 zhangshuangnan 修改 ps: 因软电话设置为 自动应答 通话时间不计时问题*/
  424. // }
  425. }
  426. }
  427. if(data.State == '7') {
  428. $(".hwzt").text('播放忙音中'); //左下角状态显示
  429. }
  430. if(data.State == '8') {
  431. $(".hwzt").text('移除IP分机'); //左下角状态显示
  432. }
  433. if(data.State == '9') {
  434. $(".hwzt").text('通话保持中'); //左下角状态显示
  435. }
  436. }
  437. function toDub(i) {
  438. return i < 10 ? "0" + i : "" + i;
  439. }
  440. //外呼
  441. function MakeCallBack() {
  442. $("#top-search li i").removeClass("active");
  443. $(".DropCall").addClass("active");
  444. // alert('1');
  445. // window.frames[obj.attr("name")].calling();
  446. }
  447. //置忙置闲
  448. function SetState(obj) {
  449. if(obj.State == '5') {
  450. $(".SayBusy").removeClass("active");
  451. $(".SayFree").addClass("active");
  452. $(".zxzt").removeClass("bl").addClass("br");
  453. $(".hwzt").text('置忙');
  454. }
  455. if(obj.State == '2') {
  456. $(".SayBusy").addClass("active");
  457. $(".SayFree").removeClass("active");
  458. $(".zxzt").removeClass("br").addClass("bl");
  459. $(".hwzt").text('空闲');
  460. }
  461. }
  462. //置忙
  463. function SayBusyBack() {
  464. $(".SayBusy").removeClass("active");
  465. $(".SayFree").addClass("active");
  466. }
  467. //坐席班长置闲
  468. function SayFreeBack() {
  469. $(".SayBusy").addClass("active");
  470. $(".SayFree").removeClass("active");
  471. $(".zxzt").removeClass("br").addClass("bl"); // 2018/05/09 by fanlongfei 增加坐席状态指示
  472. $(".hwzt").text('空闲');// 2018/05/09 by fanlongfei 增加坐席状态指示
  473. }
  474. //多方通话
  475. function MeetingBack() {
  476. }
  477. //转移
  478. function TransferBack() {
  479. $("#top-search li i").removeClass("active");
  480. $(".Logout").addClass("active");
  481. $(".SayBusy").addClass("active");
  482. $(".MakeCall").addClass("active");
  483. }
  484. //保持
  485. function HoldBack() {
  486. $(".Hold").removeClass("active");
  487. $(".Retrieve").addClass("active");
  488. }
  489. //接回
  490. function RetrieveBack() {
  491. $(".Hold").addClass("active");
  492. $(".Retrieve").removeClass("active");
  493. }
  494. //监测
  495. function SubScribeBack() {
  496. $('#content-main .J_iframeNew').each(function(i,n){
  497. if($(this).attr('data-id') == "./TelCall/SeatMonitor.html"){
  498. window.frames[$(this).attr("name")].Start();
  499. }
  500. })/*2018-05-24 zhangshuangnan 修改 解决通话中时 坐席监控状态不改变的问题*/
  501. }
  502. //取消监测
  503. function SubScribeCancelBack() {
  504. // var obj = $("iframe:visible")
  505. // if(obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  506. // window.frames[obj.attr("name")].();
  507. // }
  508. $('#content-main .J_iframeNew').each(function(i,n){
  509. if($(this).attr('data-id') == "./TelCall/SeatMonitor.html"){
  510. window.frames[$(this).attr("name")].Stop();
  511. }
  512. })/*2018-05-24 zhangshuangnan 修改 解决通话中时 坐席监控状态不改变的问题*/
  513. }
  514. //班长监测返回状态
  515. //坐席状态
  516. function AgentStateBack(data) {
  517. $('#content-main .J_iframeNew').each(function(i,n){
  518. if($(this).attr('data-id') == "./TelCall/SeatMonitor.html"){
  519. window.frames[$(this).attr("name")].UpdateAgentState(data.AgentID, data.State);
  520. }
  521. })/*2018-05-24 zhangshuangnan 修改 解决通话中时 坐席监控状态不改变的问题*/
  522. }
  523. //线路状态
  524. function LineStateBack(data) {
  525. $('#content-main .J_iframeNew').each(function(i,n){
  526. if($(this).attr('data-id') == "./TelCall/SeatMonitor.html"){
  527. window.frames[$(this).attr("name")].UpdateLineState(data.AgentID, data.State);
  528. }
  529. })/*2018-05-24 zhangshuangnan 修改 解决通话中时 坐席监控状态不改变的问题*/
  530. }
  531. //录音返回
  532. function RecordPathBack(data) {
  533. $(".hidCallID").val(data.CallID);
  534. //if ($(".hidTel").val() && $(".hidCallID").val()) {
  535. // $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateLY', { callid: $(".hidCallID").val(),path:data.RecPath, "token": $.cookie("token") }, function (result) {
  536. // result = JSON.parse(result);
  537. // if (result.state.toLowerCase() == "success") {
  538. // }
  539. // })
  540. //}
  541. if(hidTel) {
  542. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateLY', {
  543. callid: data.CallID,
  544. path: data.RecPath,
  545. "token": $.cookie("token")
  546. }, function(result) {
  547. result = JSON.parse(result);
  548. if(result.state.toLowerCase() == "success") {}
  549. })
  550. }
  551. }
  552. //获取当前的日期时间 格式“yyyy-MM-dd HH:mm:ss”
  553. function getNowFormatDate() {
  554. var date = new Date();
  555. var seperator1 = "-";
  556. var seperator2 = ":";
  557. var month = date.getMonth() + 1;
  558. var strDate = date.getDate();
  559. if(month >= 1 && month <= 9) {
  560. month = "0" + month;
  561. }
  562. if(strDate >= 0 && strDate <= 9) {
  563. strDate = "0" + strDate;
  564. }
  565. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
  566. " " + date.getHours() + seperator2 + date.getMinutes() +
  567. seperator2 + date.getSeconds();
  568. return currentdate;
  569. }