login.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. const $ = require('jquery');
  2. require("../css/reset.less")
  3. require("../css/login.less")
  4. require("./modal.js")
  5. const { api } = require('./api.js')
  6. const { post, setCookie, delCookie, getUrlArgObject, toast } = require('../js/utils.js');
  7. const hideLogo = getUrlArgObject("hideLg");
  8. const otherLogo = getUrlArgObject("adLg");
  9. let type;
  10. let is3His=false; //医院标记:湘雅三院1/通用0
  11. $(function () {
  12. //清除需要跳转到登录页的标记
  13. localStorage.removeItem('toLogin');
  14. if (hideLogo || otherLogo) {
  15. $(".copy-right").text("");
  16. }
  17. $(document).keyup(function (event) {
  18. if (event.keyCode == 13) {
  19. sureLogin()
  20. }
  21. });
  22. getHospitalMark()
  23. });
  24. //获取验证码
  25. function getSecurityCode() {
  26. var xhr = new XMLHttpRequest();
  27. var url = api.getCaptcha;//验证码请求地址
  28. xhr.open("GET", url, true);
  29. xhr.responseType = "blob";
  30. xhr.onload = function () {
  31. if (this.status == 200) {
  32. var blob = this.response;
  33. const img = document.createElement("img");
  34. const qrUrl = window.URL.createObjectURL(blob);
  35. img.src = qrUrl;
  36. //释放对象
  37. img.onload = function () {
  38. console.log('释放')
  39. window.URL.revokeObjectURL(qrUrl)
  40. }
  41. const imgDiv = document.getElementById("codeImg");
  42. $(imgDiv).html(img);
  43. }
  44. }
  45. xhr.send();
  46. }
  47. function getHospitalMark() {
  48. post(api.getHospitalMark).then((res) => {
  49. const data = res.data;//{ code: 0, data: 0 }
  50. if (data.code == 0) {
  51. type = parseInt(data.data)
  52. is3His=type===1;
  53. if (is3His) { //湘雅三院登录样式
  54. str = '<div class="bg2">'
  55. str += '<div class="login-info">'
  56. str += '<div class="info-perPub">'
  57. str += ' <p> 用户名:</p>'
  58. str += '<div class="info-iptWrap">'
  59. str += '<input type="text" placeholder="请输入用户名" id="username" value="" autocomplete="off">'
  60. str += '</div></div>'
  61. str += '<div class="info-perPub">'
  62. str += ' <p> 密码:</p>'
  63. str += '<div class="info-iptWrap">'
  64. str += '<input type="text" placeholder="请输入密码" id="password" value="" autocomplete="off">'
  65. str += '</div></div>'
  66. str += '<p class="waring"></p><div class="sureLogin">登录</div></div>'
  67. $('.login').html(str)
  68. } else { //通用版样式
  69. str = '<div class="bg fl"><p class="title">AI病案质控平台</p></div>'
  70. str += '<div class="message fr"><div class="inner"><p class="welcome">欢迎使用!</p>'
  71. str += '<div class="perPub">'
  72. str += ' <p> 用户名:</p>'
  73. str += '<div class="iptWrap">'
  74. str += '<input type="text" placeholder="请输入用户名" id="username" value="" autocomplete="off">'
  75. str += '</div></div><br>'
  76. str += '<div class="perPub">'
  77. str += ' <p> 密码:</p>'
  78. str += '<div class="iptWrap">'
  79. str += '<input type="text" placeholder="请输入密码" id="password" value="" autocomplete="off">'
  80. str += '</div></div><br>'
  81. str += '<div class="perPub">'
  82. str += ' <p> 验证码:</p>'
  83. str += '<div class="iptWrap security-code">'
  84. str += '<input type="text" placeholder="请输入验证码" id="code" value="" autocomplete="off">'
  85. str += '<div id="codeImg" class="code-img"></div>'
  86. str += '<a id="changeCode" >换一张</a>'
  87. str += '</div></div>'
  88. str += '<p class="waring"></p><div class="sureLogin">登录</div></div><div class="copy-right">由杭州朗通信息技术有限公司提供技术支持</div></div>',
  89. $('.login').html(str)
  90. getSecurityCode();
  91. }
  92. $(".sureLogin").click(() => {
  93. sureLogin()
  94. })
  95. //点击换一张事件
  96. $("#codeImg,#changeCode").click(function () {
  97. getSecurityCode();
  98. $(".security-code input").val('')
  99. })
  100. $("#password").focus(function () {
  101. $(this).attr("type", "password")
  102. })
  103. $(".iptWrap:not(.security-code) input").focus(function () {
  104. $(this).parent().addClass('inputFocus')
  105. })
  106. $(".security-code input").focus(function () {
  107. $(this).addClass('inputFocus')
  108. })
  109. $(".iptWrap input").blur(function () {
  110. $(this).parent().removeClass('inputFocus')
  111. $(this).removeClass('inputFocus')
  112. })
  113. $("body").on('input', '#username,#password', function () {
  114. if ($(this).val().trim() != '') {
  115. $(".login .waring").html('')
  116. }
  117. })
  118. }
  119. }).catch(() => {
  120. })
  121. }
  122. function sureLogin() {
  123. localStorage.removeItem('accessToken')
  124. $(".login .waring").html('')
  125. let username = $("#username").val().trim();
  126. let password = $("#password").val().trim();
  127. let captcha = !is3His?$("#code").val().trim():undefined;
  128. if (!username) {
  129. if (is3His) {
  130. toast("请输入用户名!");
  131. } else {
  132. $(".login .waring").css('visibility', 'visible').html('请输入用户名')
  133. }
  134. return
  135. } else if (!password) {
  136. if (is3His) {
  137. toast("请输入密码!");
  138. } else {
  139. $(".login .waring").css('visibility', 'visible').html('请输入密码')
  140. }
  141. return
  142. }
  143. if (!is3His&&!captcha) { //三院不用验证码
  144. $(".login .waring").css('visibility', 'visible').html('请输入验证码')
  145. return
  146. }
  147. $.alerModal({ type: "loading" });
  148. post(api.getJwt, {
  149. username: username,
  150. password: password,
  151. captcha: captcha
  152. }).then((res) => {
  153. const data = res.data;
  154. if (data.code == 0) {
  155. setCookie('isPlacefile', 1)
  156. setCookie('passwordComplexity', data.data.passwordComplexity)
  157. //setCookie('mmps',password);
  158. localStorage.setItem('accessToken', data.data.accessToken)
  159. $(".divModal").remove()
  160. $.alerModal({ "message": "登录成功", type: "tip", time: '1000', win: true });
  161. setTimeout(() => {
  162. cacheCheckDatas(data.data.selRoles);
  163. const otherLogoPm = otherLogo ? '?adLg=1' : '';
  164. const hideLgPm = hideLogo ? '?hideLg=1' : '';
  165. window.location.href = 'index.html' + hideLgPm + otherLogoPm + "?is3His="+is3His;
  166. }, 600);
  167. } else {
  168. if(!is3His){
  169. getSecurityCode();
  170. $(".security-code input").val('')
  171. }//登录失败切换验证码
  172. $(".divModal").remove()
  173. if (type == 1) {
  174. toast(data.msg);
  175. } else {
  176. $(".login .waring").css('visibility', 'visible').html(data.msg)
  177. }
  178. }
  179. }).catch(() => {
  180. $(".divModal").remove()
  181. })
  182. }
  183. //核查权限数据保存
  184. function cacheCheckDatas(data) {
  185. delCookie("checkAuth");
  186. const arr = data && data.map((it) => {
  187. return it.id;
  188. }) || [];
  189. setCookie("checkAuth", arr.join(","));
  190. }