CardSourceInfo.ets 9.19 KB
import { CompDTO, ContentDTO } from 'wdBean'
import { CommonConstants, CompStyle } from 'wdConstant/Index';
import { DateTimeUtils, Logger } from 'wdKit/Index';
import router from '@ohos.router'
import { SearchShowRed, textItem, titleInitRes } from '../../utils/searchShowRed';

@Reusable
@Component
export struct CardSourceInfo {
  @State @Watch('checkData') contentDTO: ContentDTO = new ContentDTO();
  @ObjectLink compDTO: CompDTO
  // 特殊稿件内部item展示的来源信息
  isCompInnerSource: boolean = false
  // 是否有展示的信息,如来源,标签、时间、评论
  @State viewShowData: boolean = true
  private maxLength: number = 16;
  @State private isEllipsisActive: boolean = false;
  @State private displayText: string = '';

  @State authorMarked: boolean = false;
  @State authorArr: textItem[] = []

  aboutToAppear(): void {
    this.processText();

    this.titleInit();
  }

  titleInit() {
    if (this.contentDTO?.author&&this.contentDTO?.author.length > 0) {
      const titleInitRes:titleInitRes = SearchShowRed.titleInit(this.contentDTO.author);
      this.authorMarked = titleInitRes.titleMarked;
      this.authorArr = titleInitRes.textArr;
    }
  }

  private isLimited(): boolean {
    return this.compDTO.compStyle === CompStyle.Card_13
      || this.compDTO.compStyle === CompStyle.Card_14
      || this.compDTO.compStyle === CompStyle.Card_06
      || this.compDTO.compStyle === CompStyle.Card_21;
  }

  processText() {
    const sourceText = this.contentDTO.rmhPlatform === 1 ? this.contentDTO.rmhInfo?.rmhName : this.contentDTO.source;
    if (this.isLimited() && sourceText.length > this.maxLength) {
      this.displayText = sourceText.substring(0, this.maxLength) + '...';
      this.isEllipsisActive = true;
    } else {
      this.displayText = sourceText;
      this.isEllipsisActive = false;
    }
    //Logger.warn(`cj2024 sourceText displayText=${this.displayText} isEllipsisActive=${this.isEllipsisActive}`)
  }

  aboutToReuse(params: Record<string, object>): void {
    this.contentDTO = params.contentDTO as ContentDTO
  }

  aboutToRecycle(): void {
  }

  aboutToDisappear(): void {
  }

  handleTimeStr() {
    return DateTimeUtils.getCommentTime(
      this.contentDTO.publishTime.includes(' ')
        ? Number.parseFloat(new Date(this.contentDTO.publishTime).getTime().toString())
        : Number.parseFloat(this.contentDTO.publishTime)
    )
  }

  showTime() {
    console.log('curRouter', this.contentDTO.publishTime)
    const curRouter = router.getState().name;
    const publishTime = this.contentDTO.publishTime.includes(' ')
      ? new Date(this.contentDTO.publishTime).getTime().toString()
      : this.contentDTO.publishTime
    let flag: boolean = false;
    if (curRouter === 'MainPage') {
      if (this.isTwoDaysAgo(publishTime)) {
        flag = false
      } else {
        flag = true;
      }
    } else {
      flag = true;
    }
    return flag;
  }

  showCommentNum() {
    const curRouter = router.getState().name;
    // return curRouter !== 'PeopleShipHomePage'
    return true
  }

  /**
   * 全域数字显示规则
   * 1、当数量为千位以內时,显示数字,不保留小数点,比如 4585
   * 2、当数量为万位~1亿时,显示xx 万,保留小数点后一位,比如1517.9w、2.9w
   * 3、当数量为1亿~千亿时,显示XX 亿,保留小数点后一位,比如1517.9亿、2.9亿
   * 4、不进行四舍五入
   * 5、0 和空 不显示
   */
  handlerNum(number: string) {
    const num = number ?? '0';
    if (Number.parseInt(num) <= 9999) {
      return Number.parseInt(num).toString()
    } else if (Number.parseInt(num) > 9999 && Number.parseInt(num) <= 99999999) {
      const num1: string = num.slice(0, -4); // 万
      const num2: string = num.slice(-4, -3); // 千
      return num2 === '0' ? num1 + '万' : num1 + '.' + num2 + '万'
    } else if (Number.parseInt(num) > 99999999) {
      let num1: string = num.slice(0, -8); // 亿
      let num2: string = num.slice(-8, -7);
      const num3: string = `0.${num.slice(-7, -6)}`
      if (Math.round(Number(num3)) > Number(num3)) {
        if (`${Number(num2) + 1}`.length === 2) {
          num1 = `${Number(num1) + 1}`
          num2 = '0'
        }
      }
      return num2 === '0' ? num1 + '亿' : num1 + '.' + num2 + '亿'
    }
    return num
  }

