profile.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // 修改手机
  88. $('#change-mobile').click(function () {
  89. $(this).parents('.form-group').hide();
  90. $('#mobile-form').show();
  91. });
  92. // 移除签名
  93. $('#delete-sign').click(function () {
  94. postData('/profile/sign/delete', {}, function (result) {
  95. $('#sign-show').remove();
  96. toast('移除成功', 'success');
  97. })
  98. })
  99. } catch (error) {
  100. console.log(error);
  101. }
  102. });
  103. /**
  104. * 获取成功后的操作
  105. *
  106. * @param {Object} btn - 点击的按钮
  107. * @return {void}
  108. */
  109. function codeSuccess(btn) {
  110. let counter = 60;
  111. btn.addClass('disabled').text('重新获取 ' + counter + 'S');
  112. btn.parent().siblings('input').removeAttr('readonly').attr('placeholder', '输入短信中的5位验证码');
  113. const bindBtn = $("#bind-btn");
  114. bindBtn.removeClass('btn-secondary disabled').addClass('btn-primary');
  115. const countDown = setInterval(function() {
  116. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + 'S';
  117. // 倒数结束后
  118. if (countString === '') {
  119. clearInterval(countDown);
  120. btn.removeClass('disabled');
  121. }
  122. const text = '重新获取' + countString;
  123. btn.text(text);
  124. counter -= 1;
  125. }, 1000);
  126. }