profile.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 = {
  28. auth_mobile: {
  29. mobile: true,
  30. required: true,
  31. },
  32. };
  33. $("#mobile-form").validate(options);
  34. // 获取验证码
  35. let isPosting = false;
  36. $("#get-code").click(function() {
  37. if (isPosting) {
  38. return false;
  39. }
  40. if(!$("#mobile-form").valid()) {
  41. return false;
  42. }
  43. const mobile = $("input[name='auth_mobile']").val();
  44. const btn = $(this);
  45. $.ajax({
  46. url: '/profile/code?_csrf=' + csrf,
  47. type: 'post',
  48. data: { mobile: mobile },
  49. dataTye: 'json',
  50. error: function() {
  51. isPosting = false;
  52. },
  53. beforeSend: function() {
  54. isPosting = true;
  55. },
  56. success: function(response) {
  57. isPosting = false;
  58. if (response.err === 0) {
  59. codeSuccess(btn);
  60. } else {
  61. alert(response.msg);
  62. }
  63. }
  64. });
  65. });
  66. // 绑定按钮
  67. $("#bind-btn").click(function() {
  68. const code = $("input[name='code']").val();
  69. if (code.length < 6) {
  70. alert('请填写正确的验证码');
  71. return false;
  72. }
  73. });
  74. } catch (error) {
  75. console.log(error);
  76. }
  77. });
  78. /**
  79. * 获取成功后的操作
  80. *
  81. * @param {Object} btn - 点击的按钮
  82. * @return {void}
  83. */
  84. function codeSuccess(btn) {
  85. let counter = 60;
  86. btn.addClass('disabled').text('重新获取 ' + counter + 'S');
  87. btn.parent().siblings('input').removeAttr('readonly').attr('placeholder', '输入短信中的5位验证码');
  88. const bindBtn = $("#bind-btn");
  89. bindBtn.removeClass('btn-secondary disabled').addClass('btn-primary');
  90. const countDown = setInterval(function() {
  91. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + 'S';
  92. // 倒数结束后
  93. if (countString === '') {
  94. clearInterval(countDown);
  95. btn.removeClass('disabled');
  96. }
  97. const text = '重新获取' + countString;
  98. btn.text(text);
  99. counter -= 1;
  100. }, 1000);
  101. }