profile.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 ($(this).hasClass('disabled')) {
  78. return false;
  79. }
  80. if (!(/^1[3456789]\d{9}$/.test(mobile))) {
  81. toast('请填写正确的手机号码', 'error');
  82. return false;
  83. }
  84. if (code.length < 6) {
  85. // alert('请填写正确的验证码');
  86. toast('请填写正确的验证码', 'error');
  87. return false;
  88. }
  89. $.ajax({
  90. url: '/profile/bind?_csrf=' + csrf,
  91. type: 'post',
  92. data: { auth_mobile: mobile, code: code },
  93. dataTye: 'json',
  94. success: function(response) {
  95. if (response.err === 0) {
  96. window.location.href = response.data.url;
  97. } else {
  98. toast(response.msg, 'error');
  99. }
  100. }
  101. });
  102. });
  103. // 修改手机
  104. $('#change-mobile').click(function () {
  105. $(this).parents('.form-group').hide();
  106. $('#mobile-form').show();
  107. });
  108. // 移除签名
  109. $('#delete-sign').click(function () {
  110. postData('/profile/sign/delete', {}, function (result) {
  111. $('#sign-show').remove();
  112. toast('移除成功', 'success');
  113. })
  114. })
  115. } catch (error) {
  116. console.log(error);
  117. }
  118. });
  119. /**
  120. * 获取成功后的操作
  121. *
  122. * @param {Object} btn - 点击的按钮
  123. * @return {void}
  124. */
  125. function codeSuccess(btn) {
  126. let counter = 60;
  127. btn.addClass('disabled').text('重新获取 ' + counter + 'S');
  128. btn.parent().siblings('input').removeAttr('readonly').attr('placeholder', '输入短信中的5位验证码');
  129. const bindBtn = $("#bind-btn");
  130. bindBtn.removeClass('btn-secondary disabled').addClass('btn-primary');
  131. const countDown = setInterval(function() {
  132. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + 'S';
  133. // 倒数结束后
  134. if (countString === '') {
  135. clearInterval(countDown);
  136. btn.removeClass('disabled');
  137. }
  138. const text = '重新获取' + countString;
  139. btn.text(text);
  140. counter -= 1;
  141. }, 1000);
  142. }