This repository has been archived on 2024-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
yoshop/web/assets/admin/js/app.js
2020-04-25 22:20:29 +08:00

154 lines
4.8 KiB
JavaScript

/**
* jquery全局函数封装
*/
(function ($) {
/**
* Jquery类方法
*/
$.fn.extend({
superForm: function (option) {
// 默认选项
var defaultOption = {
buildData: function () {
return {};
},
validation: function () {
return true;
}
};
option = $.extend(true, {}, defaultOption, option);
var $form = $(this)
, btn_submit = $('.j-submit');
$form.validator({
onValid: function (validity) {
$(validity.field).next('.am-alert').hide();
},
/**
* 显示错误信息
* @param validity
*/
onInValid: function (validity) {
var $field = $(validity.field)
, $group = $field.parent()
, $alert = $group.find('.am-alert');
if ($field.data('validationMessage') !== undefined) {
// 使用自定义的提示信息 或 插件内置的提示信息
var msg = $field.data('validationMessage') || this.getValidationMessage(validity);
if (!$alert.length) {
$alert = $('<div class="am-alert am-alert-danger"></div>').hide().appendTo($group);
}
$alert.html(msg).show();
}
},
submit: function () {
if (this.isFormValid() === true) {
// 自定义验证
if (!option.validation())
return false;
// 禁用按钮, 防止二次提交
btn_submit.attr('disabled', true);
// 表单提交
$form.ajaxSubmit({
type: "post",
dataType: "json",
data: option.buildData(),
success: function (result) {
result.code === 1 ? $.show_success(result.msg, result.url)
: $.show_error(result.msg);
btn_submit.attr('disabled', false);
}
});
}
return false;
}
});
},
/**
* 删除元素
*/
delete: function (index, url, msg) {
$(this).click(function () {
var param = {};
param[index] = $(this).attr('data-id');
layer.confirm(msg ? msg : '确定要删除吗?', {title: '友情提示'}
, function (index) {
$.post(url, param, function (result) {
result.code === 1 ? $.show_success(result.msg, result.url)
: $.show_error(result.msg);
});
layer.close(index);
}
);
});
},
});
/**
* Jquery全局函数
*/
$.extend({
/**
* 对象转URL
*/
urlEncode: function (data) {
var _result = [];
for (var key in data) {
var value = null;
if (data.hasOwnProperty(key)) value = data[key];
if (value.constructor === Array) {
value.forEach(function (_value) {
_result.push(key + "=" + _value);
});
} else {
_result.push(key + '=' + value);
}
}
return _result.join('&');
},
/**
* 操作成功弹框提示
* @param msg
* @param url
*/
show_success: function (msg, url) {
layer.msg(msg, {
icon: 1
, time: 1200
// , anim: 1
, shade: 0.5
, end: function () {
(url !== undefined && url.length > 0) ? window.location = url : window.location.reload();
}
});
},
/**
* 操作失败弹框提示
* @param msg
* @param reload
*/
show_error: function (msg, reload) {
var time = reload ? 1200 : 0;
layer.alert(msg, {
title: '提示'
, icon: 2
, time: time
, anim: 6
, end: function () {
reload && window.location.reload();
}
});
}
});
})(jQuery);