login.js 6.4 KB

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