Aucune description

main.js 17KB

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