Sfoglia il codice sorgente

feat: 新增 是否加入 购物车 属性

YuJing 1 mese fa
parent
commit
a965bca170

+ 6 - 1
commons/basic/src/main/ets/models/BookModel.ets

@@ -1,9 +1,10 @@
 /**
  * 书本信息
  */
+@ObservedV2
 export class BookItem {
   /** 书籍ID */
-  id?: string;
+  @Trace id?: string;
   /** 书名 */
   bookTitle?: string;
   /** 副标题 */
@@ -58,4 +59,8 @@ export class BookItem {
   updateTime?: string;
   /** 分类名称 */
   typeName?: string;
+  /** 是否加入购物车 0-未加入 1-已加入 */
+  @Trace schoolbagFlag?: string;
+
+
 }

+ 1 - 1
commons/basic/src/main/ets/models/UserInfo.ets

@@ -146,7 +146,7 @@ export class UserInfo {
     if (this.userId) {
       return this.userId.toString().padStart(8, '0')
     }
-    return '00000000'
+    return null
   }
 
   setId(id: number) {

+ 108 - 0
features/feature/src/main/ets/components/BookItemComp.ets

@@ -0,0 +1,108 @@
+import { BookItem, yTRouter } from "basic"
+import { CustomTextStyle } from "../style/CustomTextStyle"
+import { tagItemComp } from "./BuilderIndex"
+
+@ComponentV2
+export struct BookItemComp {
+  @Param @Require item: BookItem
+  // 是否为列表模式, true - 列表模式 false - 封面模式
+  @Param showModel: boolean = true
+
+  @Event addCar: () => void
+
+  router2DetailPage() {
+    yTRouter.router2BookItemDetailPage(this.item)
+  }
+
+  build() {
+    if(this.showModel) {
+      this.bookItemComp()
+    } else {
+      this.bookItemCompCover()
+    }
+  }
+
+
+  // 书籍Item - 封面模式
+  @Builder
+  bookItemCompCover(){
+  Row(){
+    RelativeContainer(){
+      Image(this.item.coverUrl)
+        .width(200)
+        .aspectRatio(1)
+        .alt($r('[basic].media.png_TopRecommend'))
+        .alignRules({
+          middle: { anchor: '__container__', align: HorizontalAlign.Center },
+          top: { anchor: '__container__', align: VerticalAlign.Top }
+        })
+
+      Image(true ? $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 }
+        })
+    }.width('100%').height('100%')
+  }
+  .width('100%').height(214)
+  .padding({right: 19, bottom: 15, top: 2})
+  .backgroundImageSize({width: '100%', height: '100%'})
+  .backgroundImage($r('[basic].media.png_book_CompCover'))
+}
+
+
+
+  // 书籍Item - 列表模式
+  @Builder
+  bookItemComp(){
+  Row({space: 11}){
+    Image(this.item.coverUrl)
+      .width(86)
+      .aspectRatio(1)
+      .borderRadius(12)
+      .alt($r('[basic].media.png_TopRecommend'))
+
+    Column({space: 3}){
+      Text(this.item?.bookTitle)
+        .attributeModifier(new CustomTextStyle({size: 14, weight: 600}))
+
+      Text(this.item?.bookSubtitle)
+        .width('100%')
+        .attributeModifier(new CustomTextStyle({size: 12, weight: 400, color: '#FF555555'}))
+
+      Blank()
+        .layoutWeight(1)
+
+      Row(){
+        Row({space: 13}){
+          tagItemComp(`${this.item.minAge}-${this.item.maxAge}岁`, '#FFFC9911', '#FFFFF5E7')
+          tagItemComp('情绪价值', '#FF4EB1EF', '#FFEDF7FD')
+        }
+
+        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()
+            }
+          })
+      }
+      .width("100%")
+      .justifyContent(FlexAlign.SpaceBetween)
+    }
+    .layoutWeight(1)
+    .alignItems(HorizontalAlign.Start)
+  }
+  .padding(8)
+  .height(102)
+  .width('100%')
+  .borderRadius(12)
+  .border({width: 2, color: '#FF000000'})
+  .onClick(() => {
+    this.router2DetailPage()
+  })
+}
+}

