民权县12345_前端

main.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. if (typeof console == "undefined") {
  2. this.console = { log: function (msg) { } };
  3. }
  4. // 如果浏览器不支持websocket,会使用这个flash自动模拟websocket协议,此过程对开发者透明
  5. WEB_SOCKET_SWF_LOCATION = "./js/websocket/WebSocketMain.swf";
  6. // 开启flash的websocket debug
  7. WEB_SOCKET_DEBUG = true;
  8. var ws, n = 0,
  9. timer;
  10. var lockReconnect = false; //避免重复连接
  11. var obj = {};
  12. var Statess;
  13. var cls = 0;
  14. var lasttime = new Date().getTime();
  15. //创建scoket连接
  16. function createWebSocket() {
  17. try {
  18. $("#top-search li i").removeClass("active");
  19. Connect();
  20. } catch (e) {
  21. reconnect();
  22. }
  23. }
  24. //连接
  25. function Connect() {
  26. ws = new WebSocket("ws://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  27. ws.onopen = function () {
  28. console.log(new Date() + " " + "建立连接");
  29. //心跳检测重置
  30. heartCheck.reset().start();
  31. cls = 0;
  32. $(".Login").addClass("active");
  33. lasttime = new Date().getTime();
  34. if ($.cookie("socket_state") != null) {
  35. SetLogin($.cookie("socket_state"));
  36. }
  37. //$(".hwzt").text('连接成功!');
  38. };
  39. //接收到消息的回调方法
  40. ws.onmessage = function (evt) {
  41. //如果获取到消息,心跳检测重置
  42. //拿到任何消息都说明当前连接是正常的
  43. heartCheck.reset().start();
  44. var myDate = new Date();
  45. console.log(myDate + " receive " + evt.data);
  46. var data = JSON.parse(evt.data)[0];
  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. case "getagentlist":
  110. GetAgentListBack(data);
  111. break;//在线坐席信息
  112. }
  113. } else {
  114. if (rlt == false) {
  115. //layer.confirm('操作失败!', {
  116. // btn: ['确定']
  117. //});
  118. $(".hwzt").text('操作失败!');
  119. } else {
  120. $(".hwzt").text(rlt);
  121. //layer.confirm(rlt, {
  122. // btn: ['确定']
  123. //});
  124. }
  125. }
  126. }
  127. };
  128. //连接关闭的回调方法
  129. ws.onclose = function (evt) {
  130. if (cls == 0) {
  131. cls = 1;
  132. //console.log("连接关闭!");
  133. //layer.confirm('连接关闭!', {
  134. // btn: ['确定']
  135. //});
  136. $(".hwzt").text('连接关闭!');
  137. $("#top-search li i").removeClass("active");
  138. reconnect();
  139. }
  140. };
  141. //连接发生错误的回调方法
  142. ws.onerror = function (evt) {
  143. //产生异常
  144. $(".hwzt").text('连接出现异常!');
  145. console.log(ws);
  146. if (ws == null || ws.readyState != ws.OPEN) {
  147. console.log(new Date() + "开始重连");
  148. reconnect();
  149. }
  150. };
  151. }
  152. //重连
  153. function reconnect() {
  154. if (lockReconnect) return;
  155. lockReconnect = true;
  156. //没连接上会一直重连,设置延迟避免请求过多
  157. setTimeout(function () {
  158. console.log(new Date() + " " + "重连中……");
  159. createWebSocket("ws://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  160. lockReconnect = false;
  161. }, 2000);
  162. }
  163. //发送
  164. function Send() {
  165. if (ws.readyState != ws.OPEN) {
  166. reconnect();
  167. }
  168. if (ws.readyState == ws.OPEN) {
  169. console.log(new Date() + " send " + JSON.stringify(obj));
  170. ws.send(JSON.stringify(obj));
  171. }
  172. }
  173. //心跳检测
  174. var heartCheck = {
  175. timeout: 25000, //25秒
  176. timeoutObj: null,
  177. serverTimeoutObj: null,
  178. reset: function () {
  179. clearTimeout(this.timeoutObj);
  180. clearTimeout(this.serverTimeoutObj);
  181. return this;
  182. },
  183. start: function () {
  184. var self = this;
  185. this.timeoutObj = setTimeout(function () {
  186. //这里发送一个心跳,后端收到后,返回一个心跳消息,
  187. //onmessage拿到返回的心跳就说明连接正常
  188. obj.Type = "Heart";
  189. Send();
  190. self.serverTimeoutObj = setTimeout(function () { //如果超过一定时间还没重置,说明后端主动断开了
  191. ws.close(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
  192. }, self.timeout)
  193. }, this.timeout)
  194. }
  195. }
  196. //签入
  197. function LoginBack() {
  198. $("#top-search li i").removeClass("active");
  199. $(".Logout").addClass("active");
  200. $(".SayBusy").addClass("active");
  201. $(".MakeCall").addClass("active");
  202. $(".zxzt").removeClass("br").addClass("bl");
  203. $(".fwzt").removeClass("br").addClass("bl");
  204. $(".hwzt").text('');
  205. if ($.cookie("socket_state") == null) {
  206. SetStateCookie(0);
  207. }
  208. }
  209. //签出
  210. function LogoutBack() {
  211. $("#top-search li i").removeClass("active");
  212. $(".Login").addClass("active");
  213. $(".zxzt").removeClass("bl").addClass("br");
  214. $(".fwzt").removeClass("bl").addClass("br");
  215. $(".hwzt").text('');
  216. SetStateCookie(2);
  217. }
  218. //来电
  219. function IncomingBack(data) {
  220. $(".ldhm").val(data.Number);
  221. $(".hidTel").val(data.Number);
  222. $(".tel").text(data.Number);
  223. $(".ldtime").text(getNowFormatDate());
  224. $(".thsc").text("00:00");
  225. $(".hidCallID").val(data.CallID);
  226. //获取电话所属地
  227. $.getJSON(huayi.config.callcenter_url + 'CallInScreen/GetPhoneLocation', {
  228. "tel": $(".hidTel").val(),
  229. "token": $.cookie("token")
  230. }, function (result) {
  231. if (result.state.toLowerCase() == "success") {
  232. $(".ldlocation").text(result.data[0].F_Name);
  233. $(".khgsd").text(result.data[0].F_Name);
  234. }
  235. })
  236. //获取客户信息
  237. $(".ttsdh").text($(".hidTel").val());
  238. $(".tsdh").val($(".hidTel").val());
  239. $.getJSON(huayi.config.callcenter_url + 'CallInScreen/GetCustomerByTel', {
  240. "tel": $(".hidTel").val(),
  241. "token": $.cookie("token")
  242. }, function (result) {
  243. if (result.state.toLowerCase() == "success") {
  244. if (result.data.length > 0) {
  245. $(".khmc").val(result.data[0].F_CustomerName);
  246. $(".tkhmc").text(result.data[0].F_CustomerName);
  247. $("#khid").val(result.data[0].F_CustomerId);
  248. $(".lxdh").val(result.data[0].F_Telephone);
  249. //$(".tsdh").val(result.data[0].F_Mobile);
  250. //$(".ttsdh").text(result.data[0].F_Mobile);
  251. $(".lxr").val(result.data[0].F_CustomerEName);
  252. $(".postcode").val(result.data[0].F_PostCode);
  253. $(".email").val(result.data[0].F_Email);
  254. }
  255. else {
  256. $(".khmc").val("");
  257. $(".tkhmc").text("");
  258. $("#khid").val("");
  259. $(".lxr").val("");
  260. $(".postcode").val("");
  261. $(".email").val("");
  262. }
  263. }
  264. })
  265. $('.maxOpen').trigger("click");
  266. $('.head-pic .lahei .la-before').show();
  267. //触发来电类型第一个 点击;
  268. $("#dicValueList li:first-child").find("label").trigger('click');
  269. $('.head-pic .lahei .la-after').hide();
  270. $(".Bacha").hide();
  271. }
  272. //挂断
  273. function DropCallBack() {
  274. $("#top-search li i").removeClass("active");
  275. $(".Logout").addClass("active");
  276. $(".SayBusy").addClass("active");
  277. $(".MakeCall").addClass("active");
  278. $(".td-call").hide();
  279. $(".hwzt").text('忙碌');
  280. $("#isml").val(1);
  281. SetStateCookie(1);
  282. obj.Type = "SayBusy";
  283. Send();
  284. //$('.ldtp-con').css("display", 'none');
  285. }
  286. function LineStateAgentBack(data) {
  287. //0分机不可用,1空闲,2摘机等待拨号,3正在拨号,4呼出振铃,5来电振铃,6通话中,7播放忙音中,8移除IP分机,9通话保持中
  288. if (data.State == '0') {
  289. $(".hwzt").text('分机不可用'); //左下角状态显示
  290. }
  291. if (data.State == '1') {
  292. $(".hwzt").text('空闲'); //左下角状态显示
  293. $(".fwzt").removeClass("br").addClass("bl");
  294. clearInterval(timer);
  295. if ($(".hidTel").val() && $(".hidCallID").val()) {
  296. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateGJ', {
  297. callid: $(".hidCallID").val(),
  298. "token": $.cookie("token")
  299. }, function (result) {
  300. result = JSON.parse(result);
  301. if (result.state.toLowerCase() == "success") { }
  302. })
  303. }
  304. $("#top-search li i").removeClass("active");
  305. $(".Logout").addClass("active");
  306. $(".SayBusy").addClass("active");
  307. $(".MakeCall").addClass("active");
  308. $(".td-call").hide();
  309. $(".Bacha").show();
  310. //$(".hidTel").val("");
  311. //$(".hidCallID").val("");
  312. }
  313. if (data.State == '2') {
  314. $(".hwzt").text('摘机等待拨号'); //左下角状态显示
  315. }
  316. if (data.State == '3') {
  317. $(".hwzt").text('正在拨号'); //左下角状态显示
  318. }
  319. if (data.State == '4') {
  320. $(".hwzt").text('呼出振铃'); //左下角状态显示
  321. }
  322. if (data.State == '5') {
  323. $(".hwzt").text('来电振铃'); //左下角状态显示
  324. $(".fwzt").removeClass("bl").addClass("br");
  325. if ($(".hidTel").val() && $(".hidCallID").val()) {
  326. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZL', {
  327. callid: $(".hidCallID").val(),
  328. "token": $.cookie("token")
  329. }, function (result) {
  330. result = JSON.parse(result);
  331. if (result.state.toLowerCase() == "success") { }
  332. })
  333. }
  334. }
  335. if (data.State == '6') {
  336. $(".hwzt").text('通话中'); //左下角状态显示
  337. $(".fwzt").removeClass("bl").addClass("br");
  338. if ($(".hidTel").val() && $(".hidCallID").val()) {
  339. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZJ', {
  340. callid: $(".hidCallID").val(),
  341. "token": $.cookie("token")
  342. }, function (result) {
  343. result = JSON.parse(result);
  344. if (result.state.toLowerCase() == "success") { }
  345. })
  346. }
  347. $(".td-call").show();
  348. n = 0;
  349. $("#top-search li i").removeClass("active");
  350. $(".DropCall").addClass("active");
  351. $(".Hold").addClass("active");
  352. $(".Transfer").addClass("active");
  353. $(".Turn110").addClass("active");
  354. $(".Meeting").addClass("active");
  355. clearInterval(timer);
  356. timer = setInterval(function () {
  357. n++;
  358. var m = parseInt(n / 60 % 60);
  359. var s = parseInt(n % 60);
  360. $(".thsc").text(toDub(m) + ":" + toDub(s));
  361. }, 1000);
  362. }
  363. if (data.State == '7') {
  364. $(".hwzt").text('播放忙音中'); //左下角状态显示
  365. }
  366. if (data.State == '8') {
  367. $(".hwzt").text('移除IP分机'); //左下角状态显示
  368. }
  369. if (data.State == '9') {
  370. $(".hwzt").text('通话保持中'); //左下角状态显示
  371. }
  372. }
  373. function toDub(i) {
  374. return i < 10 ? "0" + i : "" + i;
  375. }
  376. //外呼
  377. function MakeCallBack() {
  378. $("#top-search li i").removeClass("active");
  379. $(".DropCall").addClass("active");
  380. $(".Meeting").addClass("active");
  381. }
  382. //默认记忆上次是否签入,是否置忙置闲 0表示已签入 空闲,1表示签入置忙,2表示签出
  383. function SetStateCookie(state) {
  384. $.cookie("socket_state", state);
  385. }
  386. //置忙置闲
  387. function SetState(obj) {
  388. if (obj.State == '5') {
  389. $(".SayBusy").removeClass("active");
  390. $(".SayFree").addClass("active");
  391. $(".zxzt").removeClass("bl").addClass("br");
  392. $(".hwzt").text('置忙');
  393. SetStateCookie(1);
  394. }
  395. if (obj.State == '2') {
  396. $(".SayBusy").addClass("active");
  397. $(".SayFree").removeClass("active");
  398. $(".zxzt").removeClass("br").addClass("bl");
  399. $(".hwzt").text('空闲');
  400. SetStateCookie(0);
  401. }
  402. }
  403. function SetLogin(state) {
  404. if (state == 2) { return; }
  405. if (obj.AgentID) {
  406. if($("#RoleCode").val()=='HFZ'){
  407. obj.AgentGroup = "2";
  408. }else{
  409. obj.AgentGroup = "364";
  410. }
  411. obj.Type = "Login";
  412. obj.AgentType = "0";
  413. Send();
  414. }
  415. if (state == 1) {
  416. setTimeout('SayBusy()', 500);
  417. }
  418. }
  419. function SayBusy() {
  420. if (obj.AgentID) {
  421. obj.Type = "SayBusy";
  422. Send();
  423. }
  424. }
  425. //置忙
  426. function SayBusyBack() {
  427. $(".SayBusy").removeClass("active");
  428. $(".SayFree").addClass("active");
  429. }
  430. //置闲
  431. function SayFreeBack() {
  432. $(".SayBusy").addClass("active");
  433. $(".SayFree").removeClass("active");
  434. }
  435. //多方通话
  436. function MeetingBack() {
  437. }
  438. //转移
  439. function TransferBack() {
  440. $("#top-search li i").removeClass("active");
  441. $(".Logout").addClass("active");
  442. $(".SayBusy").addClass("active");
  443. $(".MakeCall").addClass("active");
  444. }
  445. //保持
  446. function HoldBack() {
  447. $(".Hold").removeClass("active");
  448. $(".Retrieve").addClass("active");
  449. }
  450. //接回
  451. function RetrieveBack() {
  452. $(".Hold").addClass("active");
  453. $(".Retrieve").removeClass("active");
  454. }
  455. //监测
  456. function SubScribeBack() {
  457. var obj = $("iframe:visible")
  458. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  459. window.frames[obj.attr("name")].Start();
  460. }
  461. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  462. window.frames[obj.attr("name")].Start();
  463. }
  464. }
  465. //取消监测
  466. function SubScribeCancelBack() {
  467. var obj = $("iframe:visible")
  468. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  469. window.frames[obj.attr("name")].Stop();
  470. }
  471. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  472. window.frames[obj.attr("name")].Stop();
  473. }
  474. }
  475. //班长监测返回状态
  476. //坐席状态
  477. function AgentStateBack(data) {
  478. var obj = $("iframe:visible")
  479. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  480. window.frames[obj.attr("name")].UpdateAgentState(data.AgentID, data.State);
  481. }
  482. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  483. window.frames[obj.attr("name")].UpdateAgentState(data.AgentID, data.State);
  484. }
  485. }
  486. //线路状态
  487. function LineStateBack(data) {
  488. var obj = $("iframe:visible")
  489. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  490. window.frames[obj.attr("name")].UpdateLineState(data.AgentID, data.State);
  491. }
  492. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  493. window.frames[obj.attr("name")].UpdateLineState(data.AgentID, data.State);
  494. }
  495. }
  496. //录音返回
  497. function RecordPathBack(data) {
  498. //if ($(".hidTel").val() && $(".hidCallID").val()) {
  499. // $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateLY', { callid: $(".hidCallID").val(),path:data.RecPath, "token": $.cookie("token") }, function (result) {
  500. // result = JSON.parse(result);
  501. // if (result.state.toLowerCase() == "success") {
  502. // }
  503. // })
  504. //}
  505. if ($(".hidTel").val()) {
  506. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateLY', {
  507. callid: data.CallID,
  508. path: data.RecPath,
  509. "token": $.cookie("token")
  510. }, function (result) {
  511. result = JSON.parse(result);
  512. if (result.state.toLowerCase() == "success") { }
  513. })
  514. }
  515. }
  516. //在线坐席信息
  517. function GetAgentListBack(data) {
  518. console.log(data)
  519. $.ajax({
  520. type: "get",
  521. url: huayi.config.callcenter_url + "SeatMonitoring/GetAgentList",
  522. async: true,
  523. dataType: 'json',
  524. data: {
  525. "token": $.cookie("token")
  526. },
  527. success: function (result) {
  528. var user = result.data;
  529. $(user).each(function (j, m) {
  530. $(data.AgentList).each(function (k, g) {
  531. if (g.AgentID == m.UserCode) {
  532. g.userName = m.UserName;
  533. }
  534. })
  535. })
  536. $(data.AgentList).each(function (k, m) {
  537. var strr = '';
  538. switch (m.State) {
  539. case "0":
  540. strr = "离线";
  541. break; //离线
  542. case "1":
  543. break; //登录中
  544. case "2":
  545. strr = "空闲";
  546. break; //空闲
  547. case "3":
  548. strr = "通话中";
  549. break; //通话中
  550. case "4":
  551. strr = "话后处理中";
  552. break; //话后处理中
  553. case "5":
  554. strr = "忙碌";
  555. break; //小休
  556. case "6":
  557. strr = "振铃";
  558. break; //被请求
  559. case "7":
  560. strr = "注销";
  561. break; //注销
  562. }
  563. var html = '<tr fjh="' + m.AgentExten + '">' +
  564. '<td>' + (m.userName ? '' : m.userName) + '</td>' //姓名
  565. +
  566. '<td>' + m.AgentID + '</td>' //工号
  567. +
  568. '<td>' + m.AgentExten + '</td>'//分机号
  569. +
  570. '<td class=" ' + m.UserCode + 'state">' + strr + '</td>' //状态
  571. +
  572. '</tr>';
  573. $(html).appendTo("#zxTable tbody");
  574. })
  575. }
  576. });
  577. }
  578. //获取当前的日期时间 格式“yyyy-MM-dd HH:mm:ss”
  579. function getNowFormatDate() {
  580. var date = new Date();
  581. var seperator1 = "-";
  582. var seperator2 = ":";
  583. var month = date.getMonth() + 1;
  584. var strDate = date.getDate();
  585. if (month >= 1 && month <= 9) {
  586. month = "0" + month;
  587. }
  588. if (strDate >= 0 && strDate <= 9) {
  589. strDate = "0" + strDate;
  590. }
  591. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
  592. " " + date.getHours() + seperator2 + date.getMinutes() +
  593. seperator2 + date.getSeconds();
  594. return currentdate;
  595. }
  596. var iswebcloase = 1;
  597. window.onunload = function () {
  598. if (iswebcloase) {
  599. iswebcloase = 0;
  600. if (ws.readyState == ws.OPEN) {
  601. obj.Type = 'Logout';
  602. Send();
  603. ws.onclose();
  604. }
  605. }
  606. }
  607. window.onbeforeunload = function () {
  608. if (iswebcloase) {
  609. iswebcloase = 0;
  610. if (ws.readyState == ws.OPEN) {
  611. obj.Type = 'Logout';
  612. Send();
  613. ws.onclose();
  614. }
  615. }
  616. }