No Description

main.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. var ws, RandomTimeData, callNum, extenNum, readyStateBoole, reconnecTion, recon,
  2. lockReconnect = false, timesVideoReq = 0,
  3. n = 0,
  4. loginOr = false,
  5. meetingState = null,
  6. obj = {},
  7. lasttime = new Date().getTime().toString();
  8. var xAudio = document.getElementById("music_bg");
  9. var formatDateTime = function(date) {
  10. var date = new Date();
  11. var y = date.getFullYear().toString();
  12. var m = date.getMonth() + 1;
  13. m = m < 10 ? ('0' + m) : m;
  14. var d = date.getDate();
  15. d = d < 10 ? ('0' + d) : d;
  16. console.log(y + m + d)
  17. return y + m + d
  18. // +' '+h+':'+minute+':'+second;
  19. };
  20. //创建scoket连接
  21. createWebSocket()
  22. function createWebSocket() {
  23. try {
  24. Connect();
  25. } catch(e) {
  26. reconnect();
  27. }
  28. }
  29. //连接
  30. //Connect();
  31. function Connect() {
  32. ws = new WebSocket(huayi.config.socket_url);
  33. ws.onopen = function() {
  34. lockReconnect = false
  35. console.log(new Date() + " " + "建立连接");
  36. console.log(huayi.config.socket_url)
  37. // extenNum = "1020"
  38. // sip_uri_ = "sip:"+extenNum+"@"+huayi.config.socket_ip;
  39. // sip_password_ = "123456"
  40. // ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
  41. // testStart();
  42. //建立连接时获取分机号
  43. // lasttime = new Date().getTime().toString();
  44. //申请注册分机号
  45. if($(".superviseBtn").text() == "我是督办专员") {
  46. videoReqExten()
  47. }
  48. };
  49. //接收到消息的回调方法
  50. ws.onmessage = function(evt) {
  51. //拿到任何消息都说明当前连接是正常的
  52. var myDate = new Date();
  53. console.log(myDate + " receive " + evt.data);
  54. var data = JSON.parse(evt.data)[0];
  55. if(data) {
  56. var rlt = data.Result;
  57. if(rlt == true) {
  58. var type = data.Type;
  59. switch(type.toLowerCase()) {
  60. //请求注册分机返回结果
  61. case "videoreqexten":
  62. videoreqextenBack(data);
  63. break;
  64. //申请空闲坐席返回结果
  65. case "videoreqideagent":
  66. videoreqideagentBack(data);
  67. break;
  68. case "videoreqbindagent":
  69. videoreqbindagentBack(data);
  70. break;
  71. case "meeting":
  72. meetingBack();
  73. break;
  74. //注销分机返回结果
  75. case "videodesexten":
  76. videodesextenBack(data);
  77. break;
  78. }
  79. } else {
  80. if(data.Type === "VideoReqIdeAgent") {
  81. if (timesVideoReq < 8) {
  82. $(".registStatus").text("坐席忙,请稍等")
  83. $(".registStatus").css("color", "#FF8C00")
  84. xAudio.play()
  85. }else{
  86. $(".registStatus").text("当前无人在线,请稍后再拨")
  87. $(".registStatus").css("color", "red")
  88. timesVideoReq = 0
  89. xAudio.pause()
  90. return
  91. }
  92. console.log("data.Result返回为false")
  93. setTimeout(function(){
  94. timesVideoReq+=1
  95. videoReqIdeAgent()
  96. },5000)
  97. } else if( data.Type.toLowerCase() ==='videoreqexten'){
  98. videoreqextenBack(data);
  99. }
  100. if(data.Type === "VideoReqBindAgent"){
  101. $(".registStatus").text("绑定失败")
  102. }
  103. }
  104. }
  105. };
  106. //连接关闭的回调方法
  107. ws.onclose = function(evt) {
  108. //nginx proxy_read_timeout 6000s; 设置的默认关闭时间
  109. reconnect();
  110. };
  111. //连接发生错误的回调方法
  112. ws.onerror = function(evt) {
  113. reconnect();
  114. };
  115. }
  116. //重连
  117. function reconnect() {
  118. if(lockReconnect) return;
  119. lockReconnect = true;
  120. //没连接上会一直重连,设置延迟避免请求过多
  121. setTimeout(function() {
  122. console.log(new Date() + " " + "重连中……");
  123. createWebSocket("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  124. }, 2000);
  125. }
  126. //发送
  127. function Send() {
  128. // console.log("ws.readyState==" + ws.readyState)
  129. // console.log("ws.OPEN==" + ws.OPEN)
  130. readyStateBoole = false
  131. if(ws.readyState != ws.OPEN) {
  132. readyStateBoole = true
  133. alert("页面停留时间过长,导致分机号被占用,请刷新页面后,重新使用")
  134. //reconnect();
  135. }
  136. if(ws.readyState == ws.OPEN) {
  137. console.log(new Date() + " send " + JSON.stringify(obj));
  138. ws.send(JSON.stringify(obj));
  139. }
  140. }
  141. //申请注册的分机
  142. function videoReqExten() {
  143. obj.Type = "VideoReqExten";
  144. obj.AgentID = lasttime.substring(lasttime.length-9);
  145. obj.AgentExten = "111";
  146. RandomTimeData = obj.AgentID;
  147. Send();
  148. }
  149. //请求注册分机,返回结果
  150. function videoreqextenBack(data) {
  151. console.log(data.AgentExten+"已注册")
  152. //当内容是我是督办专员时,是在市民页面
  153. if($(".superviseBtn").text() == "我是督办专员") {
  154. extenNum = data.AgentExten
  155. $("#account").val(extenNum)
  156. sip_uri_ = "sip:" + extenNum + "@" + huayi.config.socket_ip;
  157. // sip_password_ = huayi.config.sip_password_;
  158. sip_password_ = extenNum;
  159. ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
  160. contact_uri = huayi.config.contact_uri
  161. testStart();
  162. // setInterval(testStart,600*1000)
  163. }
  164. }
  165. //申请空闲坐席
  166. function videoReqIdeAgent() {
  167. obj.Type = "VideoReqIdeAgent";
  168. obj.AgentID = lasttime.substring(lasttime.length-9);
  169. obj.AgentExten = $("#account").val().toString();
  170. console.log(obj)
  171. Send();
  172. }
  173. //申请空闲坐席,返回结果
  174. function videoreqideagentBack(data) {
  175. xAudio.pause()
  176. callNum = data.AgentExten;
  177. videoCall();
  178. }
  179. //绑定坐席
  180. function VideoReqBindAgent() {
  181. obj.Type = "VideoReqBindAgent";
  182. obj.AgentID = lasttime.substring(lasttime.length-9);
  183. obj.AgentExten = $("#account").val().toString();
  184. console.log(obj)
  185. Send();
  186. }
  187. //绑定坐席 返回结果
  188. function videoreqbindagentBack(){
  189. $(".registStatus").text("登录成功")
  190. $(".loginbtn").hide();
  191. $(".videoCall").show();
  192. }
  193. //注销分机
  194. function videoDesExten() {
  195. obj = {};
  196. obj.Type = "VideoDesExten";
  197. obj.AgentID = RandomTimeData;
  198. obj.AgentExten = extenNum.toString()
  199. Send();
  200. }
  201. //注销分机,返回结果
  202. function videodesextenBack(data) {
  203. console.log(extenNum+"已注销")
  204. $(".registStatus").html("已注销")
  205. }
  206. //手机号与分机号关联
  207. function extenBind() {
  208. var signcode = "index/addrelation+" + formatDateTime()
  209. var telPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  210. if(!telPhone.test($("#telephone").val())) {
  211. $(".registStatus").text("手机号格式不正确");
  212. $(".registStatus").css("color", "red")
  213. } else {
  214. $.ajax({
  215. type: "get",
  216. url: huayi.config.callcenter_url +"index/addrelation",
  217. dataType: "json",
  218. async: true,
  219. data: {
  220. ext: $("#account").val(),
  221. telephone: $("#telephone").val(),
  222. signcode: $.md5(signcode).toUpperCase(),
  223. terminal: navigator.userAgent
  224. },
  225. success: function(data) {
  226. if(data.state == "success") {
  227. videoReqIdeAgent()
  228. } else {
  229. $(".registStatus").text("关联失败"); //toLowerCase toUpperCase
  230. $(".registStatus").css("color", "red")
  231. }
  232. }
  233. })
  234. // videoReqIdeAgent()
  235. }
  236. }
  237. function meetingBack() {
  238. meetingState = true;
  239. $(".leftText").html("");
  240. $(".videoMy").hide();
  241. $(".videoYour").unbind();
  242. videoSize()
  243. }