+ 0 - 76
features/feature/src/main/ets/components/BuilderIndex.ets

@@ -58,83 +58,7 @@ export function bookListItemComp(item: RepeatItem<BookListItem>){
 }
 
 
-// 书籍Item - 封面模式
-@Builder
-export function bookItemCompCover(item: RepeatItem<BookItem>, onClick: () => void){
- Row(){
-   RelativeContainer(){
-     Image(item.item.coverUrl)
-       .width(200)
-       .aspectRatio(1)
-       .alt($r('[basic].media.png_TopRecommend'))
-       .alignRules({
-         middle: { anchor: '__container__', align: HorizontalAlign.Center },
-         top: { anchor: '__container__', align: VerticalAlign.Top }
-       })
-
-     Image(true ? $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 }
-       })
-   }.width('100%').height('100%')
- }
- .width('100%').height(214)
- .padding({right: 19, bottom: 15, top: 2})
- .backgroundImageSize({width: '100%', height: '100%'})
- .backgroundImage($r('[basic].media.png_book_CompCover'))
-}
-
-
-
-// 书籍Item - 列表模式
-@Builder
-export function bookItemComp(item: RepeatItem<BookItem>, onClick: () => void, onAddCar?: () => void){
-  Row({space: 11}){
-    Image(item.item.coverUrl)
-      .width(86)
-      .aspectRatio(1)
-      .borderRadius(12)
-      .alt($r('[basic].media.png_TopRecommend'))
 
-    Column({space: 3}){
-      Text(item.item?.bookTitle)
-        .attributeModifier(new CustomTextStyle({size: 14, weight: 600}))
-
-      Text(item.item?.bookSubtitle)
-        .width('100%')
-        .attributeModifier(new CustomTextStyle({size: 12, weight: 400, color: '#FF555555'}))
-
-      Blank()
-        .layoutWeight(1)
-
-      Row(){
-        Row({space: 13}){
-          tagItemComp(`${item.item.minAge}-${item.item.maxAge}岁`, '#FFFC9911', '#FFFFF5E7')
-          tagItemComp('情绪价值', '#FF4EB1EF', '#FFEDF7FD')
-        }
-
-        Image(true ? $r('[basic].media.icon_deerIncrease') : $r('[basic].media.icon_reduce'))
-          .width(24)
-          .aspectRatio(1)
-      }
-      .width("100%")
-      .justifyContent(FlexAlign.SpaceBetween)
-    }
-    .layoutWeight(1)
-    .alignItems(HorizontalAlign.Start)
-  }
-  .padding(8)
-  .height(102)
-  .width('100%')
-  .borderRadius(12)
-  .border({width: 2, color: '#FF000000'})
-  .onClick(() => {
-    onClick()
-  })
-}
 
 
 

+ 6 - 1
features/feature/src/main/ets/model/Query.ets

@@ -1,7 +1,11 @@
+import { userInfo } from "basic"
+
 abstract class Query{
   pageNum: number = 1
   pageSize: number = 15
   total: number = 0
+  /** 用户id */
+  userId: string | null = userInfo.getId()
 
   reload() {
     this.pageNum = 1
@@ -47,7 +51,8 @@ export class searchBookQuery extends Query {
       maxAge: this.maxAge,
       minAge: this.minAge,
       sort: this.sort,
-      typeId: this.typeId
+      typeId: this.typeId,
+      userInfo: this.userId
     } as ESObject
   }
 }

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

