You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

phoneverify.js 10 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ; (function () {
  2. function PhoneVerifyCode(dom, options) {
  3. if (!dom) return;
  4. this.countDownNumber = 120;
  5. this.options = options;
  6. this.init(dom);
  7. }
  8. PhoneVerifyCode.prototype.init = function (dom) {
  9. if (!dom) return;
  10. this.dom = $(dom);
  11. this.isMoving = false;
  12. this.verifySucess = false;
  13. this.canSendCode = false;
  14. this.countDownEnd = false;
  15. this.imgID = '';
  16. this.eventInit();
  17. this.refreshImages();
  18. var wrap = this.dom.closest('div.use-type');
  19. var readonly = wrap.attr('readonly');
  20. if (readonly) {
  21. this.dom.find('.phone-area-c select').attr('disabled', true);
  22. this.dom.find('.phone-num-c input').attr('disabled', true);
  23. this.dom.find('.slide-bar-wrap').hide();
  24. this.dom.find('.verify-code-c').hide();
  25. this.dom.find('.modify-phone-number').show();
  26. }
  27. var oldPhoneNum = wrap.attr('ophonenumber');
  28. if (oldPhoneNum) {
  29. this.dom.find('input.phoneNumber').val(oldPhoneNum);
  30. }
  31. var showlabel = wrap.attr('showlabel');
  32. if (showlabel) {
  33. this.dom.find('._label-c ').show();
  34. } else {
  35. this.dom.find('._label-c ').hide();
  36. }
  37. var showNewpwd = wrap.attr('shownewpwd');
  38. if (showNewpwd) {
  39. this.dom.find('.new-pass-word-wrap').show();
  40. } else {
  41. this.dom.find('.new-pass-word-wrap').remove();
  42. }
  43. var autofocus = wrap.attr('autofocus');
  44. if (autofocus) {
  45. this.dom.find('input.phoneNumber').focus();
  46. }
  47. var verifyCodeNoRequired = wrap.attr('verifycodenorequired');
  48. if (verifyCodeNoRequired) {
  49. this.dom.find('input.verifyCode').removeAttr('required');
  50. }
  51. };
  52. PhoneVerifyCode.prototype.eventInit = function () {
  53. if (!this.dom) return;
  54. var self = this;
  55. var clientX = 0, oLeft = 0, imgHideTimer = null;
  56. this.dom.find('.slide-bar-bg').on('mouseenter', function (e) {
  57. if (self.verifySucess) return;
  58. imgHideTimer && clearTimeout(imgHideTimer);
  59. self.dom.find('.slide-image-big').slideDown();
  60. });
  61. this.dom.find('.slide-bar-bg').on('mouseleave', function (e) {
  62. imgHideTimer && clearTimeout(imgHideTimer);
  63. imgHideTimer = setTimeout(function () {
  64. self.dom.find('.slide-image-big').slideUp();
  65. }, 200);
  66. });
  67. this.dom.find('.slide-image-big').on('mouseenter', function (e) {
  68. imgHideTimer && clearTimeout(imgHideTimer);
  69. });
  70. this.dom.find('.slide-image-big').on('mouseleave', function (e) {
  71. imgHideTimer && clearTimeout(imgHideTimer);
  72. imgHideTimer = setTimeout(function () {
  73. self.dom.find('.slide-image-big').slideUp();
  74. }, 200);
  75. });
  76. function mouseMove(e) {
  77. var _clientX = e.clientX !== undefined ? e.clientX : e.targetTouches[0].clientX;
  78. var offset = _clientX - clientX;
  79. var triggerEl = self.dom.find('.slide-trigger');
  80. var triggerWidth = triggerEl.width();
  81. var parentWidth = triggerEl.parent().width();
  82. var maxLeft = parentWidth - triggerWidth;
  83. var left = oLeft + offset;
  84. if (oLeft + offset < 0) left = 0;
  85. if (oLeft + offset > maxLeft) left = maxLeft;
  86. triggerEl.css('left', left + 'px');
  87. self.dom.find('.slide-bar').css('width', left + triggerWidth);
  88. var imageBigWidth = self.dom.find('.slide-image-big').width();
  89. var imageSmallWidth = self.dom.find('.slide-image-small').width();
  90. self.dom.find('.slide-image-small').css('left', left / maxLeft * (imageBigWidth - imageSmallWidth) + 'px');
  91. self.isMoving = true;
  92. self.dom.find('.slide-txt').hide();
  93. imgHideTimer && clearTimeout(imgHideTimer);
  94. }
  95. function mouseUp(e) {
  96. $(document).off('mousemove', mouseMove);
  97. $(document).off('mouseup', mouseUp);
  98. $(document).off('touchmove', mouseMove);
  99. $(document).off('touchend', mouseUp);
  100. self.isMoving = false;
  101. $.ajax({
  102. url: '/verifySlideImage',
  103. type: 'post',
  104. dataType: 'json',
  105. data: {
  106. slide_id: self.imgID,
  107. x: parseInt(self.dom.find('.slide-image-small').position().left / self.dom.find('.slide-image-big').attr('scale'))
  108. },
  109. success: function (res) {
  110. if (res && res.Code === 0) {
  111. self.verifySucess = true;
  112. self.canSendCode = true;
  113. self.dom.find('.slide-bar').addClass('sucess');
  114. self.dom.find('.slide-trigger').addClass('sucess');
  115. self.dom.find('.slide-trigger .icon').hide();
  116. self.dom.find('.slide-trigger .icon.check').show();
  117. self.dom.find('.slide-image-big').slideUp();
  118. self.dom.find('.verify-code-send-btn').removeClass('__disabled');
  119. } else {
  120. self.dom.find('.slide-bar').addClass('error');
  121. self.dom.find('.slide-trigger').addClass('error');
  122. self.dom.find('.slide-trigger .icon').hide();
  123. self.dom.find('.slide-trigger .icon.close').show();
  124. setTimeout(function () {
  125. self.refreshImages();
  126. }, 300);
  127. }
  128. },
  129. error: function (err) {
  130. self.dom.find('.slide-bar').addClass('error');
  131. self.dom.find('.slide-trigger').addClass('error');
  132. setTimeout(function () {
  133. self.refreshImages();
  134. }, 300);
  135. }
  136. });
  137. }
  138. function mouseDown(e) {
  139. if (self.verifySucess) return;
  140. clientX = e.clientX !== undefined ? e.clientX : e.targetTouches[0].clientX;
  141. oLeft = $(this).position().left;
  142. $(document).on('mousemove', mouseMove);
  143. $(document).on('mouseup', mouseUp);
  144. $(document).on('touchmove', mouseMove);
  145. $(document).on('touchend', mouseUp);
  146. }
  147. this.dom.find('.slide-trigger').on('mousedown', mouseDown);
  148. this.dom.find('.slide-trigger').on('touchstart', mouseDown);
  149. this.dom.find('.verify-code-send-btn').on('click', function () {
  150. if (!self.canSendCode) return;
  151. if (self.countDownEnd) {
  152. self.refreshImages();
  153. return;
  154. }
  155. var phoneNumber = self.dom.find('.phone-num-c input').val();
  156. if (!/^1[3-9]\d{9}$/.test(phoneNumber)) {
  157. self.dom.find('.phone-num-c').addClass('error');
  158. return;
  159. } else {
  160. self.dom.find('.phone-num-c').removeClass('error');
  161. var useType = self.dom.closest('div.use-type').attr('usetype') || 0;
  162. $.ajax({
  163. url: '/sendVerifyCode',
  164. type: 'post',
  165. dataType: 'json',
  166. data: {
  167. phone_number: phoneNumber,
  168. mode: useType, // 0注册,1登录 ,2修改手机号,3找回密码
  169. slide_id: self.imgID,
  170. },
  171. success: function (res) {
  172. if (res && res.Code === 0) {
  173. self.countDown();
  174. } else {
  175. if ($('.ui.negative.message').length) {
  176. $('.ui.negative.message').eq(0).show().find('p').text(res.Message);
  177. } else {
  178. $('body').toast({
  179. message: res.Message,
  180. showProgress: 'bottom',
  181. showIcon: 'warning circle',
  182. class: 'warning',
  183. position: 'top right',
  184. });
  185. }
  186. self.refreshImages();
  187. }
  188. },
  189. error: function (err) {
  190. console.log(err);
  191. }
  192. });
  193. }
  194. });
  195. this.dom.find('.modify-phone-number a').on('click', function () {
  196. self.dom.find('.phone-area-c select').attr('disabled', false);
  197. self.dom.find('.phone-num-c input').attr('disabled', false);
  198. self.dom.find('.slide-bar-wrap').css('display', 'flex');
  199. self.dom.find('.verify-code-c').css('display', 'flex');
  200. self.dom.find('.modify-phone-number').hide();
  201. self.refreshImages();
  202. });
  203. };
  204. PhoneVerifyCode.prototype.refreshImages = function () {
  205. this.isMoving = false;
  206. this.verifySucess = false;
  207. this.canSendCode = false;
  208. this.countDownEnd = false;
  209. this.imgID = '';
  210. this.dom.find('.slide-bar').removeClass('sucess error').css('width', '30px');
  211. this.dom.find('.slide-trigger').removeClass('sucess error').css('left', '0px');
  212. var scale = this.dom.find('.slide-bar-bg').width() / 391;
  213. this.dom.find('.slide-image-big').css('transform', `scale(${scale})`).attr('scale', scale);
  214. this.dom.find('.slide-trigger .icon').hide();
  215. this.dom.find('.slide-trigger .icon.arrow').show();
  216. this.dom.find('.slide-txt').show();
  217. this.dom.find('.slide-image-small').css('left', '0');
  218. this.dom.find('.verify-code-send-btn').addClass('__disabled');
  219. var self = this;
  220. $.ajax({
  221. url: '/slideImage',
  222. type: 'get',
  223. success: function (res) {
  224. if (res && res.Code === 0) {
  225. self.imgID = res.Message;
  226. self.dom.find('.slide-image-big').css('background', `url("/slideimage/${res.Message}.png")`);
  227. self.dom.find('.slide-image-small').css('background', `url("/slideimage/${res.Message}screenshot.png")`);
  228. }
  229. },
  230. error: function (err) {
  231. console.log(err);
  232. }
  233. });
  234. };
  235. PhoneVerifyCode.prototype.countDown = function () {
  236. var self = this;
  237. var sendBtnEl = this.dom.find('.verify-code-send-btn');
  238. var count = this.countDownNumber;
  239. sendBtnEl.addClass('__disabled').text(count + (self.options && self.options.Lang && self.options.Lang.second_resend ? self.options.Lang.second_resend : 'S后重发'));
  240. this.canSendCode = false;
  241. this.countDownEnd = false;
  242. var timer = setInterval(function () {
  243. count--;
  244. sendBtnEl.addClass('__disabled').text(count + (self.options && self.options.Lang && self.options.Lang.second_resend ? self.options.Lang.second_resend : 'S后重发'));
  245. if (count <= 0) {
  246. sendBtnEl.removeClass('__disabled').text((self.options && self.options.Lang && self.options.Lang.get_verification_code ? self.options.Lang.get_verification_code : '获取验证码'));
  247. clearInterval(timer);
  248. self.canSendCode = true;
  249. self.countDownEnd = true;
  250. self.refreshImages();
  251. }
  252. }, 1000);
  253. };
  254. window.PhoneVerifyCode = PhoneVerifyCode;
  255. })();