Compare commits

..

No commits in common. "develop_xxy" and "v5.1.0" have entirely different histories.

11 changed files with 180 additions and 309 deletions

View File

@ -11,13 +11,13 @@ dataSources:
ds_1: ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/novel_plus?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai jdbcUrl: jdbc:mysql://localhost:3306/novel_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root username: root
password: test123456 password: test123456
ds_2: ds_2:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/information_schema?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai url: jdbc:mysql://localhost:3306/information_schema?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root username: root
password: test123456 password: test123456
# 规则配置 # 规则配置

View File

@ -1,53 +0,0 @@
package com.java2nb.novel.core.advice;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.util.Objects;
/**
* 在对 RestController 返回对象 json 序列化时将所有 Long 类型转为 String 类型返回避免前端数据精度丢失的问题
* 取代 spring.jackson.generator.write-numbers-as-strings=true 配置避免影响全局的 ObjectMapper
*
* @author xiongxiaoyang
* */
@RestControllerAdvice
public class CustomResponseBodyAdvice implements ResponseBodyAdvice<Object> {
private final ObjectMapper customObjectMapper;
public CustomResponseBodyAdvice(Jackson2ObjectMapperBuilder builder) {
customObjectMapper = builder.createXmlMapper(false).build();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
customObjectMapper.registerModule(simpleModule);
}
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// 返回 true 表示对所有 Controller 的响应都生效
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
// 使用自定义的 ObjectMapper 序列化响应体
if(Objects.nonNull(body)) {
return customObjectMapper.valueToTree(body);
}else{
return null;
}
}
}

View File

@ -69,8 +69,4 @@ public interface CacheKey {
* 测试爬虫规则缓存 * 测试爬虫规则缓存
*/ */
String BOOK_TEST_PARSE = "testParse"; String BOOK_TEST_PARSE = "testParse";
/**
* AI生成图片
* */
String AI_GEN_PIC = "aiGenPic";
} }

View File

@ -6,6 +6,11 @@ spring:
mode: LEGACYHTML5 #去除thymeleaf的html严格校验thymeleaf.mode=LEGACYHTML5 mode: LEGACYHTML5 #去除thymeleaf的html严格校验thymeleaf.mode=LEGACYHTML5
cache: false # 是否开启模板缓存默认true,建议在开发时关闭缓存,不然没法看到实时 cache: false # 是否开启模板缓存默认true,建议在开发时关闭缓存,不然没法看到实时
# 将所有数字转为 String 类型返回避免前端数据精度丢失的问题
jackson:
generator:
write-numbers-as-strings: true
#上传文件的最大值100M #上传文件的最大值100M
servlet: servlet:
multipart: multipart:

View File

