601 lines
14 KiB
Vue
601 lines
14 KiB
Vue
<script setup>
|
|
import { ref, onMounted, reactive, h } from 'vue'
|
|
import { message, Modal } from 'ant-design-vue'
|
|
import { EditOutlined, DeleteOutlined, PlusOutlined } from '@ant-design/icons-vue'
|
|
import { UserPromptApi } from '@/api/userPrompt'
|
|
import { usePromptStore } from '@/stores/prompt'
|
|
import dayjs from 'dayjs'
|
|
|
|
// 表格数据
|
|
const dataSource = ref([])
|
|
const loading = ref(false)
|
|
const pagination = reactive({
|
|
current: 1,
|
|
pageSize: 10,
|
|
total: 0,
|
|
showSizeChanger: true,
|
|
showTotal: (total) => `共 ${total} 条`,
|
|
})
|
|
|
|
// 搜索表单
|
|
const searchForm = reactive({
|
|
name: '',
|
|
status: undefined,
|
|
})
|
|
|
|
// 编辑弹窗
|
|
const editModalVisible = ref(false)
|
|
const editForm = reactive({
|
|
id: null,
|
|
name: '',
|
|
content: '',
|
|
status: 1,
|
|
})
|
|
const editFormRef = ref(null)
|
|
|
|
// Store
|
|
const promptStore = usePromptStore()
|
|
|
|
// 表格列定义
|
|
const columns = [
|
|
{
|
|
title: 'ID',
|
|
dataIndex: 'id',
|
|
key: 'id',
|
|
width: 80,
|
|
},
|
|
{
|
|
title: '名称',
|
|
dataIndex: 'name',
|
|
key: 'name',
|
|
width: 200,
|
|
ellipsis: true,
|
|
},
|
|
{
|
|
title: '内容',
|
|
dataIndex: 'content',
|
|
key: 'content',
|
|
ellipsis: true,
|
|
},
|
|
{
|
|
title: '状态',
|
|
dataIndex: 'status',
|
|
key: 'status',
|
|
width: 100,
|
|
},
|
|
{
|
|
title: '创建时间',
|
|
dataIndex: 'createTime',
|
|
key: 'createTime',
|
|
width: 180,
|
|
},
|
|
{
|
|
title: '操作',
|
|
key: 'action',
|
|
width: 150,
|
|
fixed: 'right',
|
|
},
|
|
]
|
|
|
|
// 加载数据
|
|
async function loadData() {
|
|
loading.value = true
|
|
try {
|
|
const params = {
|
|
pageNo: pagination.current,
|
|
pageSize: pagination.pageSize,
|
|
name: searchForm.name || undefined,
|
|
status: searchForm.status,
|
|
}
|
|
|
|
const response = await UserPromptApi.getUserPromptPage(params)
|
|
|
|
if (response && (response.code === 0 || response.code === 200)) {
|
|
dataSource.value = response.data?.list || []
|
|
pagination.total = response.data?.total || 0
|
|
} else {
|
|
throw new Error(response?.msg || response?.message || '加载失败')
|
|
}
|
|
} catch (error) {
|
|
console.error('加载提示词列表失败:', error)
|
|
message.error(error?.message || '加载失败,请稍后重试')
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
// 搜索
|
|
function handleSearch() {
|
|
pagination.current = 1
|
|
loadData()
|
|
}
|
|
|
|
// 重置搜索
|
|
function handleReset() {
|
|
searchForm.name = ''
|
|
searchForm.status = undefined
|
|
pagination.current = 1
|
|
loadData()
|
|
}
|
|
|
|
// 新增
|
|
function handleAdd() {
|
|
resetEditForm()
|
|
editModalVisible.value = true
|
|
}
|
|
|
|
// 编辑
|
|
function handleEdit(record) {
|
|
editForm.id = record.id
|
|
editForm.name = record.name || ''
|
|
editForm.content = record.content || ''
|
|
editForm.status = record.status !== null && record.status !== undefined ? record.status : 1
|
|
editModalVisible.value = true
|
|
}
|
|
|
|
// 表单重置
|
|
function resetEditForm() {
|
|
editForm.id = null
|
|
editForm.name = ''
|
|
editForm.content = ''
|
|
editForm.status = 1
|
|
}
|
|
|
|
// 通用API调用
|
|
async function apiCall(apiFunc, param, successMessage, isDelete = false) {
|
|
loading.value = true
|
|
try {
|
|
const response = await apiFunc(param)
|
|
if (response && (response.code === 0 || response.code === 200)) {
|
|
message.success(successMessage)
|
|
if (!isDelete) {
|
|
editModalVisible.value = false
|
|
}
|
|
loadData()
|
|
return true
|
|
} else {
|
|
throw new Error(response?.msg || response?.message || '操作失败')
|
|
}
|
|
} catch (error) {
|
|
console.error('API调用失败:', error)
|
|
message.error(error?.message || '操作失败,请稍后重试')
|
|
return false
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
// 保存(新增/编辑)
|
|
async function handleSave() {
|
|
try {
|
|
await editFormRef.value.validate()
|
|
} catch (error) {
|
|
console.error('表单验证失败:', error)
|
|
return
|
|
}
|
|
|
|
const payload = {
|
|
name: editForm.name.trim(),
|
|
content: editForm.content.trim(),
|
|
status: editForm.status,
|
|
}
|
|
|
|
if (editForm.id) {
|
|
payload.id = editForm.id
|
|
const result = await apiCall(
|
|
(data) => UserPromptApi.updateUserPrompt(data),
|
|
payload,
|
|
'更新成功'
|
|
)
|
|
// 同步更新 store
|
|
if (result && result.data) {
|
|
promptStore.updatePromptInList(result.data)
|
|
}
|
|
} else {
|
|
const result = await apiCall(
|
|
(data) => UserPromptApi.createUserPrompt(data),
|
|
payload,
|
|
'创建成功'
|
|
)
|
|
// 同步更新 store
|
|
if (result && result.data) {
|
|
promptStore.addPromptToList(result.data)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 删除
|
|
function handleDelete(record) {
|
|
Modal.confirm({
|
|
title: '确认删除',
|
|
content: `确定要删除提示词"${record.name}"吗?`,
|
|
onOk: async () => {
|
|
const result = await apiCall(
|
|
(id) => UserPromptApi.deleteUserPrompt(id),
|
|
record.id,
|
|
'删除成功',
|
|
true
|
|
)
|
|
// 同步更新 store
|
|
if (result) {
|
|
promptStore.removePromptFromList(record.id)
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
// 分页变化
|
|
function handleTableChange(pag) {
|
|
pagination.current = pag.current
|
|
pagination.pageSize = pag.pageSize
|
|
loadData()
|
|
}
|
|
|
|
// 初始化
|
|
onMounted(() => {
|
|
loadData()
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<div class="style-settings-page">
|
|
<!-- 筛选条件 -->
|
|
<div class="style-settings-page__filters">
|
|
<a-space :size="16">
|
|
<a-input
|
|
v-model:value="searchForm.name"
|
|
class="filter-input"
|
|
placeholder="搜索提示词名称"
|
|
allow-clear
|
|
@press-enter="handleSearch"
|
|
>
|
|
<template #prefix>
|
|
<EditOutlined />
|
|
</template>
|
|
</a-input>
|
|
|
|
<a-select
|
|
v-model:value="searchForm.status"
|
|
class="filter-select"
|
|
placeholder="状态筛选"
|
|
allow-clear
|
|
@change="handleSearch"
|
|
>
|
|
<a-select-option :value="1">启用</a-select-option>
|
|
<a-select-option :value="0">禁用</a-select-option>
|
|
</a-select>
|
|
|
|
<a-button type="primary" class="filter-button" @click="handleSearch">
|
|
搜索
|
|
</a-button>
|
|
<a-button class="filter-button" @click="handleReset">
|
|
重置
|
|
</a-button>
|
|
|
|
<a-button type="primary" class="filter-button add-btn" @click="handleAdd">
|
|
<template #icon>
|
|
<PlusOutlined />
|
|
</template>
|
|
新增提示词
|
|
</a-button>
|
|
</a-space>
|
|
</div>
|
|
|
|
<!-- 任务列表 -->
|
|
<div class="style-settings-page__content">
|
|
<a-spin :spinning="loading" tip="加载中...">
|
|
<a-table
|
|
:data-source="dataSource"
|
|
:columns="columns"
|
|
:row-key="record => record.id"
|
|
:pagination="pagination"
|
|
@change="handleTableChange"
|
|
:scroll="{ x: 1200 }"
|
|
>
|
|
<!-- 名称列 -->
|
|
<template #bodyCell="{ column, record }">
|
|
<template v-if="column.key === 'name'">
|
|
<div class="name-cell">
|
|
<strong>{{ record.name }}</strong>
|
|
</div>
|
|
</template>
|
|
|
|
<!-- 内容列 -->
|
|
<template v-else-if="column.key === 'content'">
|
|
<div class="content-cell" :title="record.content">
|
|
{{ record.content ? (record.content.length > 100 ? record.content.substring(0, 100) + '...' : record.content) : '-' }}
|
|
</div>
|
|
</template>
|
|
|
|
<!-- 状态列 -->
|
|
<template v-else-if="column.key === 'status'">
|
|
<a-tag :color="record.status === 1 ? 'success' : 'error'">
|
|
{{ record.status === 1 ? '启用' : '禁用' }}
|
|
</a-tag>
|
|
</template>
|
|
|
|
<!-- 创建时间列 -->
|
|
<template v-else-if="column.key === 'createTime'">
|
|
{{ record.createTime ? dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') : '-' }}
|
|
</template>
|
|
|
|
<!-- 操作列 -->
|
|
<template v-else-if="column.key === 'action'">
|
|
<a-space>
|
|
<a-button
|
|
type="link"
|
|
size="small"
|
|
@click="handleEdit(record)"
|
|
class="action-btn-edit"
|
|
>
|
|
<template #icon>
|
|
<EditOutlined />
|
|
</template>
|
|
编辑
|
|
</a-button>
|
|
<a-button
|
|
type="link"
|
|
size="small"
|
|
@click="handleDelete(record)"
|
|
class="action-btn-delete"
|
|
>
|
|
<template #icon>
|
|
<DeleteOutlined />
|
|
</template>
|
|
删除
|
|
</a-button>
|
|
</a-space>
|
|
</template>
|
|
</template>
|
|
</a-table>
|
|
</a-spin>
|
|
</div>
|
|
|
|
<!-- 编辑弹窗 -->
|
|
<a-modal
|
|
v-model:visible="editModalVisible"
|
|
:title="editForm.id ? '编辑提示词' : '新增提示词'"
|
|
width="800px"
|
|
:footer="null"
|
|
class="edit-modal"
|
|
>
|
|
<a-form
|
|
ref="editFormRef"
|
|
:model="editForm"
|
|
:label-col="{ span: 4 }"
|
|
:wrapper-col="{ span: 20 }"
|
|
>
|
|
<a-form-item
|
|
label="名称"
|
|
name="name"
|
|
:rules="[{ required: true, message: '请输入提示词名称' }]"
|
|
>
|
|
<a-input v-model:value="editForm.name" placeholder="请输入提示词名称" />
|
|
</a-form-item>
|
|
<a-form-item
|
|
label="内容"
|
|
name="content"
|
|
:rules="[{ required: true, message: '请输入提示词内容' }]"
|
|
>
|
|
<a-textarea
|
|
v-model:value="editForm.content"
|
|
placeholder="请输入提示词内容"
|
|
:rows="8"
|
|
/>
|
|
</a-form-item>
|
|
<a-form-item
|
|
label="状态"
|
|
name="status"
|
|
:rules="[{ required: true, message: '请选择状态' }]"
|
|
>
|
|
<a-radio-group v-model:value="editForm.status">
|
|
<a-radio :value="1">启用</a-radio>
|
|
<a-radio :value="0">禁用</a-radio>
|
|
</a-radio-group>
|
|
</a-form-item>
|
|
</a-form>
|
|
|
|
<!-- 弹窗底部按钮 -->
|
|
<div class="modal-footer">
|
|
<a-space :size="12" class="button-container">
|
|
<a-button @click="editModalVisible = false" class="footer-btn">
|
|
取消
|
|
</a-button>
|
|
<a-button type="primary" @click="handleSave" :loading="loading" class="footer-btn">
|
|
确定
|
|
</a-button>
|
|
</a-space>
|
|
</div>
|
|
</a-modal>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped lang="less">
|
|
.style-settings-page {
|
|
padding: var(--space-3);
|
|
height: 100%;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: var(--space-3);
|
|
|
|
&__filters {
|
|
padding: var(--space-3);
|
|
background: var(--color-surface);
|
|
border-radius: var(--radius-card);
|
|
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
|
|
|
|
.filter-input {
|
|
width: 200px;
|
|
|
|
@media (max-width: 1199px) {
|
|
width: 160px;
|
|
}
|
|
|
|
@media (max-width: 767px) {
|
|
width: 100%;
|
|
}
|
|
}
|
|
|
|
.filter-select {
|
|
width: 140px;
|
|
|
|
@media (max-width: 1199px) {
|
|
width: 120px;
|
|
}
|
|
|
|
@media (max-width: 767px) {
|
|
width: 100%;
|
|
}
|
|
}
|
|
|
|
.filter-button {
|
|
min-width: 80px;
|
|
|
|
@media (max-width: 767px) {
|
|
min-width: auto;
|
|
}
|
|
|
|
&.add-btn {
|
|
margin-left: auto;
|
|
|
|
@media (max-width: 767px) {
|
|
margin-left: 0;
|
|
width: 100%;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
&__content {
|
|
flex: 1;
|
|
overflow: auto;
|
|
background: var(--color-surface);
|
|
border-radius: var(--radius-card);
|
|
padding: var(--space-3);
|
|
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
|
|
}
|
|
}
|
|
|
|
/* 表格单元格样式 */
|
|
.name-cell {
|
|
font-weight: 500;
|
|
color: var(--color-text);
|
|
}
|
|
|
|
.content-cell {
|
|
color: var(--color-text-secondary);
|
|
font-size: 13px;
|
|
line-height: 1.5;
|
|
}
|
|
|
|
/* 操作按钮样式 */
|
|
.action-btn-edit {
|
|
color: var(--color-primary);
|
|
|
|
&:hover {
|
|
color: var(--color-primary-hover, var(--color-blue-600));
|
|
}
|
|
}
|
|
|
|
.action-btn-delete {
|
|
color: var(--color-error);
|
|
|
|
&:hover {
|
|
color: #dc2626;
|
|
}
|
|
}
|
|
|
|
/* 表格样式 */
|
|
:deep(.ant-table-tbody > tr > td) {
|
|
padding: 12px 8px;
|
|
}
|
|
|
|
:deep(.ant-table-thead > tr > th) {
|
|
background: var(--color-bg-2);
|
|
font-weight: 600;
|
|
}
|
|
|
|
/* 编辑弹窗样式 */
|
|
.edit-modal {
|
|
:deep(.ant-modal-body) {
|
|
padding: var(--space-3);
|
|
}
|
|
|
|
:deep(.ant-form-item-label > label) {
|
|
color: var(--color-text);
|
|
font-weight: 500;
|
|
}
|
|
|
|
:deep(.ant-input),
|
|
:deep(.ant-input-number),
|
|
:deep(.ant-select),
|
|
:deep(.ant-radio-group) {
|
|
border-radius: var(--radius-tag);
|
|
}
|
|
|
|
:deep(.ant-input:focus),
|
|
:deep(.ant-input-focused) {
|
|
border-color: var(--color-border-focus);
|
|
box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
|
|
}
|
|
|
|
.modal-footer {
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
padding-top: var(--space-3);
|
|
border-top: 1px solid var(--color-border);
|
|
margin-top: var(--space-3);
|
|
|
|
.button-container {
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.footer-btn {
|
|
min-width: 88px;
|
|
text-align: center;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 响应式优化 */
|
|
@media (max-width: 767px) {
|
|
.style-settings-page {
|
|
padding: var(--space-2);
|
|
|
|
&__filters {
|
|
padding: var(--space-2);
|
|
|
|
.ant-space {
|
|
width: 100%;
|
|
flex-wrap: wrap;
|
|
|
|
.ant-input,
|
|
.ant-select,
|
|
.ant-btn {
|
|
width: calc(50% - var(--space-1)) !important;
|
|
}
|
|
}
|
|
}
|
|
|
|
&__content {
|
|
padding: var(--space-2);
|
|
}
|
|
}
|
|
|
|
:deep(.ant-table-tbody > tr > td) {
|
|
padding: 8px 4px;
|
|
}
|
|
|
|
.action-btn-edit,
|
|
.action-btn-delete {
|
|
font-size: 12px;
|
|
padding: 0 4px;
|
|
}
|
|
}
|
|
</style>
|
|
|