profile.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * 账号相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2018/1/26
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. autoFlashHeight();
  10. $('[data-toggle="popover"]').popover();
  11. try {
  12. if (user !== '') {
  13. $(".sidebar-title").text(user);
  14. }
  15. $.validator.addMethod("isMobile", function(value, element) {
  16. var length = value.length;
  17. var mobile = /^1[3456789]\d{9}$/;
  18. return this.optional(element) || (length == 11 && mobile.test(value));
  19. }, "请正确填写您的手机号码");
  20. const options = {
  21. rules: '',
  22. errorPlacement: function(error, element) {
  23. $(element).addClass('is-invalid');
  24. $('.input-group-append').after(error);
  25. },
  26. errorClass: "invalid-feedback",
  27. errorElement: "div",
  28. highlight: false,
  29. success: function(element) {
  30. $(element).siblings('input').removeClass('is-invalid');
  31. $(element).remove();
  32. },
  33. };
  34. options.rules = {
  35. auth_mobile: {
  36. required: true,
  37. isMobile: true,
  38. },
  39. };
  40. $("#mobile-form").validate(options);
  41. // 获取验证码
  42. let isPosting = false;
  43. $("#get-code").click(function() {
  44. if (isPosting) {
  45. return false;
  46. }
  47. if(!$("#mobile-form").valid()) {
  48. return false;
  49. }
  50. const mobile = $("input[name='auth_mobile']").val();
  51. const btn = $(this);
  52. $.ajax({
  53. url: '/profile/code?_csrf_j=' + csrf,
  54. type: 'post',
  55. data: { mobile: mobile },
  56. dataTye: 'json',
  57. error: function() {
  58. isPosting = false;
  59. },
  60. beforeSend: function() {
  61. isPosting = true;
  62. },
  63. success: function(response) {
  64. isPosting = false;
  65. if (response.err === 0) {
  66. codeSuccess(btn);
  67. $("input[name='code']").removeAttr('readonly');
  68. $("#bind-btn").removeClass('disabled').removeClass('btn-secondary').addClass('btn-primary');
  69. } else {
  70. toast(response.msg, 'error');
  71. }
  72. }
  73. });
  74. });
  75. // 绑定按钮
  76. $("#bind-btn").click(function() {
  77. const code = $("input[name='code']").val();
  78. const mobile = $("input[name='auth_mobile']").val();
  79. if ($(this).hasClass('disabled')) {
  80. return false;
  81. }
  82. if (!(/^1[3456789]\d{9}$/.test(mobile))) {
  83. toast('请填写正确的手机号码', 'error');
  84. return false;
  85. }
  86. if (code.length < 6) {
  87. // alert('请填写正确的验证码');
  88. toast('请填写正确的验证码', 'error');
  89. return false;
  90. }
  91. $.ajax({
  92. url: '/profile/bind?_csrf_j=' + csrf,
  93. type: 'post',
  94. data: { auth_mobile: mobile, code: code },
  95. dataTye: 'json',
  96. success: function(response) {
  97. if (response.err === 0) {
  98. window.location.href = response.url;
  99. } else {
  100. toast(response.msg, 'error');
  101. }
  102. }
  103. });
  104. });
  105. // 修改手机
  106. $('#change-mobile').click(function () {
  107. $(this).parents('.form-group').hide();
  108. $('#mobile-form').show();
  109. });
  110. $('#hide-bind-btn').click(function () {
  111. $('#mobile-form').hide();
  112. $('#mobile-form').siblings('.form-group').show();
  113. });
  114. $('#del-mobile-btn').click(function () {
  115. postData('/profile/dsk/api', { type: 'unbindMobile' }, function (response) {
  116. toastr.success('解绑认证手机成功');
  117. setTimeout(function () {
  118. location.reload();
  119. }, 1000);
  120. });
  121. })
  122. // 移除签名
  123. $('#delete-sign').click(function () {
  124. postData('/profile/sign/delete', {}, function (result) {
  125. $('#sign-show').html('');
  126. toastr.success('移除成功');
  127. })
  128. });
  129. // 移除签章
  130. $('body').on('click', '.delete-stamp', function () {
  131. let imgSrc = $(this).siblings('img').attr('src');
  132. imgSrc = _.replace(imgSrc, fujianOssPath, '');
  133. const _self = $(this);
  134. postData('/profile/sign/delete', { type: 'stamp', src: imgSrc }, function (result) {
  135. _self.parents('.stamp-show').remove();
  136. toastr.success('移除成功');
  137. })
  138. });
  139. // 签名模式切换
  140. $('.sign-type').on('click', function () {
  141. if (parseInt($(this).val()) === 1) {
  142. $(this).parents('.form-group').siblings('.show-upload').hide();
  143. $(this).parents('.form-group').siblings('.show-qrcode').show();
  144. } else {
  145. $(this).parents('.form-group').siblings('.show-upload').show();
  146. $(this).parents('.form-group').siblings('.show-qrcode').hide();
  147. }
  148. });
  149. // 上传签名
  150. $('#sign-upload').change(function () {
  151. const file = this.files[0];
  152. const filesize = file.size;
  153. if (filesize > 0.5 * 1024 * 1024) {
  154. toastr.error('上传的文件大小不能超过512KB!');
  155. return false;
  156. }
  157. const ext = file.name.toLowerCase().split('.').splice(-1)[0];
  158. const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
  159. if (!imgStr.test(ext)) {
  160. toastr.error('请上传正确的图片格式文件');
  161. return
  162. }
  163. if ($(this).val()) {
  164. const formData = new FormData();
  165. formData.append('file', this.files[0]);
  166. postDataWithFile('/profile/sign/upload', formData, function (result) {
  167. const html = '<img src="/public/upload/sign/'+ result.sign_path +'" width="90">';
  168. $('#sign-show').html(html);
  169. $('#sign-upload').val('');
  170. });
  171. }
  172. })
  173. // 上传签章
  174. $('#stamp-upload').change(function () {
  175. const hadstamp = $('#show-stamp .stamp-show').length;
  176. const files = this.files;
  177. if (hadstamp + files.length > 5) {
  178. toastr.error('最多只能5个签章');
  179. return
  180. }
  181. const formData = new FormData();
  182. for (const file of files) {
  183. if (file === undefined) {
  184. toast('未选择上传文件!', 'error');
  185. return false;
  186. }
  187. const ext = file.name.toLowerCase().split('.').splice(-1)[0];
  188. const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
  189. if (!imgStr.test(ext)) {
  190. toastr.error('请上传正确的图片格式文件');
  191. return
  192. }
  193. const filesize = file.size;
  194. if (filesize > 0.5 * 1024 * 1024) {
  195. toastr.error('上传的文件大小不能超过512KB!');
  196. return false;
  197. }
  198. formData.append('file[]', file);
  199. }
  200. postDataWithFile('/profile/stamp/upload', formData, function (result) {
  201. let html = '';
  202. for (const [index, sp] of result.stamp_path.entries()) {
  203. html += '<div class="position-absolute fixed-top stamp-show" style="left:'+ (50 + index*130) + 'px;top: 280px;width: 100px">\n' +
  204. ' <div class="position-relative">\n' +
  205. ' <a href="javascript:void(0);" title="移除签章" class="position-absolute delete-stamp" style="right: 0;top: 0;color: red;font-size:20px;font-weight: bold;text-decoration:none;">×</a>\n' +
  206. ' <img src="'+ fujianOssPath + sp +'" width="90">\n' +
  207. ' </div>\n' +
  208. ' </div>';
  209. }
  210. $('#show-stamp').html(html);
  211. $('#stamp-upload').val('');
  212. });
  213. })
  214. } catch (error) {
  215. console.log(error);
  216. }
  217. });
  218. /**
  219. * 获取成功后的操作
  220. *
  221. * @param {Object} btn - 点击的按钮
  222. * @return {void}
  223. */
  224. function codeSuccess(btn) {
  225. let counter = 60;
  226. btn.attr('disabled', true);
  227. btn.addClass('disabled').text('重新获取 ' + counter + 'S');
  228. btn.parent().siblings('input').removeAttr('readonly').attr('placeholder', '输入短信中的6位验证码');
  229. const bindBtn = $("#bind-btn");
  230. bindBtn.removeClass('btn-secondary disabled').addClass('btn-primary');
  231. const countDown = setInterval(function() {
  232. const countString = counter - 1 <= 0 ? '' : ' ' + (counter - 1) + 'S';
  233. // 倒数结束后
  234. if (countString === '') {
  235. clearInterval(countDown);
  236. btn.removeAttr('disabled');
  237. btn.removeClass('disabled');
  238. }
  239. const text = '重新获取' + countString;
  240. btn.text(text);
  241. counter -= 1;
  242. }, 1000);
  243. }