sysPermission.js 12 KB

let action = null;
let app = null;
layui.config({
    base: "/js/",
    version: 55
}).use(['ztree','system'], function () {
    var form = layui.form,
        $ = layui.jquery,
        system = layui.system,
        enums = layui.enums,
        table = layui.table,
        u = new system.u(),
        layuimini = layui,
        areaName = "",
        tableName = "SysPermission",
        enums = {
            enumButton: "Button",
            enumMenu: "Menu",
            enumCatalog: "Catalog",
            enumModule: "Module"
        },
        sendData = {
            PermissionType: null,
            PermissionName: null,
            PermissionCode: null,
            Id: null
        };

    action = {
        addOptions: function () {
            var options = {
                url: `/${tableName}/Ins`,
                submit: "submit(fromAdd)"
            }
            switch (sendData.PermissionType) {
                case enums.enumMenu:
                    options.fromId = "#modifyFormBtn form";
                    options.content = $("#modifyFormBtn");
                    break;
                case enums.enumCatalog:
                    options.fromId = "#modifyFormMenu form";
                    options.content = $("#modifyFormMenu");
                    break;
                case enums.enumModule:
                    options.fromId = "#modifyFormDir form";
                    options.content = $("#modifyFormDir");
                    break;
            }
            return options;
        },

        editOptions: function () {
            var options = {
                url: `/${tableName}/Upd`,
                submit: "submit(fromUpdate)"
            }
            switch (sendData.PermissionType) {
                case enums.enumMenu:
                    options.fromId = "#modifyFormBtn form";
                    options.content = $("#modifyFormBtn");
                    break;
                case enums.enumCatalog:
                    options.fromId = "#modifyFormMenu form";
                    options.content = $("#modifyFormMenu");
                    break;
                case enums.enumModule:
                    options.fromId = "#modifyFormDir form";
                    options.content = $("#modifyFormDir");
                    break;
            }
            return options;
        },

        deleteOptions: function () {
            var options = {
                url: `/${tableName}/DelByIds`,
                keyId: "Id"
            }
            return options;
        },

        exportOptions: function () {
            var options = {
                fromId: "listForm",
                url: `/${tableName}/Export`,
                isDefault: false,
                excelCols: {
                    head: app.data.cols
                }
            }
            return options;
        },

        queryOptions: function () {
            var options = {
                resetFrom: "form[lay-filter=listForm]",
                fromId: "listForm",
                urlExport: `/${tableName}/Export`,
                urlQuery: `/${tableName}/Load`,
                mainTable: app.data.tableIns
            }
            return options;
        },

        uploadOptions: function () {
            var options = {
                url: `/${tableName}/Import`
            }
            return options;
        },

        addBefore: function (callBack, parm) {
            if (sendData.PermissionType === "Button") {
                layer.alert("按钮下无法增加权限,请重新选择", { icon: 7, shade: 0.4 });
                return;
            }
            if (sendData.PermissionType !== "Menu" && sendData.PermissionType !== "Catalog" && sendData.PermissionType !== "Module") {
                layer.alert("请选择树节点新增权限", { icon: 7, shade: 0.4 });
                return;
            }
            let formObj = {
                ParentName: sendData.PermissionName,
                ParentId: sendData.Id,
                MenuName: sendData.PermissionCode
            }
            switch (sendData.PermissionType) {
                case enums.enumMenu:
                    formObj.PermissionType = enums.enumButton;
                    break;
                case enums.enumCatalog:
                    formObj.PermissionType = enums.enumMenu;
                    break;
                case enums.enumModule:
                    formObj.PermissionType = enums.enumCatalog;
                    break;
            }
            var dataAll = table.cache["mainList"];
            var maxOrderNum = Math.max.apply(Math, dataAll.map(function (o) { return o.OrderNum; }));
            formObj.OrderNum = maxOrderNum + 10;

            form.val(parm.content.selector.replace("#", ""), formObj);
            //callBack是回调函数,如果editBefore有ajax 放在成功之后
            if (callBack != null) callBack();
        },

        editBefore: function (data, callBack) {
            data.ParentName = sendData.PermissionName;
            switch (sendData.PermissionType) {
                case enums.enumMenu:
                    form.val("modifyFormBtn", data);
                    break;
                case enums.enumCatalog:
                    form.val("modifyFormMenu", data);
                    break;
                case enums.enumModule:
                    form.val("modifyFormDir", data);
                    break;
            }
            //callBack是回调函数,如果editBefore有ajax 放在成功之后
            if (callBack != null) callBack();
        },

        editSaveBefore: function (dataLayuiFrom, callBack) {
            dataLayuiFrom.field.Visible = dataLayuiFrom.field.Visible == "1" ? true : false;
            if (callBack != null) callBack();
        },

        addSaveBefore: function (data, callBack) {
            if (sendData.PermissionType === "Button") {
                layer.alert("按钮下无法增加权限,请重新选择", { icon: 2, shadeClose: true, title: "错误信息" });
                return;
            }
            if (sendData.PermissionType !== "Menu" && sendData.PermissionType !== "Catalog" && sendData.PermissionType !== "Module") {
                layer.alert("请选择树节点新增权限", { icon: 6, shade: 0.4, time: u.config.errorTime });
                return;
            }
            data.field.Visible = data.field.Visible == "1" ? true : false;
            if (callBack != null) callBack();
        },

        //所有动作成功之后
        actionSuccess: (falg, toolbarId) => {

        }
    }
    
    app = {
        data: {
            cols: [[
                { checkbox: true, fixed: true },
                { field: "Id", width: 80, hide: true, title: "Id" },
                { field: "PermissionCode", width: 150, title: "编码" },
                { field: "PermissionName", width: 150, title: "权限名称"},
                { field: "PermissionType", width: 80, title: "类型", templet: (d) => { return "".GetState(window.top.EnumPermissionType, d.PermissionType) }  },
                { field: "WebUrl", width: 150, title: "Url路径" },
                { field: "Icon", width: 80, hide: true, title: "Icon图标" },

                { field: "Visible", width: 80, title: "显示", templet: (d) => { return "".GetState(window.top.EnumIsVisible, d.Visible) } },
                { field: "OrderNum", width: 80, title: "排序" },
                { field: "Url", width: 150, title: "样式" },

                { field: 'Created', width: 150, title: '创建时间' },
                { field: "CreatedBy", width: 150,  title: "创建人" },
                { field: "Updated", width: 150,  title: "更新时间" },
                { field: "UpdatedBy", width: 150,  title: "更新人" },
                { field: "ParentId", width: 80, hide: true, title: "上级Id" },
                { field: "MenuType", width: 80, hide: true, title: "模块类型" }
            ]],
            urlTable: `/${tableName}/Load`,
            tableIns: null,
            tableElem: "mainList",

            tree: $("#tree"),
            treeObj: null,
            nodes: null,
            setting: {
                view: { selectedMulti: false },
                async: {
                    enable: true,
                    url: '/SysPermission/GetAllPermission',
                    type: "get"
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "Id",
                        pIdKey: "ParentId",
                        rootPId: ""
                    }
                },
                callback: {
                    onClick: function (event, treeId, treeNode) {
                        sendData.PermissionType = treeNode.PermissionType;
                        sendData.PermissionName = treeNode.PermissionName;
                        sendData.PermissionCode = treeNode.PermissionCode;
                        sendData.Id = treeNode.Id;
                        app.methods.initTable({ Id: sendData.Id });
                    },
                    onAsyncSuccess: function (event, treeId, treeNode, msg) {
                        app.data.treeObj.expandAll(true);
                        let node = null;
                        if (app.data.nodes != null && app.data.nodes.length > 0) {
                            node = app.data.nodes[0];
                        } else {
                            node = app.data.treeObj.getNodeByParam('level', 0); // 获取根节点
                        }
                        app.data.treeObj.selectNode(node); // 选择节点
                        sendData.PermissionType = node.PermissionType;
                        sendData.PermissionName = node.PermissionName;
                        sendData.PermissionCode = node.PermissionCode;
                        sendData.Id = node.Id;
                        setTimeout(function () {
                            app.methods.initTable({ Id: sendData.Id });
                        }, 300);
                    }
                }
            }
        },
        methods: {
            initTable: function (options) {
                var config = {};
                if (options != undefined) $.extend(config, options);
                let opt = {
                    cols: u.columnRecord(app.data.tableElem, app.data.cols),
                    elem: "#" + app.data.tableElem,
                    url: app.data.urlTable,
                    toolbar: '#toolbarTable',
                    where: config,
                    menuModule: tableName
                }
                app.data.tableIns = u.initTable(opt);
            },
            initTree: function () {
                setTimeout(() => {
                    if (app.data.treeObj != null) {
                        app.data.nodes = app.data.treeObj.getSelectedNodes();
                    }
                    app.data.treeObj = $.fn.zTree.init(app.data.tree, app.data.setting);
                    app.data.tree.height($("div.layui-table-view").height() - 12);
                }, 50);
            }
        },
        registerEvent: function () {
            var IconNameindex;
            var IconInputObj;
            $('.IconName').on('click', function () {
                IconInputObj = this;
                IconNameindex = layer.open({
                    maxmin: true,
                    title: "图标选择",
                    area: ["500px", "400px"],
                    type: 1,
                    content: $('#divIconNameEdit'),
                    success: function (layero, index) {
                        layer.full(index);
                    }
                });
            });

            $('#mfromIconNameEdit .iconfont-list .icon').on('click', function () {
                var type = $(this).next().next().html();
                $(IconInputObj).val("&" + type.replace("&", ""));
                layer.close(IconNameindex);
            });
        },
        init: function () {
            app.methods.initTable({ Id: ""});
            app.methods.initTree();
            app.registerEvent(); 
        }
    };
    app.init();
})