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

const TAG = 'PlayViewModel';

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

  constructor() {
    // todo:
    this.contentId = '30013266075'
    this.relId = '500000301942'
    this.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
  }

  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]
      this.newsSourceName = contentDetailDTO.newsSourceName
      this.title = contentDetailDTO.newsTitle
      this.newsSummary = contentDetailDTO.newsSummary
      if (contentDetailDTO.videoInfo?.length > 0) {
        this.url = contentDetailDTO.videoInfo[0].videoUrl
        this.videoLandScape = contentDetailDTO.videoInfo[0].videoLandScape
      }
      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`);
      })
  }
}