app.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * jquery全局函数封装
  3. */
  4. (function ($) {
  5. /**
  6. * Jquery类方法
  7. */
  8. $.fn.extend({
  9. superForm: function (option) {
  10. // 默认选项
  11. var defaultOption = {
  12. buildData: function () {
  13. return {};
  14. },
  15. validation: function () {
  16. return true;
  17. }
  18. };
  19. option = $.extend(true, {}, defaultOption, option);
  20. var $form = $(this)
  21. , btn_submit = $('.j-submit');
  22. $form.validator({
  23. onValid: function (validity) {
  24. $(validity.field).next('.am-alert').hide();
  25. },
  26. /**
  27. * 显示错误信息
  28. * @param validity
  29. */
  30. onInValid: function (validity) {
  31. var $field = $(validity.field)
  32. , $group = $field.parent()
  33. , $alert = $group.find('.am-alert');
  34. if ($field.data('validationMessage') !== undefined) {
  35. // 使用自定义的提示信息 或 插件内置的提示信息
  36. var msg = $field.data('validationMessage') || this.getValidationMessage(validity);
  37. if (!$alert.length) {
  38. $alert = $('<div class="am-alert am-alert-danger"></div>').hide().appendTo($group);
  39. }
  40. $alert.html(msg).show();
  41. }
  42. },
  43. submit: function () {
  44. if (this.isFormValid() === true) {
  45. // 自定义验证
  46. if (!option.validation())
  47. return false;
  48. // 禁用按钮, 防止二次提交
  49. btn_submit.attr('disabled', true);
  50. // 表单提交
  51. $form.ajaxSubmit({
  52. type: "post",
  53. dataType: "json",
  54. data: option.buildData(),
  55. success: function (result) {
  56. result.code === 1 ? $.show_success(result.msg, result.url)
  57. : $.show_error(result.msg);
  58. btn_submit.attr('disabled', false);
  59. }
  60. });
  61. }
  62. return false;
  63. }
  64. });
  65. },
  66. /**
  67. * 删除元素
  68. */
  69. delete: function (index, url, msg) {
  70. $(this).click(function () {
  71. var param = {};
  72. param[index] = $(this).attr('data-id');
  73. layer.confirm(msg ? msg : '确定要删除吗?', {title: '友情提示'}
  74. , function (index) {
  75. $.post(url, param, function (result) {
  76. result.code === 1 ? $.show_success(result.msg, result.url)
  77. : $.show_error(result.msg);
  78. });
  79. layer.close(index);
  80. }
  81. );
  82. });
  83. },
  84. });
  85. /**
  86. * Jquery全局函数
  87. */
  88. $.extend({
  89. /**
  90. * 对象转URL
  91. */
  92. urlEncode: function (data) {
  93. var _result = [];
  94. for (var key in data) {
  95. var value = null;
  96. if (data.hasOwnProperty(key)) value = data[key];
  97. if (value.constructor === Array) {
  98. value.forEach(function (_value) {
  99. _result.push(key + "=" + _value);
  100. });
  101. } else {
  102. _result.push(key + '=' + value);
  103. }
  104. }
  105. return _result.join('&');
  106. },
  107. /**
  108. * 操作成功弹框提示
  109. * @param msg
  110. * @param url
  111. */
  112. show_success: function (msg, url) {
  113. layer.msg(msg, {
  114. icon: 1
  115. , time: 1200
  116. // , anim: 1
  117. , shade: 0.5
  118. , end: function () {
  119. (url !== undefined && url.length > 0) ? window.location = url : window.location.reload();
  120. }
  121. });
  122. },
  123. /**
  124. * 操作失败弹框提示
  125. * @param msg
  126. * @param reload
  127. */
  128. show_error: function (msg, reload) {
  129. var time = reload ? 1200 : 0;
  130. layer.alert(msg, {
  131. title: '提示'
  132. , icon: 2
  133. , time: time
  134. , anim: 6
  135. , end: function () {
  136. reload && window.location.reload();
  137. }
  138. });
  139. }
  140. });
  141. })(jQuery);