profile.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * 账号相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2018/1/26
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. autoFlashHeight();
  10. try {
  11. if (user !== '') {
  12. $(".title-bar h2").text(user);
  13. }
  14. $.validator.addMethod("isMobile", function(value, element) {
  15. var length = value.length;
  16. var mobile = /^1[3456789]\d{9}$/;
  17. return this.optional(element) || (length == 11 && mobile.test(value));
  18. }, "请正确填写您的手机号码");
  19. const options = {
  20. rules: '',
  21. errorPlacement: function(error, element) {
  22. $(element).addClass('is-invalid');
  23. $('.input-group-append').after(error);
  24. },
  25. errorClass: "invalid-feedback",
  26. errorElement: "div",
  27. highlight: false,
  28. success: function(element) {
  29. $(element).siblings('input').removeClass('is-invalid');
  30. $(element).remove();
  31. },
  32. };
  33. options.rules = {
  34. auth_mobile: {
  35. required: true,
  36. isMobile: true,
  37. },
  38. };
  39. $("#mobile-form").validate(options);
  40. // 获取验证码
  41. let isPosting = false;
  42. $("#get-code").click(function() {
  43. if (isPosting) {
  44. return false;
  45. }
  46. if(!$("#mobile-form").valid()) {
  47. return false;
  48. }
  49. const mobile = $("input[name='auth_mobile']").val();
  50. const btn = $(this);
  51. $.ajax({
  52. url: '/profile/code?_csrf=' + csrf,
  53. type: 'post',
  54. data: { mobile: mobile },
  55. dataTye: 'json',
  56. error: function() {
  57. isPosting = false;
  58. },
  59. beforeSend: function() {
  60. isPosting = true;
  61. },
  62. success: function(response) {
  63. isPosting = false;
  64. if (response.err === 0) {
  65. codeSuccess(btn);
  66. $("input[name='code']").removeAttr('readonly');
  67. $("#bind-btn").removeClass('disabled').removeClass('btn-secondary').addClass('btn-primary');
  68. } else {
  69. toast(response.msg, 'error');
  70. }
  71. }
  72. });
  73. });
  74. // 绑定按钮
  75. $("#bind-btn").click(function() {
  76. const code = $("input[name='code']").val();
  77. const mobile = $("input[name='auth_mobile']").val();
  78. if ($(this).hasClass('disabled')) {
  79. return false;
  80. }
  81. if (!(/^1[3456789]\d{9}$/.test(mobile))) {
  82. toast('请填写正确的手机号码', 'error');
  83. return false;
  84. }
  85. if (code.length < 6) {
  86. // alert('请填写正确的验证码');
  87. toast('请填写正确的验证码', 'error');
  88. return false;
  89. }
  90. $.ajax({
  91. url: '/profile/bind?_csrf=' + csrf,
  92. type: 'post',
  93. data: { auth_mobile: mobile, code: code },
  94. dataTye: 'json',
  95. success: function(response) {
  96. if (response.err === 0) {
  97. window.location.href = response.url;
  98. } else {
  99. toast(response.msg, 'error');
  100. }
  101. }
  102. });
  103. });
  104. // 修改手机
  105. $('#change-mobile').click(function () {
  106. $(this).parents('.form-group').hide();
  107. $('#mobile-form').show();
  108. });
  109. // 移除签名
  110. $('#delete-sign').click(function () {
  111. postData('/profile/sign/delete', {}, function (result) {
  112. $('#sign-show').remove();
  113. toast('移除成功', 'success');
  114. })
  115. })
  116. } catch (error) {
  117. console.log(error);
  118. }
  119. });
  120. /**
  121. * 获取成功后的操作
  122. *
  123. * @param {Object} btn - 点击的按钮
  124. * @return {void}
  125. */
  126. function codeSuccess(btn) {
  127. let counter = 60;
  128. btn.addClass('disabled').text('重新获取 ' + counter + 'S');
  129. btn.parent().siblings('input').removeAttr('readonly').attr('placeholder', '输入短信中的5位验证码');
  130. const bindBtn = $("#bind-btn");
  131. bindBtn.removeClass('btn-secondary disabled').addClass('btn-primary');
  132. const countDown = setInterval(function() {
  133. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + 'S';
  134. // 倒数结束后
  135. if (countString === '') {
  136. clearInterval(countDown);
  137. btn.removeClass('disabled');
  138. }
  139. const text = '重新获取' + countString;
  140. btn.text(text);
  141. counter -= 1;
  142. }, 1000);
  143. }