import { BookItem, YTAvoid, YTRequest, yTRouter } from "basic" import { AppStorageV2 } from "@kit.ArkUI" import { BookCategoryStorage } from "../../model/Storage" import { searchBookQuery } from "../../model/Query" import { bookListApi } from "../../apis/BookListApi" import { PageResponse } from "../../model/PageResponse" @ObservedV2 export class SecondViewModel{ @Trace safeTop: number = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number // 书单列表 @Trace bookList: BookItem[] = [] // 分类索引 @Trace categoryIndex: number = 0 // 是否已读 @Trace isRead: boolean = false // 显示模式 0 - 列表 1 - 封面 @Trace showMode: number = 1 // 打开年龄选择弹窗 @Trace openAgeDialog: boolean = false // 选择类型弹窗是否已打开 @Trace openTypeDialog: boolean = false // 查询参数 @Trace query: searchBookQuery = new searchBookQuery() // 年龄范围 @Trace ageRange: string = '' // 列表控制器 categoryControl: Scroller = new Scroller() // 分类列表 categoryList: BookCategoryStorage = AppStorageV2.connect(BookCategoryStorage, () => new BookCategoryStorage())! ageList: string[] = ['年龄选择', '0-2岁', '3-6岁', '7-10岁', '10岁+'] constructor() { this.changeCategory(0) } // 进入查看书籍详情页 goBookDetail(book: BookItem) { yTRouter.router2BookItemDetailPage(book) } // 添加至书包 addToBook(book: BookItem) {} // 更改选中的分类 changeCategory(index: number, toIndex: boolean = false) { if(index == this.categoryIndex && index !=+ 0) return this.categoryIndex = index if (toIndex) { this.categoryControl.scrollToIndex(index) } if(index == 0){ this.query.typeId = undefined } else { this.query.typeId = this.categoryList.list[index].id } this.getBookList() } // 更改排序方式 - 热度、最新 changeSortType(index: number) { if (this.query.sort == index) return this.query.sort = index this.getBookList() } // 切换已读模式 changeIsRead() { this.isRead = !this.isRead } // 改变显示模式 封面和列表模式的切换 changeShowMode() { this.showMode = this.showMode == 0 ? 1 : 0 } // 切换年龄范围 changeAgeRange(index: number) { this.ageRange = this.ageList[index] if(index == 0) { this.query.minAge = undefined this.query.maxAge = undefined } else if(index == 1) { this.query.minAge = 0 this.query.maxAge = 2 } else if(index == 2) { this.query.minAge = 3 this.query.maxAge = 6 } else if(index == 3) { this.query.minAge = 7 this.query.maxAge = 10 } else if(index == 4) { this.query.minAge = 10 this.query.maxAge = undefined } this.getBookList() } // 开启选择分类弹窗 openCategoryDialog() { if(this.categoryList.list.length == 0) return this.openTypeDialog = !this.openTypeDialog } // 获取书籍列表 async getBookList() { let ans: PageResponse = await bookListApi.searchBooks(this.query.clone()) this.bookList = ans.list ?? [] console.log(`获取书籍列表 = ${JSON.stringify(this.bookList)}`) } }