CardSourceInfo.ets 5.76 KB
import { CompDTO, ContentDTO } from 'wdBean'
import { CommonConstants } from 'wdConstant/Index';
import { DateTimeUtils } from 'wdKit/Index';
import router from '@ohos.router'

@Reusable
@Component
export struct CardSourceInfo {
  @State contentDTO: ContentDTO = new ContentDTO();
  @ObjectLink compDTO: CompDTO
  isCompInnerSource: boolean = false

  aboutToAppear(): void {
  }

  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) {
      const num1: string = num.slice(0, -8); // 亿
      const num2: string = num.slice(-8, -7);
      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() {
    Flex({ alignItems: ItemAlign.Center }) {
      if (this.contentDTO.corner) {
        Text(this.contentDTO.corner)
          .fontSize($r("app.float.font_size_11"))
          .fontColor($r("app.color.color_ED2800"))
          .margin({ right: 2 })
      }
      if (this.contentDTO.cornerMark) {
        Text(this.contentDTO.cornerMark)
          .fontSize($r("app.float.font_size_11"))
          .fontColor($r("app.color.color_ED2800"))
          .margin({ right: 2 })
      }
      if (this.contentDTO.rmhPlatform === 1) {
        Text(this.contentDTO.rmhInfo?.rmhName)
          .fontSize($r("app.float.font_size_11"))
          .fontColor($r("app.color.color_B0B0B0"))
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
      } else if (this.contentDTO.source) {
        Text(`${this.contentDTO.source}`)
          .fontSize($r("app.float.font_size_11"))
          .fontColor($r("app.color.color_B0B0B0"))
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
      }

      if (((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)

      }

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

      if (!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.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)
    .margin({ top: 8 })
  }

  /**
   *  获取稿件业务对象
   * @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]
  }
}