BannerComponent.ets 4.29 KB
import { CompDTO, ContentDTO, DelayTimeEnum } from 'wdBean';
import { CommonConstants } from 'wdConstant';
import { Logger } from 'wdKit';
import { CompUtils } from '../utils/CompUtils';
import { EmptyComponent } from './EmptyComponent';

const TAG = 'BannerComponent';

/**
 * 轮播组件,即Banner/轮播大图/焦点图/自动滑动
 * 样式:
 * 'Carousel_Layout-01', // 通用轮播卡:视频、直播、活动、专题、榜单、外链
 */
@Component
export struct BannerComponent {
  @StorageLink('currentBreakpoint') @Watch('watchCurrentBreakpoint') currentBreakpoint: string = 'xs';
  @State compDTO: CompDTO = {} as CompDTO

  watchCurrentBreakpoint() {
    Logger.info(TAG, `watchCurrentBreakpoint, this.currentBreakpoint: ${this.currentBreakpoint}`);
  }

  aboutToAppear() {
    Logger.info(TAG, `aboutToAppear, beanList:${this.compDTO?.operDataList?.length}, currentBreakpoint:${this.currentBreakpoint}`);
  }

  aboutToDisappear() {
    Logger.info(TAG, 'aboutToDisappear');
  }

  onPageShow() {
    Logger.info(TAG, 'onPageShow');
  }

  onPageHide() {
    Logger.info(TAG, 'onPageHide');
  }

  onBackPress() {
    Logger.info(TAG, 'onBackPress');
  }

  build() {
    if (this.compDTO && this.compDTO?.operDataList?.length > 0) {
      Swiper() {
        ForEach(this.compDTO?.operDataList, (item: ContentDTO, index: number) => {
          this.buildItemBanner01(item, index)
        })
      }
      .displayCount(1) // 仅展示1个图片
      .cachedCount(2)
      .index(1) // The default index of Swiper.
      .autoPlay(true)
      .interval(DelayTimeEnum.INTERVAL_4000)
      .indicator(Indicator.dot()
        .right(5)
        .itemWidth(4)
        .itemHeight(4)
        .selectedItemWidth(10)
        .selectedItemHeight(6))
      .loop(true)
      .duration(DelayTimeEnum.DURATION_1000)
      .vertical(false)
      .curve(Curve.Linear)
      .onChange((index: number) => {
        Logger.info(TAG, `Swiper onChange index : ${index}`);
      })
    } else {
      EmptyComponent({ emptyHeight: 200 })
    }
  }

  // public buildDisplayCount(): number {
  //   return new BreakPointType({ xs: 1, sm: 1, md: 2, lg: 3 }).getValue(this.currentBreakpoint)
  // }

  /**
   * 组件项
   *
   * @param programmeBean item 组件项
   */
  @Builder
  buildItemBanner01(item: ContentDTO, index: number) {
    RelativeContainer() {
      Image(item.hImageUrl)
        .width(CommonConstants.FULL_PARENT)
        .height(CommonConstants.FULL_PARENT)
        .objectFit(ImageFit.Cover)
        .borderRadius($r("app.float.border_radius_6"))
        .alignRules({
          top: { anchor: '__container__', align: VerticalAlign.Top },
          left: { anchor: '__container__', align: HorizontalAlign.Start }
        })
        .id('img_cover')

      // if (item.topLeftTipImgUrl) {
      //   Image(item.topLeftTipImgUrl)
      //     .width(CompCornerUtil.getCornerWidth(this.currentBreakpoint))
      //     .aspectRatio(CompCornerUtil.ASPECT_RATIO_75_45)
      //     .margin({ left: 6 })
      //     .objectFit(ImageFit.Cover)
      //     .alignRules({
      //       top: { anchor: '__container__', align: VerticalAlign.Top },
      //       left: { anchor: '__container__', align: HorizontalAlign.Start }
      //     })
      //     .id('img_corner_top_Left')
      // }

      Text(item.title)
        .width(CommonConstants.FULL_PARENT)
        .height(39)
        .padding({ left: 8, right: 69, bottom: 8 })
        .fontColor(Color.White)
        .fontSize($r('app.float.font_size_16'))
        .fontWeight(FontWeight.Medium)
        .textAlign(TextAlign.Start)
        .align(Alignment.Bottom)
        .maxLines(CompUtils.MAX_LINES_1)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .linearGradient({
          direction: GradientDirection.Top, // 渐变方向:to Top/从下往上
          colors: [[0x7508111A, 0.0], [0x7508111A, 0.3], [Color.Transparent, 1.0]]
        })
        .alignRules({
          bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
          left: { anchor: '__container__', align: HorizontalAlign.Start }
        })
        .id('txt_name')
    }
    .width(CommonConstants.FULL_PARENT)
    .aspectRatio(CompUtils.ASPECT_RATIO_2_1)
    .hoverEffect(HoverEffect.Scale)
    .onClick((event: ClickEvent) => {
      Logger.info(TAG, `BannerComponent onClick event index: ${index}`);
    })
  }
}