zhou zhou
5 天以前 aaf8a50511d77dbc209ca93bbba308c21179a8bc
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<template>
  <div class="config-page 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">
        <template #left>
          <ElSpace wrap>
            <ElButton v-auth="'add'" @click="showDialog('add')" v-ripple>新增配置</ElButton>
            <ElButton
              v-auth="'delete'"
              type="danger"
              :disabled="selectedRows.length === 0"
              @click="handleBatchDelete"
              v-ripple
            >
              批量删除
            </ElButton>
          </ElSpace>
        </template>
      </ArtTableHeader>
 
      <ArtTable
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination="pagination"
        @selection-change="handleSelectionChange"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      />
 
      <ConfigDialog
        v-model:visible="dialogVisible"
        :config-data="currentConfigData"
        @submit="handleDialogSubmit"
      />
 
      <ConfigDetailDrawer
        v-model:visible="detailDrawerVisible"
        :loading="detailLoading"
        :detail-data="detailData"
      />
    </ElCard>
  </div>
</template>
 
<script setup>
  import { ElMessage } from 'element-plus'
  import { useAuth } from '@/hooks/core/useAuth'
  import { useTable } from '@/hooks/core/useTable'
  import { useCrudPage } from '@/views/system/common/useCrudPage'
  import {
    fetchConfigPage,
    fetchDeleteConfig,
    fetchGetConfigDetail,
    fetchSaveConfig,
    fetchUpdateConfig
  } from '@/api/system-manage'
  import ConfigDialog from './modules/config-dialog.vue'
  import ConfigDetailDrawer from './modules/config-detail-drawer.vue'
  import { createConfigTableColumns } from './configTable.columns'
  import {
    buildConfigDialogModel,
    buildConfigPageQueryParams,
    buildConfigSavePayload,
    buildConfigSearchParams,
    createConfigSearchState,
    getConfigPaginationKey,
    getConfigTypeOptions,
    normalizeConfigListRow
  } from './configPage.helpers'
 
  defineOptions({ name: 'Config' })
 
  const { hasAuth } = useAuth()
  const searchForm = ref(createConfigSearchState())
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const detailData = ref({})
  let handleDeleteAction = null
 
  const searchItems = computed(() => [
    {
      label: '关键字',
      key: 'condition',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入配置名称'
      }
    },
    {
      label: '标识',
      key: 'flag',
      type: 'input',
      props: {
        clearable: true,
        placeholder: '请输入配置标识'
      }
    },
    {
      label: '类型',
      key: 'type',
      type: 'select',
      props: {
        clearable: true,
        options: getConfigTypeOptions()
      }
    },
    {
      label: '状态',
      key: 'status',
      type: 'select',
      props: {
        clearable: true,
        options: [
          { label: '正常', value: 1 },
          { label: '冻结', value: 0 }
        ]
      }
    }
  ])
 
  async function openDetail(row) {
    detailDrawerVisible.value = true
    detailLoading.value = true
    try {
      detailData.value = normalizeConfigListRow(await fetchGetConfigDetail(row.id))
    } catch (error) {
      detailDrawerVisible.value = false
      detailData.value = {}
      ElMessage.error(error?.message || '获取配置详情失败')
    } finally {
      detailLoading.value = false
    }
  }
 
  async function openEditDialog(row) {
    try {
      currentConfigData.value = buildConfigDialogModel(await fetchGetConfigDetail(row.id))
      dialogVisible.value = true
      dialogType.value = 'edit'
    } catch (error) {
      ElMessage.error(error?.message || '获取配置详情失败')
    }
  }
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    getData,
    replaceSearchParams,
    resetSearchParams,
    handleSizeChange,
    handleCurrentChange,
    refreshData,
    refreshCreate,
    refreshUpdate,
    refreshRemove
  } = useTable({
    core: {
      apiFn: fetchConfigPage,
      apiParams: buildConfigPageQueryParams(searchForm.value),
      paginationKey: getConfigPaginationKey(),
      columnsFactory: () =>
        createConfigTableColumns({
          handleView: openDetail,
          handleEdit: openEditDialog,
          handleDelete: hasAuth('delete') ? (row) => handleDeleteAction?.(row) : null
        })
    },
    transform: {
      dataTransformer: (records) => {
        if (!Array.isArray(records)) {
          return []
        }
        return records.map((item) => normalizeConfigListRow(item))
      }
    }
  })
 
  const {
    dialogVisible,
    dialogType,
    currentRecord: currentConfigData,
    selectedRows,
    handleSelectionChange,
    showDialog,
    handleDialogSubmit,
    handleDelete,
    handleBatchDelete
  } = useCrudPage({
    createEmptyModel: () => buildConfigDialogModel(),
    buildEditModel: (record) => buildConfigDialogModel(record),
    buildSavePayload: (formData) => buildConfigSavePayload(formData),
    saveRequest: fetchSaveConfig,
    updateRequest: fetchUpdateConfig,
    deleteRequest: fetchDeleteConfig,
    entityName: '配置',
    resolveRecordLabel: (record) => record?.name || record?.flag || record?.id,
    refreshCreate,
    refreshUpdate,
    refreshRemove
  })
  handleDeleteAction = handleDelete
 
  function handleSearch(params) {
    replaceSearchParams(buildConfigSearchParams(params))
    getData()
  }
 
  function handleReset() {
    Object.assign(searchForm.value, createConfigSearchState())
    resetSearchParams()
  }
</script>