frontend.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. define(['fast', 'template'], function (Fast, Template) {
  2. var Frontend = {
  3. api: Fast.api,
  4. init: function () {
  5. var si = {};
  6. //发送验证码
  7. $(document).on("click", ".btn-captcha", function (e) {
  8. var type = $(this).data("type") ? $(this).data("type") : 'mobile';
  9. var element = $(this).data("input-id") ? $("#" + $(this).data("input-id")) : $("input[name='" + type + "']", $(this).closest("form"));
  10. var text = type === 'email' ? '邮箱' : '手机号码';
  11. if (element.val() === "") {
  12. Layer.msg(text + "不能为空!");
  13. element.focus();
  14. return false;
  15. } else if (type === 'mobile' && !element.val().match(/^1[3-9]\d{9}$/)) {
  16. Layer.msg("请输入正确的" + text + "!");
  17. element.focus();
  18. return false;
  19. } else if (type === 'email' && !element.val().match(/^[\w\+\-]+(\.[\w\+\-]+)*@[a-z\d\-]+(\.[a-z\d\-]+)*\.([a-z]{2,4})$/)) {
  20. Layer.msg("请输入正确的" + text + "!");
  21. element.focus();
  22. return false;
  23. }
  24. var that = this;
  25. element.isValid(function (v) {
  26. if (v) {
  27. $(that).addClass("disabled", true).text("发送中...");
  28. var data = {event: $(that).data("event")};
  29. data[type] = element.val();
  30. Frontend.api.ajax({url: $(that).data("url"), data: data}, function () {
  31. clearInterval(si[type]);
  32. var seconds = 60;
  33. si[type] = setInterval(function () {
  34. seconds--;
  35. if (seconds <= 0) {
  36. clearInterval(si);
  37. $(that).removeClass("disabled").text("发送验证码");
  38. } else {
  39. $(that).addClass("disabled").text(seconds + "秒后可再次发送");
  40. }
  41. }, 1000);
  42. }, function () {
  43. $(that).removeClass("disabled").text('发送验证码');
  44. });
  45. } else {
  46. Layer.msg("请确认已经输入了正确的" + text + "!");
  47. }
  48. });
  49. return false;
  50. });
  51. }
  52. };
  53. Frontend.api = $.extend(Fast.api, Frontend.api);
  54. //将Template渲染至全局,以便于在子框架中调用
  55. window.Template = Template;
  56. //将Frontend渲染至全局,以便于在子框架中调用
  57. window.Frontend = Frontend;
  58. Frontend.init();
  59. return Frontend;
  60. });