V 2.2.11 优化MJ代理模式
This commit is contained in:
@@ -11,8 +11,8 @@ import { BookTaskService } from './bookTaskService'
|
||||
import { BaseRealmService } from './bookBasic.js'
|
||||
import { isEmpty } from 'lodash'
|
||||
|
||||
class BooKService extends BaseRealmService {
|
||||
static instance: BooKService | null = null
|
||||
export class BookService extends BaseRealmService {
|
||||
static instance: BookService | null = null
|
||||
realm: Realm
|
||||
|
||||
private constructor() {
|
||||
@@ -24,20 +24,59 @@ class BooKService extends BaseRealmService {
|
||||
* @returns
|
||||
*/
|
||||
public static async getInstance() {
|
||||
if (BooKService.instance === null) {
|
||||
BooKService.instance = new BooKService()
|
||||
if (BookService.instance === null) {
|
||||
BookService.instance = new BookService()
|
||||
await super.getInstance()
|
||||
}
|
||||
return BooKService.instance
|
||||
await BookService.instance.open()
|
||||
return BookService.instance
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小说信息,没有找到返回null
|
||||
* @param bookId
|
||||
*/
|
||||
GetBookDataById(bookId) {
|
||||
try {
|
||||
if (isEmpty(bookId)) {
|
||||
throw new Error('获取小说信息失败,缺少小说ID')
|
||||
}
|
||||
let books = this.realm.objects<BookModel>('Book').filtered('id = $0', bookId)
|
||||
if (books.length <= 0) {
|
||||
return successMessage(null, '通过ID获取小说数据成功', 'ReverseBook_GetBookDataById')
|
||||
} else {
|
||||
// 对返回的数据进行处理
|
||||
let resBooks = Array.from(books).map((book) => {
|
||||
// 这里可以直接操作普通对象
|
||||
let bookObj = {
|
||||
...book,
|
||||
bookFolderPath: path.resolve(
|
||||
define.project_path,
|
||||
book.bookFolderPath.replace(/\\/g, '/')
|
||||
),
|
||||
oldVideoPath: book.oldVideoPath
|
||||
? path.resolve(define.project_path, book.oldVideoPath.replace(/\\/g, '/'))
|
||||
: '',
|
||||
imageFolder: book.imageFolder
|
||||
? path.resolve(define.project_path, book.imageFolder.replace(/\\/g, '/'))
|
||||
: ''
|
||||
}
|
||||
return bookObj
|
||||
})
|
||||
|
||||
return successMessage(resBooks[0], '通过ID获取小说数据成功', 'ReverseBook_GetBookDataById')
|
||||
}
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小说信息,通过参数查询
|
||||
* @returns
|
||||
*/
|
||||
async GetBookData(bookQuery) {
|
||||
GetBookData(bookQuery) {
|
||||
try {
|
||||
await this.open()
|
||||
// 获取所有的小说数据,并进行时间降序排序
|
||||
let books = this.realm.objects<BookModel>('Book')
|
||||
let book_length = books.length
|
||||
@@ -106,7 +145,6 @@ class BooKService extends BaseRealmService {
|
||||
*/
|
||||
async AddOrModifyBook(book) {
|
||||
try {
|
||||
await this.open()
|
||||
if (book == null) {
|
||||
throw new Error('小说数据为空,无法修改')
|
||||
}
|
||||
@@ -141,6 +179,7 @@ class BooKService extends BaseRealmService {
|
||||
let bookFolderPath = path.resolve(define.project_path, book.id)
|
||||
let imageFolder = path.resolve(define.project_path, `${book.id}/tmp`)
|
||||
let oldVideoPath = path.resolve(define.project_path, `${book.id}/data/${book.id}.mp4`)
|
||||
let bookTaskImageFolder = path.resolve(imageFolder, 'output_00001')
|
||||
|
||||
// 将视频拷贝一个到项目文件下面
|
||||
if (book.oldVideoPath) {
|
||||
@@ -150,11 +189,11 @@ class BooKService extends BaseRealmService {
|
||||
// 创建对应的文件夹
|
||||
await CheckFolderExistsOrCreate(bookFolderPath)
|
||||
await CheckFolderExistsOrCreate(imageFolder)
|
||||
await CheckFolderExistsOrCreate(bookTaskImageFolder) // 创建默认的任务文件夹
|
||||
// 修改数据
|
||||
book.oldVideoPath = path.relative(define.project_path, oldVideoPath)
|
||||
this.realm.write(() => {
|
||||
this.realm.create('Book', book)
|
||||
let bookTaskImageFolder = path.resolve(imageFolder, 'output_00001')
|
||||
// 添加一个任务
|
||||
let bookTask = {
|
||||
id: uuidv4(),
|
||||
@@ -210,5 +249,3 @@ class BooKService extends BaseRealmService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default BooKService
|
||||
|
||||
Reference in New Issue
Block a user