安图前端代码

main.js 16KB

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