  isTwoDaysAgo(date: string) {
    const twoDaysAgo = new Date();
    twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
    console.log('curRouter', date)
    return parseInt(date) < twoDaysAgo.getTime()
  }

  build() {
  Column(){
    Flex({ justifyContent: FlexAlign.Start, direction: FlexDirection.Row }) {
      // 标签
      if (this.contentDTO.cornerMark || this.contentDTO.corner) {
        Text(this.contentDTO.cornerMark || this.contentDTO.corner)
          .fontSize($r("app.float.font_size_11"))
          .fontColor($r("app.color.color_ED2800"))
          .margin({ right: 6 })
          .flexShrink(0)
      }

      // 来源信息
      if (this.contentDTO.rmhPlatform === 1 || this.contentDTO.source) {
        Text(this.displayText)
          .fontSize($r("app.float.font_size_11"))
          .fontColor($r("app.color.color_B0B0B0"))
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
      }

      // 点
      if ((this.showTime()&&(this.contentDTO.rmhPlatform === 1 || this.contentDTO.source)) || ((this.contentDTO.rmhPlatform === 1 && this.contentDTO.rmhInfo?.rmhName &&
        this.contentDTO.rmhInfo?.rmhName != '') || (this.contentDTO.source && this.contentDTO.source != '')) &&
        (this.getContentDtoBean()?.interactData?.commentNum
          // || DateTimeUtils.getCommentTime(Number.parseFloat(this.contentDTO.publishTime)) != ''
          || (this.contentDTO.isSearch || this.contentDTO.isCollection ||
            !this.contentDTO.isSearch && DateTimeUtils.getCommentTime
            (Number
              .parseFloat(this
                .contentDTO.publishTime))
              .indexOf
              ('-') === -1)
        )) {

        Image($r("app.media.point"))
          .width(11)
          .height(11)
          .visibility(!this.isEllipsisActive ? Visibility.Visible : Visibility.Hidden)

      }

      // 发布日期
      if (this.showTime() && !this.isEllipsisActive) {
        Text(this.handleTimeStr())
          .fontSize($r("app.float.font_size_11"))
          .fontColor($r("app.color.color_B0B0B0"))
          .flexShrink(0)
          .margin({ right: 4 })
      }

      // 评论数
      if (!this.contentDTO.cornerMark && !this.contentDTO.corner && !this.isEllipsisActive) {
        if (this.contentDTO.objectType !=='2' && !this.isCompInnerSource && Number(this.getContentDtoBean()?.interactData?.commentNum) > 0 &&
        this.showCommentNum()) {
          Text(`${this.handlerNum(this.getContentDtoBean()?.interactData?.commentNum.toString())}评`)
            .fontSize($r("app.float.font_size_11"))
            .fontColor($r("app.color.color_B0B0B0"))
            .flexShrink(0)
        } else {

          if (this.contentDTO.objectType !=='2' && this.isCompInnerSource && this.contentDTO.interactData && this.contentDTO.interactData?.commentNum > 0) {
            Text(`${this.handlerNum(this.contentDTO.interactData?.commentNum.toString())}评`)
              .fontSize($r("app.float.font_size_11"))
              .fontColor($r("app.color.color_B0B0B0"))
              .flexShrink(0)
          }

        }
      }

    }
    .width(CommonConstants.FULL_WIDTH)

    if (this.contentDTO?.author&&this.contentDTO?.author.length > 0){
      Text() {
        if (this.authorMarked) {
          ForEach(this.authorArr, (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_11'))
      .fontColor($r("app.color.color_B0B0B0"))
      .align(Alignment.Start)
      .width('100%')
      .margin({top:5})
    }
  }
  .width(CommonConstants.FULL_WIDTH)
  .margin({ top: this.viewShowData ? 8 : 0 })

  }

  /**
   *  获取稿件业务对象
   * @returns
   */
  private getContentDtoBean(): ContentDTO {
    if (this.compDTO == undefined) {
      return this.contentDTO
    }
    if (this.compDTO.operDataList.length == 0) {
      return this.contentDTO
    }
    return this.compDTO.operDataList[0]
  }

  /**
   * 检测是否有展示的数据
   */
  checkData() {

    let have = false

    if (this.contentDTO.corner) {
      have = true
    }
    if (this.contentDTO.cornerMark) {
      have = true
    }
    if (this.contentDTO.rmhPlatform === 1) {
      have = true
    } else if (this.contentDTO.source) {
      have = true
    }

    // 发布日期
    if (this.showTime()) {
      have = true
    }

    // 评论数
    if (this.contentDTO.objectType !=='2' && !this.isCompInnerSource && Number(this.getContentDtoBean()?.interactData?.commentNum) > 0 &&
    this.showCommentNum()) {
      have = true
    } else {
      if (this.contentDTO.objectType !=='2' && this.isCompInnerSource && this.contentDTO.interactData && this.contentDTO.interactData?.commentNum > 0) {
        have = true
      }
    }

    if (have) {

    } else {
      this.viewShowData = false
    }
  }
}