Parcourir la source

fix: 修复列表项UI不能刷新的BUG

YuJing il y a 1 mois
Parent
commit
86d36bb4f5

+ 33 - 0
commons/basic/src/main/ets/models/BookModel.ets

@@ -1,3 +1,4 @@
+
 /**
  * 书本信息
  */
@@ -61,4 +62,36 @@ export class BookItem {
   typeName?: string;
   /** 是否加入购物车 0-未加入 1-已加入 */
   @Trace schoolbagFlag?: string;
+
+  constructor(o: BookItem) {
+    this.id = o.id;
+    this.bookTitle = o.bookTitle;
+    this.bookSubtitle = o.bookSubtitle;
+    this.isbn = o.isbn;
+    this.coverUrl = o.coverUrl;
+    this.author = o.author;
+    this.remark = o.remark;
+    this.language = o.language;
+    this.publisher = o.publisher;
+    this.publicationDate = o.publicationDate;
+    this.saleDate = o.saleDate;
+    this.pageNumber = o.pageNumber;
+    this.authorIntroduction = o.authorIntroduction;
+    this.ageRange = o.ageRange;
+    this.state = o.state;
+    this.rentPerDay = o.rentPerDay;
+    this.weight = o.weight;
+    this.marketPrice = o.marketPrice;
+    this.minAge = o.minAge;
+    this.maxAge = o.maxAge;
+    this.shelf = o.shelf;
+    this.readingNumber = o.readingNumber;
+    this.binding = o.binding;
+    this.createBy = o.createBy;
+    this.createTime = o.createTime;
+    this.updateBy = o.updateBy;
+    this.updateTime = o.updateTime;
+    this.typeName = o.typeName;
+    this.schoolbagFlag = o.schoolbagFlag;
+  }
 }

+ 7 - 2
features/feature/src/main/ets/apis/BookListApi.ets

