frontend.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. define(['fast', 'template', 'moment'], function (Fast, Template, Moment) {
  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. //tooltip和popover
  52. if (!('ontouchstart' in document.documentElement)) {
  53. $('body').tooltip({selector: '[data-toggle="tooltip"]'});
  54. }
  55. $('body').popover({selector: '[data-toggle="popover"]'});
  56. }
  57. };
  58. Frontend.api = $.extend(Fast.api, Frontend.api);
  59. //将Template渲染至全局,以便于在子框架中调用
  60. window.Template = Template;
  61. //将Moment渲染至全局,以便于在子框架中调用
  62. window.Moment = Moment;
  63. //将Frontend渲染至全局,以便于在子框架中调用
  64. window.Frontend = Frontend;
  65. Frontend.init();
  66. return Frontend;
  67. });