casLogin.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta
  6. name="viewport"
  7. content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
  8. />
  9. <script
  10. type="text/javascript"
  11. src="http://192.168.1.129:10022/static/jquery/dist/jquery.min.js"
  12. ></script>
  13. <script
  14. type="text/javascript"
  15. src="http://192.168.1.129:10022/static/vue/dist/vue.global.prod.js"
  16. ></script>
  17. <style scoped>
  18. .jumbotron {
  19. margin-top: 30px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="app1" class="container">
  25. <div class="jumbotron">
  26. <h1>登录中</h1>
  27. <p>{{message}}</p>
  28. <p>
  29. <button @click="logout()">注销</button>
  30. </p>
  31. </div>
  32. </div>
  33. <script>
  34. Vue.createApp({
  35. data: function () {
  36. return {
  37. urlRoot: "",
  38. ticket: undefined,
  39. message: "正在检查您的权限,请稍等...",
  40. casUrl: undefined, // cas验证url
  41. authSettingNo: undefined, // 标识
  42. serviceUrl: undefined, // 传入后台验证地址
  43. casLogoutUrl: undefined, // cas退出登录url
  44. casTicketUrl: undefined, // cas Ticket检验Url
  45. redirectUrl: undefined, // 登录成功后自动跳转的地址
  46. };
  47. },
  48. mounted: function () {
  49. this.urlRoot = this.getRootPath() + "/";
  50. this.authSettingNo = this.getQueryString("authSettingNo");
  51. this.existsCAS();
  52. },
  53. methods: {
  54. getQueryString: function (name) {
  55. const url = window.location.href;
  56. const params = new URLSearchParams(url.split("?")[1]);
  57. return params.get(name);
  58. },
  59. // 根据认证跳转系统对应页面
  60. getTicket: function () {
  61. const _self = this;
  62. const redirect_uri = window.location.href; // http:192.168.1.129:10022/casLogin.html?authSettingNo=001
  63. _self.ticket = _self.getQueryString("ticket");
  64. if (_self.ticket) {
  65. // 1.判断是否包含重定向地址
  66. // 2.判断后台验证登录是否成功
  67. const newUrl = localStorage.getItem("#redirect_uri");
  68. if (newUrl.indexOf("redirectUrl") != -1) {
  69. // 存在redirectUrl地址,调用登录接口,跳转redirectUrl地址
  70. _self.azureCasLogin(newUrl);
  71. } else {
  72. // 没有redirectUrl地址,跳转到x端(PC、Propass、App)选择界面,调用登录,authSettingNo、ticket、newUrl必须传递过去
  73. const loginUrl = `${_self.urlRoot}#/casLogin?authSettingNo=${_self.authSettingNo}&ticket=${_self.ticket}&serviceUrl=${newUrl}`;
  74. window.location.href = loginUrl;
  75. }
  76. } else {
  77. localStorage.setItem("#redirect_uri", redirect_uri);
  78. const casBoard = `${_self.casUrl}?service=${redirect_uri}`;
  79. window.location.href = casBoard;
  80. }
  81. },
  82. // 注销
  83. logout: function () {
  84. let _self = this;
  85. window.location.href = _self.urlRoot;
  86. },
  87. // 单点登录
  88. azureCasLogin: function (newUrl) {
  89. let _self = this;
  90. const params = {
  91. ticket: _self.ticket,
  92. redirect_uri: newUrl,
  93. authSettingNo: _self.authSettingNo,
  94. languageId: "zh-CN",
  95. };
  96. $.ajax({
  97. url: _self.urlRoot + "api/CasLogin/login",
  98. type: "post",
  99. async: true,
  100. data: params,
  101. success: function (loginInfoData) {
  102. if (loginInfoData.errorCode == 0) {
  103. _self.setLoginInfo(loginInfoData.data, newUrl);
  104. } else {
  105. _self.message =
  106. "不好意思你的账号不在系统中,请点击【注销】按钮,登录其他账号";
  107. console.error(loginInfoData.errorMessage);
  108. }
  109. },
  110. error: function (XMLHttpRequest, textStatus, errorThrown) {
  111. console.log("服务器出错啦。");
  112. },
  113. });
  114. },
  115. // 获取cas参数值
  116. existsCAS: function () {
  117. const _self = this;
  118. $.ajax({
  119. url:
  120. _self.urlRoot +
  121. "api/CasLogin/casServiceProviderCheck?authSettingNo=" +
  122. _self.authSettingNo,
  123. type: "get",
  124. contentType: "application/json",
  125. dataType: "json",
  126. success: function (response) {
  127. if (response.errorCode === 0) {
  128. _self.casUrl = response.data.casUrl;
  129. _self.authSettingNo = response.data.no;
  130. _self.casLogoutUrl = response.data.casLogoutUrl;
  131. _self.getTicket();
  132. } else {
  133. _self.message = response.errorMessage;
  134. console.log("错误", response.errorMessage);
  135. }
  136. },
  137. error: function (XMLHttpRequest, textStatus, errorThrown) {
  138. _self.message = "服务器出错啦。";
  139. console.log(XMLHttpRequest, textStatus, errorThrown);
  140. },
  141. });
  142. },
  143. /**
  144. * 获取主机地址
  145. */
  146. getRootPath: function () {
  147. var protocol = window.location.protocol;
  148. var host = window.location.host;
  149. var localhostPath = protocol + "//" + host;
  150. return localhostPath;
  151. },
  152. setLoginInfo: function (loginInfo, newUrl) {
  153. var _self = this;
  154. _self.clearLocalStorage();
  155. _self.setLocalStorage(loginInfo);
  156. const str = "redirectUrl=";
  157. const endStart = newUrl.indexOf("redirectUrl");
  158. const startIndex = endStart + str.length;
  159. const endIndex = newUrl.length - 1;
  160. const redirectUrl = newUrl.substring(startIndex, endIndex);
  161. window.location.href = redirectUrl;
  162. },
  163. // 设置localStorage
  164. setLocalStorage: function (loginInfo) {
  165. localStorage.setItem("#LoginInfo", JSON.stringify(loginInfo));
  166. localStorage.setItem("#token", loginInfo.token);
  167. localStorage.setItem("#accountId", loginInfo.accountId);
  168. },
  169. clearLocalStorage: function () {
  170. // 清理localStorage时需要保留的参数列表
  171. var reserveParams = [
  172. "hostPageBaseURL",
  173. "workShopId",
  174. "resourceInstanceId",
  175. "resourceInstanceName",
  176. "apsBaseUrl",
  177. "cameraBaseURL",
  178. "#rememberPassword",
  179. "#userName",
  180. "#password",
  181. "#languageSelected",
  182. ];
  183. //存放的信息
  184. var reserveParamValues = [];
  185. //获取参数信息
  186. var len = reserveParams.length;
  187. for (let i = 0; i < len; i++) {
  188. var reserveParam = reserveParams[i];
  189. var reserveParamValue = "";
  190. if (localStorage.getItem(reserveParam) != undefined) {
  191. reserveParamValue = localStorage.getItem(reserveParam);
  192. }
  193. reserveParamValues.push(reserveParamValue);
  194. }
  195. //清理localStorage
  196. window.localStorage.clear();
  197. //还原参数信息
  198. for (let i = 0; i < len; i++) {
  199. localStorage.setItem(reserveParams[i], reserveParamValues[i]);
  200. }
  201. },
  202. },
  203. }).mount("#app1");
  204. </script>
  205. </body>
  206. </html>