frontend.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 btn = this;
  10. Frontend.api.sendcaptcha = function (btn, type, data, callback) {
  11. $(btn).addClass("disabled", true).text("发送中...");
  12. Frontend.api.ajax({url: $(btn).data("url"), data: data}, function (data, ret) {
  13. clearInterval(si[type]);
  14. var seconds = 60;
  15. si[type] = setInterval(function () {
  16. seconds--;
  17. if (seconds <= 0) {
  18. clearInterval(si);
  19. $(btn).removeClass("disabled").text("发送验证码");
  20. } else {
  21. $(btn).addClass("disabled").text(seconds + "秒后可再次发送");
  22. }
  23. }, 1000);
  24. if (typeof callback == 'function') {
  25. callback.call(this, data, ret);
  26. }
  27. }, function () {
  28. $(btn).removeClass("disabled").text('发送验证码');
  29. });
  30. };
  31. if (['mobile', 'email'].indexOf(type) > -1) {
  32. var element = $(this).data("input-id") ? $("#" + $(this).data("input-id")) : $("input[name='" + type + "']", $(this).closest("form"));
  33. var text = type === 'email' ? '邮箱' : '手机号码';
  34. if (element.val() === "") {
  35. Layer.msg(text + "不能为空!");
  36. element.focus();
  37. return false;
  38. } else if (type === 'mobile' && !element.val().match(/^1[3-9]\d{9}$/)) {
  39. Layer.msg("请输入正确的" + text + "!");
  40. element.focus();
  41. return false;
  42. } else if (type === 'email' && !element.val().match(/^[\w\+\-]+(\.[\w\+\-]+)*@[a-z\d\-]+(\.[a-z\d\-]+)*\.([a-z]{2,4})$/)) {
  43. Layer.msg("请输入正确的" + text + "!");
  44. element.focus();
  45. return false;
  46. }
  47. element.isValid(function (v) {
  48. if (v) {
  49. var data = {event: $(btn).data("event")};
  50. data[type] = element.val();
  51. Frontend.api.sendcaptcha(btn, type, data);
  52. } else {
  53. Layer.msg("请确认已经输入了正确的" + text + "!");
  54. }
  55. });
  56. } else {
  57. var data = {event: $(btn).data("event")};
  58. Frontend.api.sendcaptcha(btn, type, data, function (data, ret) {
  59. Layer.open({title: false, area: ["400px", "430px"], content: "<img src='" + data.image + "' width='400' height='400' /><div class='text-center panel-title'>扫一扫关注公众号获取验证码</div>", type: 1});
  60. });
  61. }
  62. return false;
  63. });
  64. //tooltip和popover
  65. if (!('ontouchstart' in document.documentElement)) {
  66. $('body').tooltip({selector: '[data-toggle="tooltip"]'});
  67. }
  68. $('body').popover({selector: '[data-toggle="popover"]'});
  69. // 手机端左右滑动切换菜单栏
  70. if ('ontouchstart' in document.documentElement) {
  71. var startX, startY, moveEndX, moveEndY, relativeX, relativeY, element;
  72. element = $('body', document);
  73. element.on("touchstart", function (e) {
  74. startX = e.originalEvent.changedTouches[0].pageX;
  75. startY = e.originalEvent.changedTouches[0].pageY;
  76. });
  77. element.on("touchend", function (e) {
  78. moveEndX = e.originalEvent.changedTouches[0].pageX;
  79. moveEndY = e.originalEvent.changedTouches[0].pageY;
  80. relativeX = moveEndX - startX;
  81. relativeY = moveEndY - startY;
  82. // 判断标准
  83. //右滑
  84. if (relativeX > 45) {
  85. if ((Math.abs(relativeX) - Math.abs(relativeY)) > 50) {
  86. element.addClass("sidebar-open");
  87. }
  88. }
  89. //左滑
  90. else if (relativeX < -45) {
  91. if ((Math.abs(relativeX) - Math.abs(relativeY)) > 50) {
  92. element.removeClass("sidebar-open");
  93. }
  94. }
  95. });
  96. }
  97. $(document).on("click", ".sidebar-toggle", function () {
  98. $("body").toggleClass("sidebar-open");
  99. });
  100. }
  101. };
  102. Frontend.api = $.extend(Fast.api, Frontend.api);
  103. //将Template渲染至全局,以便于在子框架中调用
  104. window.Template = Template;
  105. //将Moment渲染至全局,以便于在子框架中调用
  106. window.Moment = Moment;
  107. //将Frontend渲染至全局,以便于在子框架中调用
  108. window.Frontend = Frontend;
  109. Frontend.init();
  110. return Frontend;
  111. });