@@ -41,8 +41,13 @@ class BookListApi {
    * @description 搜索图书
    * @method POST
    */
-  searchBooks(params: searchBookQuery): Promise<PageResponse<BookItem>>{
-    return YTRequest.post<PageResponse<BookItem>, searchBookQuery>(ApiUrl.searchBooks, params)
+  async searchBooks(params: searchBookQuery): Promise<Array<BookItem>>{
+    let ans = await YTRequest.post<PageResponse<BookItem>, searchBookQuery>(ApiUrl.searchBooks, params)
+    let list: Array<BookItem> = []
+    ans.list?.forEach(item => {
+      list.push(new BookItem(item))
+    })
+    return list
   }
 
   /**

+ 24 - 12
features/feature/src/main/ets/components/BookItemComp.ets

@@ -1,4 +1,5 @@
 import { BookItem, yTRouter } from "basic"
+import { OrderApi } from "../apis/OrderApi"
 import { CustomTextStyle } from "../style/CustomTextStyle"
 import { tagItemComp } from "./BuilderIndex"
 
@@ -7,19 +8,26 @@ export struct BookItemComp {
   @Param @Require item: BookItem
   // 是否为列表模式, true - 列表模式 false - 封面模式
   @Param showModel: boolean = true
-  @Param schoolbagFlag: string = '0'
 
   @Event addCar: () => void
 
-  router2DetailPage() {
-    yTRouter.router2BookItemDetailPage(this.item)
-  }
+  async addCarEvent(){
+    if(this.item.schoolbagFlag == '0') {
+      await OrderApi.addBookToBag(this.item.id!)
+      this.item.schoolbagFlag = '1'
+    } else {
+      await OrderApi.removeBookFromBag([this.item.id!])
+      this.item.schoolbagFlag = '0'
+    }
 
-  @Monitor('schoolbagFlag')
-  minor(){
-    console.log(`this.schoolbagFlag = ${JSON.stringify(this.schoolbagFlag)}`)
+    if(this.addCar){
+      this.addCar()
+    }
   }
 
+  router2DetailPage() {
+    yTRouter.router2BookItemDetailPage(this.item)
+  }
 
   build() {
     if(this.showModel) {
@@ -44,13 +52,16 @@ export struct BookItemComp {
           top: { anchor: '__container__', align: VerticalAlign.Top }
         })
 
-      Image(true ? $r('[basic].media.icon_deerIncrease') : $r('[basic].media.icon_reduce'))
+      Image(this.item.schoolbagFlag == '0' ? $r('[basic].media.icon_deerIncrease') : $r('[basic].media.icon_reduce'))
         .width(24)
         .aspectRatio(1)
         .alignRules({
           right: { anchor: '__container__', align: HorizontalAlign.End },
           bottom: { anchor: '__container__', align: VerticalAlign.Bottom }
         })
+        .onClick(() => {
+          this.addCarEvent()
+        })
     }.width('100%').height('100%')
   }
   .width('100%').height(214)
@@ -72,6 +83,9 @@ export struct BookItemComp {
     Column({space: 3}){
       Text(this.item?.bookTitle)
         .attributeModifier(new CustomTextStyle({size: 14, weight: 600}))
+        .onClick(() => {
+          this.item.bookTitle = 'hhhhhhh'
+        })
 
       Text(this.item?.bookSubtitle)
         .width('100%')
@@ -86,13 +100,11 @@ export struct BookItemComp {
           tagItemComp('情绪价值', '#FF4EB1EF', '#FFEDF7FD')
         }
 
-        Image(this.schoolbagFlag == '0' ? $r('[basic].media.icon_deerIncrease') : $r('[basic].media.icon_reduce'))
+        Image(this.item.schoolbagFlag == '0' ? $r('[basic].media.icon_deerIncrease') : $r('[basic].media.icon_reduce'))
           .width(24)
           .aspectRatio(1)
           .onClick(() => {
-            if(this.addCar){
-              this.addCar()
-            }
+            this.addCarEvent()
           })
       }
       .width("100%")

+ 2 - 3
features/feature/src/main/ets/pages/viewModel/BookSearchResultViewModel.ets

@@ -1,7 +1,6 @@
 import { BookItem } from "basic";
 import { bookListApi } from "../../apis/BookListApi";
 import { BookListItem } from "../../model/BookModelIndex";
-import { PageResponse } from "../../model/PageResponse";
 import { searchBookListQuery, searchBookQuery } from "../../model/Query";
 
 @ObservedV2
@@ -56,8 +55,8 @@ export class BookSearchResultViewModel{
 
   // 获取书籍列表
   async getBooks() {
-    let ans: PageResponse<BookItem> = await bookListApi.searchBooks(this.booksQuery.clone())
-    this.book = ans.list ?? []
+    this.book = await bookListApi.searchBooks(this.booksQuery.clone())
+    // this.book = ans.list ?? []
     console.log(`获取书籍列表 = ${JSON.stringify(this.bookList)}`)
   }
 }

+ 12 - 23
features/feature/src/main/ets/view/SecondView.ets

@@ -4,7 +4,6 @@ import { CustomTextStyle } from '../style/CustomTextStyle'
 import { BookListTypeList } from '../model/BookModelIndex'
 import { BookItem } from 'basic'
 import { BookItemComp } from '../components/BookItemComp'
-import { OrderApi } from '../apis/OrderApi'
 
 @ComponentV2
 export struct SecondView {
@@ -127,29 +126,19 @@ export struct SecondView {
             // 书籍列表项
             Column(){
               List({space: 12}){
-                // ListItem()
+                ListItem()
 
-                // todo 找到 repeat 不刷新的原因
-                // Repeat(this.vm.bookList)
-                //   .each((item: RepeatItem<BookItem>) => {
-                //     BookItemComp({
-                //       item: item.item,
-                //       showModel: this.vm.showMode == 1,
-                //       schoolbagFlag: item.item.schoolbagFlag,
-                //       addCar: () => {
-                //         item.item.schoolbagFlag = '1'
-                //         this.vm.bookList.splice(item.index, 1, item.item)
-                //         // OrderApi.addBookToBag(item.id!)
-                //     }})
-                //   })
-                //   .key((item: BookItem) => `${item?.id}${item?.schoolbagFlag}${item.bookTitle}`)
-                ForEach(this.vm.bookList, (item: BookItem, index: number) => {
-                  BookItemComp({item: item, showModel: this.vm.showMode == 1,addCar: () => {
-                      item.schoolbagFlag = '1'
-                      this.vm.bookList.splice(index, 1, item)
-                      OrderApi.addBookToBag(item.id!)
-                  }})
-                }, (item: BookItem) => JSON.stringify(item))
+                Repeat(this.vm.bookList)
+                  .each((item: RepeatItem<BookItem>) => {
+                    BookItemComp({
+                      item: item.item,
+                      showModel: this.vm.showMode == 1,
+                      addCar: () => {}
+                    })
+                  })
+                  .key((item: BookItem) => `${item?.id}${item?.schoolbagFlag}${item.bookTitle}`)
+
+                ListItem()
               }
               .width("100%")
               .height("100%")

+ 7 - 8
features/feature/src/main/ets/view/viewModel/SecondViewModel.ets

@@ -1,9 +1,8 @@
-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"
+import { BookItem, YTAvoid, yTRouter } from 'basic'
+import { AppStorageV2 } from '@kit.ArkUI'
+import { BookCategoryStorage } from '../../model/Storage'
+import { searchBookQuery } from '../../model/Query'
+import { bookListApi } from '../../apis/BookListApi'
 
 @ObservedV2
 export class SecondViewModel{
@@ -119,8 +118,8 @@ export class SecondViewModel{
 
   // 获取书籍列表
   async getBookList() {
-    let ans: PageResponse<BookItem> = await bookListApi.searchBooks(this.query.clone())
-    this.bookList = [...(ans.list ?? [])]
+    this.bookList = await bookListApi.searchBooks(this.query.clone())
+
     console.log(`获取书籍列表 = ${JSON.stringify(this.bookList)}`)
   }
 }