This commit is contained in:
2026-03-11 01:29:43 +08:00
parent dd57aa0ce5
commit 29e11056dc
3 changed files with 80 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
<script setup>
import { ref, watch } from 'vue'
import { ref, watchEffect, computed } from 'vue'
import { message } from 'ant-design-vue'
import { UserPromptApi } from '@/api/userPrompt'
import { useUserStore } from '@/stores/user'
@@ -9,6 +9,12 @@ const props = defineProps({
type: Boolean,
default: false,
},
// prompt { id, name, content, category }
prompt: {
type: Object,
default: null,
},
//
promptContent: {
type: String,
default: '',
@@ -20,17 +26,40 @@ const emit = defineEmits(['update:visible', 'success'])
const userStore = useUserStore()
const savingPrompt = ref(false)
const savePromptForm = ref({
id: null,
name: '',
category: '',
content: '',
})
watch(() => props.visible, (newVal) => {
if (newVal) {
savePromptForm.value = {
name: '',
category: '',
content: props.promptContent,
//
const isEditMode = computed(() => !!props.prompt?.id)
//
const modalTitle = computed(() => isEditMode.value ? '编辑提示词' : '保存提示词')
// 使 watchEffect
watchEffect(() => {
if (props.visible) {
// prompt
if (props.prompt) {
console.log('[SavePromptModal] edit mode, prompt:', props.prompt)
savePromptForm.value = {
id: props.prompt.id || null,
name: props.prompt.name || '',
category: props.prompt.category || '',
content: props.prompt.content || '',
}
}
// promptContent
else if (props.promptContent) {
console.log('[SavePromptModal] create mode, promptContent:', props.promptContent?.substring(0, 100))
savePromptForm.value = {
id: null,
name: savePromptForm.value.name || '',
category: savePromptForm.value.category || '',
content: props.promptContent,
}
}
}
})
@@ -40,49 +69,61 @@ async function handleSave() {
message.warning('请输入提示词名称')
return
}
if (!savePromptForm.value.content.trim()) {
message.warning('提示词内容不能为空')
return
}
const userId = Number(userStore.userId)
if (!userId) {
message.error('无法获取用户ID请先登录')
return
}
savingPrompt.value = true
try {
// content
const content = savePromptForm.value.content?.trim() || ''
if (!content) {
message.error('提示词内容不能为空')
savingPrompt.value = false
return
}
const payload = {
userId: userId,
name: savePromptForm.value.name.trim(),
content: content, // content
category: savePromptForm.value.category?.trim() || null,
isPublic: false,
sort: 0,
useCount: 0,
status: 1,
let response
if (isEditMode.value) {
//
const payload = {
id: savePromptForm.value.id,
userId: userId,
name: savePromptForm.value.name.trim(),
content: content,
category: savePromptForm.value.category?.trim() || null,
}
console.log('[SavePromptModal] 更新请求参数:', payload)
response = await UserPromptApi.updateUserPrompt(payload)
} else {
//
const payload = {
userId: userId,
name: savePromptForm.value.name.trim(),
content: content,
category: savePromptForm.value.category?.trim() || null,
isPublic: false,
sort: 0,
useCount: 0,
status: 1,
}
console.log('[SavePromptModal] 创建请求参数:', payload)
response = await UserPromptApi.createUserPrompt(payload)
}
console.log('[SavePromptModal] 发送请求参数:', payload)
const response = await UserPromptApi.createUserPrompt(payload)
if (response && (response.code === 0 || response.code === 200)) {
message.success('提示词保存成功')
message.success(isEditMode.value ? '提示词更新成功' : '提示词保存成功')
emit('update:visible', false)
emit('success')
//
savePromptForm.value = {
id: null,
name: '',
category: '',
content: '',
@@ -106,21 +147,21 @@ function handleCancel() {
<template>
<a-modal
:open="visible"
title="保存提示词"
:title="modalTitle"
:width="600"
:maskClosable="false"
@cancel="handleCancel">
<a-form :model="savePromptForm" layout="vertical">
<a-form-item label="提示词名称" required>
<a-input
v-model:value="savePromptForm.name"
<a-input
v-model:value="savePromptForm.name"
placeholder="请输入提示词名称"
:maxlength="50"
show-count />
</a-form-item>
<a-form-item label="分类/标签">
<a-input
v-model:value="savePromptForm.category"
<a-input
v-model:value="savePromptForm.category"
placeholder="可选:输入分类或标签"
:maxlength="20" />
</a-form-item>
@@ -131,17 +172,16 @@ function handleCancel() {
placeholder="提示词内容" />
</a-form-item>
</a-form>
<template #footer>
<a-space>
<a-button @click="handleCancel">取消</a-button>
<a-button
type="primary"
<a-button
type="primary"
:loading="savingPrompt"
:disabled="!savePromptForm.name.trim()"
@click="handleSave">保存</a-button>
@click="handleSave">{{ isEditMode ? '更新' : '保存' }}</a-button>
</a-space>
</template>
</a-modal>
</template>

View File

@@ -10,7 +10,7 @@ import {
} from '@ant-design/icons-vue'
import { UserPromptApi } from '@/api/userPrompt'
import SavePromptModal from '@/views/content-style/components/SavePromptModal.vue'
import SavePromptModal from '@/components/SavePromptModal.vue'
// Props
const props = defineProps({

View File

@@ -14,7 +14,7 @@ import { copyToClipboard } from '@/utils/clipboard'
import BenchmarkForm from './components/BenchmarkForm.vue'
import BenchmarkTable from './components/BenchmarkTable.vue'
import BatchAnalyzeModal from './components/BatchAnalyzeModal.vue'
import SavePromptModal from './components/SavePromptModal.vue'
import SavePromptModal from '@/components/SavePromptModal.vue'
const router = useRouter()
const promptStore = usePromptStore()