feat: ai generate

This commit is contained in:
tim
2025-07-14 18:01:51 +08:00
parent c69eeba1a9
commit d7287deadb
5 changed files with 19 additions and 7 deletions

View File

@@ -56,7 +56,7 @@ OpenIsle 基于 Spring Boot 构建,提供社区后台常见的注册、登录
- `CAPTCHA_POST_ENABLED`:发帖是否需要验证码 - `CAPTCHA_POST_ENABLED`:发帖是否需要验证码
- `CAPTCHA_COMMENT_ENABLED`:评论是否需要验证码 - `CAPTCHA_COMMENT_ENABLED`:评论是否需要验证码
- `OPENAI_API_KEY`OpenAI 接口密钥 - `OPENAI_API_KEY`OpenAI 接口密钥
- `OPENAI_MODEL`:调用的模型名称,默认为 `gpt-3.5-turbo` - `OPENAI_MODEL`:调用的模型名称,默认为 `gpt-4o`
2. 启动项目: 2. 启动项目:
```bash ```bash

View File

@@ -2,7 +2,7 @@
<div class="post-editor-container"> <div class="post-editor-container">
<div :id="editorId" ref="vditorElement"></div> <div :id="editorId" ref="vditorElement"></div>
<div v-if="loading" class="editor-loading-overlay"> <div v-if="loading" class="editor-loading-overlay">
<i class="fa-solid fa-spinner fa-spin"></i> <l-hatch size="28" stroke="4" speed="3.5" color="var(--primary-color)"></l-hatch>
</div> </div>
</div> </div>
</template> </template>
@@ -13,6 +13,9 @@ import Vditor from 'vditor'
import 'vditor/dist/index.css' import 'vditor/dist/index.css'
import { API_BASE_URL } from '../main' import { API_BASE_URL } from '../main'
import { getToken } from '../utils/auth' import { getToken } from '../utils/auth'
import { hatch } from 'ldrs'
hatch.register()
export default { export default {
name: 'PostEditor', name: 'PostEditor',
@@ -37,8 +40,10 @@ export default {
watch( watch(
() => props.loading, () => props.loading,
val => { val => {
if (vditorInstance.value && typeof vditorInstance.value.disabled === 'function') { if (val) {
vditorInstance.value.disabled(val) vditorInstance.value.disabled()
} else {
vditorInstance.value.enable()
} }
} }
) )
@@ -127,6 +132,7 @@ export default {
border: 1px solid #e2e2e2; border: 1px solid #e2e2e2;
position: relative; position: relative;
} }
.editor-loading-overlay { .editor-loading-overlay {
position: absolute; position: absolute;
top: 0; top: 0;

View File

@@ -163,9 +163,14 @@ export default {
} }
isAiLoading.value = true isAiLoading.value = true
try { try {
toast.info('AI 优化中...')
const token = getToken()
const res = await fetch(`${API_BASE_URL}/api/ai/format`, { const res = await fetch(`${API_BASE_URL}/api/ai/format`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({ text: content.value }) body: JSON.stringify({ text: content.value })
}) })
if (res.ok) { if (res.ok) {
@@ -180,6 +185,7 @@ export default {
isAiLoading.value = false isAiLoading.value = false
} }
} }
const submitPost = async () => { const submitPost = async () => {
if (!title.value.trim()) { if (!title.value.trim()) {
toast.error('标题不能为空') toast.error('标题不能为空')

View File

@@ -17,7 +17,7 @@ public class OpenAiService {
@Value("${openai.api-key:}") @Value("${openai.api-key:}")
private String apiKey; private String apiKey;
@Value("${openai.model:gpt-3.5-turbo}") @Value("${openai.model:gpt-4o}")
private String model; private String model;
private final RestTemplate restTemplate = new RestTemplate(); private final RestTemplate restTemplate = new RestTemplate();

View File

@@ -48,4 +48,4 @@ cos.bucket-name=${COS_BUCKET_NAME:}
google.client-id=${GOOGLE_CLIENT_ID:} google.client-id=${GOOGLE_CLIENT_ID:}
# OpenAI configuration # OpenAI configuration
openai.api-key=${OPENAI_API_KEY:} openai.api-key=${OPENAI_API_KEY:}
openai.model=${OPENAI_MODEL:gpt-3.5-turbo} openai.model=${OPENAI_MODEL:gpt-4o}