feat: add OpenAI markdown format endpoint

This commit is contained in:
Tim
2025-07-14 17:39:52 +08:00
parent 1974b60bdd
commit 02ddbb3fda
6 changed files with 158 additions and 2 deletions

View File

@@ -1,6 +1,9 @@
<template>
<div class="post-editor-container">
<div :id="editorId" ref="vditorElement"></div>
<div v-if="loading" class="editor-loading-overlay">
<i class="fa-solid fa-spinner fa-spin"></i>
</div>
</div>
</template>
@@ -22,11 +25,24 @@ export default {
editorId: {
type: String,
default: () => 'post-editor-' + Math.random().toString(36).slice(2)
},
loading: {
type: Boolean,
default: false
}
},
setup(props, { emit }) {
const vditorInstance = ref(null)
watch(
() => props.loading,
val => {
if (vditorInstance.value && typeof vditorInstance.value.disabled === 'function') {
vditorInstance.value.disabled(val)
}
}
)
watch(
() => props.modelValue,
val => {
@@ -109,5 +125,19 @@ export default {
<style scoped>
.post-editor-container {
border: 1px solid #e2e2e2;
position: relative;
}
.editor-loading-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.6);
display: flex;
align-items: center;
justify-content: center;
pointer-events: all;
z-index: 10;
}
</style>

View File

@@ -3,7 +3,7 @@
<div class="new-post-form">
<input class="post-title-input" v-model="title" placeholder="标题" />
<div class="post-editor-container">
<PostEditor v-model="content" />
<PostEditor v-model="content" :loading="isAiLoading" />
</div>
<div class="post-options">
<div class="post-options-left">
@@ -47,6 +47,7 @@ export default {
const selectedCategory = ref('')
const selectedTags = ref([])
const isWaitingPosting = ref(false)
const isAiLoading = ref(false)
const loadDraft = async () => {
const token = getToken()
@@ -154,6 +155,31 @@ export default {
}
}
}
const aiGenerate = async () => {
if (!content.value.trim()) {
toast.error('内容为空,无法优化')
return
}
isAiLoading.value = true
try {
const res = await fetch(`${API_BASE_URL}/api/ai/format`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: content.value })
})
if (res.ok) {
const data = await res.json()
content.value = data.content || ''
} else {
toast.error('AI 优化失败')
}
} catch (e) {
toast.error('AI 优化失败')
} finally {
isAiLoading.value = false
}
}
const submitPost = async () => {
if (!title.value.trim()) {
toast.error('标题不能为空')
@@ -203,7 +229,7 @@ export default {
isWaitingPosting.value = false
}
}
return { title, content, selectedCategory, selectedTags, submitPost, saveDraft, clearPost, isWaitingPosting }
return { title, content, selectedCategory, selectedTags, submitPost, saveDraft, clearPost, isWaitingPosting, aiGenerate, isAiLoading }
}
}
</script>