tree 递归

/*
 *公共变量
 */
//主题id
var top_Pid;
/*
 *验证正则
 */
var regPassword = /(?=.*\d)(?=.*[A-z])^[0-9A-z]{8,20}$/; //最少8为,数字和字母组合

/*
 *退出登录
 */
function loginOut() {
    layer.msg('确定要退出系统吗', {
        time: 0, //不自动关闭
        btn: ['确定', '取消'],
        yes: function(index, layero) {
                layer.close(index);
                layer.msg('退出成功!');
                setTimeout(function() {
                    //清除token
                    localStorage.removeItem('token');
                    window.location.href = window.location.origin + '/pages/login/login.html';
                }, 1000);
            }
            //取消
            // btn2: function(index, layero){

        // }
    });
};
/*
 *修改密码
 *2018-05-10
 */
//修改密码弹框
function updatePwdModal() {
    // 清空表单
    $("#upload_oldPwd").val("");
    $("#upload_newPwd").val("");
    $("#updatePwdModal").modal("show");
};
//密码保存
function updatePwd() {
    var oldPwd = $("#upload_oldPwd").val();
    var newPwd = $("#upload_newPwd").val();
    var userName = $("#upload_username").val();
    if (!userName) {
        layer.msg('请输入用户名!');
        return false;
    };
    if (!oldPwd) {
        layer.msg('请输入原密码!');
        return false;
    };
    if (!newPwd) {
        layer.msg('请输入新密码!');
        return false;
    };
    if (!regPassword.test(newPwd)) {
        layer.msg('最少8个字符,必须包含且只能填数字和字母,长度为8-20位!');
        return false;
    };
    var params = {
        username: userName,
        oldpwd: oldPwd,
        password: newPwd
    };
    ajaxJson('POST', urls + '/sys/changPwd', JSON.stringify(params), function(err, rsp) {
        if (rsp.code == 200) {
            $('#updatePwdModal').modal('hide');
            layer.msg('密码修改成功!');
        } else {
            layer.msg(rsp.message);
        }
    }, true);
};
/*
 *获取用户信息
 */
function getInfo() {
    // var infoParams = {};
    ajaxJson('GET', urls + '/user/getUserInfo', '', function(err, rsp) {
        if (rsp.code == 200) {
            var name = rsp.body.name;
            $('#login_name').text(name);
            //修改密码弹框用户名绑定
            $("#upload_username").val(name);
            $('#username').val(name);
        } else {
            $('#login_name').text('请先登录');
            layer.msg(rsp.message);
        }
    }, true);
};
/*
 *加载主题菜单
 */
function getTopicMenu() {
    var infoParams = {};
    ajaxJson('POST', urls + '/menu/topicMenu', JSON.stringify(infoParams), function(err, rsp) {
        if (rsp.code == 200) {
            var data = rsp.body;
            //主题菜单拼接
            TopnavBarListHtml(data);
        } else {
            // layer.msg(rsp.message);
            console.log(rsp.message);
        }
    }, true);
};
//主题菜单拼接
function TopnavBarListHtml(obj) {
    var data = obj;
    $('#TopnavBarList ul').empty();
    var innerHTML = '';
    for (var i = 0; i < data.length; i++) {
        innerHTML += '<li pid="' + data[i].id + '" type="' + data[i].type + '">' +
            '<a href="#">' + data[i].name +
            '</a>' +
            '</li>';
    };
    $('#TopnavBarList ul').append(innerHTML);
    // 默认第一个高亮
    $('#TopnavBarList ul li').eq(0).addClass('active');
    //切换样式
    $('#TopnavBarList ul li').on('click', function() {
        $(this).addClass('active').siblings().removeClass('active');
        var top_Pid = $(this).attr('pid');
        //加载左侧导航(请求接口)
        var pid = top_Pid;
        LeftMenuAjax(pid);
    });
    //
    $('#TopnavBarList ul li').each(function(i, items) {
        if ($(this).attr('type') == 0) {
            $(this).on('click', function() {
                window.location.reload();
            });
        } else {
            LeftMenuAjax(0);
        }
    });
};

/*
 *左侧导航拼接
 */
