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
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">地区管理</h2>
        <span class="layui-breadcrumb pull-right">
            <a href="#!console">首页</a>
            <a><cite>地区管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">&emsp;
            <input id="area-search-value" class="layui-input search-input" type="text" placeholder="按地区名称" />&emsp;
            <button id="area-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="area-btn-add" class="layui-btn icon-btn permissions" permissions="save-area"><i class="layui-icon">&#xe654;</i>添加</button>
            <button id="area-btn-expand" class="layui-btn">全部展开</button>
            <button id="area-btn-fold" class="layui-btn">全部折叠</button>
        </div>
 
        <table class="layui-table" id="area-table" lay-filter="area-table"></table>
    </div>
</div>
 
<!-- 表格操作列 -->
<script type="text/html" id="area-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs permissions" permissions="save-area" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs permissions" permissions="del-sysarea" lay-event="del">删除</a>
</script>
 
<!-- 表格状态列 -->
<script type="text/html" id="area-tpl-state">
    <input type="checkbox" lay-filter="area-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           {{d.enabled==true?'checked':''}}/>
</script>
 
<script>
    layui.use(['form', 'util', 'config', 'admin', 'treetable', 'table'], function () {
        let layer = layui.layer;
        let admin = layui.admin;
        let treetable = layui.treetable;
        let config = layui.config;
        let form = layui.form;
        let table = layui.table;
 
        // 渲染表格
        var renderTable = function (param) {
            layer.load(2);
            if (param) {
                param.access_token = config.getToken().access_token;
            } else {
                param = {
                    access_token: config.getToken().access_token
                };
            }
            treetable.render({
                treeColIndex: 0,
                treeSpid: -1,
                treeIdName: 'id',
                treePidName: 'areaParentId',
                elem: '#area-table',
                url: config.base_server + 'api-user/sysarea/findAlls',
                where: param,
                page: false,
                cols: [[
                    { field: 'areaName',  width: "10%",align: 'center', title: '区域名称' },
                    { field: 'areaCh',width: "10%",align: 'center', title: '拼音' },
                    { field: 'areaShortname',width: "10%",align: 'center', title: '简称' },
                    { field: 'areaCitycode',width: "10%",align: 'center', title: '区号' },
                    { field: 'areaZipcode',width: "10%",align: 'center', title: '邮编' },
                    { field: 'sort', width: "10%",align: 'center', title: '排序号' },
                    {
                        field: 'areaLeveltype', width: "10%",align: 'center', templet: function (d) {
                            switch (d.areaLeveltype) {
                                case 0:
                                    return '<span class="layui-badge layui-bg-red">国家</span>';
                                    break;
                                case 1:
                                    return '<span class="layui-badge layui-bg-blue">省/行政区</span>';
                                    break;
                                case 2:
                                    return '<span class="layui-badge layui-bg-gray">市</span>';
                                    break;
                                case 3:
                                    return '<span class="layui-badge layui-bg-green">区/县</span>';
                                    break;
                            }
                        }, title: '级别'
                    },
                    { field: 'enabled', width: "10%",align: 'center', sort: true, templet: '#area-tpl-state', title: '状态' },
                    { templet: '#area-table-bar', width: "10%",align: 'center', title: '操作' }
                ]],
                done: function (res) {
                    layer.closeAll('loading');
                    permissionsInput(res,config);
                }
            });
        };
        renderTable();
 
        // 修改状态
        form.on('switch(area-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-user/sysarea/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(area-table)', function (obj) {
            if (obj.event === 'del') { // 删除
                layer.confirm('确定要删除吗?', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-user/sysarea/' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        layer.msg(data.msg, { icon: 1, time: 800 }, function () {
                            renderTable();
                        });
                        obj.del();
                    }, 'DELETE');
                });
            }
            else if (obj.event == 'edit') {
                showEditModel(obj.data);
            }
        });
 
        // 显示编辑弹窗
        var showEditModel = function (data) {
            let title = data ? '修改区域' : '添加区域';
            if (!data) {
                data = {};
            }
            admin.putTempData('t_area', data);
 
            admin.popupCenter({
                title: title,
                path: 'pages/system/sysarea_form.html',
                finish: function () {
                    renderTable();
                }
            });
        };
 
        // 搜索按钮点击事件
        $('#area-btn-search').click(function () {
            var keyword = $('#area-search-value').val();
            var searchCount = 0;
            $('#area-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('#area-table');
            }
        });
        $('#area-btn-expand').click(function () {
            treetable.expandAll('#area-table');
        });
 
        $('#area-btn-fold').click(function () {
            treetable.foldAll('#area-table');
        });
        // 添加按钮点击事件
        $('#area-btn-add').click(function () {
            showEditModel();
        });
    });
</script>