login.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. if (window !== top) {
  2. top.location.href = location.href;
  3. }
  4. layui.use(['request', 'form', 'button', 'yaml'], function() {
  5. var $ = layui.jquery,
  6. request = layui.request,
  7. form = layui.form,
  8. button = layui.button,
  9. yaml = layui.yaml;
  10. var URL = yaml.load("config/cool.config.yml").admin.url;
  11. var PKI;
  12. //刷新验证码
  13. window.captcha = function() {
  14. $.get(request.getAdminServerUrl() +'/guest/captcha?' + Math.random(), function (res) {
  15. if (res.success) {
  16. $('[name="key"]').val(res.data.key);
  17. $("#captchaImage").attr('src', res.data.image);
  18. }
  19. });
  20. };
  21. //init
  22. layer.ready(function() {
  23. layui.data('server',
  24. null);
  25. layui.data('server', {
  26. key: 'url',
  27. value: URL
  28. });
  29. layui.data('server', {
  30. key: 'login',
  31. value: location.href
  32. });
  33. //初始化 RSA_PUBLIC_KEY
  34. $.get(request.getAdminServerUrl() +'/guest/pki', function (res) {
  35. if (res.success) {
  36. PKI = res.data;
  37. }
  38. });
  39. //初始化化验证码
  40. window.captcha();
  41. });
  42. //表单提交
  43. form.on('submit(login)', function (data) {
  44. var btn = button.load({elem: '.login'});
  45. data.field.captcha = $('#captcha').val();
  46. data.field.password = encrypt(data.field.password);
  47. request.http({
  48. url: '/login',
  49. data: data.field,
  50. type: "post",
  51. dataType: 'json',
  52. sendToken: false,
  53. success: function (res) {
  54. btn.stop(function() {
  55. if (res.success) {
  56. layui.data('server', {
  57. key: 'accessToken',
  58. value: {name:'accessToken', value: res.data.accessToken}
  59. });
  60. layui.data('server', {
  61. key: 'permissions',
  62. value: res.data.permissions
  63. });
  64. button.load({elem: '.login', time: 200,
  65. done: function () {
  66. location.href = "index.html"
  67. }
  68. });
  69. } else {
  70. captcha();
  71. $(".sign").hide();
  72. $(".error").show().html(res.msg);
  73. }
  74. })
  75. }
  76. });
  77. return false;
  78. });
  79. //清空input
  80. window.empty = function(name) {
  81. $('[name="' + name + '"]').val('');
  82. };
  83. //RSA加密
  84. window.encrypt = function (data) {
  85. if (PKI) {
  86. var r = new JSEncrypt();
  87. r.setPublicKey('-----BEGIN PUBLIC KEY-----' + PKI + '-----END PUBLIC KEY-----');
  88. return r.encrypt(JSON.stringify(data));
  89. } else {
  90. layer.msg("password rsa error");
  91. return false;
  92. }
  93. };
  94. });