socket.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <html>
  2. <head>
  3. <title></title>
  4. <script src="AFrontEnd/js/jquery/jquery-1.8.3.min.js"></script>
  5. <script src="apiurljs.js"></script>
  6. <script type="text/javascript">
  7. var challenge = "";
  8. function test() {
  9. $.ajax({
  10. type: "post",
  11. url: apiurljs.login + "g2app/certification/GeneratorChallenge",
  12. dataType: "json",
  13. ContentType: 'application/x-www-form-urlencoded',
  14. success: function (res) {
  15. if (res.success) {
  16. challenge = res.data;
  17. WebSocketTest()
  18. }
  19. },
  20. })
  21. }
  22. function WebSocketTest() {
  23. if ("WebSocket" in window) {
  24. //alert("WebSocket is supported by your Browser!");
  25. var ws = new WebSocket("ws://127.0.0.1:30318/");
  26. ws.onopen = function () {
  27. var str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><getsignandtokenreq version=\"1\"><challenge>" + challenge + "</challenge></getsignandtokenreq>";
  28. ws.send(str);
  29. };
  30. ws.onmessage = function (evt) {
  31. $.ajax({
  32. type: "post",
  33. url: apiurljs.login + "g2app/certification/VerifyIdentityTicket",
  34. dataType: "json",
  35. data: {
  36. challenge: challenge,
  37. tokeninfo: $(evt.data).find("tokeninfo").text()
  38. },
  39. ContentType: 'application/x-www-form-urlencoded',
  40. success: function (res) {
  41. if (res.data.SUCCESS) {
  42. $.ajax({
  43. //url: 'js/login/userData.txt',
  44. url: apiurljs.login + "connect/token",
  45. type: 'post',
  46. dataType: 'json',
  47. ContentType: 'application/x-www-form-urlencoded',
  48. data: {
  49. client_id: "androidclient",
  50. grant_type: "password",
  51. scope: "openid profile email RefineAPI",
  52. username: res.data.UINPUTNAME,
  53. password: res.data.PASSWORD
  54. },
  55. success: function (res) {
  56. console.log(res);
  57. var postAccessToken = res.access_token;
  58. localStorage.setItem("GlWorkPlatform-AccessToken", postAccessToken)
  59. var setArray = res.access_token.split(".");
  60. var base64 = new Base64(); //声明base解码和编码类
  61. let getBase = base64.decode(setArray[1]);
  62. getBase = getBase.substring(0, getBase.lastIndexOf("}") + 1);
  63. getBaseSet = JSON.parse(getBase);
  64. let get_userid = getBaseSet.sub;
  65. console.log(get_userid);
  66. /////存入userid、及英文名
  67. $.cookie("GlWorkPlatform-userid", get_userid, { path: '/', expires: 1 });
  68. $.cookie("GlWorkPlatform-username", get_userName, { path: '/', expires: 1 });
  69. $.cookie("CrossPage-ctime", sp.getLocalDate1(), { path: '/', expires: 1 });
  70. //对密码进行加密
  71. if (get_ischeck) {
  72. //对密码进行加密
  73. var encode_user = base64.encode(get_userName);
  74. var encode_pwd = base64.encode(get_password);
  75. localStorage.setItem("GlWorkPlatform-user", encode_user);
  76. localStorage.setItem("GlWorkPlatform-pwd", encode_pwd);
  77. localStorage.setItem("GlWorkPlatform-ischecked", get_ischeck);
  78. } else {
  79. localStorage.removeItem("GlWorkPlatform-user");
  80. localStorage.removeItem("GlWorkPlatform-pwd");
  81. localStorage.removeItem("GlWorkPlatform-ischecked");
  82. }
  83. $.ajax({
  84. url: apiurljs.login + "api/User/UserInfo",
  85. type: 'post',
  86. dataType: 'json',
  87. ContentType: 'application/json',
  88. headers: { Authorization: "Bearer " + postAccessToken }, ////请求头headers
  89. success: function (res2) {
  90. ///中文名
  91. $.cookie("GlWorkPlatform-chineseName", res2.data.ushowname, { path: '/', expires: 1 });
  92. //延时进行跳转
  93. timeout = setTimeout(function () {
  94. window.location.href = "index.html";
  95. sessionStorage.setItem("GlWorkPlatform-LoginFrom", "GlWorkPlatform-LoginHtml");
  96. }, 500);
  97. }, error: function (XMLHttpRequest, textStatus, errorThrown) {
  98. sp.dialog(JSON.stringify(errorThrown));
  99. }
  100. });
  101. }, error: function (XMLHttpRequest, textStatus, errorThrown) {
  102. sp.dialog("登录失败,用户名号或密码输入错误");
  103. }
  104. });
  105. }
  106. },
  107. })
  108. };
  109. ws.onclose = function () {
  110. console.log("Connection is closed...");
  111. };
  112. }
  113. else {
  114. var d = document.createElement('div');
  115. d.textContent == "" ? d.textContent = '\n浏览器不支持websocket' : d.innerText = '\n浏览器不支持websocket';
  116. document.getElementById("showResult").appendChild(d);
  117. }
  118. }
  119. </script>
  120. </head>
  121. <body>
  122. <button onclick="test(); WebSocketTest()">测试</button>
  123. <div id="result">
  124. <p>
  125. 测试结果:
  126. <div id="showResult">
  127. </div>
  128. </div>
  129. </body>
  130. </html>