BookItemComp.ets 3.4 KB

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