common.js 6.03 KB
/**
 * 通用方法封装处理
 * Copyright (c) 2018 huaheng
 */

$(function () {
    // 复选框事件绑定
    if ($.fn.select2 !== undefined) {
        $("select.form-control:not(.noselect2)").each(function () {
            $(this).select2().on("change", function () {
                $(this).valid();
            })
        })
    }
    if ($(".i-checks").length > 0) {
        $(".i-checks").iCheck({
            checkboxClass: "icheckbox_square-green",
            radioClass: "iradio_square-green",
        })
    }
    if ($(".time").length > 0) {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            var day1 = new Date();
            day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000 * 7);
            var endDay = new Date();
            endDay.setTime(endDay.getTime() + 24 * 60 * 60 * 1000);
            laydate.render({elem: '#startTime', theme: 'molv', value: new Date(day1), isInitValue: true});
            laydate.render({elem: '#endTime', theme: 'molv', value: new Date(endDay), isInitValue: true});
        });
    }
    if ($(".time2").length > 0) {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            var day1 = new Date();
            day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000 * 7);
            var endDay = new Date();
            endDay.setTime(endDay.getTime() + 24 * 60 * 60 * 1000);
            laydate.render({elem: '#startTime', theme: 'molv'});
            laydate.render({elem: '#endTime', theme: 'molv'});
        });
    }

    if ($(".select-time").length > 0) {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            var day1 = new Date();
            day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000 * 7);
            var endDay = new Date();
            endDay.setTime(endDay.getTime() + 24 * 60 * 60 * 1000);
            laydate.render({elem: '#startTime', theme: 'molv'});
            laydate.render({elem: '#endTime', theme: 'molv'});
        });
    }
    if ($(".select-time2").length > 0) {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            var day1 = new Date();
            day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000 * 7);
            var endDay = new Date();
            endDay.setTime(endDay.getTime() + 24 * 60 * 60 * 1000);
            laydate.render({elem: '#startTime', theme: 'molv'});
            laydate.render({elem: '#endTime', theme: 'molv'});
        });
    }
});

/** 创建选项卡 */
function createMenuItem(dataUrl, menuName) {
    dataIndex = $.common.random(1, 100),
        flag = true;
    if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;
    var topWindow = $(window.parent.document);
    // 选项卡菜单已存在
    $('.menuTab', topWindow).each(function () {
        //todo 增加判断 如果是detail 则需去掉后面的/id 再判断
        if ($(this).data('id') == dataUrl) {
            if (!$(this).hasClass('active')) {
                $(this).addClass('active').siblings('.menuTab').removeClass('active');
                $('.page-tabs-content').animate({marginLeft: ""}, "fast");
                // 显示tab对应的内容区
                $('.mainContent .huaheng_iframe', topWindow).each(function () {
                    if ($(this).data('id') == dataUrl) {
                        $(this).show().siblings('.huaheng_iframe').hide();
                        return false;
                    }
                });
            }
            flag = false;
            return false;
        }
    });
    // 选项卡菜单不存在
    if (flag) {
        var str = '<a href="javascript:;" class="active menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
        $('.menuTab', topWindow).removeClass('active');

        // 添加选项卡对应的iframe
        var str1 = '<iframe class="huaheng_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
        $('.mainContent', topWindow).find('iframe.huaheng_iframe').hide().parents('.mainContent').append(str1);

        // 添加选项卡
        $('.menuTabs .page-tabs-content', topWindow).append(str);
    }
    return false;
}

/** 设置全局ajax超时处理 */
$.ajaxSetup({
    complete: function (XMLHttpRequest, textStatus) {
        if (textStatus == "parsererror") {
            $.modal.confirm("登陆超时!请重新登陆!", function () {
                window.location.href = ctx + "login";
            })
        }
    }
});

function getUrlParms() {
    var args = new Object();
    //获取查询串
    var query = location.search.substring(1);
    //在&处断开
    var pairs = query.split("&");
    for (var i = 0; i < pairs.length; i++) {
        //查找name=value
        var pos = pairs[i].indexOf('=');
        //如果没有找到就跳过
        if (pos == -1) continue;
        //提取name
        var argname = pairs[i].substring(0, pos);
        //提取value
        var value = pairs[i].substring(pos + 1);
        //存为属性
        args[argname] = decodeURI(value);
    }
    return args;
}

/** 拓展方法,替换字符串中字段的值 */
function formValueReplace(formData, replaceField, replaceValue) {
    var beginIndex = formData.indexOf("&" + replaceField);
    if (beginIndex > -1) {
        var endIndex = formData.indexOf("&", beginIndex + 1);
        if (endIndex > -1) {
            formData = formData.substring(0, beginIndex) + formData.substring(endIndex, formData.length);
        } else {
            formData = formData.substring(0, beginIndex);
        }
    }
    return formData + "&" + replaceField + "=" + replaceValue;
}

// 返回当前激活的Tab页面关联的iframe的Windows对象
function activeWindow() {
    var topWindow = $(window.parent.document);
    var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-id');
    if (!currentId) {
        return window.parent;
    }
    return $('.huaheng_iframe[data-id="' + currentId + '"]', topWindow)[0].contentWindow;
}