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
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">token管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>token管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
        所属应用:<select id="token_clients" lay-filter="token_clients"></select>
        &emsp;搜索:<input id="tokens-edit-search" class="layui-input search-input" type="text" placeholder="输入用户名"/>&emsp;
        <button id="tokens-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>
 
        <!-- 数据表格 -->
        <table class="layui-table" id="tokens-table" lay-filter="tokens-table"></table>
    </div>
</div>
 
<!-- 表格操作列 -->
<script type="text/html" id="tokens-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs permissions" permissions="token-del" lay-event="del">删除</a>
</script>
 
<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'],function () {
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let util = layui.util;
        let admin = layui.admin;
        let form = layui.form;
 
        // 渲染表格
        table.render({
            elem: '#tokens-table',
            url: config.base_server + 'api-uaa/tokens',
            method: 'GET',
            headers:{'Authorization': 'Bearer ' + config.getToken().access_token},
            where: {tenantId: config.clientId},
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'tokenValue',width:300, sort: true, title: 'token'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.expiration);
                    }, title: '到期时间'
                },
                {field: 'username',width:180, sort: true, title: '用户名'},
                {field: 'grantType',width:180, sort: true, title: '授权类型'},
                {field: 'clientId',width:180, sort: true, title: '所属应用'},
                {align: 'center',width:100, toolbar: '#tokens-table-bar', title: '操作'}
            ]],
            done:function(){
                permissionsInput();
            }
        });
 
        // 获取应用列表
        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;
                    }
                    //往下拉菜单里添加元素
                    $('#token_clients').append(new Option(item.clientName, item.clientId, false, selected));
                })
                form.render();
            } else {
                layer.msg(data.msg, {icon: 2, time: 500});
            }
        }, 'GET');
 
        // 工具条点击事件
        table.on('tool(tokens-table)', function (obj) {
            if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });
 
        // 删除
        let doDelete = function (obj) {
            layer.confirm('确定要删除吗?', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-uaa/oauth/remove/token?token=' + obj.data.tokenValue, {}, function (data) {
                    layer.closeAll('loading');
                    layer.msg('成功', {icon: 1, time: 500});
                    obj.del();
                }, 'DELETE');
            });
        };
 
        // 搜索按钮点击事件
        $('#tokens-btn-search').click(function () {
            var key = $('#tokens-edit-search').val();
            table.reload('tokens-table', {where: {username: key, tenantId: $('#token_clients').val()}});
        });
 
        // 应用下来框点击事件
        form.on('select(token_clients)', function(data){
            table.reload('tokens-table', {where: {tenantId: data.value}});
        });
    });
</script>