profile.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $.validator.addMethod("isMobile", function(value, element) {
  14. var length = value.length;
  15. var mobile = /^1[3456789]\d{9}$/;
  16. return this.optional(element) || (length == 11 && mobile.test(value));
  17. }, "请正确填写您的手机号码");
  18. const options = {
  19. rules: '',
  20. errorPlacement: function(error, element) {
  21. $(element).addClass('is-invalid');
  22. $('.input-group-append').after(error);
  23. },
  24. errorClass: "invalid-feedback",
  25. errorElement: "div",
  26. highlight: false,
  27. success: function(element) {
  28. $(element).siblings('input').removeClass('is-invalid');
  29. $(element).remove();
  30. },
  31. };
  32. options.rules = {
  33. auth_mobile: {
  34. required: true,
  35. isMobile: true,
  36. },
  37. };
  38. $("#mobile-form").validate(options);
  39. // 获取验证码
  40. let isPosting = false;
  41. $("#get-code").click(function() {
  42. if (isPosting) {
  43. return false;
  44. }
  45. if(!$("#mobile-form").valid()) {
  46. return false;
  47. }
  48. const mobile = $("input[name='auth_mobile']").val();
  49. const btn = $(this);
  50. $.ajax({
  51. url: '/profile/code?_csrf=' + csrf,
  52. type: 'post',
  53. data: { mobile: mobile },
  54. dataTye: 'json',
  55. error: function() {
  56. isPosting = false;
  57. },
  58. beforeSend: function() {
  59. isPosting = true;
  60. },
  61. success: function(response) {
  62. isPosting = false;
  63. if (response.err === 0) {
  64. codeSuccess(btn);
  65. $("input[name='code']").removeAttr('readonly');
  66. $("#bind-btn").removeClass('disabled').removeClass('btn-secondary').addClass('btn-primary');
  67. } else {
  68. toast(response.msg, 'error');
  69. }
  70. }
  71. });
  72. });
  73. // 绑定按钮
  74. $("#bind-btn").click(function() {
  75. const code = $("input[name='code']").val();
  76. const mobile = $("input[name='auth_mobile']").val();
  77. if (!(/^1[3456789]\d{9}$/.test(mobile))) {
  78. toast('请填写正确的手机号码', 'error');
  79. return false;
  80. }
  81. if (code.length < 6) {
  82. // alert('请填写正确的验证码');
  83. toast('请填写正确的验证码', 'error');
  84. return false;
  85. }
  86. });
  87. $('#change-mobile').click(function () {
  88. $(this).parents('.form-group').hide();
  89. $('#mobile-form').show();
  90. });
  91. } catch (error) {
  92. console.log(error);
  93. }
  94. });
  95. /**
  96. * 获取成功后的操作
  97. *
  98. * @param {Object} btn - 点击的按钮
  99. * @return {void}
  100. */
  101. function codeSuccess(btn) {
  102. let counter = 60;
  103. btn.addClass('disabled').text('重新获取 ' + counter + 'S');
  104. btn.parent().siblings('input').removeAttr('readonly').attr('placeholder', '输入短信中的5位验证码');
  105. const bindBtn = $("#bind-btn");
  106. bindBtn.removeClass('btn-secondary disabled').addClass('btn-primary');
  107. const countDown = setInterval(function() {
  108. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + 'S';
  109. // 倒数结束后
  110. if (countString === '') {
  111. clearInterval(countDown);
  112. btn.removeClass('disabled');
  113. }
  114. const text = '重新获取' + countString;
  115. btn.text(text);
  116. counter -= 1;
  117. }, 1000);
  118. }