This commit is contained in:
2026-02-11 22:42:37 +08:00
parent e265bcc28d
commit b0ae1bb68c
8 changed files with 1387 additions and 574 deletions

View File

@@ -0,0 +1,370 @@
<template>
<a-modal
v-model:open="visible"
:title="isEdit ? '编辑配置' : '新增配置'"
:width="600"
:confirm-loading="loading"
@ok="handleOk"
@cancel="handleCancel"
>
<a-form ref="formRef" :model="formData" :rules="rules" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
<a-form-item label="配置分组" name="group">
<a-select v-model:value="formData.group" placeholder="请选择配置分组" allow-show-search>
<a-select-option value="system">系统设置</a-select-option>
<a-select-option value="site">站点配置</a-select-option>
<a-select-option value="upload">上传配置</a-select-option>
<a-select-option value="email">邮件配置</a-select-option>
<a-select-option value="sms">短信配置</a-select-option>
<a-select-option value="other">其他</a-select-option>
<template #notFoundContent>
<div style="text-align: center">
<a-input v-model:value="customGroup" placeholder="输入新分组" style="margin-bottom: 8px" />
<a-button type="primary" size="small" @click="handleAddCustomGroup">添加</a-button>
</div>
</template>
</a-select>
</a-form-item>
<a-form-item label="配置键" name="key">
<a-input v-model:value="formData.key" placeholder="请输入配置键site_name" :disabled="isEdit && formData.is_system" />
<div style="color: #999; font-size: 12px; margin-top: 4px">唯一标识建议使用英文下划线命名</div>
</a-form-item>
<a-form-item label="配置名称" name="name">
<a-input v-model:value="formData.name" placeholder="请输入配置名称" />
</a-form-item>
<a-form-item label="数据类型" name="type">
<a-select v-model:value="formData.type" placeholder="请选择数据类型">
<a-select-option value="string">字符串</a-select-option>
<a-select-option value="text">文本</a-select-option>
<a-select-option value="number">数字</a-select-option>
<a-select-option value="boolean">布尔值</a-select-option>
<a-select-option value="select">下拉框</a-select-option>
<a-select-option value="radio">单选框</a-select-option>
<a-select-option value="checkbox">多选框</a-select-option>
<a-select-option value="file">文件</a-select-option>
<a-select-option value="json">JSON</a-select-option>
</a-select>
</a-form-item>
<a-form-item
v-if="['select', 'radio', 'checkbox'].includes(formData.type)"
label="可选值"
name="options"
>
<a-textarea
v-model:value="optionsText"
placeholder="每行一个选项格式label:value&#10;例如:&#10;正常:1&#10;禁用:0"
:rows="4"
/>
</a-form-item>
<a-form-item label="配置值" name="value">
<!-- 字符串输入 -->
<a-input
v-if="formData.type === 'string'"
v-model:value="formData.value"
placeholder="请输入配置值"
/>
<!-- 文本域 -->
<a-textarea
v-else-if="formData.type === 'text'"
v-model:value="formData.value"
placeholder="请输入配置值"
:rows="4"
/>
<!-- 数字输入 -->
<a-input-number
v-else-if="formData.type === 'number'"
v-model:value="formData.value"
placeholder="请输入数字"
style="width: 100%"
/>
<!-- 布尔值 -->
<a-switch
v-else-if="formData.type === 'boolean'"
v-model:checked="formData.value"
checked-children=""
un-checked-children=""
/>
<!-- 下拉框/单选框/多选框 -->
<a-select
v-else-if="['select', 'radio'].includes(formData.type)"
v-model:value="formData.value"
placeholder="请选择配置值"
allow-clear
>
<a-select-option v-for="opt in parsedOptions" :key="opt.value" :value="opt.value">
{{ opt.label }}
</a-select-option>
</a-select>
<a-checkbox-group
v-else-if="formData.type === 'checkbox'"
v-model:value="formData.value"
>
<a-checkbox v-for="opt in parsedOptions" :key="opt.value" :value="opt.value">
{{ opt.label }}
</a-checkbox>
</a-checkbox-group>
<!-- 文件上传 -->
<sc-upload
v-else-if="formData.type === 'file'"
v-model="formData.value"
:limit="1"
accept="*/*"
list-type="picture-card"
/>
<!-- JSON编辑器 -->
<a-textarea
v-else-if="formData.type === 'json'"
v-model:value="formData.value"
placeholder='请输入JSON格式数据例如{"key": "value"}'
:rows="4"
/>
</a-form-item>
<a-form-item label="排序" name="sort">
<a-input-number v-model:value="formData.sort" placeholder="请输入排序" :min="0" style="width: 100%" />
</a-form-item>
<a-form-item label="状态" name="status">
<a-radio-group v-model:value="formData.status">
<a-radio :value="1">启用</a-radio>
<a-radio :value="0">禁用</a-radio>
</a-radio-group>
</a-form-item>
<a-form-item label="描述" name="description">
<a-textarea v-model:value="formData.description" placeholder="请输入配置描述" :rows="2" />
</a-form-item>
</a-form>
</a-modal>
</template>
<script setup>
import { ref, reactive, computed, watch } from 'vue'
import { message } from 'ant-design-vue'
import { systemApi } from '@/api/system'
const props = defineProps({
visible: Boolean,
record: {
type: Object,
default: null
}
})
const emit = defineEmits(['update:visible', 'success'])
const formRef = ref(null)
const loading = ref(false)
const customGroup = ref('')
const optionsText = ref('')
// 是否编辑模式
const isEdit = computed(() => !!props.record?.id)
// 表单数据
const formData = reactive({
group: 'system',
key: '',
name: '',
type: 'string',
value: '',
options: null,
sort: 0,
status: 1,
description: '',
is_system: false
})
// 表单验证规则
const rules = {
group: [{ required: true, message: '请选择配置分组', trigger: 'change' }],
key: [{ required: true, message: '请输入配置键', trigger: 'blur' }],
name: [{ required: true, message: '请输入配置名称', trigger: 'blur' }],
type: [{ required: true, message: '请选择数据类型', trigger: 'change' }],
value: [{ required: true, message: '请输入配置值', trigger: 'change' }]
}
// 解析选项
const parsedOptions = computed(() => {
if (!optionsText.value) return []
try {
return optionsText.value
.split('\n')
.filter((line) => line.trim())
.map((line) => {
const [label, value] = line.split(':').map((s) => s.trim())
return { label, value: value || label }
})
} catch {
return []
}
})
// 监听record变化初始化表单
watch(
() => props.record,
(newRecord) => {
if (newRecord) {
// 编辑模式
Object.assign(formData, {
group: newRecord.group || 'system',
key: newRecord.key || '',
name: newRecord.name || '',
type: newRecord.type || 'string',
value: parseValueByType(newRecord.value, newRecord.type),
options: newRecord.options,
sort: newRecord.sort || 0,
status: newRecord.status ?? 1,
description: newRecord.description || '',
is_system: newRecord.is_system || false
})
// 解析选项
if (newRecord.options && typeof newRecord.options === 'object') {
optionsText.value = newRecord.options
.map((opt) => {
if (typeof opt === 'object') {
return `${opt.label}:${opt.value}`
}
return opt
})
.join('\n')
}
} else {
// 新增模式,重置表单
resetForm()
}
},
{ immediate: true }
)
// 根据类型解析值
const parseValueByType = (value, type) => {
if (!value) return value
switch (type) {
case 'number':
return Number(value)
case 'boolean':
return value === 'true' || value === true || value === 1
case 'checkbox':
if (typeof value === 'string') {
try {
return JSON.parse(value)
} catch {
return value.split(',')
}
}
return value
default:
return value
}
}
// 重置表单
const resetForm = () => {
Object.assign(formData, {
group: 'system',
key: '',
name: '',
type: 'string',
value: '',
options: null,
sort: 0,
status: 1,
description: '',
is_system: false
})
optionsText.value = ''
formRef.value?.clearValidate()
}
// 添加自定义分组
const handleAddCustomGroup = () => {
if (!customGroup.value) {
message.warning('请输入分组名称')
return
}
formData.group = customGroup.value
customGroup.value = ''
message.success('分组已添加')
}
// 处理确定按钮
const handleOk = async () => {
try {
await formRef.value.validate()
loading.value = true
const submitData = { ...formData }
// 处理选项
if (['select', 'radio', 'checkbox'].includes(formData.type) && optionsText.value) {
submitData.options = parsedOptions.value
}
// 处理值类型转换
if (formData.type === 'number') {
submitData.value = Number(submitData.value)
} else if (formData.type === 'boolean') {
submitData.value = submitData.value ? '1' : '0'
} else if (formData.type === 'checkbox' && Array.isArray(submitData.value)) {
submitData.value = submitData.value.join(',')
} else if (formData.type === 'json') {
// 验证JSON格式
try {
JSON.parse(submitData.value)
} catch (e) {
throw new Error('JSON格式不正确')
}
}
if (isEdit.value) {
await systemApi.configs.edit.put(formData.id, submitData)
message.success('更新成功')
} else {
await systemApi.configs.add.post(submitData)
message.success('创建成功')
}
emit('success')
emit('update:visible', false)
resetForm()
} catch (error) {
if (error.message) {
message.error(error.message || '操作失败')
}
} finally {
loading.value = false
}
}
// 处理取消按钮
const handleCancel = () => {
resetForm()
emit('update:visible', false)
}
</script>
<style scoped lang="scss">
:deep(.ant-modal-body) {
max-height: 60vh;
overflow-y: auto;
}
:deep(.ant-select-dropdown) {
.ant-input {
margin-bottom: 8px;
}
}
</style>

