Nenhuma Descrição

main.js 14KB

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