(function ($) { $.extend({ operate: { // 提交数据 submit: function(url, type, dataType, data, callback) { var config = { url: url, type: type, dataType: dataType, data: data, /*beforeSend: function () { $.modal.loading("正在处理中,请稍后..."); },*/ success: function(result) { if (typeof callback == "function") { callback(result); } } }; $.ajax(config) }, // post请求传输 post: function(url, data, callback) { $.operate.submit(url, "post", "json", data, callback); }, // get请求传输 get: function(url, callback) { $.operate.submit(url, "get", "json", "", callback); } }, common: { // 判断字符串是否为空 isEmpty: function (value) { if (value == null || this.trim(value) == "") { return true; } return false; }, // 判断一个字符串是否为非空串 isNotEmpty: function (value) { return !$.common.isEmpty(value); }, // 空对象转字符串 nullToStr: function(value) { if ($.common.isEmpty(value)) { return "-"; } return value; }, // 是否显示数据 为空默认为显示 visible: function (value) { if ($.common.isEmpty(value) || value == true) { return true; } return false; }, // 空格截取 trim: function (value) { if (value == null) { return ""; } return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, ""); }, // 比较两个字符串(大小写敏感) equals: function (str, that) { return str == that; }, // 比较两个字符串(大小写不敏感) equalsIgnoreCase: function (str, that) { return String(str).toUpperCase() === String(that).toUpperCase(); }, // 将字符串按指定字符分割 split: function (str, sep, maxLen) { if ($.common.isEmpty(str)) { return null; } var value = String(str).split(sep); return maxLen ? value.slice(0, maxLen - 1) : value; }, // 字符串格式化(%s ) sprintf: function (str) { var args = arguments, flag = true, i = 1; str = str.replace(/%s/g, function () { var arg = args[i++]; if (typeof arg === 'undefined') { flag = false; return ''; } return arg; }); return flag ? str : ''; }, // 获取节点数据,支持多层级访问 getItemField: function (item, field) { var value = item; if (typeof field !== 'string' || item.hasOwnProperty(field)) { return item[field]; } var props = field.split('.'); for (var p in props) { value = value && value[props[p]]; } return value; }, // 指定随机数返回 random: function (min, max) { return Math.floor((Math.random() * max) + min); }, // 判断字符串是否是以start开头 startWith: function(value, start) { var reg = new RegExp("^" + start); return reg.test(value) }, // 判断字符串是否是以end结尾 endWith: function(value, end) { var reg = new RegExp(end + "$"); return reg.test(value) }, // 数组去重 uniqueFn: function(array) { var result = []; var hashObj = {}; for (var i = 0; i < array.length; i++) { if (!hashObj[array[i]]) { hashObj[array[i]] = true; result.push(array[i]); } } return result; }, // 数组中的所有元素放入一个字符串 join: function(array, separator) { if ($.common.isEmpty(array)) { return null; } return array.join(separator); }, // 获取form下所有的字段并转换为json对象 formToJSON: function(formId) { var json = {}; $.each($("#" + formId).serializeArray(), function(i, field) { if(json[field.name]) { json[field.name] += ("," + field.value); } else { json[field.name] = field.value; } }); return json; }, // 获取obj对象长度 getLength: function(obj) { var count = 0; for (var i in obj) { if (obj.hasOwnProperty(i)) { count++; } } return count; } }, // 弹出层封装处理 modal: { // 显示图标 icon: function(type) { var icon = ""; if (type == modal_status.WARNING) { icon = 0; } else if (type == modal_status.SUCCESS) { icon = 1; } else if (type == modal_status.FAIL) { icon = 2; } else { icon = 3; } return icon; }, // 消息提示 msg: function(content, type) { if (type != undefined) { layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 }); } else { layer.msg(content); } }, // 错误消息 msgError: function(content) { $.modal.msg(content, modal_status.FAIL); }, // 成功消息 msgSuccess: function(content) { $.modal.msg(content, modal_status.SUCCESS); }, // 警告消息 msgWarning: function(content) { $.modal.msg(content, modal_status.WARNING); }, // 弹出提示 alert: function(content, type) { layer.alert(content, { icon: $.modal.icon(type), title: "系统提示", btn: ['确认'], btnclass: ['btn btn-primary'], }); }, // 错误提示 alertError: function(content) { $.modal.alert(content, modal_status.FAIL); }, // 成功提示 alertSuccess: function(content) { $.modal.alert(content, modal_status.SUCCESS); }, // 警告提示 alertWarning: function(content) { $.modal.alert(content, modal_status.WARNING); }, // 确认窗体 confirm: function (content, callBack) { layer.confirm(content, { icon: 3, title: "系统提示", btn: ['确认', '取消'] }, function (index) { layer.close(index); callBack(true); }); }, // 弹出层指定宽度 open: function (title, url, width, height, callback) { //如果是移动端,就使用自适应大小弹窗 /*if ($.common.isMobile()) { width = 'auto'; height = 'auto'; }*/ if ($.common.isEmpty(title)) { title = false; } if ($.common.isEmpty(url)) { url = "/404.html"; } if ($.common.isEmpty(width)) { width = 800; } if ($.common.isEmpty(height)) { height = ($(window).height() - 50); } if ($.common.isEmpty(callback)) { callback = function(index, layero) { var iframeWin = layero.find('iframe')[0]; iframeWin.contentWindow.submitHandler(index, layero); } } layer.open({ type: 2, area: [width + 'px', height + 'px'], fix: false, //不固定 maxmin: true, shade: 0.3, title: title, content: url, btn: ['确定', '关闭'], // 弹层外区域关闭 shadeClose: true, yes: callback, cancel: function(index) { return true; } }); }, // 弹出层指定参数选项 openOptions: function (options) { var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url; var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title; var _width = $.common.isEmpty(options.width) ? "800" : options.width; var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height; var _btn = [' 确认', ' 关闭']; if ($.common.isEmpty(options.yes)) { options.yes = function(index, layero) { options.callBack(index, layero); } } var btnCallback = {}; if(options.btn instanceof Array){ for (var i = 1, len = options.btn.length; i < len; i++) { var btn = options["btn" + (i + 1)]; if (btn) { btnCallback["btn" + (i + 1)] = btn; } } } var index = layer.open($.extend({ type: 2, maxmin: $.common.isEmpty(options.maxmin) ? true : options.maxmin, shade: 0.3, title: _title, fix: false, area: [_width + 'px', _height + 'px'], content: _url, shadeClose: $.common.isEmpty(options.shadeClose) ? true : options.shadeClose, skin: options.skin, btn: $.common.isEmpty(options.btn) ? _btn : options.btn, yes: options.yes, cancel: function () { return true; } }, btnCallback)); if ($.common.isNotEmpty(options.full) && options.full === true) { layer.full(index); } }, // 禁用按钮 disable: function() { var doc = window.top == window.parent ? window.document : window.parent.document; $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled"); }, // 启用按钮 enable: function() { var doc = window.top == window.parent ? window.document : window.parent.document; $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled"); }, // 打开遮罩层 loading: function (message) { $.blockUI({ message: '
' + message + '
' }); }, // 关闭遮罩层 closeLoading: function () { setTimeout(function(){ $.unblockUI(); }, 50); } } }); })(jQuery);