View File

@@ -0,0 +1,423 @@
<template>
<div class="pages system-configs-page">
<div class="tool-bar">
<div class="left-panel">
<a-space>
<a-input
v-model:value="searchForm.keyword"
placeholder="配置名称/键名"
allow-clear
style="width: 180px"
/>
<a-select
v-model:value="searchForm.group"
placeholder="配置分组"
allow-clear
style="width: 140px"
:options="groupOptions"
/>
<a-button type="primary" @click="handleSearch">
<template #icon><search-outlined /></template>
搜索
</a-button>
<a-button @click="handleReset">
<template #icon><redo-outlined /></template>
重置
</a-button>
</a-space>
</div>
<div class="right-panel">
<a-button type="primary" @click="handleAdd">
<template #icon><plus-outlined /></template>
新增
</a-button>
<a-dropdown>
<a-button>
批量操作
<down-outlined />
</a-button>
<template #overlay>
<a-menu>
<a-menu-item @click="handleBatchDelete">
<delete-outlined />
批量删除
</a-menu-item>
<a-menu-item @click="handleBatchStatus(1)">
<check-outlined />
批量启用
</a-menu-item>
<a-menu-item @click="handleBatchStatus(0)">
<stop-outlined />
批量禁用
</a-menu-item>
</a-menu>
</template>
</a-dropdown>
</div>
</div>
<div class="table-content">
<scTable
ref="tableRef"
:columns="columns"
:data-source="tableData"
:loading="loading"
:pagination="pagination"
:row-selection="rowSelection"
:row-key="(record) => record.id"
@refresh="refreshTable"
@paginationChange="handlePaginationChange"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'type'">
<a-tag :color="getTypeColor(record.type)">{{ getTypeText(record.type) }}</a-tag>
</template>
<template v-if="column.key === 'value'">
<span v-if="['string', 'number', 'boolean'].includes(record.type)" class="value-text">
{{ formatValue(record.value, record.type) }}
</span>
<span v-else-if="record.type === 'file'" class="file-value">
<file-outlined />
{{ record.value }}
</span>
<a v-else-if="record.type === 'image'" :href="record.value" target="_blank" class="image-value">
<img :src="record.value" alt="预览" class="config-image" />
</a>
<span v-else class="json-value">{{ record.value }}</span>
</template>
<template v-if="column.key === 'status'">
<a-badge :status="record.status ? 'success' : 'default'" :text="record.status ? '启用' : '禁用'" />
</template>
<template v-if="column.key === 'action'">
<a-space>
<a-button type="link" size="small" @click="handleEdit(record)">
<edit-outlined />
编辑
</a-button>
<a-button
type="link"
size="small"
danger
:disabled="record.is_system"
@click="handleDelete(record)"
>
<delete-outlined />
删除
</a-button>
</a-space>
</template>
</template>
</scTable>
</div>
<!-- 新增/编辑弹窗 -->
<SaveDialog v-model:visible="showSaveDialog" :record="currentRecord" @success="handleSaveSuccess" />
</div>
</template>
<script setup>
import { ref, reactive, onMounted, h } from 'vue'
import { message, Modal } from 'ant-design-vue'
import {
SearchOutlined,
RedoOutlined,
PlusOutlined,
DownOutlined,
DeleteOutlined,
CheckOutlined,
StopOutlined,
EditOutlined,
FileOutlined
} from '@ant-design/icons-vue'
import { useTable } from '@/hooks/useTable'
import { systemApi } from '@/api/system'
import SaveDialog from './components/SaveDialog.vue'
// 表格引用
const tableRef = ref(null)
// 搜索表单
const searchForm = reactive({
keyword: '',
group: undefined
})
// 分组选项
const groupOptions = ref([])
// 当前记录
const currentRecord = ref(null)
// 显示新增/编辑弹窗
const showSaveDialog = ref(false)
// 使用 useTable Hook
const { tableData, loading, pagination, rowSelection, handleSearch, handleReset, handlePaginationChange, refreshTable } =
useTable({
api: systemApi.configs.list.get,
searchForm,
needPagination: true
})
// 表格列配置
const columns = [
{
title: 'ID',
dataIndex: 'id',
key: 'id',
width: 80
},
{
title: '配置分组',
dataIndex: 'group',
key: 'group',
width: 120
},
{
title: '配置键',
dataIndex: 'key',
key: 'key',
width: 200,
ellipsis: true
},
{
title: '配置名称',
dataIndex: 'name',
key: 'name',
width: 180
},
{
title: '配置值',
dataIndex: 'value',
key: 'value',
ellipsis: true,
width: 250
},
{
title: '类型',
dataIndex: 'type',
key: 'type',
width: 100,
align: 'center'
},
{
title: '排序',
dataIndex: 'sort',
key: 'sort',
width: 80,
align: 'center'
},
{
title: '状态',
dataIndex: 'status',
key: 'status',
width: 100,
align: 'center'
},
{
title: '描述',
dataIndex: 'description',
key: 'description',
ellipsis: true
},
{
title: '操作',
key: 'action',
width: 150,
fixed: 'right'
}
]
// 获取类型颜色
const getTypeColor = (type) => {
const colors = {
string: 'blue',
text: 'cyan',
number: 'green',
boolean: 'orange',
select: 'purple',
radio: 'purple',
checkbox: 'purple',
file: 'pink',
json: 'geekblue'
}
return colors[type] || 'default'
}
// 获取类型文本
const getTypeText = (type) => {
const texts = {
string: '字符串',
text: '文本',
number: '数字',
boolean: '布尔值',
select: '下拉框',
radio: '单选框',
checkbox: '多选框',
file: '文件',
json: 'JSON'
}
return texts[type] || type
}
// 格式化值
const formatValue = (value, type) => {
if (type === 'boolean') {
return value === 'true' || value === true ? '是' : '否'
}
if (type === 'number') {
return Number(value)
}
return value
}
// 获取分组列表
const loadGroups = async () => {
try {
const res = await systemApi.configs.groups.get()
groupOptions.value = res.data.map((item) => ({ label: item, value: item }))
} catch (error) {
console.error('获取分组列表失败:', error)
}
}
// 新增
const handleAdd = () => {
currentRecord.value = null
showSaveDialog.value = true
}
// 编辑
const handleEdit = (record) => {
currentRecord.value = { ...record }
showSaveDialog.value = true
}
// 删除
const handleDelete = (record) => {
if (record.is_system) {
message.warning('系统配置不能删除')
return
}
Modal.confirm({
title: '确认删除',
content: `确定要删除配置"${record.name}"吗?`,
okText: '确定',
cancelText: '取消',
onOk: async () => {
try {
await systemApi.configs.delete.delete(record.id)
message.success('删除成功')
refreshTable()
} catch (error) {
message.error(error.message || '删除失败')
}
}
})
}
// 批量删除
const handleBatchDelete = () => {
const selectedRowKeys = rowSelection.selectedRowKeys
if (selectedRowKeys.length === 0) {
message.warning('请先选择要删除的配置')
return
}
Modal.confirm({
title: '确认删除',
content: `确定要删除选中的 ${selectedRowKeys.length} 条配置吗?`,
okText: '确定',
cancelText: '取消',
onOk: async () => {
try {
await systemApi.configs.batchDelete.post({ ids: selectedRowKeys })
message.success('批量删除成功')
rowSelection.selectedRowKeys = []
refreshTable()
} catch (error) {
message.error(error.message || '批量删除失败')
}
}
})
}
// 批量更新状态
const handleBatchStatus = (status) => {
const selectedRowKeys = rowSelection.selectedRowKeys
if (selectedRowKeys.length === 0) {
message.warning('请先选择要操作的配置')
return
}
Modal.confirm({
title: status === 1 ? '确认启用' : '确认禁用',
content: `确定要${status === 1 ? '启用' : '禁用'}选中的 ${selectedRowKeys.length} 条配置吗?`,
okText: '确定',
cancelText: '取消',
onOk: async () => {
try {
await systemApi.configs.batchStatus.post({ ids: selectedRowKeys, status })
message.success(`${status === 1 ? '启用' : '禁用'}成功`)
rowSelection.selectedRowKeys = []
refreshTable()
} catch (error) {
message.error(error.message || '操作失败')
}
}
})
}
// 保存成功
const handleSaveSuccess = () => {
showSaveDialog.value = false
refreshTable()
}
// 初始化
onMounted(() => {
loadGroups()
})
</script>
<style scoped lang="scss">
.system-configs-page {
@extend .pages-base-layout;
.value-text {
color: #666;
font-family: monospace;
}
.file-value {
display: flex;
align-items: center;
gap: 4px;
color: #1890ff;
}
.image-value {
display: inline-block;
width: 40px;
height: 40px;
overflow: hidden;
border-radius: 4px;
cursor: pointer;
.config-image {
width: 100%;
height: 100%;
object-fit: cover;
}
&:hover .config-image {
transform: scale(1.1);
transition: transform 0.3s;
}
}
.json-value {
color: #999;
font-family: monospace;
font-size: 12px;
}
}
</style>