//列表数据请求后台
function LeftMenuAjax(pid) {
    var id = pid;
    ajaxJson('POST', urls + '/menu/treeMenus?pid=' + id, '', function(err, rsp) {
        if (rsp.code == 200) {
            var data = rsp.body;
            //左侧导航拼接
            var htl = forTree(data);
            $('#navlist').html(htl);
            menuTree();
        } else {
            // layer.msg(rsp.message);
            console.log(rsp.message);
        }
    }, true);
};
//递归实现获取无级树数据并生成DOM结构
var forTree = function(data) {
    var str = "";
    var dataForTree = function(o) {
        for (var i = 0; i < o.length; i++) {
            var urlstr = "";
            try {
                if (typeof o[i]["url"] == "undefined") {
                    urlstr = "<div><span>" + o[i]["name"] + "</span><ul>";
                } else {
                    // urlstr = "<div><span><a href=" + o[i]["url"] + ">" + o[i]["name"] + "</a><i class='glyphicon'></i></span><ul>";
                    urlstr = '<div><span><font class="' + o[i].icon + '"></font><a href="javascript:void(0)" onclick="goPage(' + "\'" + o[i].path + "\'" + ',this,' + o[i].id + ',' + o[i].type + ')">' + o[i]['name'] +
                        '</a><i class="glyphicon icon-fw"></i></span><ul>';
                }
                str += urlstr;
                if (o[i].items != null) {
                    dataForTree(o[i].items);
                }
                str += "</ul></div>";
            } catch (e) {}
        }
    };
    if (data && data[0] && data[0].items && data[0].items != undefined) {
        dataForTree(data[0].items);
    }
    return str;
};
//树形菜单
function menuTree() {
    //给有子对象的元素加[+-]
    $("#navlist ul").each(function(index, element) {
        var ulContent = $(element).html();
        if (ulContent) {
            $(element).siblings("span").children('i').attr('class', 'icon-fw fa fa-chevron-left');
        };
    });
    $("#navlist").find("div span").on('click', function() {
        // console.log('type',$(this).attr('data-type'));
        var ul = $(this).siblings("ul");
        if ($(this).attr('data-type') == 1 && ul.find("div").html() != null) {
            if (ul.css("display") == "none") {
                ul.show(300);
                $(this).children('i').attr('class', 'icon-fw fa fa-chevron-down');
                //另一ul隐藏,span 去除样式,+-替换
                ul.parent().siblings().children('ul').hide(300);
                //如果图标有且正确
                // if (ul.parent().siblings('div').children('span').children('i').attr('class') == 'icon-fw fa fa-chevron-down' || ul.parent().siblings('div').children('span').children('i').attr('class') == 'icon-fw fa fa-chevron-left') {
                //     ul.parent().siblings('div').children('span').children('i').attr('class', 'icon-fw fa fa-chevron-left');
                // };
                // console.log('11', $(this).parent('div').siblings('div').children('span').children('i').attr('class'))
                // ul.parent().siblings().children('ul').siblings('span').children('i').attr('class','icon-fw fa fa-chevron-left');

            } else {
                $(this).children('i').attr('class', 'icon-fw fa fa-chevron-left');
                ul.hide(300);
            };
        } else {
            ul.hide(300);
            // $(this).children('i').attr('class', 'icon-fw fa fa-chevron-left');
        };
        
        // if (ul.find("div").html() != null) {
        //     if (ul.css("display") == "none") {
        //         ul.show(300);
        //         $(this).children('i').attr('class', 'icon-fw fa fa-chevron-down');
        //         //另一ul隐藏,span 去除样式,+-替换
        //         ul.parent().siblings().children('ul').hide(300);
        //         ul.parent().siblings().children('ul').children('div').children('span').removeClass('active');
        //         //如果图标有且正确
        //         if (ul.parent().siblings('div').children('span').children('i').attr('class') == 'icon-fw fa fa-chevron-down' || ul.parent().siblings('div').children('span').children('i').attr('class') == 'icon-fw fa fa-chevron-left') {
        //             ul.parent().siblings('div').children('span').children('i').attr('class', 'icon-fw fa fa-chevron-left');
        //         };
        //     } else {
        //         ul.hide(300);
        //         $(this).children('i').attr('class', 'icon-fw fa fa-chevron-left');
        //     }
        // }
    });
};
/*
 *页面iframe跳转
 */
function goPage(url, e, id, type) {
    $(e).parent().attr('data-type', type)
        //加上点击样式
    $(e).parent('span').addClass('active');
    $(e).parent().parent().siblings().children('span').removeClass('active');
    //如果有子集
    $(e).parent().parent().siblings().children().children().children('span').removeClass('active');
    $(e).parent().parent().parent().parent().siblings().children('span').removeClass('active');
    //页面跳转
    var nurl = url;
    // if (nurl == '#') {
    //     // $('#ifm').prop('src', '/pages/test');
    //     return;
    // };
    if (type == 1) { //目录节点不做跳转页面
        return;
    };
    $('#ifm').prop('src', nurl);
    console.log('pages', nurl);
    //局部刷新iframe
    // loadIframe(nurl);

    //2018-08-13权限按钮显隐
    $('#menuId').val(id);
};

/*
 *设置局部刷新iframe
 */
