SecondViewModel.ets 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { BookItem, YTAvoid, YTRequest, yTRouter } from "basic"
  2. import { AppStorageV2 } from "@kit.ArkUI"
  3. import { BookCategoryStorage } from "../../model/Storage"
  4. import { searchBookQuery } from "../../model/Query"
  5. import { bookListApi } from "../../apis/BookListApi"
  6. import { PageResponse } from "../../model/PageResponse"
  7. @ObservedV2
  8. export class SecondViewModel{
  9. @Trace safeTop: number = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
  10. // 书单列表
  11. @Trace bookList: BookItem[] = []
  12. // 分类索引
  13. @Trace categoryIndex: number = 0
  14. // 是否已读
  15. @Trace isRead: boolean = false
  16. // 显示模式 0 - 列表 1 - 封面
  17. @Trace showMode: number = 1
  18. // 打开年龄选择弹窗
  19. @Trace openAgeDialog: boolean = false
  20. // 选择类型弹窗是否已打开
  21. @Trace openTypeDialog: boolean = false
  22. // 查询参数
  23. @Trace query: searchBookQuery = new searchBookQuery()
  24. // 年龄范围
  25. @Trace ageRange: string = ''
  26. // 列表控制器
  27. categoryControl: Scroller = new Scroller()
  28. // 分类列表
  29. categoryList: BookCategoryStorage = AppStorageV2.connect(BookCategoryStorage, () => new BookCategoryStorage())!
  30. ageList: string[] = ['年龄选择', '0-2岁', '3-6岁', '7-10岁', '10岁+']
  31. constructor() {
  32. this.changeCategory(0)
  33. }
  34. // 进入查看书籍详情页
  35. goBookDetail(book: BookItem) {
  36. yTRouter.router2BookItemDetailPage(book)
  37. }
  38. // 添加至书包
  39. addToBook(book: BookItem) {}
  40. // 更改选中的分类
  41. changeCategory(index: number, toIndex: boolean = false) {
  42. if(index == this.categoryIndex && index !=+ 0) return
  43. this.categoryIndex = index
  44. if (toIndex) {
  45. this.categoryControl.scrollToIndex(index)
  46. }
  47. if(index == 0){
  48. this.query.typeId = undefined
  49. } else {
  50. this.query.typeId = this.categoryList.list[index].id
  51. }
  52. this.getBookList()
  53. }
  54. // 更改排序方式 - 热度、最新
  55. changeSortType(index: number) {
  56. if (this.query.sort == index)
  57. return
  58. this.query.sort = index
  59. this.getBookList()
  60. }
  61. // 切换已读模式
  62. changeIsRead() {
  63. this.isRead = !this.isRead
  64. }
  65. // 改变显示模式 封面和列表模式的切换
  66. changeShowMode() {
  67. this.showMode = this.showMode == 0 ? 1 : 0
  68. }
  69. // 切换年龄范围
  70. changeAgeRange(index: number) {
  71. this.ageRange = this.ageList[index]
  72. if(index == 0) {
  73. this.query.minAge = undefined
  74. this.query.maxAge = undefined
  75. } else if(index == 1) {
  76. this.query.minAge = 0
  77. this.query.maxAge = 2
  78. } else if(index == 2) {
  79. this.query.minAge = 3
  80. this.query.maxAge = 6
  81. } else if(index == 3) {
  82. this.query.minAge = 7
  83. this.query.maxAge = 10
  84. } else if(index == 4) {
  85. this.query.minAge = 10
  86. this.query.maxAge = undefined
  87. }
  88. this.getBookList()
  89. }
  90. // 开启选择分类弹窗
  91. openCategoryDialog() {
  92. if(this.categoryList.list.length == 0) return
  93. this.openTypeDialog = !this.openTypeDialog
  94. }
  95. // 获取书籍列表
  96. async getBookList() {
  97. let ans: PageResponse<BookItem> = await bookListApi.searchBooks(this.query.clone())
  98. this.bookList = ans.list ?? []
  99. console.log(`获取书籍列表 = ${JSON.stringify(this.bookList)}`)
  100. }
  101. }