forked from kidgrow-microservices-platform

zhaoxiaohao
2020-08-18 56aaf185c6c906f74549a7551b29b1b107d69819
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">菜单管理</h2>
        <span class="layui-breadcrumb pull-right">
            <a href="#!home_console">首页</a>
            <a><cite>菜单管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            所属应用:<select id="menu_clients" lay-filter="menu_clients"></select>
            &emsp;搜索:<input id="menus-edit-search" class="layui-input search-input" type="text"
                placeholder="按关键字" />&emsp;
            <button id="menus-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="menus-btn-add" class="layui-btn icon-btn permissions" permissions="saveOrUpdate"><i class="layui-icon">&#xe654;</i>添加</button>
            <button id="menus-btn-expand" class="layui-btn">全部展开</button>
            <button id="menus-btn-fold" class="layui-btn">全部折叠</button>
        </div>
 
        <!-- 数据表格 -->
        <table class="layui-table" id="menus-table" lay-filter="menus-table"></table>
    </div>
</div>
 
<!-- 表格操作列 -->
<script type="text/html" id="menus-state">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">下级</a>-->
<a class="layui-btn layui-btn-primary layui-btn-xs permissions" permissions="saveOrUpdate" lay-event="edit">修改</a>
<a class="layui-btn layui-btn-danger layui-btn-xs permissions" permissions="menus-del" lay-event="del">删除</a>
</script>
 
 
<!-- 表格状态列 -->
<script type="text/html" id="menu-tpl-state">
    <input type="checkbox" lay-filter="menu-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>
 
<script>
 
    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects', 'treetable'], function () {
        let table = layui.table;
        let layer = layui.layer;
        let admin = layui.admin;
        let treetable = layui.treetable;
        let config = layui.config;
        let form = layui.form;
 
        // 渲染表格
        var renderTable = function (param) {
            if (param) {
                param.access_token = config.getToken().access_token;
            } else {
                param = {
                    access_token: config.getToken().access_token
                };
            }
            treetable.render({
                treeColIndex: 1,
                treeSpid: -1,
                treeIdName: 'id',
                treePidName: 'parentId',
                elem: '#menus-table',
                url: config.base_server + 'api-user/menus/findAlls',
                where: param,
                page: false,
                cols: [[
                    { type: 'numbers' ,width: "10%" },
                    { field: 'name',width: "10%", minWidth: 200, title: '菜单名称' },
                    { field: 'tenantId',width: "10%", minWidth: 80, title: '应用标识' },
                    { field: 'url',width: "10%", title: '菜单url' },
                    { field: 'path',width: "10%", title: '菜单path' },
                    { field: 'css',width: "10%", align: 'center', title: '样式' },
                    { field: 'sort', width: "10%", align: 'center', title: '排序号' },
                    {
                        field: 'type', width: "10%", align: 'center', templet: function (d) {
                            if (d.type === 2) {
                                return '<span class="layui-badge layui-bg-gray">资源</span>';
                            }
                            if (d.parentId === -1) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '类型'
                    },
                    { field: 'enabled', width: "10%", sort: true, templet: '#menu-tpl-state', title: '状态' },
                    { templet: '#menus-state', width: "10%", align: 'center', title: '操作' }
                ]],
                done: function (res) {
                    permissionsInput(res,config);
                }
            });
        };
        renderTable({ tenantId: config.clientId });
 
        // 获取应用列表
        layer.load(2);
        admin.req('api-uaa/clients/all', {}, function (data) {
            layer.closeAll('loading');
            if (0 === data.code) {
                let selected = false;
                $.each(data.data, function (index, item) {
                    if (config.clientId === item.clientId) {
                        selected = true;
                    } else {
                        selected = false;
                    }
                    //往下拉菜单里添加元素
                    $('#menu_clients').append(new Option(item.clientName, item.clientId, false, selected));
                })
                form.render();
            } else {
                layer.msg(data.msg, { icon: 2, time: 2000 });
            }
        }, 'GET');
 
        // 修改menu状态
        form.on('switch(menu-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-user/menus/updateEnabled', {
                id: obj.elem.value,
                enabled: obj.elem.checked ? true : false
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, { icon: 1, time: 2000 });
                } else {
                    layer.msg(data.msg, { icon: 2, time: 2000 });
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
 
        // 工具条点击事件
        table.on('tool(menus-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
 
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('确定删除此菜单吗?', function () {
                    layer.load(2);
                    let tenantId = $('#menu_clients').val();
                    admin.req('api-user/menus/' + obj.data.id + '?tenantId=' + tenantId, {}, function (data) {
                        layer.closeAll('loading');
                        if (0 === data.code) {
                            layer.msg(data.msg, { icon: 1, time: 2000 });
                            renderTable({ tenantId: tenantId });
                        } else {
                            layer.msg(data.msg, { icon: 2, time: 2000 });
                        }
                    }, 'DELETE');
                });
            }
        });
 
        // 显示编辑弹窗
        var showEditModel = function (data) {
            let tenantId = $('#menu_clients').val();
            let title = data ? '修改菜单' : '添加菜单';
            if (data) {
                data.type = data.type.toString();
                data.hidden = data.hidden.toString();
            } else {
                data = {};
            }
            data.tenantId = tenantId;
            admin.putTempData('t_menus', data);
 
            admin.popupCenter({
                title: title,
                path: 'pages/system/menus_form.html',
                finish: function () {
                    renderTable({ tenantId: tenantId });
                }
            });
        };
 
        // 添加按钮点击事件
        $('#menus-btn-add').click(function () {
            showEditModel();
        });
 
        // 搜索按钮点击事件
        $('#menus-btn-search').click(function () {
            var keyword = $('#menus-edit-search').val();
            var searchCount = 0;
            $('#menus-table').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
                $(this).css('background-color', 'transparent');
                var text = $(this).text();
                if (keyword !== '' && text.indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount === 0) {
                        $('.layui-tab-item.layui-show').stop(true);
                        $('.layui-tab-item.layui-show').animate({ scrollTop: $(this).offset().top - 150 }, 500);
                    }
                    searchCount++;
                }
            });
            if (keyword !== '' && searchCount === 0) {
                layer.msg("没有匹配结果", { icon: 5, time: 2000 });
            } else {
                treetable.expandAll('#menus-table');
            }
        });
 
        $('#menus-btn-expand').click(function () {
            treetable.expandAll('#menus-table');
        });
 
        $('#menus-btn-fold').click(function () {
            treetable.foldAll('#menus-table');
        });
 
        // 应用下来框点击事件
        form.on('select(menu_clients)', function (data) {
            renderTable({ tenantId: data.value });
        });
    });
</script>