瀏覽代碼

feat: 完成 精品书单 相关页面的接口请求和逻辑交互

YuJing 1 月之前
父節點
當前提交
d47f575e29

+ 1 - 1
commons/basic/src/main/ets/apis/YTRequest.ets

@@ -13,7 +13,7 @@ import { userInfo } from '../models/UserInfo';
 import { yTRouter } from '../utils/arkts/utils/YTRouter';
 
 
-export const baseURL: string = 'https://hm-test.ytpm.net/prod-book/'
+export const baseURL: string = 'https://hm-test.ytpm.net/prod-book'
 
 // export const baseURL: string = 'http://192.168.1.160:48103'
 

+ 1 - 1
commons/basic/src/main/ets/utils/arkts/utils/ContextHelper.ets

@@ -91,7 +91,7 @@ export class ContextHelper {
 
     PersistentStorage.persistProp(AppStorageKeyCollect.TOKEN, '')
     YTUserRequest.refreshUserInfo()
-    tkAdHelper.init()
+    // tkAdHelper.init()
 
     permissionController.init(ContextHelper.context)
       .then(() => {

+ 2 - 2
commons/basic/src/main/ets/utils/arkts/utils/YTRouter.ets

@@ -78,8 +78,8 @@ class YTRouter extends NavPathStack {
   }
 
   // 跳转至精选书单页面
-  router2BookListPage(){
-    this.pushPathByName('BookListPage', null)
+  router2BookListPage(selectCategories: string = ''){
+    this.pushPathByName('BookListPage', selectCategories)
   }
 
   // 跳转至书籍搜索页面

+ 3 - 3
features/feature/src/main/ets/apis/ApiUrl.ets

@@ -9,19 +9,19 @@ export class ApiUrl {
    * @description 获取所有的书单分类集合
    * @method GET
    */
-  static getAllBookCategories = '/prod-book/api/book/bookListType/getAllBookListType';
+  static getAllBookCategories = '/api/book/bookListType/getAllBookListType';
 
   /**
    * @description 获取最热的10条搜索记录
    * @method GET
    */
-  static getHotSearchRecords = '/prod-book/api/book/bookListType/getAllBookListType';
+  static getHotSearchRecords = '/api/book/bookListType/getAllBookListType';
 
   /**
    * @description 获取所有的主题分类集合
    * @method GET
    */
-  static getAllThemes = '/prod-book/api/book/bookListType/getAllBookListType';
+  static getAllThemes = '/api/book/bookListType/getAllBookListType';
 
   /**
    * @description 搜索图书

+ 4 - 1
features/feature/src/main/ets/apis/BookListApi.ets

@@ -4,7 +4,7 @@ import { searchBookListQuery, searchBookQuery } from "../model/Query";
 import { PageResponse } from "../model/PageResponse";
 import { ApiUrl } from "./ApiUrl";
 
-export class BookListApi {
+class BookListApi {
   /**
    * @description 搜索书单
    * @method POST
@@ -45,3 +45,6 @@ export class BookListApi {
     return YTRequest.post<PageResponse<BookItem>, searchBookQuery>(ApiUrl.searchBooks, params)
   }
 }
+
+
+export const bookListApi = new BookListApi()

+ 11 - 5
features/feature/src/main/ets/components/BuilderIndex.ets

@@ -1,4 +1,5 @@
 import { yTRouter } from "basic"
+import { BookListItem } from "../model/BookModelIndex"
 import { CustomTextStyle } from "../style/CustomTextStyle"
 
 // 标签Item
@@ -17,25 +18,30 @@ export function tagItemComp(text: string, fontColor: ResourceColor, bgColor: Res
 
 // 书单Item
 @Builder
-export function bookListItemComp(title: string){
+export function bookListItemComp(item: RepeatItem<BookListItem>){
   Row({space: 11}){
-    Image($r('[basic].media.png_TopRecommend'))
+    Image(item.item.coverUrl)
       .width(86)
       .aspectRatio(1)
       .borderRadius(12)
+      .alt($r('[basic].media.png_TopRecommend'))
 
     Column({space: 3}){
-      Text(title)
+      Text(item.item?.bookListName)
+        .maxLines(1)
+        .textOverflow({overflow: TextOverflow.Ellipsis})
         .attributeModifier(new CustomTextStyle({size: 14, weight: 600}))
 
-      Text('豆瓣平均分9.2分推荐!红红火火恍恍惚惚哈哈哈哈哈给红红火火恍恍惚惚哈哈哈...')
+      Text(item.item?.synopsis)
         .width('100%')
+        .maxLines(3)
+        .textOverflow({overflow: TextOverflow.Ellipsis})
         .attributeModifier(new CustomTextStyle({size: 12, weight: 400, color: '#FF555555'}))
 
       Blank()
         .layoutWeight(1)
 
-      Text('共92本')
+      Text(`共${item.item?.bookCount}本`)
         .attributeModifier(new CustomTextStyle({size: 12, weight: 400, color: '#FF555555'}))
     }
     .layoutWeight(1)

+ 2 - 0
features/feature/src/main/ets/model/BookModelIndex.ets

@@ -92,6 +92,8 @@ export class BookListItem {
   updateBy?: string;
   /** 更新时间 */
   updateTime?: string;
+  /** 书籍数量 */
+  bookCount?: string
 }
 
 

+ 10 - 0
features/feature/src/main/ets/model/Query.ets

@@ -2,6 +2,16 @@ abstract class Query{
   pageNum: number = 1
   pageSize: number = 15
   total: number = 0
+
+  reload() {
+    this.pageNum = 1
+    this.pageSize = 15
+    this.total = 1
+  }
+
+  reachEnd() {
+    this.pageNum++
+  }
 }
 
 // 搜索书单

+ 28 - 1
features/feature/src/main/ets/model/Storage.ets

@@ -1,4 +1,31 @@
+import { bookListApi } from "../apis/BookListApi"
+import { BookListTypeItem } from "./BookModelIndex"
+
+// 搜索记录缓存
 @ObservedV2
 export class HistoryStorage{
   @Trace history: string[] = new Array(8).fill('好的好的好的额')
-}
+}
+
+// 书单分类
+@ObservedV2
+export class BookCategoriesStorage{
+  @Trace bookList: BookListTypeItem[] = []
+
+  constructor() {
+    if(this.bookList.length == 0){
+      this.init()
+    }
+  }
+
+  async init(){
+    let ans = await bookListApi.getAllBookCategories()
+    if(ans.length == 0) return
+
+    ans.sort((a, b) => a.sort! > b.sort! ? 1 : -1)
+    console.log(`ans = ${JSON.stringify(ans)}`)
+    this.bookList = []
+    this.bookList.push({ bookListTypeName: "全部" }, ...ans)
+  }
+}
+

+ 13 - 7
features/feature/src/main/ets/pages/BookList/BookListPage.ets

@@ -3,12 +3,13 @@ import { DeerSearch } from '../../components/DeerSearch';
 import { CustomTextStyle } from '../../style/CustomTextStyle';
 import { BookListViewModel } from '../viewModel/BookListViewModel';
 import { bookListItemComp } from '../../components/BuilderIndex';
+import { BookListTypeItem } from '../../model/BookModelIndex';
 
 // 精选书单
 @ComponentV2
 @RouterPage
 struct BookListPage {
-  @Local vm: BookListViewModel = new BookListViewModel();
+  @Param @Require vm: BookListViewModel;
 
   build() {
     NavDestination() {
@@ -19,11 +20,11 @@ struct BookListPage {
         })
 
         Column(){
-          List({space: 22}){
-            ForEach(this.vm.categoryList, (item: string, index) => {
+          List({space: 22, scroller: this.vm.categoryControl}){
+            ForEach(this.vm.bookCategories.bookList, (item: BookListTypeItem, index) => {
               ListItem(){
                 Column(){
-                  Text(item)
+                  Text(item.bookListTypeName)
                     .zIndex(99)
                     .onClick(() => { this.vm.changeCategory(index) })
                     .attributeModifier(new CustomTextStyle({size: 16, weight: 600, color: this.vm.categoryIndex == index ? '#FF111111' : '#FF777777'}))
@@ -44,6 +45,11 @@ struct BookListPage {
           .width("100%")
           .scrollBar(BarState.Off)
           .listDirection(Axis.Horizontal)
+          .onAppear(() => {
+            if(this.vm.categoryIndex != 0) {
+              this.vm.categoryControl.scrollToIndex(this.vm.categoryIndex)
+            }
+          })
 
           Row(){
             ForEach(['最新', '最热'], (item: string, index: number) => {
@@ -71,7 +77,7 @@ struct BookListPage {
 
             Repeat(this.vm.bookList)
               .each((item) => {
-                bookListItemComp(item.item)
+                bookListItemComp(item)
               })
           }
           .width("100%")
@@ -99,6 +105,6 @@ struct BookListPage {
 }
 
 @Builder
-function BookListBuilder() {
-  BookListPage()
+function BookListBuilder(_: string, selectCategories: string) {
+  BookListPage({vm: new BookListViewModel(selectCategories)})
 }

+ 60 - 3
features/feature/src/main/ets/pages/viewModel/BookListViewModel.ets

@@ -1,23 +1,80 @@
+import { AppStorageV2 } from "@kit.ArkUI"
+import { bookListApi } from "../../apis/BookListApi"
+import { BookListItem } from "../../model/BookModelIndex"
+import { searchBookListQuery } from "../../model/Query"
+import { BookCategoriesStorage } from "../../model/Storage"
 
 @ObservedV2
 export class BookListViewModel{
-  // 分类列表
-  @Trace categoryList: string[] = ['全部', '精品主题', '名家大师', '国际获奖', '经典系列', '畅销榜TOP', '权威推荐', '月度推荐']
   // 书单列表
-  @Trace bookList: string[] = ['书单1', '书单2', '书单3', '书单4', '书单5', '书单6', '书单7', '书单8', '书单9', '书单10', '书单11', '书单12', '书单13', '书单14', '书单15', '书单16', '书单17']
+  @Trace bookList: BookListItem[] = []
 
   // 分类索引
   @Trace categoryIndex: number = 0
   // 排序方式 0 - 最新 1 - 最热
   @Trace sortType: number = 0
 
+  // 分类列表
+  bookCategories: BookCategoriesStorage = AppStorageV2.connect(BookCategoriesStorage, () => new BookCategoriesStorage())!
+  // 分类列表控制器
+  categoryControl: Scroller = new Scroller()
+  // 书单查询器
+  query: searchBookListQuery = new searchBookListQuery()
+
+  constructor(categories: string) {
+    if(categories) {
+      this.init(categories)
+    } else {
+      this.changeCategory(0)
+    }
+  }
+
+  // 初始化
+  async init(categories: string){
+    if(this.bookCategories.bookList.length == 0) {
+      await this.bookCategories.init()
+      return
+    }
+    let index = this.bookCategories.bookList.findIndex(item => item.bookListTypeName == categories)
+    if(index != -1) {
+      this.changeCategory(index)
+    }
+  }
+
   // 更改选中的分类
   changeCategory(index: number) {
     this.categoryIndex = index
+    if(index != 0){
+      this.query.bookListTypeId = this.bookCategories.bookList[index].id
+    } else {
+      this.query.bookListTypeId = undefined
+    }
+    this.getBookList()
   }
 
   // 更改排序方式
   changeSortType(index: number) {
     this.sortType = index
   }
+
+  // 获取书单列表
+  async getBookList(isReload: boolean = true) {
+    if(isReload) {
+      this.query.reload()
+    } else {
+      // if()
+
+      this.query.reachEnd()
+    }
+
+    try {
+      let ans = await bookListApi.searchBookList(this.query)
+
+      this.bookList = [...ans?.list ?? []]
+
+      console.log(`书单列表数据 = ${JSON.stringify(ans)}`)
+    } catch (e) {
+
+    }
+  }
 }

+ 2 - 2
features/feature/src/main/ets/view/MainView.ets

@@ -73,7 +73,7 @@ export struct MainView {
 
               // 榜单
               Row({space: 7}){
-                ForEach(['畅销单TOP', '月度推荐'], (item: string, index) => {
+                ForEach(['畅销单TOP', '月度推荐'], (item: string, index) => {
                   Column({space: 5}){
                     Text(item)
                       .attributeModifier(new CustomTextStyle({size: 16, weight: 600, color: '#663815'}))
@@ -91,7 +91,7 @@ export struct MainView {
                   $r('[basic].media.png_MonthRecommend') )
                   .backgroundImageSize({ width: '100%', height: '100%'})
                   .onClick(() => {
-                    // todo 完成榜单的跳转
+                    yTRouter.router2BookListPage(item)
                   })
                 })
               }