RmhTitle.ets 7.14 KB
/**
 * 这里是人民号动态中的顶部信息:人民号logo,名字,描述,关注等
 */
import {
  ContentDetailRequest,
  postInteractAccentionOperateParams
} from 'wdDetailPlayApi/src/main/ets/request/ContentDetailRequest';
import { RmhInfoDTO, CompDTO, ContentDTO } from 'wdBean'
import { CommonConstants } from 'wdConstant/Index';
import { DateTimeUtils, SPHelper, Logger, ToastUtils } from 'wdKit';
import { SpConstants } from 'wdConstant/Index'
import { ProcessUtils, WDRouterPage, WDRouterRule } from 'wdRouter/Index';
import router from '@ohos.router'
import { postBatchAttentionStatusParams } from 'wdBean/Index';
import { MultiPictureDetailViewModel } from '../../viewmodel/MultiPictureDetailViewModel'
import { onlyWifiLoadImg } from '../../utils/lazyloadImg';
import { InfomationCardClick } from '../../utils/infomationCardClick'

@Component
export struct RmhTitle {
  @State compDTO: CompDTO = new CompDTO()
  @State contentDTO: ContentDTO = new ContentDTO();
  @State pageId: string = '';
  @State pageName: string = '';
  @Prop rmhInfo: RmhInfoDTO
  @Prop publishTime: string | undefined
  @State loadImg: boolean = false;
  isPeopleShipHome: boolean = false;
  /**
   * 是否需要隐藏发布时间超过2天的时间展示,默认不隐藏
   */
  @Prop hideTime: boolean
  /**
   * 默认未关注 点击去关注
   */
  @State followStatus: string = '0';

  /**
   * 关注号主
   */
  async handleAccention() {
    // 未登录,跳转登录
    const user_id = await SPHelper.default.get(SpConstants.USER_ID, '')
    if (!user_id) {
      WDRouterRule.jumpWithPage(WDRouterPage.loginPage)
      return
    }

    const params2: postInteractAccentionOperateParams = {
      attentionUserType: this.rmhInfo?.userType || '', //被关注用户类型(1 普通用户 2 视频号 3 矩阵号)
      attentionUserId: this.rmhInfo?.userId || '', // 被关注用户号主id
      attentionCreatorId: this.rmhInfo?.rmhId || '', // 被关注用户号主id
      status: this.followStatus == '0' ? 1 : 0,
    }
    ContentDetailRequest.postInteractAccentionOperate(params2).then(res => {
      console.log('rmhTitle-data', JSON.stringify(res.data))

      InfomationCardClick.track(
        this.compDTO,
        this.contentDTO,
        this.pageId,
        this.pageName,
        'follow', // like, commentClick, follow
        this.followStatus == '0' ? true : false,
      )

      if (this.followStatus == '1') {
        this.followStatus = '0'
      } else {
        this.followStatus = '1'
        // 弹窗样式与何时调用待确认
        ContentDetailRequest.postPointLevelOperate({ operateType: 6 }).then((res) => {
          console.log('关注号主获取积分==', JSON.stringify(res.data))
          if (res.data?.showToast) {
            ToastUtils.showToast(res.data.ruleName + '+' + res.data.rulePoint + '积分', 1000);
          }
        })
      }
    })
  }
  /**
   * 查询当前登录用户是否关注作品号主
   * */
  async getBatchAttentionStatus() {
    try {
      const params: postBatchAttentionStatusParams = {
        creatorIds: [{ creatorId: this.rmhInfo?.rmhId ?? '' }]
      }
      let data = await MultiPictureDetailViewModel.getBatchAttentionStatus(params)
      this.followStatus = data[0]?.status;
      Logger.info(`rmhTitle-followStatus:${JSON.stringify(this.followStatus)}`)
    } catch (exception) {
      Logger.info(`rmhTitle-followStatus:${JSON.stringify(exception)}`)
    }
  }

