profile.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * 账号相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2018/1/26
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. try {
  10. if (user !== '') {
  11. $(".title-bar h2").text(user);
  12. }
  13. const options = {
  14. rules: '',
  15. errorPlacement: function(error, element) {
  16. $(element).addClass('is-invalid');
  17. $(element).after(error);
  18. },
  19. errorClass: "invalid-feedback",
  20. errorElement: "div",
  21. highlight: false,
  22. success: function(element) {
  23. $(element).prev('input').removeClass('is-invalid');
  24. $(element).remove();
  25. },
  26. };
  27. options.rules = baseRule;
  28. $("#base-form").validate(options);
  29. // 基础信息提交
  30. $("#base-submit").click(function() {
  31. $("#base-form").valid();
  32. });
  33. options.rules = passwordRule;
  34. // 修改密码验证
  35. $("#password-form").validate(options);
  36. $("#modify-password").click(function() {
  37. $("#password-form").valid();
  38. });
  39. options.rules = {
  40. auth_mobile: {
  41. mobile: true,
  42. required: true,
  43. },
  44. };
  45. $("#mobile-form").validate(options);
  46. // 获取验证码
  47. let isPosting = false;
  48. $("#get-code").click(function() {
  49. if (isPosting) {
  50. return false;
  51. }
  52. if(!$("#mobile-form").valid()) {
  53. return false;
  54. }
  55. const mobile = $("input[name='auth_mobile']").val();
  56. console.log(mobile);
  57. const btn = $(this);
  58. $.ajax({
  59. url: '/profile/code?_csrf=' + csrf,
  60. type: 'post',
  61. data: { mobile: mobile },
  62. dataTye: 'json',
  63. error: function() {
  64. isPosting = false;
  65. },
  66. beforeSend: function() {
  67. isPosting = true;
  68. },
  69. success: function(response) {
  70. isPosting = false;
  71. if (response.err === 0) {
  72. codeSuccess(btn);
  73. } else {
  74. alert(response.msg);
  75. }
  76. }
  77. });
  78. });
  79. // 绑定按钮
  80. $("#bind-btn").click(function() {
  81. const code = $("input[name='code']").val();
  82. if (code.length < 6) {
  83. alert('请填写正确的验证码');
  84. return false;
  85. }
  86. });
  87. } catch (error) {
  88. console.log(error);
  89. }
  90. });
  91. /**
  92. * 获取成功后的操作
  93. *
  94. * @param {Object} btn - 点击的按钮
  95. * @return {void}
  96. */
  97. function codeSuccess(btn) {
  98. let counter = 60;
  99. btn.addClass('disabled').text('重新获取 ' + counter + 'S');
  100. btn.parent().siblings('input').removeAttr('readonly').attr('placeholder', '输入短信中的5位验证码');
  101. const bindBtn = $("#bind-btn");
  102. bindBtn.removeClass('btn-secondary disabled').addClass('btn-primary');
  103. const countDown = setInterval(function() {
  104. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + 'S';
  105. // 倒数结束后
  106. if (countString === '') {
  107. clearInterval(countDown);
  108. btn.removeClass('disabled');
  109. }
  110. const text = '重新获取' + countString;
  111. btn.text(text);
  112. counter -= 1;
  113. }, 1000);
  114. }