var lock = {}; var load = {}; var like = { ajax: function (json) { var load_index = null; if (json.beforeSend === undefined) { if (lock[json.url.replace("/", "_")] !== undefined) { return } lock[json.url.replace("/", "_")] = true; json.beforeSend = function () { load[json.url.replace("/", "_")] = setTimeout(function () { load_index = layer.load(1, {shade: [0.1, "#fff"]}) }, 1500) } } if (json.error === undefined) { json.error = function (res) { layer.msg("网络错误", {offset: "240px", icon: 2, time: 1500}, function () { return }) } } if (json.timeout === undefined) { json.timeout = 30000 } if (json.type === undefined) { json.type = "get" } json.complete = function (xhr, status) { delete lock[json.url.replace("/", "_")]; if (status == "timeout") { layer.msg("请求超时,请重试", {offset: "240px", icon: 2, time: 1500}, function () { if (load_index !== undefined) { layer.close(load_index) } return }); return } clearTimeout(load[json.url.replace("/", "_")]); if (load_index !== undefined) { layer.close(load_index) } res = xhr.responseJSON; if (res !== undefined && res.code == -1) { window.location.href = window.location.href } if (res !== undefined && res.code == 0 && res.show == 1) { layer.msg(res.msg, {offset: "240px", icon: 2, time: 1500}, function () { return; }) } }; $.ajax(json); }, tableLists: function (elem, url, cols, where,page = true) { where = where === undefined ? {} : where; page = page === undefined ? true : page; layui.use(['table'], function () { var table = layui.table; table.render({ elem: elem , url: url , cols: [cols] , where: where , text: {none: '暂无相关数据'} , response: { statusCode: 1 } , page: page , parseData: function (res) { return { "code": res.code, "msg": res.msg, "count": res.data.count, "data": res.data.lists }; } , done: function () { setTimeout(function () { $(".layui-table-main tr").each(function (index, val) { $($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height()); $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height()); }); }, 200); } }); }); }, keyUpClick: function (elem, target) { $(elem).keyup(function (event) { if (event.keyCode === 13) { $(target).trigger('click'); } }); }, eventClick: function (active) { $(document).on("click", ".layEvent", function () { var type = $(this).attr("lay-event"); var obj = $(this).attr("lay-data"); active[type] ? active[type].call(this, obj) : ""; }); layui.use(["table"], function () { layui.table.on("tool(like-table-lists)", function(obj){ var type = obj.event; active[type] ? active[type].call(this, obj) : ""; }); }); }, setSelect: function (defaultId, data, targetName, optionName) { layui.use(['form'], function () { var form = layui.form; var selectHtml = ''; for (var i in data) { selectHtml += ''; } $('select[name=' + targetName + ']').html(selectHtml); $('select[name=' + targetName + ']').val(defaultId); form.render('select'); }); }, /** * 上传图片 * @author fzr * @param options */ imageUpload: function (options) { var that = options.that; var limit = options.limit || 1; var field = options.field || "image"; var content = options.content || '/admin/file/lists?type=10&&shop_id=0'; var isSpecImage = options.isSpecImage || false; parent.layer.open({ type: 2 ,title: "上传图片" ,shadeClose: true ,maxmin: true ,anim: 1 ,shade: 0.3 ,area: ["90%", "90%"] ,content: content, success: function (layero, index) { var iframeNode = $(layero).find("iframe").contents(); iframeNode.find("#okFile").click(function () { var fileUrls = iframeNode.find("#fileUrl").val(); var urls = fileUrls.split(','); if (urls.length <= 0) { parent.layer.msg("请至少选择一张图片"); return false; } if (urls.length > limit) { parent.layer.msg("限制只能选"+limit+"张"); return false; } if (isSpecImage) { urls.forEach(function (url) { // 商品多规格特殊处理 var template = '