CardSourceInfo.ets 11.1 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';
import measure from '@ohos.measure'
import display from '@ohos.display';

const TAG = "CardSourceInfo"

@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[] = []

  @State onlyShowCornerAndSource: boolean = false;

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

    this.titleInit();
    this.calcContentSpace();
  }

  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() {
    let time = this.contentDTO.updateTime && this.contentDTO.updateTime.length > 0?this.contentDTO.updateTime:this.contentDTO.publishTime
    let str = DateTimeUtils.getCommentTime(
      time.includes(' ')
        ? Number.parseFloat(new Date(time).getTime().toString())
        : Number.parseFloat(time)
    )
    console.log('cj2024 str', str)
    return str
  }

  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)) {
        console.log('cj2024 isTwoDaysAgo', this.isTwoDaysAgo(publishTime))
        flag = false
      } else {
        flag = true;
      }
    } else {
      flag = true;
    }
    console.log('cj2024 flag', flag)
    return flag;
  }

  /**
   * 全域数字显示规则
   * 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) {
      let num1: string = num.slice(0, -4); // 万
      let num2: string = num.slice(-4, -3); // 千
      const num3: string = num.slice(-3, -2); // 百
      if (Math.round(Number(`0.${num3}`)) === 1) num2 = `${Number(num2) + 1}`
      if (Number(num2) === 10) {
        num2 = '0';
        num1 = `${Number(num1) + 1}`
      }
      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 = num.slice(-3, -2); // 百
      if (Math.round(Number(`0.${num3}`)) === 1) num2 = `${Number(num2) + 1}`
      if (Number(num2) === 10) {
        num2 = '0';
        num1 = `${Number(num1) + 1}`
      }
      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()
  }

  showDot() {
    let flag = false;
    if (this.contentDTO.rmhPlatform === 1 || this.contentDTO.source) {
      if (this.showTime() && !this.isEllipsisActive) {
        flag = true
      } else if (!this.isEllipsisActive) {
        if (this.contentDTO.objectType !=='2' && !this.isCompInnerSource && Number(this.getContentDtoBean()?.interactData?.commentNum) > 0) {
          flag = true;
        } else if (this.contentDTO.objectType !=='2' && this.isCompInnerSource && this.contentDTO.interactData && this.contentDTO.interactData?.commentNum > 0) {
          flag = true;
        }
      } else if (this.contentDTO?.author&&this.contentDTO?.author.length > 0) {
        flag = true;
      }
    }

    return flag;
  }

  getContentSize(textContent: string, fontSize: number ) {
    return Number(measure.measureTextSize({
      textContent,
      fontSize
    }).width)
  }

  calcContentSpace() {

    // Logger.debug(TAG, 'display-text ' + this.displayText)
    if (this.isLimited()) return;

    const screenWidth = display.getDefaultDisplaySync().width
    let leftSpace = screenWidth;
    // Logger.debug(TAG, 'display-leftSpace ' + leftSpace)
    const souceSize = this.getContentSize(this.displayText, 12);
    const dotSize = 11;

    if (this.contentDTO.cornerMark || this.contentDTO.corner) {
      const cornerSize = this.getContentSize(this.contentDTO.cornerMark || this.contentDTO.corner, 12);
      leftSpace = leftSpace - cornerSize
      // Logger.debug(TAG, ('display-cornerMark ' + cornerSize)
    }

    if (this.showTime()) {
      const timeSize = this.getContentSize(this.handleTimeStr(), 12);
      leftSpace = leftSpace - dotSize - timeSize
      // Logger.debug(TAG, 'display-showtime')
    }

    if (!this.isEllipsisActive) {
      let commentSize = 0;
      const commentNum = this.getContentDtoBean()?.interactData?.commentNum
      if (commentNum) {
        commentSize = this.getContentSize(`${this.handlerNum(commentNum.toString())}评`, 12);
      }
      leftSpace = leftSpace - dotSize - commentSize
      // Logger.debug(TAG, 'display-commentSize ' + commentSize)
    }

    if (leftSpace < souceSize) {
      this.onlyShowCornerAndSource = true;
      // Logger.debug(TAG, 'display-size 1')
    }
    // Logger.debug(TAG, 'display-size 2,' + leftSpace + " " + souceSize + " " + this.onlyShowCornerAndSource)
  }

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

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

      if (!this.onlyShowCornerAndSource) {
        // 点
        if (this.showDot()) {

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

        }

        // 发布日期
        if (this.showTime()) {
          Text(this.handleTimeStr())
            .fontSize(12)
            .fontColor($r("app.color.color_B0B0B0"))
            .flexShrink(0)
            .margin({ right: 4 })
            .visibility(!this.isEllipsisActive ? Visibility.Visible : Visibility.Hidden)
        }

        // 评论数
        if (!this.isEllipsisActive) {
          if (this.contentDTO.objectType !=='2' && !this.isCompInnerSource && Number(this.getContentDtoBean()?.interactData?.commentNum) > 0) {
            Text(`${this.handlerNum(this.getContentDtoBean()?.interactData?.commentNum.toString())}评`)
              .fontSize(12)
              .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(12)
                .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(12)
      .fontColor($r("app.color.color_B0B0B0"))
      .align(Alignment.Start)
      .width('100%')
      .margin({top:8})
    }
  }
  .width(CommonConstants.FULL_WIDTH)
  .margin({ top: this.viewShowData ? 8 : 0 })

  }

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

  /**
   * 检测是否有展示的数据
   */
  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) {
      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
    }
  }
}