login.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. $(function () {
  2. var isSSo = location.search.length > 0;//是否单点登录
  3. if (isSSo) {
  4. $('.container').hide();
  5. login();
  6. }
  7. var hasClick = false; //是否已经点击过登录按钮
  8. // 兼容ie的placeholder
  9. $('input, textarea').placeholder();
  10. // 获取验证码
  11. function getVerificationCodeImg(){
  12. $('#verificationCodeImg').attr('src', baseUrl + "auth/getCaptcha?" + Date.now());
  13. }
  14. getVerificationCodeImg();
  15. // 点击刷新验证码
  16. $("#verificationCodeImg").on('click', getVerificationCodeImg);
  17. //aes加密
  18. function encryptByEnAESLogin(data) {
  19. data = CryptoJS.enc.Utf8.parse(data);
  20. let Key = CryptoJS.enc.Utf8.parse('Aes2Util666AQWER');
  21. let tmpAES = CryptoJS.AES.encrypt(data, Key, {
  22. mode: CryptoJS.mode.ECB,
  23. padding: CryptoJS.pad.Pkcs7,
  24. });
  25. return tmpAES.toString();
  26. }
  27. // 登录点击
  28. $('#login').on('click', login);
  29. // 登录方法
  30. function login() {
  31. if (hasClick) return;
  32. hasClick = true;
  33. var name = $('#name').val()
  34. var pwd = $('#pwd').val()
  35. var captcha = $('#captcha').val()
  36. var data = {
  37. username: name,
  38. password: pwd,
  39. captcha: captcha
  40. }
  41. // 单点登录 start
  42. var ssoStr = '';
  43. var ssoJson;
  44. if (isSSo) {
  45. ssoStr = location.search.replace('?', '').split('&')[0].split('=')[1];
  46. ssoStr = Base64.decode(decodeURIComponent(ssoStr));
  47. ssoJson = JSON.parse(ssoStr);
  48. }else if(!name || !name.trim()){
  49. alert("请输入用户名");
  50. hasClick = false;
  51. return;
  52. }else if(!pwd || !pwd.trim()){
  53. alert("请输入密码");
  54. hasClick = false;
  55. return;
  56. }else if(!captcha || !captcha.trim()){
  57. alert("请输入验证码");
  58. hasClick = false;
  59. return;
  60. }
  61. var postData = isSSo ? { username: ssoJson.a, password: ssoJson.r, t: false } : data;
  62. postData = {
  63. k: encryptByEnAESLogin(JSON.stringify(postData))
  64. };
  65. // 单点登录 end
  66. $.ajax({
  67. type: "POST",
  68. contentType: "application/json;charset=UTF-8",
  69. url: baseUrl + "auth/reqlogin",
  70. data: JSON.stringify(postData),
  71. success: function (res) {
  72. if (res.state == 200) {
  73. if (isSSo) {
  74. sessionStorage.setItem('isSSo', '1');//单点登录
  75. } else {
  76. sessionStorage.setItem('isSSo', '0');//正常登录
  77. }
  78. sessionStorage.setItem('loginUser', JSON.stringify(res.data.requester));
  79. //判断版本类别
  80. $.ajax({
  81. type: "POST",
  82. contentType: "application/json;charset=UTF-8",
  83. url: baseUrl + "sysinfo/data/fetchDataList/systemConfiguration",
  84. data: JSON.stringify({ "idx": 0, "sum": 1000 }),
  85. success: function (result) {
  86. if (result.status == 200) {
  87. var list = result.list;
  88. var repairMain = list.find((v) => v.keyconfig == "repairMain"); //报修主体
  89. var incidentWithConsumable = list.find(
  90. (v) => v.keyconfig == "incidentWithConsumable"
  91. ); //是否绑定耗材
  92. var wxIncidentWithCmdb = list.find(
  93. (v) => v.keyconfig == "wxIncidentWithCmdb"
  94. ); //是否绑定资产
  95. var reqHasCategory = list.find(
  96. (v) => v.keyconfig == "reqHasCategory"
  97. ); //获取是否自动建单
  98. sessionStorage.setItem(
  99. "repair_main",
  100. JSON.stringify(repairMain)
  101. );
  102. sessionStorage.setItem(
  103. "incidentWithConsumable",
  104. incidentWithConsumable.valueconfig
  105. );
  106. sessionStorage.setItem(
  107. "wxIncidentWithCmdb",
  108. wxIncidentWithCmdb.valueconfig
  109. );
  110. sessionStorage.setItem("reqHasCategory", JSON.stringify(reqHasCategory));
  111. window.location.href = 'index.html';
  112. }
  113. hasClick = false;
  114. },
  115. //请求失败,包含具体的错误信息
  116. error: function (e) {
  117. console.log(e.status);
  118. console.log(e.responseText);
  119. }
  120. });
  121. } else if(res.state == 403){
  122. alert('验证码错误');
  123. hasClick = false;
  124. getVerificationCodeImg();
  125. } else {
  126. alert('用户名或密码错误,请重试!');
  127. hasClick = false;
  128. }
  129. },
  130. //请求失败,包含具体的错误信息
  131. error: function (e) {
  132. console.log(e.status);
  133. console.log(e.responseText);
  134. }
  135. });
  136. }
  137. // enter登录
  138. $(document).bind('keypress', function (e) {
  139. var keyCode;
  140. if (window.event) {
  141. keyCode = e.keyCode
  142. } else if (e.which) {
  143. keycode = e.which;
  144. }
  145. if (e.keyCode != 13) {
  146. return;
  147. }
  148. $("#login").trigger("click");
  149. return false;
  150. });
  151. })