LaiTool/src/stores/reverseManage.ts

222 lines
6.5 KiB
TypeScript
Raw Normal View History

2024-07-13 15:44:13 +08:00
import { messageDark, useMessage } from 'naive-ui'
import { defineStore } from 'pinia'
2024-08-03 12:46:12 +08:00
import { errorMessage, successMessage } from '../main/Public/generalTools'
2024-07-13 15:44:13 +08:00
import { BookTaskStatus } from '../define/enum/bookEnum'
2024-08-03 12:46:12 +08:00
import { Book } from '../model/book'
2024-06-24 13:11:19 +08:00
// 系统相关设置
export const useReverseManageStore = defineStore('reverseManage', {
state: () => ({
bookType: [],
bookData: [], // 当前显示的所有小说数据
selectBook: {
id: null,
name: null,
bookFolderPath: null,
type: null,
oldVideoPath: null,
srtPath: null,
audioPath: null,
imageFolder: null,
2024-07-13 15:44:13 +08:00
subtitlePosition: null
2024-08-03 12:46:12 +08:00
} as Book.SelectBook, // 当前选中的小说
2024-06-24 13:11:19 +08:00
2024-08-03 12:46:12 +08:00
bookTaskData: [] as Book.SelectBookTask[], // 当前显示的所有小说任务数据
2024-06-24 13:11:19 +08:00
selectBookTask: {
no: null,
id: null,
bookId: null,
name: null,
generateVideoPath: null,
srtPath: null,
audioPath: null,
2024-08-03 12:46:12 +08:00
draftSrtStyle: null, // 草稿字幕样式
backgroundMusic: null, // 背景音乐ID
friendlyReminder: null, // 友情提示
2024-06-24 13:11:19 +08:00
imageFolder: null,
styleList: null,
prefix: null,
status: BookTaskStatus.WAIT,
2024-07-13 15:44:13 +08:00
errorMsg: null
2024-08-03 12:46:12 +08:00
} as Book.SelectBookTask// 当前选中的小说任务
,
selectBookTaskDetail: [] as Book.SelectBookTaskDetail[] // 当前选中的小说任务的详细数据
2024-06-24 13:11:19 +08:00
}),
getters: {
// 获取小说数据
GetBookData(state) {
return (bookId = null) => {
// 要是返回的数据为空,返回全部数据
2024-07-13 15:44:13 +08:00
if (bookId == null) return state.bookData
2024-06-24 13:11:19 +08:00
return state.bookData.find((item) => item.id === bookId)
2024-07-13 15:44:13 +08:00
}
}
2024-06-24 13:11:19 +08:00
},
actions: {
/**
*
2024-07-13 15:44:13 +08:00
* @param {*} condition
2024-06-24 13:11:19 +08:00
*/
async GetBookDataFromDB(condition) {
try {
debugger
2024-08-03 12:46:12 +08:00
//@ts-ignore
2024-06-24 13:11:19 +08:00
let res = await window.book.GetBookData(condition)
if (res.code == 0) {
throw new Error(res.message)
}
if (res.data.res_book.length <= 0) {
2024-07-13 15:44:13 +08:00
throw new Error('没有找到对应的小说数据,请先添加小说')
2024-06-24 13:11:19 +08:00
}
this.SetBookData(res.data.res_book)
this.selectBook = res.data.res_book[0]
return successMessage(res.data)
} catch (error) {
return errorMessage(error.message)
}
},
// 获取小说任务数据
async GetBookTaskDataFromDB(condition) {
try {
2024-08-18 16:22:19 +08:00
debugger
this.bookTaskData = []
2024-08-03 12:46:12 +08:00
//@ts-ignore
2024-07-13 15:44:13 +08:00
let res = await window.book.GetBookTaskData(condition)
2024-06-24 13:11:19 +08:00
if (res.code == 0) {
throw new Error(res.message)
}
if (res.data.bookTasks.length > 0) {
2024-07-13 15:44:13 +08:00
this.bookTaskData = res.data.bookTasks
2024-06-24 13:11:19 +08:00
this.selectBookTask = res.data.bookTasks[0]
} else {
2024-08-03 12:46:12 +08:00
this.selectBookTask = {
no: null,
id: null,
bookId: null,
name: null,
generateVideoPath: null,
srtPath: null,
audioPath: null,
draftSrtStyle: null, // 草稿字幕样式
backgroundMusic: null, // 背景音乐ID
friendlyReminder: null, // 友情提示
imageFolder: null,
styleList: null,
prefix: null,
status: BookTaskStatus.WAIT,
errorMsg: null
} as Book.SelectBookTask
2024-06-24 13:11:19 +08:00
throw new Error('没有找到对应的子批次数据,请先创建')
}
return successMessage(true)
} catch (error) {
return errorMessage(error.message)
}
},
/**
*
2024-07-13 15:44:13 +08:00
* @param value
2024-06-24 13:11:19 +08:00
*/
SetBookData(value) {
try {
// 判断传入的数据不能为空,为空报错
if (!value) {
2024-07-13 15:44:13 +08:00
throw new Error('value不能为空')
2024-06-24 13:11:19 +08:00
}
// 如果是函数则执行函数如果是BookModel对象修改对应的行的数据如果是数组则直接赋值
if (typeof value === 'function') {
2024-07-13 15:44:13 +08:00
this.bookData = value()
} else if (typeof value === 'object' && Array.isArray(value)) {
this.bookData = []
this.bookData = value
} else if (typeof value === 'object') {
const index = this.bookData.findIndex((item) => item.id === value.id)
2024-06-24 13:11:19 +08:00
if (index !== -1) {
2024-07-13 15:44:13 +08:00
this.bookData[index] = value
2024-06-24 13:11:19 +08:00
} else {
2024-07-13 15:44:13 +08:00
throw new Error('未找到对应的数据')
2024-06-24 13:11:19 +08:00
}
2024-07-13 15:44:13 +08:00
} else {
throw new Error('value的类型不正确')
2024-06-24 13:11:19 +08:00
}
} catch (error) {
throw new Error(error.message)
}
},
// 设置选中的小说
SetSelectBook(value) {
2024-07-13 15:44:13 +08:00
this.selectBook = value
2024-06-24 13:11:19 +08:00
},
/**
*
* pinia中的小说类型数组的数据是不是存在true
2024-07-13 15:44:13 +08:00
* @param {*} value
2024-06-24 13:11:19 +08:00
*/
async SetBookType(value = false) {
try {
if (this.bookType.length <= 0 || value) {
2024-08-03 12:46:12 +08:00
//@ts-ignore
2024-07-13 15:44:13 +08:00
let _bookType = await book.GetBookType()
2024-06-24 13:11:19 +08:00
if (_bookType.code == 0) {
2024-07-13 15:44:13 +08:00
throw new Error(_bookType.message)
2024-06-24 13:11:19 +08:00
}
2024-07-13 15:44:13 +08:00
this.bookType = _bookType.data
2024-06-24 13:11:19 +08:00
}
return successMessage(true)
} catch (error) {
return errorMessage(error.toString())
}
},
/**
*
2024-07-13 15:44:13 +08:00
* @param {*} obj
2024-06-24 13:11:19 +08:00
*/
async UpdateSelectBook(obj) {
// 直接修改使用object.assign合并对象中的数据
2024-07-13 15:44:13 +08:00
this.selectBook = Object.assign(this.selectBook, obj)
2024-06-24 13:11:19 +08:00
},
/**
*
* @param {*} book
*/
async SaveSelectBook(book = null) {
try {
let save_res = null
if (book == null) {
// 保存this.selectBook
2024-08-03 12:46:12 +08:00
//@ts-ignore
2024-07-13 15:44:13 +08:00
save_res = await window.book.AddOrModifyBook({ ...this.selectBook })
2024-06-24 13:11:19 +08:00
} else {
// 保存传入的数据
2024-08-03 12:46:12 +08:00
//@ts-ignore
2024-06-24 13:11:19 +08:00
save_res = await window.book.AddOrModifyBook({ ...book })
}
debugger
if (save_res.code == 0) {
throw new Error(save_res.message)
}
if (this.selectBook.id == null || (book && book.id == null)) {
this.bookData.unshift(save_res.data)
// 将最后一个删掉
this.bookData.pop()
}
if (save_res.data) {
this.selectBook = save_res.data
}
return successMessage(save_res.data)
} catch (error) {
2024-07-13 15:44:13 +08:00
return errorMessage(error.message)
2024-06-24 13:11:19 +08:00
}
}
}
2024-07-13 15:44:13 +08:00
})