Card16Component.ets 6.47 KB
import { ContentDTO, CompDTO } from 'wdBean';
import { RmhTitle } from '../cardCommon/RmhTitle'
import { CardMediaInfo } from '../cardCommon/CardMediaInfo'
import { CommonConstants } from 'wdConstant/Index';
import { ProcessUtils } from 'wdRouter';
import {CarderInteraction} from '../CarderInteraction'
import { onlyWifiLoadImg } from '../../utils/lazyloadImg';
import { persistentStorage, hasClicked } from '../../utils/persistentStorage';
import { InfomationCardClick } from '../../utils/infomationCardClick'
import { SearchShowRed, titleInitRes, textItem } from '../../utils/searchShowRed';
import router from '@ohos.router'

const TAG = 'Card16Component';

interface fullColumnImgUrlItem {
  url: string,
  fullUrl: string
}


/**
 * 人民号-动态---16:人民号三图卡;
 */
@Component
export struct Card16Component {
  @ObjectLink compDTO: CompDTO
  @State pageId: string = '';
  @State pageName: string = '';
  @State contentDTO: ContentDTO = new ContentDTO();
  @State loadImg: boolean = false;
  @State clicked: boolean = false;
  @State titleMarked: boolean = false;
  @State textArr: textItem[] = []

  async aboutToAppear(): Promise<void> {
    this.titleInit();
    const curRouter = router.getState().name;
    this.clicked = hasClicked(this.contentDTO.objectId, curRouter)
    this.loadImg = await onlyWifiLoadImg();
  }

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

  build() {
    Column() {
      // rmh信息
      if (this.contentDTO.rmhInfo) {
        RmhTitle(
          {
            rmhInfo: this.contentDTO.rmhInfo,
            publishTime: this.contentDTO.publishTime,
            contentDTO: this.contentDTO,
            compDTO: this.compDTO,
            pageId: this.pageId,
            pageName: this.pageName
          }
        )
      }
      // 标题
      if (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)
          }
        }
          .fontSize($r('app.float.font_size_18'))
          .fontColor(this.clicked ? 0x848484 : $r('app.color.color_222222'))
          .width(CommonConstants.FULL_WIDTH)
          .textOverflowStyle(2)
          .margin({ bottom: 8 })
          .lineHeight(25)
      }
      if (this.contentDTO.fullColumnImgUrls?.length > 0) {
        //三图
        Stack(){
          Row() {
            GridRow({ gutter: 2 }) {
              ForEach(this.contentDTO.fullColumnImgUrls.slice(0, 3), (item: fullColumnImgUrlItem, index: number) => {
                if (index < 3) {
                  GridCol({ span: { xs: 4 } }) {
                    Image(this.loadImg ? item.url || item.fullUrl : '')
                      .backgroundColor(0xf5f5f5)
                      .width('100%')
                      .aspectRatio(113 / 75)
                      .borderRadius({
                        topLeft: index === 0 ? $r('app.float.image_border_radius') : 0,
                        topRight: index === 2 ? $r('app.float.image_border_radius') : 0,
                        bottomLeft: index === 0 ? $r('app.float.image_border_radius') : 0,
                        bottomRight: index === 2 ? $r('app.float.image_border_radius') : 0,
                      })
                  }
                }
              })
            }
          }
          .width(CommonConstants.FULL_PARENT)
          .margin({ top: 8 })
          CardMediaInfo({
            contentDTO: this.contentDTO
          })
        }
        .width(CommonConstants.FULL_PARENT)
        .alignContent(Alignment.BottomEnd)
        // Flex() {
        //   ForEach(this.contentDTO.fullColumnImgUrls.slice(0, 3), (item: fullColumnImgUrlItem, index: number) => {
        //     Image(this.loadImg ? item.url : '')
        //       .backgroundColor(0xf5f5f5)
        //       .flexBasis(113)
        //       .height(75)
        //       .margin({ right: index > 1 ? 0 : 2 })
        //   })
        // }
      }
      CarderInteraction({contentDTO: this.contentDTO})
      //TODO  底部的:分享、评论、点赞 功能;需要引用一个公共组件
    }
    .padding({
      left: 10,
      right: 10,
      top: $r('app.float.card_comp_pagePadding_tb'),
      bottom: $r('app.float.card_comp_pagePadding_tb')
    })
    .backgroundColor(0xffffff)
    .width(CommonConstants.FULL_WIDTH)
    .onClick((event: ClickEvent) => {
      InfomationCardClick.track(this.compDTO, this.contentDTO, this.pageId, this.pageName)
      if (router.getState().name !== 'MyCollectionListPage') {
        this.clicked = true;
      }
      persistentStorage(this.contentDTO.objectId);
      ProcessUtils.processPage(this.contentDTO)
    })
  }
}

interface radiusType {
  topLeft: number | Resource;
  topRight: number | Resource;
  bottomLeft: number | Resource;
  bottomRight: number | Resource;
}

@Component
struct createImg {
  @Prop contentDTO: ContentDTO
  @State loadImg: boolean = false;

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

  build() {
    GridRow() {
      if (this.contentDTO.fullColumnImgUrls[0].landscape === 1) {
        // 横屏
        GridCol({
          span: { xs: 12 }
        }) {
          Stack() {
            Image(this.loadImg ? this.contentDTO.coverUrl : '')
              .backgroundColor(0xf5f5f5)
              .width(CommonConstants.FULL_WIDTH)
              .aspectRatio(16 / 9)
              .borderRadius($r('app.float.image_border_radius'))
            CardMediaInfo({ contentDTO: this.contentDTO })
          }
          .align(Alignment.BottomEnd)
        }
      } else {
        // 竖图显示,宽度占50%,高度自适应
        GridCol({
          span: { xs: 6 }
        }) {
          Stack() {
            Image(this.loadImg ? this.contentDTO.coverUrl : '')
              .backgroundColor(0xf5f5f5)
              .width(CommonConstants.FULL_WIDTH)
              .borderRadius($r('app.float.image_border_radius'))
            CardMediaInfo({ contentDTO: this.contentDTO })
          }
          .align(Alignment.BottomEnd)
        }
      }
    }
  }
}


@Extend(Text)
function textOverflowStyle(maxLine: number) {
  .maxLines(maxLine)
  .textOverflow({ overflow: TextOverflow.Ellipsis })
}