Card10Component.ets 7.56 KB
import { ContentDTO, slideShows, VideoInfoDTO, CompDTO } from 'wdBean';
import { CommonConstants } from 'wdConstant';
import { ProcessUtils } from 'wdRouter';
import { CardSourceInfo } from '../cardCommon/CardSourceInfo';
import { CardMediaInfo } from '../cardCommon/CardMediaInfo';
import { Notes } from './notes';
import { onlyWifiLoadImg } from '../../utils/lazyloadImg';
import { InfomationCardClick } from '../../utils/infomationCardClick'
import { SearchShowRed, titleInitRes, textItem } from '../../utils/searchShowRed';

/**
 * 大专题卡--CompStyle:  10
 */
const TAG: string = 'Card10Component';

@Preview
@Component
export struct Card10Component {
  @State pageId: string = '';
  @State pageName: string = '';
  @State contentDTO: ContentDTO = new ContentDTO();
  @State loadImg: boolean = false;
  @ObjectLink compDTO: CompDTO
  @State titleMarked: boolean = false;
  @State textArr: textItem[] = []

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

  titleInit() {
    const titleInitRes:titleInitRes = SearchShowRed.titleInit(this.contentDTO.title);
    this.titleMarked = titleInitRes.titleMarked;
    this.textArr = titleInitRes.textArr;
  }

  build() {
    Column() {
      // 顶部标题,最多两行
      if (this.contentDTO.titleShow === 1 && this.contentDTO.newsTitle) {
        Text() {
          if (this.titleMarked) {
            ForEach(this.textArr, (textItem: textItem) => {
              if (textItem.isRed) {
                Span(textItem.content)
                  .fontColor(0xED2800)
              } else {
                Span(textItem.content)
              }
            })
          } else {
            Span(this.contentDTO.newsTitle)
          }
        }
          .width(CommonConstants.FULL_WIDTH)
          .fontSize($r('app.float.font_size_18'))
          .fontWeight(600)
          .maxLines(2)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .margin({ bottom: 19 })
          .onClick((event: ClickEvent) => {
            InfomationCardClick.track(this.compDTO, this.contentDTO, this.pageId, this.pageName)
            ProcessUtils.processPage(this.contentDTO)
          })
      }
      // 大图
      Stack() {
        Image(this.loadImg ? this.contentDTO?.coverUrl : '')
          .backgroundColor(0xf5f5f5)
          .width('100%')
          .borderRadius({
            topLeft: $r('app.float.image_border_radius'),
            topRight: $r('app.float.image_border_radius')
          })
          .onClick((event: ClickEvent) => {
            InfomationCardClick.track(this.compDTO, this.contentDTO, this.pageId, this.pageName)
            ProcessUtils.processPage(this.contentDTO)
          })
        if(this.contentDTO.objectType == '5'){
          Notes({ objectType: this.contentDTO.objectType }).height(30).align(Alignment.Center).margin({ left: 5, bottom: 5 })
        } else {
          if (this.contentDTO.seoTags) {
            Notes({ newTags: this.contentDTO.seoTags }).height(30).align(Alignment.Center)
          }
          if (this.contentDTO.newTags) {
            Notes({ newTags: this.contentDTO.newTags }).height(30).align(Alignment.Center)
          }
        }
      }.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 })
        .onClick((event: ClickEvent) => {
          InfomationCardClick.track(this.compDTO, this.contentDTO, this.pageId, this.pageName)
          ProcessUtils.processPage(this.contentDTO)
        })
      }
    }
    .width(CommonConstants.FULL_WIDTH)
    .padding({
      left: $r('app.float.card_comp_pagePadding_lf'),
      right: $r('app.float.card_comp_pagePadding_lf'),
      top: $r('app.float.card_comp_pagePadding_tb'),
      bottom: $r('app.float.card_comp_pagePadding_tb')
    })
    .backgroundColor($r("app.color.white"))
    .margin({ bottom: 8 })
  }

  @Builder
  timelineItem(item: slideShows, index: number) {
    Row() {
      Column() {
        Stack() {
          if(this.contentDTO.objectType == '5'){
            Notes({ objectType: this.contentDTO.objectType }).height(30).align(Alignment.Center)
          } else {
            if (this.contentDTO.seoTags) {
              Notes({ newTags: this.contentDTO.seoTags }).height(30).align(Alignment.Center)
            }
            if (this.contentDTO.newTags) {
              Notes({ newTags: this.contentDTO.newTags }).height(30).align(Alignment.Center)
            }
          }
          Text(item.newsTitle)
            .fontSize($r('app.float.font_size_17'))
            .fontWeight(400)
            .fontColor($r('app.color.color_222222'))
            .maxLines(2)
            .textOverflow({ overflow: TextOverflow.Ellipsis })
            .textIndent((this.contentDTO.newTags?.length || this.contentDTO.seoTags?.length) > 2 &&
              (this.contentDTO.newTags?.length || this.contentDTO.seoTags?.length) < 5 ? 58 :
              ((this.contentDTO.newTags?.length > 0 || this.contentDTO.seoTags?.length > 0) || this.contentDTO.objectType == '5') ? 35 :
                0 )
        }.alignContent(Alignment.TopStart)

        //bottom 评论等信息
        CardSourceInfo(
          {
            compDTO: this.compDTO,
            contentDTO: this.createContent(item)
          }
        )
      }
      .layoutWeight(1)
      .alignItems(HorizontalAlign.Start)

      // 右侧图片
      if (item.fullColumnImgUrls[0] && item.fullColumnImgUrls[0].url) {
        Stack() {
          Image(this.loadImg ? item.fullColumnImgUrls[0].url : '')
            .backgroundColor(0xf5f5f5)
            .width(117)
            .height(78)
            .objectFit(ImageFit.Cover)
            .borderRadius($r('app.float.image_border_radius'))
            .margin({ left: 12 })
          CardMediaInfo({
            contentDTO: this.createMediaInfoContent(item)
          })
        }
        .alignContent(Alignment.BottomEnd)
      }
    }
    .padding({ top: 10, bottom: 10 })
    .onClick((event: ClickEvent) => {
      const str: string = JSON.stringify(this.contentDTO);
      const data: ContentDTO = JSON.parse(str)
      data.objectId = item.newsId
      data.relId = item.relId
      data.objectType = String(item.objectType)
      ProcessUtils.processPage(data)
    })
  }

  private createContent(item: slideShows): ContentDTO {
    let contentDTO = new ContentDTO()
    contentDTO.publishTime = item.publishTime.toString() || '';
    contentDTO.source = item.source || '';
    return contentDTO;
  }

  private createMediaInfoContent(item: slideShows): ContentDTO {
    let contentDTO = new ContentDTO()
    contentDTO.objectType = String(item.objectType);
    contentDTO.videoInfo = { videoDuration: Number(item.videoDuration) as number } as VideoInfoDTO;
    contentDTO.photoNum = Number(item.photoNum);
    contentDTO.voiceInfo = { voiceDuration: Number(item.voiceDuration) as number };
    return contentDTO;
  }
}