admin_component.blade.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <div id="ajaxForm" style="z-index:500;display:none;"></div>
  2. <script type="text/javascript">
  3. $(function () {
  4. var t = new Vue({
  5. el:'#ajaxForm',
  6. methods: {
  7. ajaxSuccess: function(message,redirect) {
  8. this.$notify({
  9. title: '成功',
  10. message: message,
  11. type: 'success',
  12. duration: 1500,
  13. onClose: function(){
  14. if( ! redirect ){
  15. var tip = tips_layer.pop();
  16. refresh();
  17. layer.close(tip);
  18. layer.closeAll();
  19. }else{
  20. parent.location.href = redirect;
  21. }
  22. }
  23. });
  24. },
  25. ajaxError: function (message) {
  26. this.$notify.error({
  27. title: '失败',
  28. message: message,
  29. duration: 2500
  30. });
  31. },
  32. ajaxWaring: function () {
  33. this.$notify({
  34. title: '警告',
  35. message: '请求失败',
  36. type: 'warning',
  37. duration: 3500
  38. });
  39. },
  40. ajaxInfo: function () {
  41. this.$notify.info({
  42. title: '提示',
  43. message: '请编辑后再保存!!!',
  44. duration: 2000
  45. });
  46. }
  47. }
  48. });
  49. $('form').submit(function (event) {
  50. if( window.form && !window.form.valid() ){
  51. return false;
  52. }
  53. // 是否开启验证机制 and 验证数据是否修改
  54. if( window.formDatum && window.formDatum == $('form').serialize() ){
  55. //> 提示信息
  56. t.ajaxInfo();
  57. return false;
  58. }
  59. var evt = event || window.event;
  60. var th = $(evt.target);
  61. // 前端防止重复提交数据 注释按钮
  62. $($(th).find('button')[0]).prop('disabled',true);
  63. $($(th).find('button')[0]).css('cursor','not-allowed');
  64. $.ajax({
  65. url: th.prop('action'),
  66. type: th.prop('method'),
  67. data: th.serialize(),
  68. dataType: 'json',
  69. success: function (res) {
  70. if( res.status == 'success' ){
  71. t.ajaxSuccess(res.message,res.redirect);
  72. }else{
  73. // 处理回调错误信息
  74. if( res.error ){
  75. window.tipsC = [];
  76. // 数据后台验证失败
  77. for( key in res.message ){
  78. window.tipsC[key] = layer.tips(res.message[key].join(' | '),'form input[name="'+key+'"]',{tips: [3,'#FF4949'],time:4000,tipesMore:true,
  79. end:function () {
  80. // 失败取消提交按钮
  81. $($(th).find('button')[0]).prop('disabled',false);
  82. $($(th).find('button')[0]).css('cursor','pointer');
  83. layer.close(window.tipsC[key]);
  84. return false;
  85. }});
  86. }
  87. }else{
  88. // 数据保存失败
  89. t.ajaxError(res.message);
  90. // 失败取消提交按钮
  91. $($(th).find('button')[0]).prop('disabled',false);
  92. $($(th).find('button')[0]).css('cursor','pointer');
  93. }
  94. }
  95. },
  96. error: function () {
  97. // 失败取消提交按钮
  98. $($(th).find('button')[0]).prop('disabled',false);
  99. $($(th).find('button')[0]).css('cursor','pointer');
  100. t.ajaxWaring();
  101. }
  102. });
  103. return false;
  104. });
  105. });
  106. </script>