Card9Component.ets 5.54 KB
import { ContentDTO, slideShows } from 'wdBean';
import { CommonConstants } from 'wdConstant';
import { DateTimeUtils } from 'wdKit';
import { ProcessUtils } from 'wdRouter';
import { Notes } from './notes';
import { onlyWifiLoadImg } from '../../utils/lazyloadImg';

/**
 * 时间链卡--CompStyle:  09
 */
const TAG: string = 'Card9Component';

@Component
export struct Card9Component {
  @State contentDTO: ContentDTO = new ContentDTO();
  // @State loadImg: boolean = true;
  @State clicked: boolean = false;
  @State titleMarked: boolean = false;
  @State str01: string = '';
  @State str02: string = '';
  @State str03: string = '';

  async aboutToAppear(): Promise<void> {
    this.titleInit();
    // this.loadImg = await onlyWifiLoadImg();
  }

  titleInit() {
    if (this.contentDTO.title?.includes('<em>') && this.contentDTO.title?.includes('</em>')) {
      this.titleMarked = true;
      this.str01 = this.contentDTO.title?.split('<em>')[0] || '';
      this.str02 = this.contentDTO.title?.split('<em>')[1].split('</em>')[0] || '';
      this.str03 = this.contentDTO.title?.split('<em>')[1].split('</em>')[1] || '';
    }
  }

  build() {
    Column() {
      // 顶部标题,最多两行
      if (this.contentDTO.titleShow === 1 && this.contentDTO.newsTitle) {
        Text() {
          if (this.titleMarked) {
            Span(this.str01)
            Span(this.str02)
              .fontColor(0xED2800)
            Span(this.str03)
          } else {
            Span(this.contentDTO.newsTitle)
          }
        }
        .fontColor(this.clicked ? 0x848484 : 0x222222)
        .width(CommonConstants.FULL_WIDTH)
        .fontSize($r('app.float.font_size_18'))
        .maxLines(2)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .margin({ top: 10, bottom: 10 })
        .fontWeight(400)
      }
      // 大图
      Stack() {
        Image(this.contentDTO.coverUrl)
          .backgroundColor(0xf5f5f5)
          .width('100%')
          .height(133)
          .borderRadius({
            topLeft: $r('app.float.image_border_radius'),
            topRight: $r('app.float.image_border_radius')
          })

        if (this.contentDTO.objectType == '5') {
          Notes({ objectType: 5 })
            .margin({ left: 5, bottom: 5 })
        }

      }.alignContent(Alignment.BottomStart)

      // 时间线--后端返回三个,
      Column() {
        ForEach(this.contentDTO.slideShows, (item: slideShows, index: number) => {
          this.timelineItem(item, index)
        })
      }

      // 底部-查看更多。根据接口返回的isMore判断是否显示查看更多
      if (this.contentDTO.hasMore == 1) {
        Row() {
          Text("查看更多")
            .fontSize($r("app.float.font_size_14"))
            .fontColor($r("app.color.color_222222"))
            .margin({ right: 1 })
          Image($r("app.media.more"))
            .width(14)
            .height(14)
        }
        .backgroundColor($r('app.color.color_F5F5F5'))
        .width(CommonConstants.FULL_WIDTH)
        .height(40)
        .borderRadius($r('app.float.button_border_radius'))
        .justifyContent(FlexAlign.Center)
        .margin({ top: 5 })
      }
    }
    .width(CommonConstants.FULL_WIDTH)
    .padding({
      top: 5,
      left: 16,
      right: 16,
      bottom: 14
    })
    .backgroundColor($r("app.color.white"))
    .margin({ bottom: 8 })
    .onClick((event: ClickEvent) => {
      this.clicked = true;
      ProcessUtils.processPage(this.contentDTO)
    })
  }

  @Builder
  timelineItem(item: slideShows, index: number) {
    Column() {
      Stack() {
        if (index < this.contentDTO.slideShows.length - 1) {
          Divider()
            .vertical(true)
            .color($r('app.color.color_EDEDED'))
            .strokeWidth(1)
            .margin({ top: index > 0 ? 0 : 16, left: 4 })
        }
        if (index > 0 && index == this.contentDTO.slideShows.length - 1) {
          Divider()
            .vertical(true)
            .color($r('app.color.color_EDEDED'))
            .strokeWidth(1)
            .height(16)
            .margin({ left: 4 })
        }

        Column() {
          Row() {
            // 标题
            Image($r("app.media.timeAxis"))
              .width(9)
              .height(9)
              .margin({ right: 5 })
              .fillColor(item.newsTitleColor)

            Text(DateTimeUtils.formatDate(item.publishTime, "MM月dd日 HH:mm"))
              .fontSize($r('app.float.font_size_12'))
              .fontColor($r('app.color.color_222222'))
              .fontWeight(600)
          }
          .width(CommonConstants.FULL_WIDTH)
          .height(32)
          .alignItems(VerticalAlign.Center)

          Row() {
            Text(item.newsTitle)
              .fontSize($r('app.float.font_size_17'))
              .fontWeight(400)
              .fontColor($r('app.color.color_222222'))
              .layoutWeight(1)
              .maxLines(2)
              .textOverflow({ overflow: TextOverflow.Ellipsis })
              .alignSelf(ItemAlign.Center)
              .margin({ left: 12 })
            // if (item.fullColumnImgUrls[0] && item.fullColumnImgUrls[0].url) {
            //   Image(this.loadImg? item.fullColumnImgUrls[0].url : '')
            //     .backgroundColor(0xf5f5f5)
            //     .width(90)
            //     .height(60)
            //     .borderRadius($r('app.float.image_border_radius'))
            // }
          }
        }
      }
      .alignContent(Alignment.TopStart)
    }
    .height(item.fullColumnImgUrls[0] && item.fullColumnImgUrls[0].url ? 100 : 78)
    .alignItems(HorizontalAlign.Start)
  }
}