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 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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;
  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. self.isMoving = false;
  99. $.ajax({
  100. url: '/verifySlideImage',
  101. type: 'post',
  102. dataType: 'json',
  103. data: {
  104. slide_id: self.imgID,
  105. x: parseInt(self.dom.find('.slide-image-small').position().left)
  106. },
  107. success: function (res) {
  108. if (res && res.Code === 0) {
  109. self.verifySucess = true;
  110. self.canSendCode = true;
  111. self.dom.find('.slide-bar').addClass('sucess');
  112. self.dom.find('.slide-trigger').addClass('sucess');
  113. self.dom.find('.slide-trigger .icon').hide();
  114. self.dom.find('.slide-trigger .icon.check').show();
  115. self.dom.find('.slide-image-big').slideUp();
  116. self.dom.find('.verify-code-send-btn').removeClass('__disabled');
  117. } else {
  118. self.dom.find('.slide-bar').addClass('error');
  119. self.dom.find('.slide-trigger').addClass('error');
  120. self.dom.find('.slide-trigger .icon').hide();
  121. self.dom.find('.slide-trigger .icon.close').show();
  122. setTimeout(function () {
  123. self.refreshImages();
  124. }, 300);
  125. }
  126. },
  127. error: function (err) {
  128. self.dom.find('.slide-bar').addClass('error');
  129. self.dom.find('.slide-trigger').addClass('error');
  130. setTimeout(function () {
  131. self.refreshImages();
  132. }, 300);
  133. }
  134. });
  135. }
  136. this.dom.find('.slide-trigger').on('mousedown', function (e) {
  137. if (self.verifySucess) return;
  138. clientX = e.clientX;
  139. oLeft = $(this).position().left;
  140. $(document).on('mousemove', mouseMove);
  141. $(document).on('mouseup', mouseUp);
  142. });
  143. this.dom.find('.verify-code-send-btn').on('click', function () {
  144. if (!self.canSendCode) return;
  145. if (self.countDownEnd) {
  146. self.refreshImages();
  147. return;
  148. }
  149. var phoneNumber = self.dom.find('.phone-num-c input').val();
  150. if (!/^1[3578]\d{9}$/.test(phoneNumber)) {
  151. self.dom.find('.phone-num-c').addClass('error');
  152. return;
  153. } else {
  154. self.dom.find('.phone-num-c').removeClass('error');
  155. var useType = self.dom.closest('div.use-type').attr('usetype') || 0;
  156. $.ajax({
  157. url: '/sendVerifyCode',
  158. type: 'post',
  159. dataType: 'json',
  160. data: {
  161. phone_number: phoneNumber,
  162. mode: useType, // 0注册,1登录 ,2修改手机号,3找回密码
  163. slide_id: self.imgID,
  164. },
  165. success: function (res) {
  166. if (res && res.Code === 0) {
  167. self.countDown();
  168. } else {
  169. if ($('.ui.negative.message').length) {
  170. $('.ui.negative.message').eq(0).show().find('p').text(res.Message);
  171. } else {
  172. $('body').toast({
  173. message: res.Message,
  174. showProgress: 'bottom',
  175. showIcon: 'warning circle',
  176. class: 'warning',
  177. position: 'top right',
  178. });
  179. }
  180. self.refreshImages();
  181. }
  182. },
  183. error: function (err) {
  184. console.log(err);
  185. }
  186. });
  187. }
  188. });
  189. this.dom.find('.modify-phone-number a').on('click', function () {
  190. self.dom.find('.phone-area-c select').attr('disabled', false);
  191. self.dom.find('.phone-num-c input').attr('disabled', false);
  192. self.dom.find('.slide-bar-wrap').css('display', 'flex');
  193. self.dom.find('.verify-code-c').css('display', 'flex');
  194. self.dom.find('.modify-phone-number').hide();
  195. });
  196. };
  197. PhoneVerifyCode.prototype.refreshImages = function () {
  198. this.isMoving = false;
  199. this.verifySucess = false;
  200. this.canSendCode = false;
  201. this.countDownEnd = false;
  202. this.imgID = '';
  203. this.dom.find('.slide-bar').removeClass('sucess error').css('width', '30px');
  204. this.dom.find('.slide-trigger').removeClass('sucess error').css('left', '0px');
  205. this.dom.find('.slide-trigger .icon').hide();
  206. this.dom.find('.slide-trigger .icon.arrow').show();
  207. this.dom.find('.slide-txt').show();
  208. this.dom.find('.slide-image-small').css('left', '0');
  209. this.dom.find('.verify-code-send-btn').addClass('__disabled');
  210. var self = this;
  211. $.ajax({
  212. url: '/slideImage',
  213. type: 'get',
  214. success: function (res) {
  215. if (res && res.Code === 0) {
  216. self.imgID = res.Message;
  217. self.dom.find('.slide-image-big').css('background', `url("/slideimage/${res.Message}.png")`);
  218. self.dom.find('.slide-image-small').css('background', `url("/slideimage/${res.Message}screenshot.png")`);
  219. }
  220. },
  221. error: function (err) {
  222. console.log(err);
  223. }
  224. });
  225. };
  226. PhoneVerifyCode.prototype.countDown = function () {
  227. var self = this;
  228. var sendBtnEl = this.dom.find('.verify-code-send-btn');
  229. var count = this.countDownNumber;
  230. sendBtnEl.addClass('__disabled').text(count + (self.options && self.options.Lang && self.options.Lang.second_resend ? self.options.Lang.second_resend : 'S后重发'));
  231. this.canSendCode = false;
  232. this.countDownEnd = false;
  233. var timer = setInterval(function () {
  234. count--;
  235. sendBtnEl.addClass('__disabled').text(count + (self.options && self.options.Lang && self.options.Lang.second_resend ? self.options.Lang.second_resend : 'S后重发'));
  236. if (count <= 0) {
  237. sendBtnEl.removeClass('__disabled').text(+ (self.options && self.options.Lang && self.options.Lang.get_verification_code ? self.options.Lang.get_verification_code : '获取验证码'));
  238. clearInterval(timer);
  239. self.canSendCode = true;
  240. self.countDownEnd = true;
  241. self.refreshImages();
  242. }
  243. }, 1000);
  244. };
  245. window.PhoneVerifyCode = PhoneVerifyCode;
  246. })();