@ -17,14 +17,7 @@ import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import java.util.Date; import java.util.Date;
@ -35,7 +28,7 @@ import java.util.Date;
@RestController @RestController
@Slf4j @Slf4j
@RequiredArgsConstructor @RequiredArgsConstructor
public class AuthorController extends BaseController { public class AuthorController extends BaseController{
private final AuthorService authorService; private final AuthorService authorService;
@ -43,64 +36,62 @@ public class AuthorController extends BaseController {
private final ChatClient chatClient; private final ChatClient chatClient;
private final OpenAiChatModel chatModel;
/** /**
* 校验笔名是否存在 * 校验笔名是否存在
*/ * */
@GetMapping("checkPenName") @GetMapping("checkPenName")
public RestResult<Boolean> checkPenName(String penName) { public RestResult<Boolean> checkPenName(String penName){
return RestResult.ok(authorService.checkPenName(penName)); return RestResult.ok(authorService.checkPenName(penName));
} }
/** /**
* 作家发布小说分页列表查询 * 作家发布小说分页列表查询
*/ * */
@GetMapping("listBookByPage") @GetMapping("listBookByPage")
public RestResult<PageBean<Book>> listBookByPage(@RequestParam(value = "curr", defaultValue = "1") int page, public RestResult<PageBean<Book>> listBookByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int pageSize , HttpServletRequest request){
@RequestParam(value = "limit", defaultValue = "10") int pageSize, HttpServletRequest request) {
return RestResult.ok(bookService.listBookPageByUserId(getUserDetails(request).getId(), page, pageSize)); return RestResult.ok(bookService.listBookPageByUserId(getUserDetails(request).getId(),page,pageSize));
} }
/** /**
* 发布小说 * 发布小说
*/ * */
@PostMapping("addBook") @PostMapping("addBook")
public RestResult<Void> addBook(@RequestParam("bookDesc") String bookDesc, Book book, HttpServletRequest request) { public RestResult<Void> addBook(@RequestParam("bookDesc") String bookDesc, Book book, HttpServletRequest request){
Author author = checkAuthor(request); Author author = checkAuthor(request);
//bookDesc不能使用book对象来接收否则会自动去掉前面的空格 //bookDesc不能使用book对象来接收否则会自动去掉前面的空格
book.setBookDesc(bookDesc book.setBookDesc(bookDesc
.replaceAll("\\n", "<br>") .replaceAll("\\n","<br>")
.replaceAll("\\s", "&nbsp;")); .replaceAll("\\s","&nbsp;"));
//发布小说 //发布小说
bookService.addBook(book, author.getId(), author.getPenName()); bookService.addBook(book,author.getId(),author.getPenName());
return RestResult.ok(); return RestResult.ok();
} }
/** /**
* 更新小说状态,上架或下架 * 更新小说状态,上架或下架
*/ * */
@PostMapping("updateBookStatus") @PostMapping("updateBookStatus")
public RestResult<Void> updateBookStatus(Long bookId, Byte status, HttpServletRequest request) { public RestResult<Void> updateBookStatus(Long bookId,Byte status,HttpServletRequest request){
Author author = checkAuthor(request); Author author = checkAuthor(request);
//更新小说状态,上架或下架 //更新小说状态,上架或下架
bookService.updateBookStatus(bookId, status, author.getId()); bookService.updateBookStatus(bookId,status,author.getId());
return RestResult.ok(); return RestResult.ok();
} }
/** /**
* 删除章节 * 删除章节
*/ */
@DeleteMapping("deleteIndex/{indexId}") @DeleteMapping("deleteIndex/{indexId}")
public RestResult<Void> deleteIndex(@PathVariable("indexId") Long indexId, HttpServletRequest request) { public RestResult<Void> deleteIndex(@PathVariable("indexId") Long indexId, HttpServletRequest request) {
Author author = checkAuthor(request); Author author = checkAuthor(request);
@ -114,7 +105,7 @@ public class AuthorController extends BaseController {
* 更新章节名 * 更新章节名
*/ */
@PostMapping("updateIndexName") @PostMapping("updateIndexName")
public RestResult<Void> updateIndexName(Long indexId, String indexName, HttpServletRequest request) { public RestResult<Void> updateIndexName(Long indexId, String indexName, HttpServletRequest request) {
Author author = checkAuthor(request); Author author = checkAuthor(request);
@ -125,18 +116,19 @@ public class AuthorController extends BaseController {
} }
/** /**
* 发布章节内容 * 发布章节内容
*/ */
@PostMapping("addBookContent") @PostMapping("addBookContent")
public RestResult<Void> addBookContent(Long bookId, String indexName, String content, Byte isVip, public RestResult<Void> addBookContent(Long bookId, String indexName, String content,Byte isVip, HttpServletRequest request) {
HttpServletRequest request) {
Author author = checkAuthor(request); Author author = checkAuthor(request);
content = content.replaceAll("\\n", "<br>") content = content.replaceAll("\\n", "<br>")
.replaceAll("\\s", "&nbsp;"); .replaceAll("\\s", "&nbsp;");
//发布章节内容 //发布章节内容
bookService.addBookContent(bookId, indexName, content, isVip, author.getId()); bookService.addBookContent(bookId, indexName, content,isVip, author.getId());
return RestResult.ok(); return RestResult.ok();
} }
@ -145,14 +137,14 @@ public class AuthorController extends BaseController {
* 查询章节内容 * 查询章节内容
*/ */
@GetMapping("queryIndexContent/{indexId}") @GetMapping("queryIndexContent/{indexId}")
public RestResult<String> queryIndexContent(@PathVariable("indexId") Long indexId, HttpServletRequest request) { public RestResult<String> queryIndexContent(@PathVariable("indexId") Long indexId, HttpServletRequest request) {
Author author = checkAuthor(request); Author author = checkAuthor(request);
String content = bookService.queryIndexContent(indexId, author.getId()); String content = bookService.queryIndexContent(indexId, author.getId());
content = content.replaceAll("<br>", "\n") content = content.replaceAll("<br>", "\n")
.replaceAll("&nbsp;", " "); .replaceAll("&nbsp;", " ");
return RestResult.ok(content); return RestResult.ok(content);
} }
@ -161,12 +153,11 @@ public class AuthorController extends BaseController {
* 更新章节内容 * 更新章节内容
*/ */
@PostMapping("updateBookContent") @PostMapping("updateBookContent")
public RestResult<Void> updateBookContent(Long indexId, String indexName, String content, public RestResult<Void> updateBookContent(Long indexId, String indexName, String content, HttpServletRequest request) {
HttpServletRequest request) {
Author author = checkAuthor(request); Author author = checkAuthor(request);
content = content.replaceAll("\\n", "<br>") content = content.replaceAll("\\n", "<br>")
.replaceAll("\\s", "&nbsp;"); .replaceAll("\\s", "&nbsp;");
//更新章节内容 //更新章节内容
bookService.updateBookContent(indexId, indexName, content, author.getId()); bookService.updateBookContent(indexId, indexName, content, author.getId());
@ -177,44 +168,38 @@ public class AuthorController extends BaseController {
* 修改小说封面 * 修改小说封面
*/ */
@PostMapping("updateBookPic") @PostMapping("updateBookPic")
public RestResult<Void> updateBookPic(@RequestParam("bookId") Long bookId, @RequestParam("bookPic") String bookPic, public RestResult<Void> updateBookPic(@RequestParam("bookId") Long bookId,@RequestParam("bookPic") String bookPic,HttpServletRequest request) {
HttpServletRequest request) {
Author author = checkAuthor(request); Author author = checkAuthor(request);
bookService.updateBookPic(bookId, bookPic, author.getId()); bookService.updateBookPic(bookId,bookPic, author.getId());
return RestResult.ok(); return RestResult.ok();
} }
/** /**
* 作家日收入统计数据分页列表查询 * 作家日收入统计数据分页列表查询
*/ * */
@GetMapping("listIncomeDailyByPage") @GetMapping("listIncomeDailyByPage")
public RestResult<PageBean<AuthorIncomeDetail>> listIncomeDailyByPage( public RestResult<PageBean<AuthorIncomeDetail>> listIncomeDailyByPage(@RequestParam(value = "curr", defaultValue = "1") int page,
@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int pageSize ,
@RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(value = "bookId", defaultValue = "0") Long bookId,
@RequestParam(value = "bookId", defaultValue = "0") Long bookId, @RequestParam(value = "startTime",defaultValue = "2020-05-01") Date startTime,
@RequestParam(value = "startTime", defaultValue = "2020-05-01") Date startTime, @RequestParam(value = "endTime",defaultValue = "2030-01-01") Date endTime,
@RequestParam(value = "endTime", defaultValue = "2030-01-01") Date endTime, HttpServletRequest request){
HttpServletRequest request) {
return RestResult.ok( return RestResult.ok(authorService.listIncomeDailyByPage(page,pageSize,getUserDetails(request).getId(),bookId,startTime,endTime));
authorService.listIncomeDailyByPage(page, pageSize, getUserDetails(request).getId(), bookId, startTime,
endTime));
} }
/** /**
* 作家月收入统计数据分页列表查询 * 作家月收入统计数据分页列表查询
*/ * */
@GetMapping("listIncomeMonthByPage") @GetMapping("listIncomeMonthByPage")
public RestResult<PageBean<AuthorIncome>> listIncomeMonthByPage( public RestResult<PageBean<AuthorIncome>> listIncomeMonthByPage(@RequestParam(value = "curr", defaultValue = "1") int page,
@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int pageSize ,
@RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(value = "bookId", defaultValue = "0") Long bookId,
@RequestParam(value = "bookId", defaultValue = "0") Long bookId, HttpServletRequest request){
HttpServletRequest request) {
return RestResult.ok( return RestResult.ok(authorService.listIncomeMonthByPage(page,pageSize,getUserDetails(request).getId(),bookId));
authorService.listIncomeMonthByPage(page, pageSize, getUserDetails(request).getId(), bookId));
} }
private Author checkAuthor(HttpServletRequest request) { private Author checkAuthor(HttpServletRequest request) {
@ -233,29 +218,22 @@ public class AuthorController extends BaseController {
throw new BusinessException(ResponseStatus.AUTHOR_STATUS_FORBIDDEN); throw new BusinessException(ResponseStatus.AUTHOR_STATUS_FORBIDDEN);
} }
return author; return author;
} }
/**
* 查询AI生成图片
*/
@GetMapping("queryAiGenPic")
public RestResult<String> queryAiGenPic(@RequestParam("bookId") Long bookId) {
return RestResult.ok(bookService.queryAiGenPic(bookId));
}
/** /**
* AI扩写 * AI扩写
*/ */
@PostMapping("ai/expand") @PostMapping("ai/expand")
public RestResult<String> expandText(@RequestParam("text") String text, @RequestParam("ratio") Double ratio) { public RestResult<String> expandText(@RequestParam("text") String text, @RequestParam("ratio") Double ratio) {
String prompt = "请将以下文本扩写为原长度的" + ratio / 100 + "倍:" + text; String prompt = "请将以下文本扩写为原长度的" + ratio/100 + "倍:" + text;
return RestResult.ok(chatClient.prompt() return RestResult.ok(chatClient.prompt()
.user(prompt) .user(prompt)
.call() .call()
.content()); .content());
} }
/** /**
@ -263,11 +241,11 @@ public class AuthorController extends BaseController {
*/ */
@PostMapping("ai/condense") @PostMapping("ai/condense")
public RestResult<String> condenseText(@RequestParam("text") String text, @RequestParam("ratio") Integer ratio) { public RestResult<String> condenseText(@RequestParam("text") String text, @RequestParam("ratio") Integer ratio) {
String prompt = "请将以下文本缩写为原长度的" + 100 / ratio + "分之一:" + text; String prompt = "请将以下文本缩写为原长度的" + 100/ratio + "分之一:" + text;
return RestResult.ok(chatClient.prompt() return RestResult.ok(chatClient.prompt()
.user(prompt) .user(prompt)
.call() .call()
.content()); .content());
} }
/** /**
@ -277,9 +255,9 @@ public class AuthorController extends BaseController {
public RestResult<String> continueText(@RequestParam("text") String text, @RequestParam("length") Integer length) { public RestResult<String> continueText(@RequestParam("text") String text, @RequestParam("length") Integer length) {
String prompt = "请续写以下文本,续写长度约为" + length + "字:" + text; String prompt = "请续写以下文本,续写长度约为" + length + "字:" + text;
return RestResult.ok(chatClient.prompt() return RestResult.ok(chatClient.prompt()
.user(prompt) .user(prompt)
.call() .call()
.content()); .content());
} }
/** /**
@ -289,57 +267,12 @@ public class AuthorController extends BaseController {
public RestResult<String> polishText(@RequestParam("text") String text) { public RestResult<String> polishText(@RequestParam("text") String text) {
String prompt = "请润色优化以下文本,保持原意:" + text; String prompt = "请润色优化以下文本,保持原意:" + text;
return RestResult.ok(chatClient.prompt() return RestResult.ok(chatClient.prompt()
.user(prompt) .user(prompt)
.call() .call()
.content()); .content());
} }
/**
* AI扩写
*/
@GetMapping(value = "ai/stream/expand", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamExpandText(@RequestParam("text") String text, @RequestParam("ratio") Double ratio) {
String prompt = "请将以下文本扩写为原长度的" + ratio / 100 + "倍:" + text;
return chatClient.prompt()
.user(prompt)
.stream()
.content();
}
/**
* AI缩写
*/
@GetMapping(value = "ai/stream/condense", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamCondenseText(@RequestParam("text") String text, @RequestParam("ratio") Integer ratio) {
String prompt = "请将以下文本缩写为原长度的" + 100 / ratio + "分之一:" + text;
return chatClient.prompt()
.user(prompt)
.stream()
.content();
}
/**
* AI续写
*/
@GetMapping(value = "ai/stream/continue", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamContinueText(@RequestParam("text") String text, @RequestParam("length") Integer length) {
String prompt = "请续写以下文本,续写长度约为" + length + "字:" + text;
return chatClient.prompt()
.user(prompt)
.stream()
.content();
}
/**
* AI润色
*/
@GetMapping(value = "/ai/stream/polish", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamPolishText(@RequestParam("text") String text) {
String prompt = "请润色优化以下文本,保持原意:" + text;
return chatClient.prompt()
.user(prompt)
.stream()
.content();
}
} }

View File

@ -18,7 +18,16 @@ import org.springframework.web.client.RestClient;
public class AiConfig { public class AiConfig {
/** /**
* 配置自定义的 RestClientBuilder 对象 * 目的配置自定义的 RestClientBuilder 对象
* <p>
* 原因Spring AI 框架的 ChatClient 内部通过 RestClientSpring Framework 6 Spring Boot 3 中引入 发起 HTTP REST 请求与远程的大模型服务进行通信
* 如果项目中没有配置自定义的 RestClientBuilder 对象 那么在 RestClient 的自动配置类 org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration
* 中配置的 RestClientBuilder 对象会使用 Spring 容器中提供的 HttpMessageConverters 由于本项目中配置了 spring.jackson.generator.write-numbers-as-strings
* = true 所以 Spring 容器中的 HttpMessageConverters RestClient 发起 HTTP REST 请求转换 Java 对象为 JSON 字符串时会自动将 Number 类型的
* Java 对象属性转换为字符串而导致请求参数错误
* <p>
* 示例"temperature": 0.7 ="temperature": "0.7"
* {"code":20015,"message":"The parameter is invalid. Please check again.","data":null}
*/ */
@Bean @Bean
public RestClient.Builder restClientBuilder() { public RestClient.Builder restClientBuilder() {

View File

@ -290,9 +290,4 @@ public interface BookService {
* @param authorId * @param authorId
*/ */
void updateBookPic(Long bookId, String bookPic, Long authorId); void updateBookPic(Long bookId, String bookPic, Long authorId);
/**
* 查询AI生成图片
*/
String queryAiGenPic(Long bookId);
} }

View File

@ -116,7 +116,7 @@ public class BookServiceImpl implements BookService {
} }
result = new ObjectMapper().writeValueAsString( result = new ObjectMapper().writeValueAsString(
list.stream().collect(Collectors.groupingBy(BookSettingVO::getType))); list.stream().collect(Collectors.groupingBy(BookSettingVO::getType)));
cacheService.set(CacheKey.INDEX_BOOK_SETTINGS_KEY, result, 3600 * 24); cacheService.set(CacheKey.INDEX_BOOK_SETTINGS_KEY, result);
} }
return new ObjectMapper().readValue(result, Map.class); return new ObjectMapper().readValue(result, Map.class);
} }
@ -559,7 +559,6 @@ public class BookServiceImpl implements BookService {
.where(id, isEqualTo(book.getId())) .where(id, isEqualTo(book.getId()))
.build() .build()
.render(RenderingStrategies.MYBATIS3)); .render(RenderingStrategies.MYBATIS3));
cacheService.set(CacheKey.AI_GEN_PIC + book.getId(), picUrl, 60 * 60);
}); });
} }
} }
@ -883,10 +882,5 @@ public class BookServiceImpl implements BookService {
.render(RenderingStrategies.MYBATIS3)); .render(RenderingStrategies.MYBATIS3));
} }
@Override
public String queryAiGenPic(Long bookId) {
return cacheService.get(CacheKey.AI_GEN_PIC + bookId);
}
}
}

