import { messageDark, useMessage } from 'naive-ui' import { defineStore } from 'pinia' import { errorMessage, successMessage } from '../main/generalTools' import { BookTaskStatus } from '../define/enum/bookEnum' // 系统相关设置 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, subtitlePosition: null }, // 当前选中的小说 bookTaskData: [], // 当前显示的所有小说任务数据 selectBookTask: { no: null, id: null, bookId: null, name: null, generateVideoPath: null, srtPath: null, audioPath: null, imageFolder: null, styleList: null, prefix: null, status: BookTaskStatus.WAIT, errorMsg: null } // 当前选中的小说任务 }), getters: { // 获取小说数据 GetBookData(state) { return (bookId = null) => { // 要是返回的数据为空,返回全部数据 if (bookId == null) return state.bookData return state.bookData.find((item) => item.id === bookId) } } }, actions: { /** * 获取小说的数据 * @param {*} condition */ async GetBookDataFromDB(condition) { try { debugger let res = await window.book.GetBookData(condition) if (res.code == 0) { throw new Error(res.message) } if (res.data.res_book.length <= 0) { throw new Error('没有找到对应的小说数据,请先添加小说') } 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 { debugger let res = await window.book.GetBookTaskData(condition) if (res.code == 0) { throw new Error(res.message) } if (res.data.bookTasks.length > 0) { this.bookTaskData = res.data.bookTasks this.selectBookTask = res.data.bookTasks[0] } else { throw new Error('没有找到对应的子批次数据,请先创建') } return successMessage(true) } catch (error) { return errorMessage(error.message) } }, /** * 修改小说数据 * @param value */ SetBookData(value) { try { // 判断传入的数据不能为空,为空报错 if (!value) { throw new Error('value不能为空') } // 如果是函数,则执行函数,如果是BookModel对象,修改对应的行的数据,如果是数组,则直接赋值 if (typeof value === 'function') { 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) if (index !== -1) { this.bookData[index] = value } else { throw new Error('未找到对应的数据') } } else { throw new Error('value的类型不正确') } } catch (error) { throw new Error(error.message) } }, // 设置选中的小说 SetSelectBook(value) { this.selectBook = value }, /** * 设置小说类型 * 判断pinia中的小说类型数组的数据是不是存在,默认存在不修改,要出传入参数为true,会强制修改 * @param {*} value */ async SetBookType(value = false) { try { if (this.bookType.length <= 0 || value) { let _bookType = await book.GetBookType() if (_bookType.code == 0) { throw new Error(_bookType.message) } this.bookType = _bookType.data } return successMessage(true) } catch (error) { return errorMessage(error.toString()) } }, /** * 更新选中的小说数据 * @param {*} obj */ async UpdateSelectBook(obj) { // 直接修改,使用object.assign合并对象中的数据 this.selectBook = Object.assign(this.selectBook, obj) }, /** * 将但钱选择的数据进行报错 * @param {*} book 外部传入的修改 */ async SaveSelectBook(book = null) { try { let save_res = null if (book == null) { // 保存this.selectBook save_res = await window.book.AddOrModifyBook({ ...this.selectBook }) } else { // 保存传入的数据 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) { return errorMessage(error.message) } } } })