forked from kidgrow-microservices-platform

zhaoxiaohao
2020-05-14 1738edbe080a76d6954f90a6306ebe372627c6d4
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
<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="product-search-value" class="layui-input search-input" type="text" placeholder="输入产品名" />&emsp;
            <button id="product-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="product-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>
        <table class="layui-table" id="product-table" lay-filter="product-table"></table>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="product-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="product-tpl-state">
    <input type="checkbox" lay-filter="product-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="禁用|启用"
           {{d.enabled==true?'checked':''}}/>
</script>
 
<script>
    layui.use(['table', 'util', 'admin', 'config', 'form'], function () {
        let table = layui.table;
        let util = layui.util;
        let admin = layui.admin;
        let form = layui.form;
        let laydate = layui.laydate;
        let config = layui.config;
 
        // 渲染表格
        var renderTable = function () {
            table.render({
                elem: '#product-table',
                url: config.base_server + 'api-opration/product',
                method: 'GET',
                headers: { 'Authorization': 'Bearer ' + config.getToken().access_token },
                page: true,
                cols: [[
                    { field: 'proName', width: 300, title: '产品名称' },
                    {
                        field: 'proType',align: 'center', width: 100, templet: function (d) {
                            if (d.proType === 0)
                                return "试用"
                            else
                                return "正式"
                        }, title: '类型'
                    },
                    {
                        field: 'proTime', width: 150, title: '有效时长', templet: function (d) {
                            switch (d.proTimeUnit) {
                                case 0:
                                    return d.proTime + "天";
                                    break;
                                case 1:
                                    return d.proTime + "月";
                                    break;
                                case 2:
                                    return d.proTime + "年";
                                    break;
                            }
                        }
                    },
                    { field: 'proPrice', width: 100, title: '价格' },
                    { field: 'recordCount', width: 180, title: '报告数' },
                    { field: 'ailightCount', width: 180, title: '读片量' },
                    { field: 'enabled', width: 150, templet: '#product-tpl-state', sort: true, title: '状态' },
                    // { field: 'createTime', width: 200, sort: true, title: '创建时间' },
                    { field: 'createTime', width: 250, sort: true, title: '创建时间', templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy/MM/dd HH:mm')}}</div>" },
                    { align: 'center', width: 100, toolbar: '#product-table-bar', title: '操作' }
                ]]
            });
        }
        renderTable();
 
        // 修改状态
        form.on('switch(product-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-opration/product/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: 500 });
                } else {
                    layer.msg(data.msg, { icon: 2, time: 500 });
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
 
        // 工具条点击事件
        table.on('tool(product-table)', function (obj) {
            if (obj.event === 'del') { // 删除
                layer.confirm('确定要删除吗?', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-opration/product/' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        layer.msg(data.msg, { icon: 1, time: 500 }, function () {
 
                            renderTable();
                        });
                        obj.del();
                    }, 'DELETE');
                });
            }
        });
 
        // 搜索按钮点击事件
        $('#product-btn-search').click(function () {
            var proName = $('#product-search-value').val();
            table.reload('product-table', { where: { proName: proName } });
        });
        // 添加按钮点击事件
        $('#product-btn-add').click(function () {
            admin.popupCenter({
                title: '添加产品',
                path: 'pages/opration/product_form.html',
                finish: function () {
                    renderTable();
                }
            })
        });
    });
</script>