This commit is contained in:
2026-03-11 00:42:46 +08:00
parent a125b5922f
commit a12cb5e75a
23 changed files with 1593 additions and 101 deletions

View File

@@ -0,0 +1,306 @@
<template>
<a-modal
:open="visible"
title="我创建的"
:width="700"
:footer="null"
@cancel="handleClose"
>
<div class="favorites-content">
<!-- 操作栏 -->
<div class="action-bar">
<a-button type="primary" @click="handleCreate">
<template #icon><PlusOutlined /></template>
新建风格
</a-button>
<a-button @click="loadList" :loading="loading">
<template #icon><ReloadOutlined /></template>
刷新
</a-button>
</div>
<!-- 列表 -->
<div class="favorites-list" v-if="!loading && list.length > 0">
<div
v-for="item in list"
:key="item.id"
class="favorite-card"
>
<div class="card-header">
<span class="card-name">{{ item.name }}</span>
<div class="card-actions">
<a-button type="link" size="small" @click="handleEdit(item)">编辑</a-button>
<a-popconfirm
title="确定删除此风格?"
@confirm="handleDelete(item.id)"
>
<a-button type="link" size="small" danger>删除</a-button>
</a-popconfirm>
</div>
</div>
<div class="card-content">{{ item.content }}</div>
<div class="card-footer">
<span class="card-category" v-if="item.category">{{ item.category }}</span>
<span class="card-use-count">使用 {{ item.useCount || 0 }} </span>
</div>
</div>
</div>
<!-- 空状态 -->
<div class="empty-state" v-if="!loading && list.length === 0">
<p>暂无收藏的风格</p>
<a-button type="primary" @click="handleCreate">立即创建</a-button>
</div>
<!-- 加载状态 -->
<div class="loading-state" v-if="loading">
<a-spin />
<span>加载中...</span>
</div>
</div>
<!-- 编辑弹窗 -->
<a-modal
v-model:open="editVisible"
:title="editingItem ? '编辑风格' : '新建风格'"
:width="500"
@ok="handleSave"
:confirmLoading="saving"
>
<a-form layout="vertical">
<a-form-item label="风格名称" required>
<a-input v-model:value="editForm.name" placeholder="请输入风格名称" />
</a-form-item>
<a-form-item label="风格内容" required>
<a-textarea
v-model:value="editForm.content"
:rows="6"
placeholder="请输入风格提示词内容"
/>
</a-form-item>
<a-form-item label="分类">
<a-input v-model:value="editForm.category" placeholder="可选,用于分类管理" />
</a-form-item>
</a-form>
</a-modal>
</a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { PlusOutlined, ReloadOutlined } from '@ant-design/icons-vue'
import { UserPromptApi } from '@/api/userPrompt'
const props = defineProps({
visible: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['update:visible', 'refresh'])
const loading = ref(false)
const list = ref([])
const editVisible = ref(false)
const editingItem = ref(null)
const saving = ref(false)
const editForm = ref({
name: '',
content: '',
category: ''
})
// 加载列表
async function loadList() {
loading.value = true
try {
const res = await UserPromptApi.getUserPromptList()
if (res.code === 0) {
list.value = res.data || []
}
} catch (error) {
console.error('加载列表失败:', error)
} finally {
loading.value = false
}
}
// 创建新风格
function handleCreate() {
editingItem.value = null
editForm.value = {
name: '',
content: '',
category: ''
}
editVisible.value = true
}
// 编辑风格
function handleEdit(item) {
editingItem.value = item
editForm.value = {
name: item.name,
content: item.content,
category: item.category || ''
}
editVisible.value = true
}
// 保存风格
async function handleSave() {
if (!editForm.value.name.trim()) {
message.warning('请输入风格名称')
return
}
if (!editForm.value.content.trim()) {
message.warning('请输入风格内容')
return
}
saving.value = true
try {
const data = {
name: editForm.value.name.trim(),
content: editForm.value.content.trim(),
category: editForm.value.category.trim() || null,
status: 1
}
if (editingItem.value) {
// 更新
await UserPromptApi.updateUserPrompt({
id: editingItem.value.id,
...data
})
message.success('更新成功')
} else {
// 创建
await UserPromptApi.createUserPrompt(data)
message.success('创建成功')
}
editVisible.value = false
loadList()
emit('refresh')
} catch (error) {
console.error('保存失败:', error)
message.error('保存失败')
} finally {
saving.value = false
}
}
// 删除风格
async function handleDelete(id) {
try {
await UserPromptApi.deleteUserPrompt(id)
message.success('删除成功')
loadList()
emit('refresh')
} catch (error) {
console.error('删除失败:', error)
message.error('删除失败')
}
}
// 关闭弹窗
function handleClose() {
emit('update:visible', false)
}
// 监听 visible 变化
watch(() => props.visible, (newVal) => {
if (newVal) {
loadList()
}
})
</script>
<style scoped lang="less">
.favorites-content {
min-height: 300px;
}
.action-bar {
display: flex;
gap: 12px;
margin-bottom: 16px;
}
.favorites-list {
display: grid;
gap: 12px;
}
.favorite-card {
padding: 16px;
border: 1px solid var(--color-border);
border-radius: 8px;
background: var(--color-surface);
transition: all 0.2s;
&:hover {
border-color: var(--color-primary);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.card-name {
font-weight: 600;
font-size: 15px;
color: var(--color-text);
}
.card-actions {
display: flex;
gap: 4px;
}
.card-content {
color: var(--color-text-secondary);
font-size: 13px;
line-height: 1.6;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
margin-bottom: 12px;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 12px;
color: var(--color-text-tertiary);
}
.card-category {
padding: 2px 8px;
background: rgba(24, 144, 255, 0.1);
color: #1890ff;
border-radius: 10px;
}
.empty-state,
.loading-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 60px 20px;
color: var(--color-text-secondary);
gap: 16px;
}
</style>

View File

@@ -0,0 +1,283 @@
<template>
<a-select
v-model:value="selectedId"
:placeholder="placeholder"
:loading="loading"
style="width: 100%"
show-search
:filter-option="filterOption"
@change="handleChange"
>
<!-- 用户风格分组 -->
<a-select-opt-group v-if="userPrompts.length > 0">
<template #label>
<span class="group-label">用户风格</span>
</template>
<a-select-option
v-for="item in userPrompts"
:key="`prompt-${item.id}`"
:value="`prompt-${item.id}`"
>
<div class="option-content">
<span class="option-name">{{ item.name }}</span>
<span class="option-tag style-tag">风格</span>
</div>
</a-select-option>
</a-select-opt-group>
<!-- 收藏的智能体分组 -->
<a-select-opt-group v-if="favoriteAgents.length > 0">
<template #label>
<span class="group-label">收藏的智能体</span>
</template>
<a-select-option
v-for="item in favoriteAgents"
:key="`agent-${item.id}`"
:value="`agent-${item.id}`"
>
<div class="option-content">
<img v-if="item.avatar" :src="item.avatar" class="option-avatar" />
<span class="option-name">{{ item.name }}</span>
<span class="option-tag agent-tag">{{ item.categoryName || '智能体' }}</span>
</div>
</a-select-option>
</a-select-opt-group>
<!-- 空状态 -->
<template v-if="!loading && userPrompts.length === 0 && favoriteAgents.length === 0">
<a-select-option disabled value="__empty__">
<span class="empty-text">暂无可选项</span>
</a-select-option>
</template>
</a-select>
</template>
<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { usePromptStore } from '@/stores/prompt'
import { getAgentList } from '@/api/agent'
import { getJSON, setJSON } from '@/utils/storage'
const props = defineProps({
modelValue: {
type: [String, Number],
default: null
},
placeholder: {
type: String,
default: '选择文案风格'
},
storageKey: {
type: String,
default: 'style_selector'
}
})
const emit = defineEmits(['update:modelValue', 'change'])
const promptStore = usePromptStore()
const loading = ref(false)
const selectedId = ref(null)
const agentList = ref([])
// 用户风格列表
const userPrompts = computed(() => promptStore.promptList || [])
// 收藏的智能体列表
const favoriteAgents = computed(() => {
return agentList.value.filter(agent => agent.isFavorite)
})
// 过滤选项
function filterOption(input, option) {
const key = option.value
const name = option.label || ''
// 根据 key 查找对应的项目名称
if (key.startsWith('prompt-')) {
const id = parseInt(key.replace('prompt-', ''))
const prompt = userPrompts.value.find(p => p.id === id)
return prompt?.name?.toLowerCase().includes(input.toLowerCase())
} else if (key.startsWith('agent-')) {
const id = parseInt(key.replace('agent-', ''))
const agent = agentList.value.find(a => a.id === id)
return agent?.name?.toLowerCase().includes(input.toLowerCase())
}
return false
}
// 处理选择变化
function handleChange(value) {
if (!value || value === '__empty__') {
emit('update:modelValue', null)
emit('change', null)
return
}
const [type, idStr] = value.split('-')
const id = parseInt(idStr)
let item = null
if (type === 'prompt') {
item = userPrompts.value.find(p => p.id === id)
emit('change', { id, type: 'prompt', item })
} else if (type === 'agent') {
item = agentList.value.find(a => a.id === id)
emit('change', { id, type: 'agent', item })
}
emit('update:modelValue', id)
// 保存选中状态到本地
saveSelectedValue(value)
}
// 加载智能体列表
async function loadAgentList() {
try {
const res = await getAgentList()
if (res.code === 0 && res.data) {
agentList.value = res.data.map(item => ({
id: item.id,
agentId: item.agentId,
name: item.agentName,
description: item.description,
systemPrompt: item.systemPrompt,
avatar: item.icon,
categoryName: item.categoryName || '其他',
isFavorite: item.isFavorite || false
}))
}
} catch (error) {
console.error('加载智能体列表失败:', error)
}
}
// 加载用户风格列表
async function loadUserPrompts() {
try {
await promptStore.loadPromptList()
} catch (error) {
console.error('加载用户风格列表失败:', error)
}
}
// 保存选中状态
async function saveSelectedValue(value) {
try {
await setJSON(`${props.storageKey}_selected`, value)
} catch (error) {
console.error('保存选择状态失败:', error)
}
}
// 恢复选中状态
async function restoreSelectedValue() {
try {
const savedValue = await getJSON(`${props.storageKey}_selected`, null)
if (savedValue) {
// 验证保存的值是否有效
const [type, idStr] = savedValue.split('-')
const id = parseInt(idStr)
if (type === 'prompt') {
const exists = userPrompts.value.some(p => p.id === id)
if (exists) {
selectedId.value = savedValue
emit('update:modelValue', id)
const item = userPrompts.value.find(p => p.id === id)
emit('change', { id, type: 'prompt', item })
}
} else if (type === 'agent') {
const exists = agentList.value.some(a => a.id === id)
if (exists) {
selectedId.value = savedValue
emit('update:modelValue', id)
const item = agentList.value.find(a => a.id === id)
emit('change', { id, type: 'agent', item })
}
}
}
} catch (error) {
console.error('恢复选择状态失败:', error)
}
}
// 初始化加载
async function init() {
loading.value = true
try {
await Promise.all([loadUserPrompts(), loadAgentList()])
await restoreSelectedValue()
} finally {
loading.value = false
}
}
// 监听 modelValue 变化
watch(() => props.modelValue, (newValue) => {
if (newValue === null) {
selectedId.value = null
}
})
onMounted(() => {
init()
})
// 暴露刷新方法
defineExpose({
refresh: init
})
</script>
<style scoped lang="less">
.group-label {
font-weight: 600;
color: var(--color-text);
font-size: 13px;
}
.option-content {
display: flex;
align-items: center;
gap: 8px;
}
.option-avatar {
width: 20px;
height: 20px;
border-radius: 50%;
object-fit: cover;
}
.option-name {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.option-tag {
font-size: 11px;
padding: 1px 6px;
border-radius: 10px;
flex-shrink: 0;
}
.style-tag {
background: rgba(24, 144, 255, 0.1);
color: #1890ff;
}
.agent-tag {
background: rgba(82, 196, 26, 0.1);
color: #52c41a;
}
.empty-text {
color: var(--color-text-secondary);
font-style: italic;
}
</style>

View File

@@ -0,0 +1,303 @@
<script setup>
import { ref, computed, watch } from 'vue'
import { message } from 'ant-design-vue'
import {
StarFilled,
CloseOutlined,
EditOutlined,
DeleteOutlined,
PlusOutlined
} from '@ant-design/icons-vue'
import { UserPromptApi } from '@/api/userPrompt'
import SavePromptModal from '@/views/content-style/components/SavePromptModal.vue'
// Props
const props = defineProps({
visible: {
type: Boolean,
default: false
}
})
// Emits
const emit = defineEmits(['update:visible', 'refresh'])
// State
const loading = ref(false)
const promptList = ref([])
const searchKeyword = ref('')
const showSaveModal = ref(false)
const editingPrompt = ref(null)
// Computed
const filteredList = computed(() => {
if (!searchKeyword.value.trim()) return promptList.value
const keyword = searchKeyword.value.trim().toLowerCase()
return promptList.value.filter(item =>
item.name.toLowerCase().includes(keyword) ||
(item.content && item.content.toLowerCase().includes(keyword))
)
})
// Watch
watch(() => props.visible, (newVal) => {
if (newVal) {
loadList()
}
})
// Methods
async function loadList() {
loading.value = true
try {
const res = await UserPromptApi.getUserPromptList()
if (res.code === 0 && res.data) {
promptList.value = res.data.map(item => ({
id: item.id,
name: item.name,
content: item.content,
category: item.category,
useCount: item.useCount || 0
}))
}
} catch (error) {
console.error('加载风格列表失败:', error)
message.error('加载风格列表失败')
} finally {
loading.value = false
}
}
function handleClose() {
emit('update:visible', false)
}
function handleCreate() {
editingPrompt.value = null
showSaveModal.value = true
}
function handleEdit(item) {
editingPrompt.value = { ...item }
showSaveModal.value = true
}
async function handleDelete(id) {
try {
await UserPromptApi.deleteUserPrompt(id)
// 从列表中移除
const index = promptList.value.findIndex(p => p.id === id)
if (index !== -1) {
promptList.value.splice(index, 1)
}
message.success('删除成功')
emit('refresh')
} catch (error) {
console.error('删除失败:', error)
message.error('删除失败')
}
}
function handleSaveSuccess() {
showSaveModal.value = false
loadList()
emit('refresh')
}
</script>
<template>
<a-modal
:open="visible"
title="我创建的"
:width="700"
:footer="null"
@cancel="handleClose"
>
<!-- 操作栏 -->
<div class="action-bar">
<a-button type="primary" @click="handleCreate">
<template #icon><PlusOutlined /></template>
新建风格
</a-button>
<a-button @click="loadList" :loading="loading">
刷新
</a-button>
</div>
<!-- 搜索框 -->
<div class="search-bar">
<a-input
v-model:value="searchKeyword"
placeholder="搜索风格..."
allow-clear
/>
</div>
<!-- 列表 -->
<div class="prompt-list" v-if="!loading && filteredList.length > 0">
<div
v-for="item in filteredList"
:key="item.id"
class="prompt-card"
>
<div class="card-header">
<span class="card-name">{{ item.name }}</span>
<div class="card-actions">
<a-button type="link" size="small" @click="handleEdit(item)">
<EditOutlined /> 编辑
</a-button>
<a-popconfirm
title="确定删除此风格?"
@confirm="handleDelete(item.id)"
>
<a-button type="link" size="small" danger>
<DeleteOutlined /> 删除
</a-button>
</a-popconfirm>
</div>
</div>
<div class="card-content">{{ item.content }}</div>
<div class="card-footer">
<span class="card-category" v-if="item.category">{{ item.category }}</span>
<span class="card-use-count">使用 {{ item.useCount || 0 }} </span>
</div>
</div>
</div>
<!-- 空状态 -->
<div class="empty-state" v-else-if="!loading">
<StarFilled class="empty-icon" />
<p class="empty-title">暂无收藏的风格</p>
<p class="empty-desc">点击"新建风格"创建您的第一个风格</p>
</div>
<!-- 加载状态 -->
<div class="loading-state" v-if="loading">
<a-spin />
</div>
<!-- 保存弹窗 -->
<SavePromptModal
v-model:visible="showSaveModal"
:prompt="editingPrompt"
@success="handleSaveSuccess"
/>
</a-modal>
</template>
<style scoped lang="less">
.action-bar {
display: flex;
gap: 12px;
margin-bottom: 16px;
}
.search-bar {
margin-bottom: 16px;
}
.prompt-list {
display: flex;
flex-direction: column;
gap: 12px;
max-height: 400px;
overflow-y: auto;
}
.prompt-card {
padding: 16px;
border: 1px solid var(--color-border);
border-radius: 8px;
background: var(--color-surface);
transition: all 0.2s ease;
&:hover {
border-color: var(--color-primary);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.card-name {
font-weight: 600;
font-size: 14px;
color: var(--color-text);
}
.card-actions {
display: flex;
gap: 8px;
}
.card-content {
font-size: 13px;
color: var(--color-text-secondary);
line-height: 1.5;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
margin-bottom: 8px;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 12px;
}
.card-category {
padding: 2px 8px;
background: rgba(24, 144, 255, 0.1);
color: #1890ff;
border-radius: 10px;
}
.card-use-count {
color: var(--color-text-tertiary);
}
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 48px 24px;
text-align: center;
}
.empty-icon {
font-size: 48px;
color: #f59e0b;
margin-bottom: 16px;
opacity: 0.5;
}
.empty-title {
font-size: 15px;
font-weight: 500;
color: var(--color-text);
margin: 0 0 8px 0;
}
.empty-desc {
font-size: 13px;
color: var(--color-text-secondary);
margin: 0;
}
.loading-state {
display: flex;
align-items: center;
justify-content: center;
padding: 48px;
}
</style>