Bladeren bron

feat: 完成搜索页面的热门搜索接口请求

YuJing 1 maand geleden
bovenliggende
commit
3ce8d0e71d

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

@@ -15,7 +15,7 @@ export class ApiUrl {
    * @description 获取最热的10条搜索记录
    * @method GET
    */
-  static getHotSearchRecords = '/api/book/bookListType/getAllBookListType';
+  static getHotSearchRecords = '/api/book/bookSearch/getTenRecord';
 
   /**
    * @description 获取所有的主题分类集合

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

@@ -1,5 +1,5 @@
 import { YTRequest } from "basic";
-import { BookItem, BookListItem, BookListTypeItem, BookListTypeList } from "../model/BookModelIndex";
+import { BookItem, BookListItem, BookListTypeItem, BookListTypeList, HotSearchItem } from "../model/BookModelIndex";
 import { searchBookListQuery, searchBookQuery } from "../model/Query";
 import { PageResponse } from "../model/PageResponse";
 import { ApiUrl } from "./ApiUrl";
@@ -25,8 +25,8 @@ class BookListApi {
    * @description 获取最热的10条搜索记录
    * @method GET
    */
-  getHotSearchRecords(): Promise<ESObject>{
-    return YTRequest.get<ESObject>(ApiUrl.getHotSearchRecords)
+  getHotSearchRecords(): Promise<Array<HotSearchItem>>{
+    return YTRequest.get<Array<HotSearchItem>>(ApiUrl.getHotSearchRecords)
   }
 
   /**

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

@@ -137,3 +137,16 @@ export class BookListTypeList {
   /** 更新时间 */
   updateTime?: string;
 }
+
+
+/** 最热的搜索记录词条 */
+export class HotSearchItem {
+  id?: string;
+  /** 搜索词条 */
+  keyWord?: string;
+  searchTime?: string;
+  createBy?: string;
+  createTime?: string;
+  updateBy?: string;
+  updateTime?: string;
+}

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

@@ -4,7 +4,17 @@ import { BookListTypeItem, BookListTypeList } from "./BookModelIndex"
 // 搜索记录缓存
 @ObservedV2
 export class HistoryStorage{
-  @Trace history: string[] = new Array(8).fill('好的好的好的额')
+  @Trace history: string[] = []
+
+  increaseKey(key: string){
+    let index = this.history.indexOf(key)
+    if(index == -1) {
+      this.history.unshift(key)
+    } else {
+      this.history.splice(index, 1)
+      this.history.unshift(key)
+    }
+  }
 }
 
 // 书单分类

+ 26 - 8
features/feature/src/main/ets/pages/BookList/BookSearchPage.ets

@@ -1,14 +1,22 @@
-import { YTAvoid, YTHeader } from 'basic'
+import { YTAvoid, YTHeader, YTRequest } from 'basic'
 import { DeerSearch } from '../../components/DeerSearch'
 import { CustomTextStyle } from '../../style/CustomTextStyle'
 import { LengthMetrics, PersistenceV2 } from '@kit.ArkUI'
 import { HistoryStorage } from '../../model/Storage'
+import { HotSearchItem } from '../../model/BookModelIndex'
+import { bookListApi } from '../../apis/BookListApi'
 
 // 书籍搜索页面
 @ComponentV2
 struct BookSearchPage {
   @Local safeBottom: number = AppStorage.get(YTAvoid.SAFE_BOTTOM_KEY) as number
 
+  // 热门搜索
+  @Local hotSearchKeys: HotSearchItem[] = []
+  // 热门主题
+  @Local hotSearchClassifyKeys: HotSearchItem[] = []
+
+  // 搜索历史记录
   searHistory: HistoryStorage = PersistenceV2.connect(HistoryStorage, () => new HistoryStorage())!
 
   // 清空历史记录
@@ -18,9 +26,19 @@ struct BookSearchPage {
 
   // 开始搜索
   onSearch(text: string) {
-    let key = text
+    if(!text) return
+
+    this.searHistory.increaseKey(text.trim())
+
+  }
+
+  // 获取最热词条
+  async getHotSearchKeys() {
+    this.hotSearchKeys = await bookListApi.getHotSearchRecords()
+  }
 
-    console.log('开始搜索searchKey: ' + key)
+  aboutToAppear(): void {
+    this.getHotSearchKeys()
   }
 
   build() {
@@ -32,7 +50,7 @@ struct BookSearchPage {
           List(){
             ListItem(){
               DeerSearch({
-                onSearch: this.onSearch,
+                onSearch: (text: string) => { this.onSearch(text) },
               })
             }
             .margin({top: 14})
@@ -71,7 +89,7 @@ struct BookSearchPage {
             ListItem(){
               Scroll(){
                 Row({space: 15}){
-                  ForEach(['热门搜索', '主分类'], (item: string, index) => {
+                  ForEach(['热门搜索', '主分类'], (item: string, index) => {
                     Column(){
                       Row({space: 8}){
                         Image(index == 0 ?
@@ -90,18 +108,18 @@ struct BookSearchPage {
                         .width('90%')
                         .margin({left: 15, top: 13, bottom: 13})
 
-                      ForEach(new Array(20).fill(''), (item: string, index) => {
+                      ForEach(this.hotSearchKeys, (item: HotSearchItem, index) => {
                        Row({space: 5}){
                          Text(`${index<9?' ':''}${index+1}`)
                            .attributeModifier(new CustomTextStyle({ size: 14, weight: 400, color: index < 3 ? '#FFFC3636' : '#FF000000'}))
 
-                         Text('西游记')
+                         Text(item.keyWord)
                            .attributeModifier(new CustomTextStyle({ size: 14, weight: 400, color: index < 3 ? '#FF000000' : '#80000000'}))
                        }
                        .width('100%')
                        .margin({top: 12})
                        .justifyContent(FlexAlign.Start)
-                        .onClick(() => { this.onSearch('西游记') })
+                        .onClick(() => { this.onSearch(item.keyWord ?? '') })
                       })
                     }
                     .width(230)

+ 1 - 0
features/feature/src/main/ets/pages/BookList/QAPage.ets

@@ -113,6 +113,7 @@ struct QAItem {
               Image($r('[basic].media.icon_bottom'))
                 .width(16)
                 .aspectRatio(1)
+                .fillColor(this.openIndex == index ? '#FD9A4F' : undefined)
                 .rotate({angle: this.openIndex == index ? 180 : 0})
             }
             .width(22)