193 lines
6.0 KiB
JavaScript
193 lines
6.0 KiB
JavaScript
$('[data-href]').on('click', function (event) {
|
|
event.preventDefault();
|
|
if ($(this).hasClass('ajax-get') || $(this).hasClass('ajax-post')) {
|
|
return;
|
|
}
|
|
window.open($(this).data('href'));
|
|
// 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)
|
|
}
|
|
}
|