暫無描述

main.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_ = extenNum;
  158. ws_uri_ = huayi.config.ws_uri_; //wss://sip.800100.com.cn:7443 218.29.229.185:8011
  159. contact_uri = huayi.config.contact_uri
  160. testStart();
  161. // setInterval(testStart,600*1000)
  162. }
  163. }
  164. //申请空闲坐席
  165. function videoReqIdeAgent() {
  166. obj.Type = "VideoReqIdeAgent";
  167. obj.AgentID = lasttime.substring(lasttime.length-9);
  168. obj.AgentExten = $("#account").val().toString();
  169. console.log(obj)
  170. Send();
  171. }
  172. //申请空闲坐席,返回结果
  173. function videoreqideagentBack(data) {
  174. xAudio.pause()
  175. callNum = data.AgentExten;
  176. videoCall();
  177. }
  178. //绑定坐席
  179. function VideoReqBindAgent() {
  180. obj.Type = "VideoReqBindAgent";
  181. obj.AgentID = lasttime.substring(lasttime.length-9);
  182. obj.AgentExten = $("#account").val().toString();
  183. console.log(obj)
  184. Send();
  185. }
  186. //绑定坐席 返回结果
  187. function videoreqbindagentBack(){
  188. $(".registStatus").text("登录成功")
  189. $(".loginbtn").hide();
  190. $(".videoCall").show();
  191. }
  192. //注销分机
  193. function videoDesExten() {
  194. obj = {};
  195. obj.Type = "VideoDesExten";
  196. obj.AgentID = RandomTimeData;
  197. obj.AgentExten = extenNum.toString()
  198. Send();
  199. }
  200. //注销分机,返回结果
  201. function videodesextenBack(data) {
  202. console.log(extenNum+"已注销")
  203. $(".registStatus").html("已注销")
  204. }
  205. //手机号与分机号关联
  206. function extenBind() {
  207. var signcode = "api/addrelation+" + formatDateTime()
  208. var telPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  209. if(!telPhone.test($("#telephone").val())) {
  210. $(".registStatus").text("手机号格式不正确");
  211. $(".registStatus").css("color", "red")
  212. } else {
  213. // $.ajax({
  214. // type: "get",
  215. // url: "http://222.143.106.45:65525/Api/AddRelation",
  216. // dataType: "json",
  217. // async: true,
  218. // data: {
  219. // ext: $("#account").val(),
  220. // telephone: $("#telephone").val(),
  221. // signcode: $.md5(signcode).toUpperCase()
  222. // },
  223. // success: function(data) {
  224. // if(data.state == "success") {
  225. // videoReqIdeAgent()
  226. // } else {
  227. // $(".registStatus").text("关联失败");
  228. // $(".registStatus").css("color", "red")
  229. // }
  230. //
  231. // }
  232. // })
  233. videoReqIdeAgent()
  234. }
  235. }
  236. function meetingBack() {
  237. meetingState = true;
  238. $(".leftText").html("");
  239. $(".videoMy").hide();
  240. $(".videoYour").unbind();
  241. videoSize()
  242. }