PlayViewModel.ets 4.09 KB
import { BusinessError } from '@ohos.base'
import { Logger, ToastUtils } from 'wdKit'
import { ResponseDTO } from 'wdNetwork'
import { ContentDetailRequest } from 'wdDetailPlayApi'
import { ContentDetailDTO, RmhInfoDTO, UserInfoDTO } from 'wdBean'

const TAG = 'PlayViewModel';

@Observed
export class PlayViewModel {
  contentId: string
  relId: string
  relType: string
  newsSourceName?: string
  newsTitle?: string
  editorName?: string
  newsSummary?: string
  rmhInfo?: RmhInfoDTO | null
  userInfo?: UserInfoDTO | null
  url?: string
  // 视频朝向,
  // 横屏视频:1(进入竖屏,立即展示半屏播放窗口,当切换横屏时,再展示全部播放窗口;后续可再转换到竖屏):
  // 竖屏视频:2(进入竖屏,直接展示全屏播放窗口,且不能切换横屏);
  videoLandScape?: number
  nextContId?: string
  canStart?: boolean
  message?: string

  constructor(contentId: string = '', relId: string = '', relType: string = '') {
    // todo:
    this.contentId = contentId // '30013266075'
    this.relId = relId // '500000301942'
    this.relType = relType // '1'

    // this.getGlobalInfo();
  }

  playWithContentId(contentId: string) {
    this.contentId = contentId;
    // this.getContentDetailData() // 包括播放地址PlayUrl
  }

  playWithIds(contentId: string, relId: string, relType: string) {
    this.contentId = contentId;
    this.relId = relId;
    this.relType = relType;
    // this.getContentDetailData() // 包括播放地址PlayUrl
  }

  setContentDetailData(data: ContentDetailDTO | undefined) {
    if (!data) {
      Logger.error(TAG, 'setContentDetailData is empty');
      return
    }

    Logger.infoOptimize(TAG, () => {
      return JSON.stringify(data)
    })
    this.newsTitle = data.newsTitle
    this.editorName = data.editorName
    this.newsSummary = data.newsSummary
    this.userInfo = data.userInfo
    this.rmhInfo = data.rmhInfo
    if (data.videoInfo?.length > 0) {
      this.url = data.videoInfo[0].videoUrl
      this.videoLandScape = data.videoInfo[0].videoLandScape
    }
    if (data.authorList?.length > 0) {
      this.newsSourceName = data.authorList[0].authorName
    }
    this.canStart = true;
    this.message = '';
  }

  getContentDetailData() {
    ContentDetailRequest.getContentDetail({
      contentId: this.contentId,
      relId: this.relId,
      relType: this.relType
    }).then((resDTO: ResponseDTO<ContentDetailDTO[]>) => {
      if (!resDTO) {
        Logger.error(TAG, 'getContentDetailData then resDTO is empty');
        return
      }
      // Logger.info(TAG, "getNavData then,navResDTO.timeStamp:" + navResDTO.timeStamp);
      if (!resDTO.data || resDTO.data.length == 0) {
        Logger.error(TAG, `getContentDetailData then body is empty`);
        return
      }

      let contentDetailDTO: ContentDetailDTO = resDTO.data[0]
      Logger.infoOptimize(TAG, () => {
        return JSON.stringify(contentDetailDTO)
      })
      this.newsTitle = contentDetailDTO.newsTitle
      this.editorName = contentDetailDTO.editorName
      this.newsSummary = contentDetailDTO.newsSummary
      this.userInfo = contentDetailDTO.userInfo
      this.rmhInfo = contentDetailDTO.rmhInfo
      if (contentDetailDTO.videoInfo?.length > 0) {
        this.url = contentDetailDTO.videoInfo[0].videoUrl
        this.videoLandScape = contentDetailDTO.videoInfo[0].videoLandScape
      }
      if (contentDetailDTO.authorList?.length > 0) {
        this.newsSourceName = contentDetailDTO.authorList[0].authorName
      }
      this.canStart = true;
      this.message = '';
    }).catch((err: BusinessError) => {
      Logger.error(TAG, `getContentDetailData catch, error.code : ${err.code},  error.message:${err.message}`);
      // todo:
      // this.title = '旅途平安!这首歌送给即将启程回家的你'
      // this.url = 'https://cdnjdout.aikan.pdnews.cn/zhbj-20240127/vod/content/output/f45ef51bb33f4ffd9458f8b386aa3227_opt.mp4'
      this.canStart = false;
      this.message = '获取播放地址异常';
      ToastUtils.showToast(this.message, 1000);
    })
      .finally(() => {
        Logger.debug(TAG, `getContentDetailData finally`);
      })
  }
}