-- 新版提交

This commit is contained in:
yc13649764453
2023-09-09 13:51:35 +08:00
parent 763e24b4e0
commit 0b193caa03
92 changed files with 3451 additions and 1120 deletions

View File

@@ -10,12 +10,8 @@ import java.util.concurrent.Executors;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peanut.common.utils.BaiduVoicesUtils;
import com.peanut.modules.book.entity.BookChapterEntity;
import com.peanut.modules.book.entity.BookEntity;
import com.peanut.modules.book.entity.MyUserEntity;
import com.peanut.modules.book.service.BookChapterService;
import com.peanut.modules.book.service.BookService;
import com.peanut.modules.book.service.MyUserService;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.oss.service.OssService;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
@@ -28,8 +24,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.peanut.modules.book.entity.BookChapterContentEntity;
import com.peanut.modules.book.service.BookChapterContentService;
import com.peanut.common.utils.PageUtils;
import com.peanut.common.utils.R;
import org.springframework.web.multipart.MultipartFile;
@@ -55,6 +49,8 @@ public class BookChapterContentController {
private BookChapterService bookChapterService;
@Autowired
private MyUserService myUserService;
@Autowired
private UserEbookBuyService userEbookBuyService;
/**
* 列表
@@ -124,14 +120,13 @@ public class BookChapterContentController {
singleThreadExecutor.execute(new Runnable() {
@Override
public void run() {
// bookChapterContentService.getBookVoices(id);
bookChapterContentService.getWordChapterParagraph(14);
bookChapterContentService.getWordChapterParagraph(id);
}
});
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
bookEntity.setContentStatus("1");
bookEntity.setContentStatus("2");
bookService.updateById(bookEntity);
return R.ok();
@@ -141,9 +136,8 @@ public class BookChapterContentController {
*/
@RequestMapping("/signVoices")
public R signVoices(@RequestParam("content") String content) throws Exception {
//调用百度语音合成 API
String voices = BaiduVoicesUtils.run(content);
File file = new File(voices);
if (!file.exists()) {
return R.error("语音文件未生成");
@@ -173,7 +167,7 @@ public class BookChapterContentController {
public R allVoices(@RequestParam("id") Integer id) throws Exception {
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
//创建单线程
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(new Runnable() {
@SneakyThrows
@@ -185,7 +179,7 @@ public class BookChapterContentController {
for (BookChapterContentEntity bookContent:book_id) {
String content = bookContent.getContent();
//生成相应的语音文件
String voices = BaiduVoicesUtils.run(content);
File file = new File(voices);
@@ -194,9 +188,12 @@ public class BookChapterContentController {
}
FileInputStream fileInputStream = new FileInputStream(file);
//把 voices 音频文件读入到内存中并将其转换成MultipartFile格式文件 multipartFile 用于OSS上传
// application/x-www-form-urlencoded form表单数据被编码为key/value格式发送到服务器
// text/plain 纯文本格式
// MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
//上传 multipartFile 文件到阿里云OSS上
String path = ossService.uploadFileAvatar(multipartFile);
bookContent.setVoices(path);
@@ -218,6 +215,232 @@ public class BookChapterContentController {
bookService.updateById(bookEntity);
return R.ok();
}
//章节2.0转音频
@RequestMapping("/AllVOices")
public R allVoicess2(@RequestParam("id") Integer id) throws Exception {
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
//创建单线程
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(new Runnable() {
@SneakyThrows
@Override
public void run() {
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
List<BookChapterEntity> book_id = bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>().eq("book_id", id));
for (BookChapterEntity bookChapter:book_id) {
String content = bookChapter.getContent();
//生成相应的语音文件
String voices = BaiduVoicesUtils.run(content);
File file = new File(voices);
if (!file.exists()) {
bookEntity.setVoicesStatus("3");
}
FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
//上传 multipartFile 文件到阿里云OSS上
String path = ossService.uploadFileAvatar(multipartFile);
bookChapter.setVoices(path);
fileInputStream.close();
file.delete();
bookChapterService.updateById(bookChapter);
if (StringUtils.isEmpty(path)){
bookEntity.setVoicesStatus("3");
}
}
bookEntity.setVoicesStatus("2");
bookService.updateById(bookEntity);
}
});
bookEntity.setVoicesStatus("1");
bookService.updateById(bookEntity);
return R.ok();
}
/**
* 鉴权获取章节
* @param bookid
* @param userId
* @return
*/
@RequestMapping("/getBooksCatal")
public R getBooksCatal(@RequestParam("id") Integer id,
@RequestParam("bookid") Integer bookid,
@RequestParam("userId") Integer userId) {
// TODO 验证 当前请求的书 是否存在免费章节数
BookEntity bookEntity = bookService.getBaseMapper().selectById(bookid);
Integer freeChapterCount = bookEntity.getFreeChapterCount();
//TODO 0-免费 1-会免(改为电子书听书) 2-付费 // 阅读章节数 大于 免费章节数
Integer isVip = bookEntity.getIsVip();
if ((bookid > freeChapterCount) || freeChapterCount == 0) {
// 书籍为 会免
if (isVip == 1) {
//查询用户身份
MyUserEntity user = myUserService.getById(userId);
String vip = user.getVip();
if (!"1".equals(vip)) {
return R.error("当前为VIP");
}
}
}
if (isVip == 2) {
// 鉴权 查询权限表中 用户是否开通
boolean b = myUserService.bookAuthenticate(bookid, userId);
if (!b) {
return R.error(500, "请购买书籍!");
}
}
if(isVip == 3){
boolean b = myUserService.bookAuthenticate(bookid,userId);
if (!b){
List<BookChapterEntity> book = bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
.eq("book_id", bookid));
ArrayList<Object> chapterList = new ArrayList<>();
int chapterIndex=0;
for (BookChapterEntity chapter : book) {
if (chapterIndex >= freeChapterCount) {
break; // 取出前freeChapterCount条记录后退出循环
}
//获取条数记录取出对应name,url
Map<String, Object> map = new HashMap<>();
map.put("name", chapter.getChapter());
map.put("url", chapter.getVoices());
chapterList.add(map);
chapterIndex++;
}
return R.ok("当前为试听章节:"+chapterList).put("bookCatalogue", chapterList).put("image", bookEntity.getImages()).put("chapterIndex",chapterIndex);
}
}
List<BookChapterEntity> book = bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
.eq("book_id", bookid));
ArrayList<Object> chapterList = new ArrayList<>();
for (BookChapterEntity chapter : book) {
Map<String, Object> map = new HashMap<>();
map.put("name",chapter.getChapter());
map.put("url",chapter.getVoices());
chapterList.add(map);
}
return R.ok().put("bookCatalogue", chapterList).put("image", bookEntity.getImages());
}
/**
* app 获取电子书章节内容 2.0 (在用鉴权)
*/
@RequestMapping("/appBooksChapterContent")
public R getBooksCatalogue(@RequestParam("chapterid") Integer id,
@RequestParam("bookid") Integer bookid,
@RequestParam("userId") Integer userId) {
// TODO 验证 当前请求的书 是否存在免费章节数
BookEntity bookEntity = bookService.getBaseMapper().selectById(bookid);
Integer freeChapterCount = bookEntity.getFreeChapterCount();
BookChapterEntity bookChapterEntity = bookChapterService.getBaseMapper().selectById(id);
Integer number = bookChapterEntity.getNumber();
//TODO 0-免费 1-会免(改为电子书听书) 2-付费 // 阅读章节数 大于 免费章节数
Integer isVip = bookEntity.getIsVip();
Boolean canListen = bookEntity.getCanListen();
//todo 暂未开通听书功能
// if (canListen==false) {
// return R.error1("暂未开通听书功能");
// }
if ((number > freeChapterCount) || freeChapterCount == 0) {
// 书籍为 会免
if (isVip == 1) {
//查询用户身份
MyUserEntity user = myUserService.getById(userId);
String vip = user.getVip();
if (!"1".equals(vip)) {
return R.error("当前为VIP");
}
}
if(canListen == true){
boolean b = myUserService.bookAuthenticate(bookid,userId);
if (!b) {
return R.error("未购买书籍!");
}
}
}
if (isVip == 2) {
// 鉴权 查询权限表中 用户是否开通
boolean b = myUserService.bookAuthenticate(bookid, userId);
if (!b) {
return R.error(500, "请购买书籍!").put("code",500);
}
}
ArrayList<Object> chapterList = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("name",bookChapterEntity.getChapter());
map.put("url",bookChapterEntity.getVoices());
map.put("bookid",bookChapterEntity.getBookId());
map.put("chapterid",bookChapterEntity.getId());
//todo sort先注释调
// map.put("sort",bookChapterEntity.getSort());
chapterList.add(map);
return R.ok().put("bookCatalogue", chapterList).put("image", bookEntity.getImages());
}
/**
* app 获取电子书章节内容
*/
@@ -227,20 +450,14 @@ public class BookChapterContentController {
@RequestParam("userId") Integer userId){
// TODO 验证 当前请求的书 是否存在免费章节数
BookEntity bookEntity = bookService.getBaseMapper().selectById(bookid);
Integer freeChapterCount = bookEntity.getFreeChapterCount();
Integer isVip = bookEntity.getIsVip(); // 0-免费 1-会免 2-付费
BookChapterEntity bookChapterEntity = bookChapterService.getBaseMapper().selectById(chapterid);
Integer number = bookChapterEntity.getNumber();
if ((number > freeChapterCount) || freeChapterCount ==0){ // 阅读章节数 大于 免费章节数
// 书籍为 会免
// 阅读章节数 大于 免费章节数
if ((number > freeChapterCount) || freeChapterCount ==0){
if (isVip == 1) {
//查询用户身份
MyUserEntity user = myUserService.getById(userId);
String vip = user.getVip();
if (!"1".equals(vip)) {
@@ -248,13 +465,10 @@ public class BookChapterContentController {
}
}
if (isVip == 2) {
// 鉴权 查询权限表中 用户是否开通
boolean b = myUserService.bookAuthenticate(bookid, userId);
if (!b) {
return R.error(500,"开通会员或购买此书籍!");
return R.error(500,"请购买此书籍!");
}
}
}
@@ -262,8 +476,6 @@ public class BookChapterContentController {
ArrayList<Object> list = new ArrayList<>();
List<BookChapterContentEntity> bookChapterContentEntities = bookChapterContentService.getBaseMapper().selectList(new QueryWrapper<BookChapterContentEntity>()
.eq("book_id",bookid)
.eq("book_chatper_id",chapterid));
@@ -273,7 +485,6 @@ public class BookChapterContentController {
String substring = "";
if (bookChapterContentEntity.getOtherContent() != null){
// substring = bookChapterContentEntity.getOtherContent().substring(3, bookChapterContentEntity.getOtherContent().length() - 3);
substring = bookChapterContentEntity.getOtherContent().replace("<p>","").replace("</p>","");
}
StringBuffer stringBuffer = new StringBuffer(substring);
@@ -286,8 +497,6 @@ public class BookChapterContentController {
String chapter = chapterEntity.getChapter();
content = bookChapterContentEntity.getContent().replace(chapter, "");
}
// System.out.println(content.length());
// System.out.println(content.replaceAll("[\\\\s\\\\u00A0]",""));
bookChapterContentEntity.setPicAndWord(content.replaceAll(" ","") + stringBuffer);
list.add(bookChapterContentEntity);
}