diff --git a/public/assets/index/js/plugin.js b/public/assets/index/js/plugin.js new file mode 100644 index 0000000..ac11c61 --- /dev/null +++ b/public/assets/index/js/plugin.js @@ -0,0 +1,188 @@ +$('[data-href]').on('click', function(event) { + event.preventDefault(); + if ($(this).hasClass('ajax-get') || $(this).hasClass('ajax-post')) { + return; + } + location.href = $(this).data('href'); +}); + +// ajax GET 请求 +$('body').on('click', '.ajax-get', function(event) { + event.preventDefault(); + if ($(this).hasClass('disabled') || $(this).attr('disabled')) { + return false; + }; + var $this = $(this); + var $tips = $this.attr('tip') || '确认要执行该操作吗?'; + var $target = $this.data('href') || $this.attr('href') || $this.attr('url') || $this.data('url'); + + if ($this.hasClass('confirm')) { + if(!confirm($tips)){ + return false; + } + } + $.ajax({ + type: "GET", + url: $target, + success: function(data){ + if(data.code==1){ + updateAlert(data.msg, 'success', function() { + if ($this.hasClass('no-refresh')) { + } else if (data.url == null) { + location.reload(); + } else if (data.url) { + location.href = data.url + '?_=' + (new Date()).getTime(); + } else { + location.reload(); + } + }); + } else { + updateAlert(data.msg); + } + }, + error: function(error){ + if (error.responseJSON.message) { + updateAlert(error.responseJSON.message, 'warning'); + } else { + updateAlert('发生未知错误', 'warning'); + } + } + }); +}); + +// ajax POST 请求 +$('body').on('click', '.ajax-post', function(event) { + if ($(this).hasClass('disabled') || $(this).attr('disabled')) { + return false; + }; + + event.preventDefault(); + var $this = $(this); + var $form = $this.parents('form'); + var $tips = $this.attr('tip') || '确认要执行该操作吗?'; + var $action = $form.attr("action"); + if ($this.hasClass('confirm')) { + if(!confirm($tips)){ + return false; + } + } + $this.attr('disabled', 'disabled'); + var query = $form.serialize(); + + $.ajax({ + type: "POST", + url: $action, + data: query, + success: function(data) { + if (data.code == 0) { + updateAlert(data.msg); + $this.removeAttr('disabled'); + } else { + updateAlert(data.msg, 'success', function() { + if (data.url) { + location.href = data.url; + } else if (data.url == null) { + location.reload(); + } else { + location.reload(); + } + }); + } + }, + error: function(error) { + $this.removeAttr('disabled'); + if (error.responseJSON.errors) { + var err = ''; + $.each(error.responseJSON.errors, function(i, n) { + // err += n + "\r\n"; + updateAlert(n[0], 'warning'); + return false; + }) + // updateAlert(err, 'warning'); + } else if (error.responseJSON.message) { + updateAlert(error.responseJSON.message, 'warning'); + } else { + updateAlert('发生未知错误', 'warning'); + } + } + }); +}); + +$('body').on('click', '.ajax-post-confirm', function(event) { + if ($(this).hasClass('disabled') || $(this).attr('disabled')) { + return false; + }; + + event.preventDefault(); + var $this = $(this); + var $form = $this.parents('form'); + var $action = $form.attr("action"); + var $tips = $this.attr('tip') || '确认要执行该操作吗?'; + var query = $form.serialize(); + $this.attr('disabled', 'disabled'); + layer.open({ + content: $tips + ,btn: ['确定', '取消'] + ,yes: function(index){ + $.ajax({ + type: "POST", + url: $action, + data: query, + success: function(data) { + if (data.code == 0) { + updateAlert(data.msg); + $this.removeAttr('disabled'); + } else { + updateAlert(data.msg, data.error, function() { + if (data.url) { + location.href = data.url; + } else if (data.url == null) { + location.reload(); + } else { + location.reload(); + } + }); + } + }, + error: function(error) { + $this.removeAttr('disabled'); + if (error.responseJSON.errors) { + var err = ''; + $.each(error.responseJSON.errors, function(i, n) { + // err += n + "\r\n"; + updateAlert(n[0], 'warning'); + return false; + }) + // updateAlert(err, 'warning'); + } else if (error.responseJSON.message) { + updateAlert(error.responseJSON.message, 'warning'); + } else { + updateAlert('发生未知错误', 'warning'); + } + } + }); + layer.close(index); + } + }); +}); + +window.updateAlert = function(text, type, callback) { + if (typeof type != 'string') { + if (type) { + type = "success"; + } else { + type = "error"; + } + } + swal({ + title: text, + type: type, + timer: 1500, + showConfirmButton: false + }); + if (typeof callback == "function") { + setTimeout(function() { + callback(); + }, 1500) + } +} diff --git a/resources/views/index/index.blade.php b/resources/views/index/index.blade.php index 88e1b3d..ff577fa 100644 --- a/resources/views/index/index.blade.php +++ b/resources/views/index/index.blade.php @@ -10,8 +10,11 @@
@foreach (getAdvertsByCate(23) as $advert) + @if($advert->url) data-href="" @endif +
-
+
url) data-href="{{ $advert->url }}" @endif>
{{ $advert->title }} @@ -146,7 +149,8 @@ @if (getArticlesBYCate(24,5,'all',false,'asc')->isNotEmpty()) @foreach (getArticlesBYCate(24,5) as $article) @if($loop->index==4) - +
@@ -228,7 +232,8 @@
@@ -287,7 +292,8 @@ @if (getArticlesBYCate(12,5)->isNotEmpty()) @foreach (getArticlesBYCate(12,5) as $article)
  • - +
    {{ $article->title }}
    @@ -304,7 +310,8 @@ @if (getArticlesBYCate(30,5)->isNotEmpty()) @foreach (getArticlesBYCate(30,5) as $article)
  • - +
    {{ $article->title }}
    @@ -397,7 +404,7 @@ 客座教授
    {{ $article->title }}
  • -
    @@ -62,6 +63,7 @@ +