zhou zhou
3 天以前 0a1d91e42e6c5af96e1108e9ebcc37e99eb3b22c
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
<template>
  <div class="art-full-height">
    <ArtSearchBar
      v-model="searchForm"
      :items="searchItems"
      :showExpand="false"
      @search="handleSearch"
      @reset="handleReset"
    />
 
    <ElCard class="art-table-card">
      <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData" />
 
      <ArtTable
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination="pagination"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      />
    </ElCard>
  </div>
</template>
 
<script setup>
  import { fetchGetUserLoginList } from '@/api/system-manage'
  import { useTable } from '@/hooks/core/useTable'
  import { ElTag } from 'element-plus'
  import { useI18n } from 'vue-i18n'
  import { createUserLoginApiParams, userLoginPaginationKey } from './userLoginTable.config'
 
  defineOptions({ name: 'UserLogin' })
  const { t } = useI18n()
 
  const LOGIN_TYPE_MAP = {
    0: { labelKey: 'pages.system.userLogin.types.loginSuccess', type: 'success' },
    1: { labelKey: 'pages.system.userLogin.types.loginFailed', type: 'danger' },
    2: { labelKey: 'pages.system.userLogin.types.logout', type: 'info' },
    3: { labelKey: 'pages.system.userLogin.types.tokenRenew', type: 'warning' }
  }
 
  const initialSearchForm = {
    token: '',
    ip: '',
    type: void 0,
    system: ''
  }
 
  const searchForm = ref({ ...initialSearchForm })
 
  const searchItems = computed(() => [
    {
      label: t('pages.system.userLogin.search.token'),
      key: 'token',
      type: 'input',
      props: {
        placeholder: t('pages.system.userLogin.search.tokenPlaceholder'),
        clearable: true
      }
    },
    {
      label: t('pages.system.userLogin.search.ip'),
      key: 'ip',
      type: 'input',
      props: {
        placeholder: t('pages.system.userLogin.search.ipPlaceholder'),
        clearable: true
      }
    },
    {
      label: t('pages.system.userLogin.search.system'),
      key: 'system',
      type: 'input',
      props: {
        placeholder: t('pages.system.userLogin.search.systemPlaceholder'),
        clearable: true
      }
    },
    {
      label: t('pages.system.userLogin.search.type'),
      key: 'type',
      type: 'select',
      props: {
        placeholder: t('pages.system.userLogin.search.typePlaceholder'),
        clearable: true,
        options: Object.entries(LOGIN_TYPE_MAP).map(([value, item]) => ({
          label: t(item.labelKey),
          value: Number(value)
        }))
      }
    }
  ])
 
  const getLoginTypeConfig = (type) => {
    return (
      LOGIN_TYPE_MAP[type] || {
        label: type ?? t('common.placeholder.empty'),
        type: 'info'
      }
    )
  }
 
  const getUserDisplayName = (row) => {
    return row.userId$ || row.userName || row.nickname || row.username || row.userId || '-'
  }
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    getData,
    resetSearchParams,
    replaceSearchParams,
    handleSizeChange,
    handleCurrentChange,
    refreshData
  } = useTable({
    core: {
      apiFn: fetchGetUserLoginList,
      apiParams: createUserLoginApiParams(searchForm.value),
      paginationKey: userLoginPaginationKey,
      columnsFactory: () => [
        { type: 'index', width: 60, label: t('table.index') },
        { prop: 'id', label: t('table.id'), minWidth: 90 },
        {
          prop: 'userDisplayName',
          label: t('pages.system.userLogin.table.user'),
          minWidth: 140,
          formatter: (row) => getUserDisplayName(row)
        },
        { prop: 'token', label: t('pages.system.userLogin.table.token'), minWidth: 260, showOverflowTooltip: true },
        { prop: 'ip', label: t('pages.system.userLogin.table.ip'), minWidth: 140 },
        {
          prop: 'type',
          label: t('pages.system.userLogin.search.type'),
          width: 120,
          formatter: (row) => {
            const config = getLoginTypeConfig(row.type)
            return h(ElTag, { type: config.type }, () => config.labelKey ? t(config.labelKey) : config.label)
          }
        },
        { prop: 'system', label: t('pages.system.userLogin.table.system'), minWidth: 140 },
        {
          prop: 'createTime',
          label: t('table.createTime'),
          minWidth: 180,
          sortable: true,
          formatter: (row) => row.createTime$ || row.createTime || t('common.placeholder.empty')
        }
      ]
    }
  })
 
  const handleSearch = (params) => {
    replaceSearchParams(params)
    getData()
  }
 
  const handleReset = () => {
    Object.assign(searchForm.value, { ...initialSearchForm })
    resetSearchParams()
  }
</script>