民权县12345_前端

main.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. console.log($("#RoleCode").val())
  209. if($("#RoleCode").val()=='HFZY'){
  210. $(".hwzt").text('置忙');
  211. SetStateCookie(1);
  212. }
  213. }
  214. //签出
  215. function LogoutBack() {
  216. $("#top-search li i").removeClass("active");
  217. $(".Login").addClass("active");
  218. $(".zxzt").removeClass("bl").addClass("br");
  219. $(".fwzt").removeClass("bl").addClass("br");
  220. $(".hwzt").text('');
  221. SetStateCookie(2);
  222. }
  223. //来电
  224. function IncomingBack(data) {
  225. $(".ldhm").val(data.Number);
  226. $(".hidTel").val(data.Number);
  227. $(".tel").text(data.Number);
  228. $(".ldtime").text(getNowFormatDate());
  229. $(".thsc").text("00:00");
  230. $(".hidCallID").val(data.CallID);
  231. //获取电话所属地
  232. $.getJSON(huayi.config.callcenter_url + 'CallInScreen/GetPhoneLocation', {
  233. "tel": $(".hidTel").val(),
  234. "token": $.cookie("token")
  235. }, function (result) {
  236. if (result.state.toLowerCase() == "success") {
  237. $(".ldlocation").text(result.data[0].F_Name);
  238. $(".khgsd").text(result.data[0].F_Name);
  239. }
  240. })
  241. //获取客户信息
  242. $(".ttsdh").text($(".hidTel").val());
  243. $(".tsdh").val($(".hidTel").val());
  244. $.getJSON(huayi.config.callcenter_url + 'CallInScreen/GetCustomerByTel', {
  245. "tel": $(".hidTel").val(),
  246. "token": $.cookie("token")
  247. }, function (result) {
  248. if (result.state.toLowerCase() == "success") {
  249. if (result.data.length > 0) {
  250. $(".khmc").val(result.data[0].F_CustomerName);
  251. $(".tkhmc").text(result.data[0].F_CustomerName);
  252. $("#khid").val(result.data[0].F_CustomerId);
  253. $(".lxdh").val(result.data[0].F_Telephone);
  254. //$(".tsdh").val(result.data[0].F_Mobile);
  255. //$(".ttsdh").text(result.data[0].F_Mobile);
  256. $(".lxr").val(result.data[0].F_CustomerEName);
  257. $(".postcode").val(result.data[0].F_PostCode);
  258. $(".email").val(result.data[0].F_Email);
  259. }
  260. else {
  261. $(".khmc").val("");
  262. $(".tkhmc").text("");
  263. $("#khid").val("");
  264. $(".lxr").val("");
  265. $(".postcode").val("");
  266. $(".email").val("");
  267. }
  268. }
  269. })
  270. $('.maxOpen').trigger("click");
  271. $('.head-pic .lahei .la-before').show();
  272. //触发来电类型第一个 点击;
  273. $("#dicValueList li:first-child").find("label").trigger('click');
  274. $('.head-pic .lahei .la-after').hide();
  275. $(".Bacha").hide();
  276. }
  277. //挂断
  278. function DropCallBack() {
  279. $("#top-search li i").removeClass("active");
  280. $(".Logout").addClass("active");
  281. $(".SayBusy").addClass("active");
  282. $(".MakeCall").addClass("active");
  283. $(".td-call").hide();
  284. $(".hwzt").text('忙碌');
  285. $("#isml").val(1);
  286. SetStateCookie(1);
  287. obj.Type = "SayBusy";
  288. Send();
  289. //$('.ldtp-con').css("display", 'none');
  290. }
  291. function LineStateAgentBack(data) {
  292. //0分机不可用,1空闲,2摘机等待拨号,3正在拨号,4呼出振铃,5来电振铃,6通话中,7播放忙音中,8移除IP分机,9通话保持中
  293. if (data.State == '0') {
  294. $(".hwzt").text('分机不可用'); //左下角状态显示
  295. }
  296. if (data.State == '1') {
  297. $(".hwzt").text('空闲'); //左下角状态显示
  298. $(".fwzt").removeClass("br").addClass("bl");
  299. clearInterval(timer);
  300. if ($(".hidTel").val() && $(".hidCallID").val()) {
  301. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateGJ', {
  302. callid: $(".hidCallID").val(),
  303. "token": $.cookie("token")
  304. }, function (result) {
  305. result = JSON.parse(result);
  306. if (result.state.toLowerCase() == "success") { }
  307. })
  308. }
  309. $("#top-search li i").removeClass("active");
  310. $(".Logout").addClass("active");
  311. $(".SayBusy").addClass("active");
  312. $(".MakeCall").addClass("active");
  313. $(".td-call").hide();
  314. $(".Bacha").show();
  315. //$(".hidTel").val("");
  316. //$(".hidCallID").val("");
  317. }
  318. if (data.State == '2') {
  319. $(".hwzt").text('摘机等待拨号'); //左下角状态显示
  320. }
  321. if (data.State == '3') {
  322. $(".hwzt").text('正在拨号'); //左下角状态显示
  323. }
  324. if (data.State == '4') {
  325. $(".hwzt").text('呼出振铃'); //左下角状态显示
  326. }
  327. if (data.State == '5') {
  328. $(".hwzt").text('来电振铃'); //左下角状态显示
  329. $(".fwzt").removeClass("bl").addClass("br");
  330. if ($(".hidTel").val() && $(".hidCallID").val()) {
  331. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZL', {
  332. callid: $(".hidCallID").val(),
  333. "token": $.cookie("token")
  334. }, function (result) {
  335. result = JSON.parse(result);
  336. if (result.state.toLowerCase() == "success") { }
  337. })
  338. }
  339. }
  340. if (data.State == '6') {
  341. $(".hwzt").text('通话中'); //左下角状态显示
  342. $(".fwzt").removeClass("bl").addClass("br");
  343. if ($(".hidTel").val() && $(".hidCallID").val()) {
  344. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateZJ', {
  345. callid: $(".hidCallID").val(),
  346. "token": $.cookie("token")
  347. }, function (result) {
  348. result = JSON.parse(result);
  349. if (result.state.toLowerCase() == "success") { }
  350. })
  351. }
  352. $(".td-call").show();
  353. n = 0;
  354. $("#top-search li i").removeClass("active");
  355. $(".DropCall").addClass("active");
  356. $(".Hold").addClass("active");
  357. $(".Transfer").addClass("active");
  358. $(".Turn110").addClass("active");
  359. $(".Meeting").addClass("active");
  360. clearInterval(timer);
  361. timer = setInterval(function () {
  362. n++;
  363. var m = parseInt(n / 60 % 60);
  364. var s = parseInt(n % 60);
  365. $(".thsc").text(toDub(m) + ":" + toDub(s));
  366. }, 1000);
  367. }
  368. if (data.State == '7') {
  369. $(".hwzt").text('播放忙音中'); //左下角状态显示
  370. }
  371. if (data.State == '8') {
  372. $(".hwzt").text('移除IP分机'); //左下角状态显示
  373. }
  374. if (data.State == '9') {
  375. $(".hwzt").text('通话保持中'); //左下角状态显示
  376. }
  377. }
  378. function toDub(i) {
  379. return i < 10 ? "0" + i : "" + i;
  380. }
  381. //外呼
  382. function MakeCallBack() {
  383. $("#top-search li i").removeClass("active");
  384. $(".DropCall").addClass("active");
  385. $(".Meeting").addClass("active");
  386. }
  387. //默认记忆上次是否签入,是否置忙置闲 0表示已签入 空闲,1表示签入置忙,2表示签出
  388. function SetStateCookie(state) {
  389. $.cookie("socket_state", state);
  390. }
  391. //置忙置闲
  392. function SetState(obj) {
  393. if (obj.State == '5') {
  394. $(".SayBusy").removeClass("active");
  395. $(".SayFree").addClass("active");
  396. $(".zxzt").removeClass("bl").addClass("br");
  397. $(".hwzt").text('置忙');
  398. SetStateCookie(1);
  399. }
  400. if (obj.State == '2') {
  401. $(".SayBusy").addClass("active");
  402. $(".SayFree").removeClass("active");
  403. $(".zxzt").removeClass("br").addClass("bl");
  404. $(".hwzt").text('空闲');
  405. SetStateCookie(0);
  406. }
  407. }
  408. function SetLogin(state) {
  409. if (state == 2) { return; }
  410. if (obj.AgentID) {
  411. if($("#RoleCode").val()=='HFZY'){
  412. obj.AgentGroup = "2";
  413. }else{
  414. obj.AgentGroup = "364";
  415. }
  416. obj.Type = "Login";
  417. obj.AgentType = "0";
  418. Send();
  419. }
  420. if (state == 1) {
  421. setTimeout('SayBusy()', 500);
  422. }
  423. }
  424. function SayBusy() {
  425. if (obj.AgentID) {
  426. obj.Type = "SayBusy";
  427. Send();
  428. }
  429. }
  430. //置忙
  431. function SayBusyBack() {
  432. $(".SayBusy").removeClass("active");
  433. $(".SayFree").addClass("active");
  434. }
  435. //置闲
  436. function SayFreeBack() {
  437. $(".SayBusy").addClass("active");
  438. $(".SayFree").removeClass("active");
  439. }
  440. //多方通话
  441. function MeetingBack() {
  442. }
  443. //转移
  444. function TransferBack() {
  445. $("#top-search li i").removeClass("active");
  446. $(".Logout").addClass("active");
  447. $(".SayBusy").addClass("active");
  448. $(".MakeCall").addClass("active");
  449. }
  450. //保持
  451. function HoldBack() {
  452. $(".Hold").removeClass("active");
  453. $(".Retrieve").addClass("active");
  454. }
  455. //接回
  456. function RetrieveBack() {
  457. $(".Hold").addClass("active");
  458. $(".Retrieve").removeClass("active");
  459. }
  460. //监测
  461. function SubScribeBack() {
  462. var obj = $("iframe:visible")
  463. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  464. window.frames[obj.attr("name")].Start();
  465. }
  466. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  467. window.frames[obj.attr("name")].Start();
  468. }
  469. }
  470. //取消监测
  471. function SubScribeCancelBack() {
  472. var obj = $("iframe:visible")
  473. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  474. window.frames[obj.attr("name")].Stop();
  475. }
  476. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  477. window.frames[obj.attr("name")].Stop();
  478. }
  479. }
  480. //班长监测返回状态
  481. //坐席状态
  482. function AgentStateBack(data) {
  483. var obj = $("iframe:visible")
  484. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  485. window.frames[obj.attr("name")].UpdateAgentState(data.AgentID, data.State);
  486. }
  487. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  488. window.frames[obj.attr("name")].UpdateAgentState(data.AgentID, data.State);
  489. }
  490. }
  491. //线路状态
  492. function LineStateBack(data) {
  493. var obj = $("iframe:visible")
  494. if (obj.attr("data-id") == "./TelCall/SeatMonitor.html") {
  495. window.frames[obj.attr("name")].UpdateLineState(data.AgentID, data.State);
  496. }
  497. if (obj.attr("data-id") == "./TelCall/zxKong.html") {
  498. window.frames[obj.attr("name")].UpdateLineState(data.AgentID, data.State);
  499. }
  500. }
  501. //录音返回
  502. function RecordPathBack(data) {
  503. //if ($(".hidTel").val() && $(".hidCallID").val()) {
  504. // $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateLY', { callid: $(".hidCallID").val(),path:data.RecPath, "token": $.cookie("token") }, function (result) {
  505. // result = JSON.parse(result);
  506. // if (result.state.toLowerCase() == "success") {
  507. // }
  508. // })
  509. //}
  510. if ($(".hidTel").val()) {
  511. $.post(huayi.config.callcenter_url + 'CallInScreen/UpdateLY', {
  512. callid: data.CallID,
  513. path: data.RecPath,
  514. "token": $.cookie("token")
  515. }, function (result) {
  516. result = JSON.parse(result);
  517. if (result.state.toLowerCase() == "success") { }
  518. })
  519. }
  520. }
  521. //在线坐席信息
  522. function GetAgentListBack(data) {
  523. console.log(data)
  524. $.ajax({
  525. type: "get",
  526. url: huayi.config.callcenter_url + "SeatMonitoring/GetAgentList",
  527. async: true,
  528. dataType: 'json',
  529. data: {
  530. "token": $.cookie("token")
  531. },
  532. success: function (result) {
  533. var user = result.data;
  534. $(user).each(function (j, m) {
  535. $(data.AgentList).each(function (k, g) {
  536. if (g.AgentID == m.UserCode) {
  537. g.userName = m.UserName;
  538. }
  539. })
  540. })
  541. $(data.AgentList).each(function (k, m) {
  542. var strr = '';
  543. switch (m.State) {
  544. case "0":
  545. strr = "离线";
  546. break; //离线
  547. case "1":
  548. break; //登录中
  549. case "2":
  550. strr = "空闲";
  551. break; //空闲
  552. case "3":
  553. strr = "通话中";
  554. break; //通话中
  555. case "4":
  556. strr = "话后处理中";
  557. break; //话后处理中
  558. case "5":
  559. strr = "忙碌";
  560. break; //小休
  561. case "6":
  562. strr = "振铃";
  563. break; //被请求
  564. case "7":
  565. strr = "注销";
  566. break; //注销
  567. }
  568. var html = '<tr fjh="' + m.AgentExten + '">' +
  569. '<td>' + (m.userName ? '' : m.userName) + '</td>' //姓名
  570. +
  571. '<td>' + m.AgentID + '</td>' //工号
  572. +
  573. '<td>' + m.AgentExten + '</td>'//分机号
  574. +
  575. '<td class=" ' + m.UserCode + 'state">' + strr + '</td>' //状态
  576. +
  577. '</tr>';
  578. $(html).appendTo("#zxTable tbody");
  579. })
  580. }
  581. });
  582. }
  583. //获取当前的日期时间 格式“yyyy-MM-dd HH:mm:ss”
  584. function getNowFormatDate() {
  585. var date = new Date();
  586. var seperator1 = "-";
  587. var seperator2 = ":";
  588. var month = date.getMonth() + 1;
  589. var strDate = date.getDate();
  590. if (month >= 1 && month <= 9) {
  591. month = "0" + month;
  592. }
  593. if (strDate >= 0 && strDate <= 9) {
  594. strDate = "0" + strDate;
  595. }
  596. var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
  597. " " + date.getHours() + seperator2 + date.getMinutes() +
  598. seperator2 + date.getSeconds();
  599. return currentdate;
  600. }
  601. var iswebcloase = 1;
  602. window.onunload = function () {
  603. if (iswebcloase) {
  604. iswebcloase = 0;
  605. if (ws.readyState == ws.OPEN) {
  606. obj.Type = 'Logout';
  607. Send();
  608. ws.onclose();
  609. }
  610. }
  611. }
  612. window.onbeforeunload = function () {
  613. if (iswebcloase) {
  614. iswebcloase = 0;
  615. if (ws.readyState == ws.OPEN) {
  616. obj.Type = 'Logout';
  617. Send();
  618. ws.onclose();
  619. }
  620. }
  621. }