Нет описания

main.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. }
  98. if(data.Type === "VideoReqBindAgent"){
  99. $(".registStatus").text("绑定失败")
  100. }
  101. }
  102. }
  103. };
  104. //连接关闭的回调方法
  105. ws.onclose = function(evt) {
  106. //nginx proxy_read_timeout 6000s; 设置的默认关闭时间
  107. reconnect();
  108. };
  109. //连接发生错误的回调方法
  110. ws.onerror = function(evt) {
  111. reconnect();
  112. };
  113. }
  114. //重连
  115. function reconnect() {
  116. if(lockReconnect) return;
  117. lockReconnect = true;
  118. //没连接上会一直重连,设置延迟避免请求过多
  119. setTimeout(function() {
  120. console.log(new Date() + " " + "重连中……");
  121. createWebSocket("wss://" + huayi.config.socket_ip + ":" + huayi.config.socket_port);
  122. }, 2000);
  123. }
  124. //发送
  125. function Send() {
  126. // console.log("ws.readyState==" + ws.readyState)
  127. // console.log("ws.OPEN==" + ws.OPEN)
  128. readyStateBoole = false
  129. if(ws.readyState != ws.OPEN) {
  130. readyStateBoole = true
  131. alert("页面停留时间过长,导致分机号被占用,请刷新页面后,重新使用")
  132. //reconnect();
  133. }
  134. if(ws.readyState == ws.OPEN) {
  135. console.log(new Date() + " send " + JSON.stringify(obj));
  136. ws.send(JSON.stringify(obj));
  137. }
  138. }
  139. //申请注册的分机
  140. function videoReqExten() {
  141. obj.Type = "VideoReqExten";
  142. obj.AgentID = lasttime.substring(lasttime.length-9);
  143. obj.AgentExten = "111";
  144. RandomTimeData = obj.AgentID;
  145. Send();
  146. }
  147. //请求注册分机,返回结果
  148. function videoreqextenBack(data) {
  149. console.log(data.AgentExten+"已注册")
  150. //当内容是我是督办专员时,是在市民页面
  151. if($(".superviseBtn").text() == "我是督办专员") {
  152. extenNum = data.AgentExten
  153. $("#account").val(extenNum)
  154. sip_uri_ = "sip:" + extenNum + "@" + huayi.config.socket_ip;
  155. sip_password_ = huayi.config.sip_password_;
  156. ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
  157. contact_uri = huayi.config.contact_uri
  158. testStart();
  159. // setInterval(testStart,600*1000)
  160. }
  161. }
  162. //申请空闲坐席
  163. function videoReqIdeAgent() {
  164. obj.Type = "VideoReqIdeAgent";
  165. obj.AgentID = lasttime.substring(lasttime.length-9);
  166. obj.AgentExten = $("#account").val().toString();
  167. console.log(obj)
  168. Send();
  169. }
  170. //申请空闲坐席,返回结果
  171. function videoreqideagentBack(data) {
  172. xAudio.pause()
  173. callNum = data.AgentExten;
  174. videoCall();
  175. }
  176. //绑定坐席
  177. function VideoReqBindAgent() {
  178. obj.Type = "VideoReqBindAgent";
  179. obj.AgentID = lasttime.substring(lasttime.length-9);
  180. obj.AgentExten = $("#account").val().toString();
  181. console.log(obj)
  182. Send();
  183. }
  184. //绑定坐席 返回结果
  185. function videoreqbindagentBack(){
  186. $(".registStatus").text("登录成功")
  187. $(".loginbtn").hide();
  188. $(".videoCall").show();
  189. }
  190. //注销分机
  191. function videoDesExten() {
  192. obj = {};
  193. obj.Type = "VideoDesExten";
  194. obj.AgentID = RandomTimeData;
  195. obj.AgentExten = extenNum.toString()
  196. Send();
  197. }
  198. //注销分机,返回结果
  199. function videodesextenBack(data) {
  200. console.log(extenNum+"已注销")
  201. $(".registStatus").html("已注销")
  202. }
  203. //手机号与分机号关联
  204. function extenBind() {
  205. var signcode = "index/addrelation+" + formatDateTime()
  206. var telPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  207. if(!telPhone.test($("#telephone").val())) {
  208. $(".registStatus").text("手机号格式不正确");
  209. $(".registStatus").css("color", "red")
  210. } else {
  211. $.ajax({
  212. type: "get",
  213. url: huayi.config.callcenter_url +"index/addrelation",
  214. dataType: "json",
  215. async: true,
  216. data: {
  217. ext: $("#account").val(),
  218. telephone: $("#telephone").val(),
  219. signcode: $.md5(signcode).toUpperCase(),
  220. terminal: navigator.userAgent
  221. },
  222. success: function(data) {
  223. if(data.state == "success") {
  224. videoReqIdeAgent()
  225. } else {
  226. $(".registStatus").text("关联失败"); //toLowerCase toUpperCase
  227. $(".registStatus").css("color", "red")
  228. }
  229. }
  230. })
  231. // videoReqIdeAgent()
  232. }
  233. }
  234. function meetingBack() {
  235. meetingState = true;
  236. $(".leftText").html("");
  237. $(".videoMy").hide();
  238. $(".videoYour").unbind();
  239. videoSize()
  240. }