@@ -237,7 +237,7 @@ struct BookItemDetailPage {
 
           ForEach(['加入书包', '立刻借阅'], (item: string, index) => {
             Row(){
-              Text(item)
+              Text(index == 0 && this.vm.book.schoolbagFlag == '1' ? '已加入' : item)
                 .attributeModifier(new CustomTextStyle({size: 18, weight: 400}))
             }
             .layoutWeight(1)

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

@@ -1,5 +1,4 @@
 import { RouterPage, YTHeader } from 'basic';
-import { bookItemComp } from '../../components/BuilderIndex';
 import { CustomTextStyle } from '../../style/CustomTextStyle';
 import { BookListDetailViewModel } from '../viewModel/BookListDetailViewModel';
 

+ 5 - 2
features/feature/src/main/ets/pages/BookList/BookSearchResultPage.ets

@@ -1,5 +1,6 @@
 import { BookItem, YTHeader } from 'basic'
-import { bookItemComp, bookListItemComp } from '../../components/BuilderIndex';
+import { BookItemComp } from '../../components/BookItemComp';
+import { bookListItemComp } from '../../components/BuilderIndex';
 import { ytEmptyComp } from '../../components/ytComp/ytEmptyComp';
 import { BookListItem } from '../../model/BookModelIndex';
 import { CustomTextStyle } from '../../style/CustomTextStyle';
@@ -71,7 +72,9 @@ struct BookSearchResultPage {
         Repeat(this.vm.book)
           .each((item: RepeatItem<BookItem>) => {
             ListItem(){
-              bookItemComp(item, () => {}, () => {})
+              BookItemComp({item: item.item, addCar: () => {
+                item.item.schoolbagFlag = '1'
+              }})
             }
           })
 

+ 17 - 17
features/feature/src/main/ets/view/SecondView.ets

@@ -1,14 +1,13 @@
 import { DeerSearch } from '../components/DeerSearch'
 import { SecondViewModel } from './viewModel/SecondViewModel'
 import { CustomTextStyle } from '../style/CustomTextStyle'
-import { bookItemComp, bookItemCompCover } from '../components/BuilderIndex'
 import { BookListTypeList } from '../model/BookModelIndex'
-import { cert } from '@kit.DeviceCertificateKit'
 import { BookItem } from 'basic'
+import { BookItemComp } from '../components/BookItemComp'
 
 @ComponentV2
 export struct SecondView {
-  vm: SecondViewModel = new SecondViewModel()
+  @Local vm: SecondViewModel = new SecondViewModel()
 
   build() {
     Column(){
@@ -127,21 +126,22 @@ export struct SecondView {
             // 书籍列表项
             Column(){
               List({space: 12}){
-                ListItem()
+                // ListItem()
 
-                Repeat(this.vm.bookList)
-                  .each((item: RepeatItem<BookItem>) => {
-                    if(this.vm.showMode) {
-                      // 列表模式
-                      bookItemComp(
-                        item,
-                        () => { this.vm.goBookDetail(item.item) },
-                        () => { this.vm.addToBook(item.item) })
-                    } else {
-                      // 封面模式
-                      bookItemCompCover(item, () => {})
-                    }
-                  })
+                // Repeat(this.vm.bookList)
+                //   .each((item: RepeatItem<BookItem>) => {
+                //     BookItemComp({item: item.item, showModel: this.vm.showMode == 1,addCar: () => {
+                //       item.item.schoolbagFlag = '1'
+                //       this.vm.bookList.splice(item.index, 1, item.item)
+                //     }})
+                //   })
+                //   .key((item: BookItem) => JSON.stringify(item))
+                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)
+                  }})
+                }, (item: BookItem) => JSON.stringify(item))
               }
               .width("100%")
               .height("100%")

+ 5 - 3
features/feature/src/main/ets/view/viewModel/SecondViewModel.ets

@@ -10,7 +10,7 @@ export class SecondViewModel{
   @Trace safeTop: number = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
 
   // 书籍列表
-  @Trace bookList: BookItem[] = []
+  @Trace bookList: Array<BookItem> = []
   // 分类索引
   @Trace categoryIndex: number = 0
   // 是否已读
@@ -45,7 +45,9 @@ export class SecondViewModel{
   }
 
   // 添加至书包
-  addToBook(book: BookItem) {}
+  addToBook(book: BookItem) {
+    book.schoolbagFlag = '1'
+  }
 
   // 更改选中的分类
   changeCategory(index: number, toIndex: boolean = false) {
@@ -118,7 +120,7 @@ export class SecondViewModel{
   // 获取书籍列表
   async getBookList() {
     let ans: PageResponse<BookItem> = await bookListApi.searchBooks(this.query.clone())
-    this.bookList = ans.list ?? []
+    this.bookList = [...ans.list ?? []]
     console.log(`获取书籍列表 = ${JSON.stringify(this.bookList)}`)
   }
 }