BookItemComp.ets 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { BookItem, yTRouter } from "basic"
  2. import { OrderApi } from "../apis/OrderApi"
  3. import { CustomTextStyle } from "../style/CustomTextStyle"
  4. import { tagItemComp } from "./BuilderIndex"
  5. @ComponentV2
  6. export struct BookItemComp {
  7. @Param @Require item: BookItem
  8. // 是否为列表模式, true - 列表模式 false - 封面模式
  9. @Param showModel: boolean = true
  10. @Event addCar: () => void
  11. async addCarEvent(){
  12. if(this.item.schoolbagFlag == '0') {
  13. await OrderApi.addBookToBag(this.item.id!)
  14. this.item.schoolbagFlag = '1'
  15. } else {
  16. await OrderApi.removeBookFromBag([this.item.id!])
  17. this.item.schoolbagFlag = '0'
  18. }
  19. if(this.addCar){
  20. this.addCar()
  21. }
  22. }
  23. router2DetailPage() {
  24. yTRouter.router2BookItemDetailPage(this.item)
  25. }
  26. build() {
  27. if(this.showModel) {
  28. this.bookItemComp()
  29. } else {
  30. this.bookItemCompCover()
  31. }
  32. }
  33. // 书籍Item - 封面模式
  34. @Builder
  35. bookItemCompCover(){
  36. Row(){
  37. RelativeContainer(){
  38. Image(this.item.coverUrl)
  39. .width(200)
  40. .aspectRatio(1)
  41. .alt($r('[basic].media.png_TopRecommend'))
  42. .alignRules({
  43. middle: { anchor: '__container__', align: HorizontalAlign.Center },
  44. top: { anchor: '__container__', align: VerticalAlign.Top }
  45. })
  46. Image(this.item.schoolbagFlag == '0' ? $r('[basic].media.icon_deerIncrease') : $r('[basic].media.icon_reduce'))
  47. .width(24)
  48. .aspectRatio(1)
  49. .alignRules({
  50. right: { anchor: '__container__', align: HorizontalAlign.End },
  51. bottom: { anchor: '__container__', align: VerticalAlign.Bottom }
  52. })
  53. .onClick(() => {
  54. this.addCarEvent()
  55. })
  56. }.width('100%').height('100%')
  57. }
  58. .width('100%').height(214)
  59. .padding({right: 19, bottom: 15, top: 2})
  60. .backgroundImageSize({width: '100%', height: '100%'})
  61. .backgroundImage($r('[basic].media.png_book_CompCover'))
  62. }
  63. // 书籍Item - 列表模式
  64. @Builder
  65. bookItemComp(){
  66. Row({space: 11}){
  67. Image(this.item.coverUrl)
  68. .width(86)
  69. .aspectRatio(1)
  70. .borderRadius(12)
  71. .alt($r('[basic].media.png_TopRecommend'))
  72. Column({space: 3}){
  73. Text(this.item?.bookTitle)
  74. .attributeModifier(new CustomTextStyle({size: 14, weight: 600}))
  75. .onClick(() => {
  76. this.item.bookTitle = 'hhhhhhh'
  77. })
  78. Text(this.item?.bookSubtitle)
  79. .width('100%')
  80. .attributeModifier(new CustomTextStyle({size: 12, weight: 400, color: '#FF555555'}))
  81. Blank()
  82. .layoutWeight(1)
  83. Row(){
  84. Row({space: 13}){
  85. tagItemComp(`${this.item.minAge}-${this.item.maxAge}岁`, '#FFFC9911', '#FFFFF5E7')
  86. tagItemComp('情绪价值', '#FF4EB1EF', '#FFEDF7FD')
  87. }
  88. Image(this.item.schoolbagFlag == '0' ? $r('[basic].media.icon_deerIncrease') : $r('[basic].media.icon_reduce'))
  89. .width(24)
  90. .aspectRatio(1)
  91. .onClick(() => {
  92. this.addCarEvent()
  93. })
  94. }
  95. .width("100%")
  96. .justifyContent(FlexAlign.SpaceBetween)
  97. }
  98. .layoutWeight(1)
  99. .alignItems(HorizontalAlign.Start)
  100. }
  101. .padding(8)
  102. .height(102)
  103. .width('100%')
  104. .borderRadius(12)
  105. .border({width: 2, color: '#FF000000'})
  106. .onClick(() => {
  107. this.router2DetailPage()
  108. })
  109. }
  110. }