  async aboutToAppear(): Promise<void> {
    this.getBatchAttentionStatus()
    const curRouter = router.getState().name;
    if (curRouter === 'MainPage') {
      this.hideTime = true;
    }

    this.loadImg = await onlyWifiLoadImg();
  }

  getDaysBetweenDates(date: number) {
    const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
    const time1 = new Date().getTime(); // 今天日期的时间戳
    const time2 = new Date(date).getTime(); // 要比较日期的时间戳
    const diffDays = Math.round(Math.abs((time1 - time2) / oneDay)); // 两个日期时间戳差值除以一天的毫秒数得到天数,取绝对值并四舍五入
    return Math.ceil(diffDays);
  }

  build() {
    Flex() {
      Stack() {
        Image(this.loadImg ? this.rmhInfo?.rmhHeadUrl : this.rmhInfo.userType == '1'?$r('app.media.default_head_userPage'):$r('app.media.comment_rmh_tag'))
          .alt(this.rmhInfo.userType == '1'?$r('app.media.default_head_userPage'):$r('app.media.comment_rmh_tag'))
          .width(36)
          .height(36)
          .borderRadius(50)
        Image(this.rmhInfo?.authIcon)
          .width(14)
          .height(14)
          .borderRadius(50)
      }
      .margin({ right: 8 })
      .alignContent(Alignment.BottomEnd)
      .flexShrink(0)

      Column() {
        Text(this.rmhInfo?.rmhName)
          .fontSize(15)
          .fontColor($r('app.color.color_222222'))
          .fontWeight(600)
          .alignSelf(ItemAlign.Start)
          .height(21)
          .lineHeight(21)
          .margin({bottom: 1})
        Flex({alignContent: FlexAlign.Start, wrap: FlexWrap.NoWrap}) {
          Row() {
            if (!(this.hideTime && this.getDaysBetweenDates(Number(this.publishTime)) > 2)) {
              if (this.publishTime) {
                Text(DateTimeUtils.getCommentTime(Number.parseFloat(this.publishTime)))
                  .fontSize($r("app.float.font_size_12"))
                  .fontColor($r("app.color.color_B0B0B0"))
                  .height(14)
                  .lineHeight(14)
              }
              if (this.publishTime && this.rmhInfo?.rmhDesc) {
                Image($r('app.media.point'))
                  .width(14)
                  .height(14)
              }
            }
            if(this.rmhInfo?.rmhDesc != undefined){
              Text(this.rmhInfo?.rmhDesc.replaceAll('\n', ''))
                .fontSize($r("app.float.font_size_12"))
                .fontColor($r("app.color.color_B0B0B0"))
                .maxLines(1)
                .alignSelf(ItemAlign.Start)
                .textOverflow({ overflow: TextOverflow.Ellipsis })
                .textAlign(TextAlign.Start)
                .height(14)
                .lineHeight(14)
            }

          }
          .width('75%')
          .height(14)
        }
      }
      .justifyContent(FlexAlign.SpaceBetween)

      Blank()
      if (this.rmhInfo?.cnIsAttention) {
        Row() {
          if (Number(this.followStatus) === 0) {
            Image($r('app.media.rmh_follow'))
              .width(16)
              .height(16)
          }

          Text(Number(this.followStatus) === 0 ? '关注' : '已关注')
            .fontSize($r('app.float.font_size_13'))
            .fontColor(Number(this.followStatus) === 0 ? $r('app.color.color_ED2800') : 0xc6c6c6)
        }
        .flexShrink(0)
        .alignSelf(ItemAlign.Center)
        .onClick(() => {
          this.handleAccention();
        })
      }
    }
    .width(CommonConstants.FULL_WIDTH)
    .margin({ bottom: 10 })
    .onClick(()=>{
      ///个人主页 动态头像不能跳转
      if (this.isPeopleShipHome) return

      // 跳转号主
      ProcessUtils.gotoPeopleShipHomePage( this.rmhInfo?.rmhId,this.rmhInfo?.banControl,this.rmhInfo?.cnMainControl,this.rmhInfo?.userId,this.rmhInfo?.userType)

    })
  }
}