0
0

更新代码

This commit is contained in:
2020-08-04 10:18:02 +08:00
parent 55514ed071
commit 6c03488864
272 changed files with 130 additions and 0 deletions

97
public/js/cjango.js Normal file
View File

@@ -0,0 +1,97 @@
$('[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();
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;
}
}
$.get($target, function(data) {
updateAlert(data.message, data.error, function() {
if ($this.hasClass('no-refresh')) {
} else if (data.redirect == null) {
if($this.hasClass("refresh")){
location.reload(true);
}
} else if (data.redirect) {
location.href = data.redirect + '?_=' + (new Date()).getTime();
} else {
location.reload(true);
}
});
});
});
// 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) {
updateAlert(data.message, data.error, function() {
if (data.redirect) {
location.href = data.redirect + '?_=' + (new Date()).getTime();
} else if (data.redirect == null) {
} else {
location.reload(true);
}
$this.removeAttr('disabled');
});
},
error: function(error) {
$this.removeAttr('disabled');
if (error.responseJSON.errors) {
var err = '';
$.each(error.responseJSON.errors, function(i, n) {
err += n + "\r\n";
})
updateAlert(err, 'warning');
} else if (error.responseJSON.message) {
updateAlert(error.responseJSON.message, 'warning');
} else {
updateAlert('发生未知错误', 'warning');
}
}
});
});
window.updateAlert = function(text, type, callback) {
alert(text);
if (typeof callback == "function") {
setTimeout(function() {
callback();
}, 1000)
}
}