wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
<template>
    <cl-crud ref="Crud">
        <cl-row>
            <cl-refresh-btn />
 
            <el-button
                v-permission="service.base.sys.log.permission.clear"
                type="danger"
                @click="clear"
            >
                {{ $t('清空') }}
            </el-button>
 
            <cl-filter :label="$t('日志保存天数')">
                <el-input-number
                    v-model="day"
                    controls-position="right"
                    :max="10000"
                    :min="1"
                    @change="saveDay"
                />
            </cl-filter>
 
            <cl-flex1 />
            <cl-search-key :placeholder="$t('搜索请求地址、用户昵称、ip')" />
        </cl-row>
 
        <cl-row>
            <cl-table ref="Table" />
        </cl-row>
 
        <cl-row>
            <cl-flex1 />
            <cl-pagination />
        </cl-row>
    </cl-crud>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'sys-log'
});
 
import { onMounted, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useCool } from '/@/cool';
import { useCrud, useTable } from '@cool-vue/crud';
import { useI18n } from 'vue-i18n';
 
const { service } = useCool();
const { t } = useI18n();
 
// 天数
const day = ref(1);
 
// cl-crud
const Crud = useCrud({ service: service.base.sys.log }, app => {
    app.refresh();
});
 
// cl-table
const Table = useTable({
    contextMenu: ['refresh'],
    columns: [
        {
            type: 'index',
            label: '#',
            width: 60
        },
        {
            prop: 'userId',
            label: t('用户ID'),
            minWidth: 100
        },
        {
            prop: 'name',
            label: t('用户昵称'),
            minWidth: 120
        },
        {
            prop: 'action',
            label: t('请求地址'),
            minWidth: 200,
            showOverflowTooltip: true
        },
        {
            prop: 'params',
            label: t('参数'),
            minWidth: 200,
            component: {
                name: 'cl-code-json',
                props: {
                    popover: true
                }
            }
        },
        {
            prop: 'ip',
            label: 'ip',
            minWidth: 150,
            dict: [],
            dictColor: true,
            formatter(row) {
                return row.ip.split(',');
            }
        },
        {
            prop: 'createTime',
            label: t('请求时间'),
            minWidth: 170,
            sortable: 'desc'
        }
    ]
});
 
// 保存天数
function saveDay() {
    service.base.sys.log
        .setKeep({ value: day.value })
        .then(() => {
            ElMessage.success(t('保存成功'));
        })
        .catch(err => {
            ElMessage.error(err.message);
        });
}
 
// 清空日志
function clear() {
    ElMessageBox.confirm(t('是否要清空日志?'), t('提示'), {
        type: 'warning'
    })
        .then(() => {
            service.base.sys.log
                .clear()
                .then(() => {
                    ElMessage.success(t('清空成功'));
                    Crud.value?.refresh();
                })
                .catch(err => {
                    ElMessage.error(err.message);
                });
        })
        .catch(() => null);
}
 
onMounted(() => {
    // 获取天数
    service.base.sys.log.getKeep().then(res => {
        day.value = Number(res);
    });
});
</script>