// function loadIframe(url) {
//     //获取url链接
//     var u = window.location.href;
//     //因为每次获取的链接中都有之前的旧锚点,
//     //所以需要把#之后的旧锚点去掉再来加新的锚点(即传入的url参数)
//     var end = u.indexOf("#");
//     var rurl = u.substring(0, end);
//     //设置新的锚点
//     window.location.href = rurl + "#" + url;
// };
// var hash = location.hash;
// var url = hash.substring(1, hash.length);
// $("#ifm").attr("src", url);
/*
 *菜单添加---暂存
 */
function menuAdd() {
    var menuAddParams = {
        name: '企业安全员',
        type: 0,
        url: '/user',
        path: '/pages/gridUserPageqy',
        icon: 'layui-icon-list',
        enabled: true,
        parentId: 1,
        sortNum: 8
    };
    ajaxJson('POST', urls + '/menu/add', JSON.stringify(menuAddParams), function(err, rsp) {
        if (rsp.code == 200) {
            console.log(rsp);
            // layer.msg(rsp.message);
        } else {
            // layer.msg(rsp.message);
            console.log(rsp.message);
        }
    }, true);
};
/*
 *菜单修改---暂存
 */
function menuUpdate() {
    var menuAddParams = {
        id: 61,
        name: '文件资料管理',
        type: 0,
        url: '#',
        path: 'pages/docPage',
        icon: 'layui-icon-list',
        enabled: true,
        parentId: 39,
        sortNum: 2
    };
    ajaxJson('POST', urls + '/menu/update', JSON.stringify(menuAddParams), function(err, rsp) {
        if (rsp.code == 200) {
            console.log(rsp);
            // layer.msg(rsp.message);
        } else {
            // layer.msg(rsp.message);
            console.log(rsp.message);
        }
    }, true);
};
/*
 *菜单删除---暂存
 */
function menuDel() {
    ajaxJson('POST', urls + '/menu/del?id=' + 66, '', function(err, rsp) {
        if (rsp.code == 200) {
            console.log(rsp);
            // layer.msg(rsp.message);
        } else {
            // layer.msg(rsp.message);
            console.log(rsp.message);
        }
    }, true);
};

/*
 *其他公共事件
 */
// $(function() {
//     /*
//      *调用公共事件
//      */
//     //layui使用
//     ;
//     ! function() {
//         var layer = layui.layer,
//             form = layui.form;
//     }();
//     //获取用户信息
//     getInfo();
//     //加载主题菜单
//     getTopicMenu();
//     //菜单添加
//     // menuAdd()
//     //菜单修改
//     // menuUpdate();
//     //菜单删除
//     // menuDel();
//     //
//     //-------------------------------------------------------功能函数

//     /*
//      *标题名称
//      */
//     $('.title').text('浦江后台管理系统');
//     /*
//      *个人信息
//      */
//     //弹框显隐
//     $('.info-box-title').on('click', function() {
//         $('.info-box-nav').toggle('fast');
//     });
//     //取消显示
//     $('.info-box-nav ul li').on('click', function() {
//         $('.info-box-nav').hide();
//     });

// });
/*
 *调用公共事件
 */
//layui使用
;
! function() {
    var layer = layui.layer,
        form = layui.form;
}();
//获取用户信息
getInfo();
//加载主题菜单
getTopicMenu();
//菜单添加
// menuAdd()
//菜单修改
// menuUpdate();
//菜单删除
// menuDel();
//
//-------------------------------------------------------功能函数

/*
 *标题名称
 */
// $('.title').text('智慧管控平台');
$('.title span').on('click', function() {
    window.location.reload();
});
//菜单收缩
var expanded = true;
$('.title-icon').on('click',function(){
    if(expanded){
        $('.navlist').css('position','relative');
        $('.navlist').animate({left:'-230px'});
        $('.navlist-wrap').animate({left:'-230px'});
        $('.header .title').css('position','relative');
        $('.header .title').animate({left:'-200px'});
        $('.content-wrap').animate({left:'0px'});
        $('.title-icon').css('background-position','-25px 0px');
    }else{
        $('.navlist').css('position','relative');
        $('.navlist').animate({left:'0px'});
        $('.navlist-wrap').animate({left:'0px'});
        $('.header .title').css('position','relative');
        $('.header .title').animate({left:'0px'});
        $('.content-wrap').animate({left:'230px'});
        $('.title-icon').css('background-position','0px 0px');
    }
    expanded = !expanded;
});
/*
 *个人信息
 */
//弹框显隐
$('.info-box-title').on('click', function() {
    $('.info-box-nav').toggle('fast');
});
//取消显示
$('.info-box-nav ul li').on('click', function() {
    $('.info-box-nav').hide();
});

 

posted @ 2019-04-29 18:56  abcByme  阅读(62)  评论(0编辑  收藏  举报