View File

@ -15,7 +15,7 @@
padding: 20px; padding: 20px;
background: #fff; background: #fff;
border-radius: 8px; border-radius: 8px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1); box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
} }
/* 文本域样式 */ /* 文本域样式 */
@ -124,10 +124,10 @@
<li id="contentLi" style="width: 500px"> <li id="contentLi" style="width: 500px">
<div class="editor-container"> <div class="editor-container">
<div class="ai-toolbar"> <div class="ai-toolbar">
<a class="ai-link expand" data-type="stream/expand">AI扩写</a> <a class="ai-link expand" data-type="expand">AI扩写</a>
<a class="ai-link condense" data-type="stream/condense">AI缩写</a> <a class="ai-link condense" data-type="condense">AI缩写</a>
<a class="ai-link continue" data-type="stream/continue">AI续写</a> <a class="ai-link continue" data-type="continue">AI续写</a>
<a class="ai-link polish" data-type="stream/polish">AI润色</a> <a class="ai-link polish" data-type="polish">AI润色</a>
</div> </div>
<textarea id="bookContent" name="bookContent" <textarea id="bookContent" name="bookContent"
placeholder="请输入文本内容..."></textarea> placeholder="请输入文本内容..."></textarea>
@ -268,7 +268,7 @@
}, speed); }, speed);
} }
$('.ai-toolbar .ai-link').click(function (e) { $('.ai-toolbar .ai-link').click(function(e){
e.preventDefault(); // 阻止默认链接行为 e.preventDefault(); // 阻止默认链接行为
const type = $(this).data('type'); const type = $(this).data('type');
const textarea = $('#bookContent'); const textarea = $('#bookContent');
@ -284,27 +284,27 @@
// 参数配置 // 参数配置
let params = {text: selectedText}; let params = {text: selectedText};
if (type === 'stream/expand' || type === 'stream/condense') { if(type === 'expand' || type === 'condense'){
layer.prompt({ layer.prompt({
title: '请输入比例', title: '请输入比例',
value: 2, value: 2,
btn: ['确定', '取消'], btn: ['确定', '取消'],
btn2: function () { btn2: function(){
layer.close(loading); layer.close(loading);
}, },
cancel: function () { cancel: function(){
layer.close(loading); layer.close(loading);
} }
}, function (value, index) { }, function(value, index){
if (isNaN(Number(value)) || isNaN(parseFloat(value))) { if(isNaN(Number(value)) || isNaN(parseFloat(value))){
layer.msg('请输入正确的比例'); layer.msg('请输入正确的比例');
return; return;
} }
if (type === 'stream/expand' && value <= 1) { if(type === 'expand' && value <= 1){
layer.msg('请输入正确的比例'); layer.msg('请输入正确的比例');
return; return;
} }
if (type === 'stream/condense' && (value <= 0 || value >= 1)) { if(type === 'condense' && (value <=0 || value >= 1)){
layer.msg('请输入正确的比例'); layer.msg('请输入正确的比例');
return; return;
} }
@ -313,19 +313,19 @@
sendRequest(type, params, loading, textarea); sendRequest(type, params, loading, textarea);
}); });
return; return;
} else if (type === 'stream/continue') { }else if(type === 'continue'){
layer.prompt({ layer.prompt({
title: '请输入续写长度字数', title: '请输入续写长度字数',
value: 200, value: 200,
btn: ['确定', '取消'], btn: ['确定', '取消'],
btn2: function () { btn2: function(){
layer.close(loading); layer.close(loading);
}, },
cancel: function () { cancel: function(){
layer.close(loading); layer.close(loading);
} }
}, function (value, index) { }, function(value, index){
if (!Number.isInteger(Number(value)) || value <= 0) { if(!Number.isInteger(Number(value)) || value <= 0){
layer.msg('请输入正确的长度'); layer.msg('请输入正确的长度');
return; return;
} }
@ -339,28 +339,26 @@
sendRequest(type, params, loading, textarea); sendRequest(type, params, loading, textarea);
}); });
function sendRequest(type, params, loading, textarea) { function sendRequest(type, params, loading, textarea){
const url = `/author/ai/${type}?text=${encodeURIComponent(params.text)}&ratio=${params.ratio}&length=${params.length}`; $.ajax({
const eventSource = new EventSource(url); url: '/author/ai/' + type,
type: 'POST',
// 监听消息事件 data: params,
eventSource.onmessage = function (event) { success: function(res){
layer.close(loading); layer.close(loading);
const data = event.data; // 将生成的内容追加到文本末尾
console.log('Received data:', data); if(res.code == '200'){
const newText = "\n\n【AI生成内容】" + res.data; // 添加换行符分隔
textarea.val(textarea.val() + data); typeWriter(textarea, newText); // 使用打字机效果
// 滚动到底部 }else{
textarea.scrollTop(textarea[0].scrollHeight); layer.msg('AI内容生成失败请稍后重试');
}; }
},
// 监听错误事件 error: function(){
eventSource.onerror = function (error) { layer.msg('请求失败请稍后重试');
layer.close(loading); layer.close(loading);
console.error('EventSource failed:', error); }
eventSource.close(); // 关闭连接 });
};
} }
</script> </script>

View File

@ -51,7 +51,8 @@
</div> </div>
<div class="my_r"> <div class="my_r">
<div id="noContentDiv"> <div id="noContentDiv">
<div class="tc" style="margin-top: 200px"><a href="/author/book_add.html" class="btn_red">创建作品</a></div> <div class="tc" style="margin-top: 200px"><a href="/author/book_add.html" class="btn_red">创建作品</a>
</div>
</div> </div>
<div class="my_bookshelf" id="hasContentDiv" style="display: none"> <div class="my_bookshelf" id="hasContentDiv" style="display: none">
@ -142,12 +143,13 @@
<script src="/javascript/common.js" type="text/javascript"></script> <script src="/javascript/common.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript"> <script language="javascript" type="text/javascript">
var coverUpdateInterval; var searchCount = 0;
var timeout;
search(1, 5); search(1, 5);
function search(curr, limit) { function search(curr, limit) {
clearInterval(coverUpdateInterval); searchCount++;
clearTimeout(timeout);
$.ajax({ $.ajax({
type: "get", type: "get",
url: "/author/listBookByPage", url: "/author/listBookByPage",
@ -157,25 +159,7 @@
if (data.code == 200) { if (data.code == 200) {
var bookList = data.data.list; var bookList = data.data.list;
if (bookList.length > 0) { if (bookList.length > 0) {
if(curr == 1 && bookList[0].picUrl == '/images/default.gif'){ var aiPicGenerating = bookList[0].picUrl == '/images/default.gif'
coverUpdateInterval = setInterval(function(){
$.ajax({
type: "get",
url: "/author/queryAiGenPic",
data: {'bookId': bookList[0].id},
dataType: "json",
success: function (data) {
if(data.code == 200 && data.data){
$("#cover"+bookList[0].id).attr("src", data.data);
clearInterval(coverUpdateInterval);
}
}
});
}, 3000);
setTimeout(() => {
clearInterval(coverUpdateInterval);
}, 10000);
}
$("#hasContentDiv").css("display", "block"); $("#hasContentDiv").css("display", "block");
$("#noContentDiv").css("display", "none"); $("#noContentDiv").css("display", "none");
var bookListHtml = ""; var bookListHtml = "";
@ -187,12 +171,15 @@
" </td>\n" +*/ " </td>\n" +*/
" <td style=\"position: relative\" class=\"goread\">\n" + " <td style=\"position: relative\" class=\"goread\">\n" +
"<input class=\"opacity\" onchange=\"picChange('" + book.id + "')\"\n" + "<input class=\"opacity\" onchange=\"picChange('" + book.id + "'," + i + ")\"\n" +
" type=\"file\" id=\"file" + book.id + "\" name=\"file\"\n" + " type=\"file\" id=\"file" + i + "\" name=\"file\"\n" +
" title=\"点击上传图片\"\n" + " title=\"点击上传图片\"\n" +
" style=\"z-index: 100;cursor: pointer;left: 30px; top: 0px; width: 60px; height: 80px; opacity: 0; position: absolute; \"\n" + " style=\"z-index: 100;cursor: pointer;left: 30px; top: 0px; width: 60px; height: 80px; opacity: 0; position: absolute; \"\n" +
" />" + " />" +
"<img id=\"cover" + book.id + "\" width='50' height='70' src='" + book.picUrl + "'/><br/>" + " " + book.bookName + "</td>\n" + "<img width='50' height='70' src='" + book.picUrl + "'/><br/>" +
" " + book.bookName + "</td>\n" +
" <td class=\"goread\" >" " <td class=\"goread\" >"
+ book.catName + "</td>\n" + + book.catName + "</td>\n" +
@ -244,6 +231,12 @@
}); });
}); });
if (curr === 1 && aiPicGenerating && searchCount < 10) {
timeout = setTimeout(function () {
search(curr, limit);
}, 3000);
}
} }
@ -295,20 +288,19 @@
} }
function picChange(bookId) { function picChange(bookId, i) {
var file = $("#file" + bookId).val(); //文件名称 var file = $("#file" + i).val(); //文件名称
if (file != "") { if (file != "") {
if (checkPicUpload($("#file" + bookId)[0])) { if (checkPicUpload($("#file" + i)[0])) {
$.ajaxFileUpload({ $.ajaxFileUpload({
url: "/file/picUpload", //用于文件上传的服务器端请求地址 url: "/file/picUpload", //用于文件上传的服务器端请求地址
secureuri: false, //是否需要安全协议一般设置为false secureuri: false, //是否需要安全协议一般设置为false
fileElementId: "file" + bookId, //文件上传域的ID fileElementId: "file" + i, //文件上传域的ID
dataType: "json", //返回值类型 一般设置为json dataType: "json", //返回值类型 一般设置为json
type: "post", type: "post",
success: function (data) { //服务器成功响应处理函数 success: function (data) { //服务器成功响应处理函数
if (data.code == 200) { if (data.code == 200) {
let picUrl = data.data;
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "/author/updateBookPic", url: "/author/updateBookPic",
@ -316,13 +308,17 @@
dataType: "json", dataType: "json",
success: function (data) { success: function (data) {
if (data.code == 200) { if (data.code == 200) {
$("#cover"+bookId).attr("src", picUrl);
location.reload();
} else { } else {
lock = false;
layer.alert(data.msg); layer.alert(data.msg);
} }
}, },
error: function () { error: function () {
lock = false;
layer.alert('网络异常'); layer.alert('网络异常');
} }
}) })

View File

@ -15,7 +15,7 @@
padding: 20px; padding: 20px;
background: #fff; background: #fff;
border-radius: 8px; border-radius: 8px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1); box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
} }
/* 文本域样式 */ /* 文本域样式 */
@ -124,10 +124,10 @@
<li id="contentLi" style="width: 500px"> <li id="contentLi" style="width: 500px">
<div class="editor-container"> <div class="editor-container">
<div class="ai-toolbar"> <div class="ai-toolbar">
<a class="ai-link expand" data-type="stream/expand">AI扩写</a> <a class="ai-link expand" data-type="expand">AI扩写</a>
<a class="ai-link condense" data-type="stream/condense">AI缩写</a> <a class="ai-link condense" data-type="condense">AI缩写</a>
<a class="ai-link continue" data-type="stream/continue">AI续写</a> <a class="ai-link continue" data-type="continue">AI续写</a>
<a class="ai-link polish" data-type="stream/polish">AI润色</a> <a class="ai-link polish" data-type="polish">AI润色</a>
</div> </div>
<textarea id="bookContent" name="bookContent" <textarea id="bookContent" name="bookContent"
placeholder="请输入文本内容..."></textarea> placeholder="请输入文本内容..."></textarea>
@ -268,7 +268,7 @@
}, speed); }, speed);
} }
$('.ai-toolbar .ai-link').click(function (e) { $('.ai-toolbar .ai-link').click(function(e){
e.preventDefault(); // 阻止默认链接行为 e.preventDefault(); // 阻止默认链接行为
const type = $(this).data('type'); const type = $(this).data('type');
const textarea = $('#bookContent'); const textarea = $('#bookContent');
@ -284,27 +284,27 @@
// 参数配置 // 参数配置
let params = {text: selectedText}; let params = {text: selectedText};
if (type === 'stream/expand' || type === 'stream/condense') { if(type === 'expand' || type === 'condense'){
layer.prompt({ layer.prompt({
title: '请输入比例', title: '请输入比例',
value: 2, value: 2,
btn: ['确定', '取消'], btn: ['确定', '取消'],
btn2: function () { btn2: function(){
layer.close(loading); layer.close(loading);
}, },
cancel: function () { cancel: function(){
layer.close(loading); layer.close(loading);
} }
}, function (value, index) { }, function(value, index){
if (isNaN(Number(value)) || isNaN(parseFloat(value))) { if(isNaN(Number(value)) || isNaN(parseFloat(value))){
layer.msg('请输入正确的比例'); layer.msg('请输入正确的比例');
return; return;
} }
if (type === 'stream/expand' && value <= 1) { if(type === 'expand' && value <= 1){
layer.msg('请输入正确的比例'); layer.msg('请输入正确的比例');
return; return;
} }
if (type === 'stream/condense' && (value <= 0 || value >= 1)) { if(type === 'condense' && (value <=0 || value >= 1)){
layer.msg('请输入正确的比例'); layer.msg('请输入正确的比例');
return; return;
} }
@ -313,19 +313,19 @@
sendRequest(type, params, loading, textarea); sendRequest(type, params, loading, textarea);
}); });
return; return;
} else if (type === 'stream/continue') { }else if(type === 'continue'){
layer.prompt({ layer.prompt({
title: '请输入续写长度字数', title: '请输入续写长度字数',
value: 200, value: 200,
btn: ['确定', '取消'], btn: ['确定', '取消'],
btn2: function () { btn2: function(){
layer.close(loading); layer.close(loading);
}, },
cancel: function () { cancel: function(){
layer.close(loading); layer.close(loading);
} }
}, function (value, index) { }, function(value, index){
if (!Number.isInteger(Number(value)) || value <= 0) { if(!Number.isInteger(Number(value)) || value <= 0){
layer.msg('请输入正确的长度'); layer.msg('请输入正确的长度');
return; return;
} }
@ -339,28 +339,26 @@
sendRequest(type, params, loading, textarea); sendRequest(type, params, loading, textarea);
}); });
function sendRequest(type, params, loading, textarea) { function sendRequest(type, params, loading, textarea){
const url = `/author/ai/${type}?text=${encodeURIComponent(params.text)}&ratio=${params.ratio}&length=${params.length}`; $.ajax({
const eventSource = new EventSource(url); url: '/author/ai/' + type,
type: 'POST',
// 监听消息事件 data: params,
eventSource.onmessage = function (event) { success: function(res){
layer.close(loading); layer.close(loading);
const data = event.data; // 将生成的内容追加到文本末尾
console.log('Received data:', data); if(res.code == '200'){
const newText = "\n\n【AI生成内容】" + res.data; // 添加换行符分隔
textarea.val(textarea.val() + data); typeWriter(textarea, newText); // 使用打字机效果
// 滚动到底部 }else{
textarea.scrollTop(textarea[0].scrollHeight); layer.msg('AI内容生成失败请稍后重试');
}; }
},
// 监听错误事件 error: function(){
eventSource.onerror = function (error) { layer.msg('请求失败请稍后重试');
layer.close(loading); layer.close(loading);
console.error('EventSource failed:', error); }
eventSource.close(); // 关闭连接 });
};
} }
</script> </script>