772 lines
18 KiB
Vue
772 lines
18 KiB
Vue
<script setup lang="ts">
|
||
import { computed, nextTick, onMounted, reactive, ref } from 'vue'
|
||
import { useRouter } from 'vue-router'
|
||
import AppStage from '@/components/AppStage.vue'
|
||
import PageHeader from '@/components/PageHeader.vue'
|
||
import {
|
||
SystemConfigApiError,
|
||
clearAdminSession,
|
||
createSystemConfig,
|
||
deleteSystemConfig,
|
||
listSystemConfigs,
|
||
updateSystemConfig,
|
||
type SystemConfig,
|
||
type SystemConfigInput,
|
||
} from '@/api/systemConfig'
|
||
import { showToast } from '@/composables/useToast'
|
||
|
||
const router = useRouter()
|
||
|
||
const items = ref<SystemConfig[]>([])
|
||
const keyword = ref('')
|
||
const appliedKeyword = ref('')
|
||
const loading = ref(false)
|
||
|
||
const formOpen = ref(false)
|
||
const editingId = ref<number | null>(null)
|
||
const formError = ref('')
|
||
const saving = ref(false)
|
||
const form = reactive<SystemConfigInput>({
|
||
configClass: '',
|
||
configName: '',
|
||
configValue: '',
|
||
configDescript: '',
|
||
})
|
||
const nameInput = ref<HTMLInputElement | null>(null)
|
||
|
||
const deleting = ref<SystemConfig | null>(null)
|
||
const removing = ref(false)
|
||
|
||
const emptyText = computed(() => {
|
||
if (loading.value) return '加载中...'
|
||
return appliedKeyword.value ? '没有匹配的系统参数' : '暂无系统参数'
|
||
})
|
||
|
||
function goHome() {
|
||
void router.push({ name: 'home' })
|
||
}
|
||
|
||
async function load() {
|
||
loading.value = true
|
||
try {
|
||
items.value = await listSystemConfigs(appliedKeyword.value)
|
||
} catch (error) {
|
||
handleError(error)
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function search() {
|
||
appliedKeyword.value = keyword.value.trim()
|
||
void load()
|
||
}
|
||
|
||
function handleError(error: unknown, fallback = '操作失败') {
|
||
if (error instanceof SystemConfigApiError) {
|
||
if (error.status === 401) {
|
||
clearAdminSession()
|
||
showToast('管理员凭证已过期,请重新进入系统参数管理')
|
||
void router.push({ name: 'home' })
|
||
return
|
||
}
|
||
showToast(error.message || fallback)
|
||
return
|
||
}
|
||
|
||
showToast(error instanceof Error ? error.message : fallback)
|
||
}
|
||
|
||
function openCreate() {
|
||
editingId.value = null
|
||
form.configClass = ''
|
||
form.configName = ''
|
||
form.configValue = ''
|
||
form.configDescript = ''
|
||
formError.value = ''
|
||
formOpen.value = true
|
||
void focusName()
|
||
}
|
||
|
||
function openEdit(item: SystemConfig) {
|
||
editingId.value = item.configId
|
||
form.configClass = item.configClass
|
||
form.configName = item.configName
|
||
form.configValue = item.configValue
|
||
form.configDescript = item.configDescript
|
||
formError.value = ''
|
||
formOpen.value = true
|
||
void focusName()
|
||
}
|
||
|
||
async function focusName() {
|
||
await nextTick()
|
||
nameInput.value?.focus()
|
||
}
|
||
|
||
function closeForm() {
|
||
if (saving.value) return
|
||
formOpen.value = false
|
||
}
|
||
|
||
let backdropPressed = false
|
||
|
||
function onBackdropPointerDown(event: PointerEvent) {
|
||
backdropPressed = event.target === event.currentTarget
|
||
}
|
||
|
||
function onBackdropPointerUp(event: PointerEvent) {
|
||
if (event.target !== event.currentTarget) backdropPressed = false
|
||
}
|
||
|
||
function closeOnBackdrop(event: MouseEvent, action: () => void) {
|
||
const shouldClose = backdropPressed && event.target === event.currentTarget
|
||
backdropPressed = false
|
||
if (shouldClose) action()
|
||
}
|
||
|
||
async function save() {
|
||
if (saving.value) return
|
||
|
||
if (!form.configName.trim()) {
|
||
formError.value = '配置名称不能为空'
|
||
return
|
||
}
|
||
|
||
saving.value = true
|
||
formError.value = ''
|
||
|
||
const payload: SystemConfigInput = {
|
||
configClass: form.configClass.trim(),
|
||
configName: form.configName.trim(),
|
||
configValue: form.configValue.trim(),
|
||
configDescript: form.configDescript.trim(),
|
||
}
|
||
|
||
try {
|
||
if (editingId.value === null) {
|
||
await createSystemConfig(payload)
|
||
showToast('系统参数已新增')
|
||
} else {
|
||
await updateSystemConfig(editingId.value, payload)
|
||
showToast('系统参数已更新')
|
||
}
|
||
|
||
formOpen.value = false
|
||
await load()
|
||
} catch (error) {
|
||
if (error instanceof SystemConfigApiError && error.status === 401) {
|
||
formOpen.value = false
|
||
handleError(error)
|
||
return
|
||
}
|
||
formError.value = error instanceof Error ? error.message : '保存失败'
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
function askDelete(item: SystemConfig) {
|
||
deleting.value = item
|
||
}
|
||
|
||
function cancelDelete() {
|
||
if (removing.value) return
|
||
deleting.value = null
|
||
}
|
||
|
||
async function confirmDelete() {
|
||
const target = deleting.value
|
||
if (!target || removing.value) return
|
||
|
||
removing.value = true
|
||
try {
|
||
await deleteSystemConfig(target.configId)
|
||
deleting.value = null
|
||
showToast('系统参数已删除')
|
||
await load()
|
||
} catch (error) {
|
||
handleError(error)
|
||
} finally {
|
||
removing.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(() => {
|
||
void load()
|
||
})
|
||
</script>
|
||
|
||
<template>
|
||
<AppStage>
|
||
<PageHeader title="系统参数管理" subtitle="系统运行参数集中配置">
|
||
<template #left>
|
||
<button class="header-button header-button--outline" type="button" @click="goHome">
|
||
返回
|
||
</button>
|
||
</template>
|
||
</PageHeader>
|
||
|
||
<main class="system-main">
|
||
<section class="config-card">
|
||
<div class="config-toolbar">
|
||
<div class="config-search">
|
||
<input
|
||
v-model="keyword"
|
||
class="config-search__input"
|
||
type="text"
|
||
placeholder="搜索分类 / 名称 / 值 / 描述"
|
||
@keyup.enter="search"
|
||
/>
|
||
<button class="toolbar-button" type="button" @click="search">查询</button>
|
||
</div>
|
||
|
||
<div class="config-toolbar__right">
|
||
<button class="toolbar-button toolbar-button--ghost" type="button" @click="load">
|
||
刷新
|
||
</button>
|
||
<button class="toolbar-button toolbar-button--primary" type="button" @click="openCreate">
|
||
新增参数
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="config-table">
|
||
<div class="config-table__head">
|
||
<span class="col-class">分类</span>
|
||
<span class="col-name">名称</span>
|
||
<span class="col-value">值</span>
|
||
<span class="col-descript">描述</span>
|
||
<span class="col-actions">操作</span>
|
||
</div>
|
||
|
||
<div class="config-table__body">
|
||
<div v-for="item in items" :key="item.configId" class="config-row">
|
||
<span class="col-class">{{ item.configClass || '—' }}</span>
|
||
<span class="col-name" :title="item.configName">{{ item.configName }}</span>
|
||
<span class="col-value" :title="item.configValue">{{ item.configValue || '—' }}</span>
|
||
<span class="col-descript" :title="item.configDescript">
|
||
{{ item.configDescript || '—' }}
|
||
</span>
|
||
<span class="col-actions">
|
||
<button class="row-button" type="button" @click="openEdit(item)">编辑</button>
|
||
<button
|
||
class="row-button row-button--danger"
|
||
type="button"
|
||
@click="askDelete(item)"
|
||
>
|
||
删除
|
||
</button>
|
||
</span>
|
||
</div>
|
||
|
||
<p v-if="!items.length" class="config-empty">{{ emptyText }}</p>
|
||
</div>
|
||
</div>
|
||
|
||
<footer class="config-footer">共 {{ items.length }} 条参数</footer>
|
||
</section>
|
||
</main>
|
||
|
||
<Teleport to="body">
|
||
<Transition name="config-modal">
|
||
<div
|
||
v-if="formOpen"
|
||
class="config-backdrop"
|
||
@pointerdown="onBackdropPointerDown"
|
||
@pointerup="onBackdropPointerUp"
|
||
@click="closeOnBackdrop($event, closeForm)"
|
||
>
|
||
<section class="form-card" role="dialog" aria-modal="true" aria-label="系统参数表单">
|
||
<h2>{{ editingId === null ? '新增系统参数' : '编辑系统参数' }}</h2>
|
||
|
||
<div class="form-grid">
|
||
<label class="form-field">
|
||
<span>配置分类</span>
|
||
<input v-model="form.configClass" type="text" placeholder="例如:设备、界面" />
|
||
</label>
|
||
|
||
<label class="form-field">
|
||
<span>配置名称 <em>*</em></span>
|
||
<input
|
||
ref="nameInput"
|
||
v-model="form.configName"
|
||
type="text"
|
||
placeholder="例如:ExposureUs"
|
||
/>
|
||
</label>
|
||
|
||
<label class="form-field form-field--wide">
|
||
<span>配置值</span>
|
||
<input v-model="form.configValue" type="text" placeholder="参数值" />
|
||
</label>
|
||
|
||
<label class="form-field form-field--wide">
|
||
<span>配置描述</span>
|
||
<textarea
|
||
v-model="form.configDescript"
|
||
rows="3"
|
||
placeholder="说明该参数的用途"
|
||
></textarea>
|
||
</label>
|
||
</div>
|
||
|
||
<p v-if="formError" class="form-error">{{ formError }}</p>
|
||
|
||
<div class="form-actions">
|
||
<button
|
||
class="form-button form-button--ghost"
|
||
type="button"
|
||
:disabled="saving"
|
||
@click="closeForm"
|
||
>
|
||
取消
|
||
</button>
|
||
<button
|
||
class="form-button form-button--primary"
|
||
type="button"
|
||
:disabled="saving"
|
||
@click="save"
|
||
>
|
||
{{ saving ? '保存中...' : '保存' }}
|
||
</button>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
</Transition>
|
||
</Teleport>
|
||
|
||
<Teleport to="body">
|
||
<Transition name="config-modal">
|
||
<div
|
||
v-if="deleting"
|
||
class="config-backdrop"
|
||
@pointerdown="onBackdropPointerDown"
|
||
@pointerup="onBackdropPointerUp"
|
||
@click="closeOnBackdrop($event, cancelDelete)"
|
||
>
|
||
<section class="confirm-card" role="dialog" aria-modal="true" aria-label="删除确认">
|
||
<h2>删除系统参数</h2>
|
||
<p>
|
||
确定删除「{{ deleting.configName }}」吗?删除后不可恢复。
|
||
</p>
|
||
|
||
<div class="form-actions">
|
||
<button
|
||
class="form-button form-button--ghost"
|
||
type="button"
|
||
:disabled="removing"
|
||
@click="cancelDelete"
|
||
>
|
||
取消
|
||
</button>
|
||
<button
|
||
class="form-button form-button--danger"
|
||
type="button"
|
||
:disabled="removing"
|
||
@click="confirmDelete"
|
||
>
|
||
{{ removing ? '删除中...' : '删除' }}
|
||
</button>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
</Transition>
|
||
</Teleport>
|
||
</AppStage>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.system-main {
|
||
position: absolute;
|
||
top: 176px;
|
||
right: 44px;
|
||
bottom: 40px;
|
||
left: 44px;
|
||
}
|
||
|
||
.config-card {
|
||
display: flex;
|
||
flex-direction: column;
|
||
height: 100%;
|
||
overflow: hidden;
|
||
border-radius: var(--radius-lg);
|
||
background: #fff;
|
||
box-shadow: var(--shadow-card);
|
||
}
|
||
|
||
.config-toolbar {
|
||
display: flex;
|
||
flex: none;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 24px;
|
||
padding: 24px 30px;
|
||
border-bottom: 1px solid var(--line);
|
||
}
|
||
|
||
.config-search {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
}
|
||
|
||
.config-search__input {
|
||
width: 460px;
|
||
height: 54px;
|
||
padding: 0 20px;
|
||
border: 2px solid var(--line-strong);
|
||
border-radius: 10px;
|
||
color: var(--ink);
|
||
font-family: inherit;
|
||
font-size: 19px;
|
||
outline: none;
|
||
transition: border-color 0.2s ease, box-shadow 0.2s ease;
|
||
}
|
||
|
||
.config-search__input:focus {
|
||
border-color: var(--blue);
|
||
box-shadow: 0 0 0 4px rgba(47, 111, 208, 0.12);
|
||
}
|
||
|
||
.config-toolbar__right {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
}
|
||
|
||
.toolbar-button {
|
||
height: 54px;
|
||
padding: 0 26px;
|
||
border-radius: 10px;
|
||
border: 2px solid var(--blue);
|
||
color: var(--blue);
|
||
font-size: 19px;
|
||
font-weight: 600;
|
||
letter-spacing: 2px;
|
||
background: #fff;
|
||
transition: background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
|
||
}
|
||
|
||
.toolbar-button:hover {
|
||
background: var(--blue-soft);
|
||
transform: translateY(-1px);
|
||
}
|
||
|
||
.toolbar-button--ghost {
|
||
border-color: var(--line-strong);
|
||
color: var(--ink-soft);
|
||
}
|
||
|
||
.toolbar-button--ghost:hover {
|
||
background: var(--surface-soft);
|
||
}
|
||
|
||
.toolbar-button--primary {
|
||
border-color: transparent;
|
||
color: #fff;
|
||
background: linear-gradient(180deg, #4b89e4 0%, #2f6fd0 100%);
|
||
box-shadow: 0 8px 18px rgba(47, 111, 208, 0.24);
|
||
}
|
||
|
||
.toolbar-button--primary:hover {
|
||
background: linear-gradient(180deg, #4b89e4 0%, #2f6fd0 100%);
|
||
box-shadow: 0 12px 24px rgba(47, 111, 208, 0.32);
|
||
}
|
||
|
||
.config-table {
|
||
display: flex;
|
||
flex: 1;
|
||
flex-direction: column;
|
||
min-height: 0;
|
||
padding: 0 30px;
|
||
}
|
||
|
||
.config-table__head,
|
||
.config-row {
|
||
display: grid;
|
||
grid-template-columns: 180px 280px minmax(0, 1fr) minmax(0, 1fr) 200px;
|
||
align-items: center;
|
||
gap: 20px;
|
||
padding: 0 18px;
|
||
}
|
||
|
||
.config-table__head {
|
||
flex: none;
|
||
height: 62px;
|
||
margin-top: 18px;
|
||
border-radius: 12px;
|
||
background: var(--surface-soft);
|
||
color: var(--muted);
|
||
font-size: 18px;
|
||
font-weight: 600;
|
||
letter-spacing: 1px;
|
||
}
|
||
|
||
.config-table__body {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow-y: auto;
|
||
}
|
||
|
||
.config-row {
|
||
min-height: 72px;
|
||
border-bottom: 1px solid var(--line);
|
||
color: var(--ink-soft);
|
||
font-size: 19px;
|
||
transition: background 0.18s ease;
|
||
}
|
||
|
||
.config-row:hover {
|
||
background: #f8fafc;
|
||
}
|
||
|
||
.col-name {
|
||
color: var(--ink);
|
||
font-weight: 600;
|
||
}
|
||
|
||
.col-value,
|
||
.col-descript {
|
||
overflow: hidden;
|
||
white-space: nowrap;
|
||
text-overflow: ellipsis;
|
||
}
|
||
|
||
.col-actions {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
}
|
||
|
||
.row-button {
|
||
height: 42px;
|
||
padding: 0 18px;
|
||
border: 1px solid var(--line-strong);
|
||
border-radius: 8px;
|
||
color: var(--blue);
|
||
font-size: 17px;
|
||
font-weight: 600;
|
||
letter-spacing: 1px;
|
||
background: #fff;
|
||
transition: background 0.18s ease, border-color 0.18s ease;
|
||
}
|
||
|
||
.row-button:hover {
|
||
border-color: var(--blue);
|
||
background: var(--blue-soft);
|
||
}
|
||
|
||
.row-button--danger {
|
||
color: var(--red);
|
||
}
|
||
|
||
.row-button--danger:hover {
|
||
border-color: var(--red);
|
||
background: #fdeeee;
|
||
}
|
||
|
||
.config-empty {
|
||
padding: 80px 0;
|
||
color: var(--muted-light);
|
||
font-size: 20px;
|
||
letter-spacing: 1px;
|
||
text-align: center;
|
||
}
|
||
|
||
.config-footer {
|
||
flex: none;
|
||
height: 64px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: flex-end;
|
||
padding: 0 48px;
|
||
border-top: 1px solid var(--line);
|
||
color: var(--muted);
|
||
font-size: 18px;
|
||
letter-spacing: 1px;
|
||
}
|
||
|
||
.config-backdrop {
|
||
position: fixed;
|
||
inset: 0;
|
||
z-index: 70;
|
||
display: grid;
|
||
place-items: center;
|
||
padding: 24px;
|
||
background: rgba(18, 33, 54, 0.44);
|
||
backdrop-filter: blur(5px);
|
||
}
|
||
|
||
.form-card {
|
||
width: 760px;
|
||
padding: 36px 40px 34px;
|
||
border: 1px solid rgba(255, 255, 255, 0.65);
|
||
border-radius: 24px;
|
||
background: #fff;
|
||
box-shadow: 0 30px 80px rgba(12, 30, 55, 0.3);
|
||
}
|
||
|
||
.form-card h2,
|
||
.confirm-card h2 {
|
||
color: #1f2937;
|
||
font-size: 30px;
|
||
font-weight: 600;
|
||
letter-spacing: 1px;
|
||
text-align: center;
|
||
}
|
||
|
||
.form-grid {
|
||
display: grid;
|
||
grid-template-columns: 1fr 1fr;
|
||
gap: 22px;
|
||
margin-top: 28px;
|
||
}
|
||
|
||
.form-field {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 10px;
|
||
color: var(--ink-soft);
|
||
font-size: 18px;
|
||
letter-spacing: 0.5px;
|
||
}
|
||
|
||
.form-field--wide {
|
||
grid-column: 1 / -1;
|
||
}
|
||
|
||
.form-field em {
|
||
color: var(--red);
|
||
font-style: normal;
|
||
}
|
||
|
||
.form-field input,
|
||
.form-field textarea {
|
||
width: 100%;
|
||
padding: 14px 18px;
|
||
border: 2px solid var(--line-strong);
|
||
border-radius: 10px;
|
||
color: var(--ink);
|
||
font-family: inherit;
|
||
font-size: 19px;
|
||
outline: none;
|
||
resize: vertical;
|
||
transition: border-color 0.2s ease, box-shadow 0.2s ease;
|
||
}
|
||
|
||
.form-field input {
|
||
height: 56px;
|
||
}
|
||
|
||
.form-field input:focus,
|
||
.form-field textarea:focus {
|
||
border-color: var(--blue);
|
||
box-shadow: 0 0 0 4px rgba(47, 111, 208, 0.12);
|
||
}
|
||
|
||
.form-error {
|
||
margin-top: 16px;
|
||
color: var(--red);
|
||
font-size: 17px;
|
||
letter-spacing: 0.5px;
|
||
text-align: center;
|
||
}
|
||
|
||
.form-actions {
|
||
display: flex;
|
||
justify-content: center;
|
||
gap: 20px;
|
||
margin-top: 30px;
|
||
}
|
||
|
||
.form-button {
|
||
width: 190px;
|
||
height: 56px;
|
||
border-radius: 12px;
|
||
font-size: 20px;
|
||
font-weight: 600;
|
||
letter-spacing: 3px;
|
||
transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
|
||
}
|
||
|
||
.form-button:disabled {
|
||
cursor: default;
|
||
opacity: 0.65;
|
||
}
|
||
|
||
.form-button--ghost {
|
||
border: 2px solid var(--line-strong);
|
||
color: var(--ink-soft);
|
||
background: #fff;
|
||
}
|
||
|
||
.form-button--ghost:hover:not(:disabled) {
|
||
background: var(--surface-soft);
|
||
}
|
||
|
||
.form-button--primary {
|
||
color: #fff;
|
||
background: linear-gradient(180deg, #4b89e4 0%, #2f6fd0 100%);
|
||
box-shadow: 0 10px 22px rgba(47, 111, 208, 0.28);
|
||
}
|
||
|
||
.form-button--primary:hover:not(:disabled) {
|
||
transform: translateY(-1px);
|
||
box-shadow: 0 14px 28px rgba(47, 111, 208, 0.36);
|
||
}
|
||
|
||
.form-button--danger {
|
||
color: #fff;
|
||
background: linear-gradient(180deg, #ef6d66 0%, #e15b55 100%);
|
||
box-shadow: 0 10px 22px rgba(225, 91, 85, 0.26);
|
||
}
|
||
|
||
.form-button--danger:hover:not(:disabled) {
|
||
transform: translateY(-1px);
|
||
box-shadow: 0 14px 28px rgba(225, 91, 85, 0.34);
|
||
}
|
||
|
||
.confirm-card {
|
||
width: 600px;
|
||
padding: 38px 44px 34px;
|
||
border: 1px solid rgba(255, 255, 255, 0.65);
|
||
border-radius: 24px;
|
||
background: #fff;
|
||
box-shadow: 0 30px 80px rgba(12, 30, 55, 0.3);
|
||
}
|
||
|
||
.confirm-card p {
|
||
margin-top: 18px;
|
||
color: var(--muted);
|
||
font-size: 19px;
|
||
letter-spacing: 0.5px;
|
||
text-align: center;
|
||
}
|
||
|
||
.config-modal-enter-active,
|
||
.config-modal-leave-active {
|
||
transition: opacity 0.24s ease;
|
||
}
|
||
|
||
.config-modal-enter-active .form-card,
|
||
.config-modal-leave-active .form-card,
|
||
.config-modal-enter-active .confirm-card,
|
||
.config-modal-leave-active .confirm-card {
|
||
transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.24s ease;
|
||
}
|
||
|
||
.config-modal-enter-from,
|
||
.config-modal-leave-to {
|
||
opacity: 0;
|
||
}
|
||
|
||
.config-modal-enter-from .form-card,
|
||
.config-modal-leave-to .form-card,
|
||
.config-modal-enter-from .confirm-card,
|
||
.config-modal-leave-to .confirm-card {
|
||
opacity: 0;
|
||
transform: translateY(16px) scale(0.97);
|
||
}
|
||
</style>
|