PageUtils.ets 5.2 KB
import { ArrayUtils, Logger } from 'wdKit';
import { CompDTO } from '../bean/CompDTO';
import { ExtraDTO } from '../bean/ExtraDTO';
import { LabelDTO } from '../bean/LabelDTO';
import { Pic } from '../bean/Pic';
import { ProgrammeBean } from '../bean/ProgrammeBean';

const TAG = "PageUtils";

export class PageUtils {
  /**
   * 选择图片
   * @param data
   * @param isUseVerticalImage 是否竖图;true是竖图,否则是横图
   * @param isUseHighImage,是否高清,true是高清,否则不是高清
   * @returns
   */
  static imgUrlChoose(pics: Pic | undefined, isUseVerticalImage: boolean, isUseHighImage: boolean): string {
    if (!pics) {
      return "";
    }

    if (isUseVerticalImage) {
      if (isUseHighImage) {
        if (pics.highResolutionV) {
          return pics.highResolutionV;
        } else if (pics.lowResolutionV) {
          return pics.lowResolutionV;
        }
      } else {
        if (pics.lowResolutionV) {
          return pics.lowResolutionV;
        } else if (pics.highResolutionV) {
          return pics.highResolutionV;
        }
      }
    } else {
      if (isUseHighImage) {
        if (pics.highResolutionH) {
          return pics.highResolutionH;
        } else if (pics.lowResolutionH) {
          return pics.lowResolutionH;
        }
      } else {
        if (pics.lowResolutionH) {
          return pics.lowResolutionH;
        } else if (pics.highResolutionH) {
          return pics.highResolutionH;
        }
      }
    }

    return pics.gkResolution1 ?? "";
  }

  /**
   * 获取Label标题
   *
   * @param component 组件bean
   * @return Label标题
   */
  static getLabelTitle(componentBean: CompDTO): string {
    if (!componentBean) {
      return ""
    }
    let extraData: ExtraDTO | undefined = componentBean.extraData;
    if (!extraData) {
      return ""
    }

    if (!extraData.labels) {
      Logger.info(TAG, 'getLabelTitle, labels is null');
      return ""
    }

    for (let index = 0; index < extraData.labels?.length; index++) {
      let label: LabelDTO = ArrayUtils.getListElement(extraData.labels, index)
      if (!label) {
        // Logger.info(TAG, 'getLabelTitle, labels is null');
        continue;
      }
      if (label.isShow != undefined && label.isShow == false) {
        // Logger.info(TAG, 'getLabelTitle, isShow is false');
        continue;
      }
      if (label.label == '主标题') {
        return label.title ?? ""
      }
    }

    return "";
  }

  /**
   * 选择图片
   * @param data
   * @param isUseVerticalImage 是否竖图;true是竖图,否则是横图
   * @param isUseHighImage,是否高清,true是高清,否则不是高清
   * @returns
   */
  static chooseImgUrl(item: ProgrammeBean, isUseVerticalImage: boolean, isUseHighImage: boolean): string {
    if (!item) {
      return "";
    }
    if (item.programs && item.programs.length > 0) {
      return PageUtils.imgUrlChoose(item.programs[0].pics, isUseVerticalImage, isUseHighImage)
    } else {
      return PageUtils.imgUrlChoose(item.pics, isUseVerticalImage, isUseHighImage)
    }
  }

  static getProgramName(item: ProgrammeBean): string {
    if (!item) {
      return "";
    }
    if (item.programs && item.programs.length > 0 && item.programs[0].name) {
      return item.programs[0].name
    }

    return item.name ?? "";
  }

  static getProgramTitle(item: ProgrammeBean): string {
    if (!item) {
      return "";
    }
    if (item.programs && item.programs.length > 0 && item.programs[0].title) {
      return item.programs[0].title
    }

    return item.title ?? "";
  }

  private static readonly TIP_LIVE: string = "LIVE"; // 直播

  // 获取右上角角标
  static getTopRightTipImgUrl(bean: ProgrammeBean): string | undefined {
    if (!bean) {
      return undefined;
    }

    let tipCode: string | undefined = undefined
    if (bean.programTypeV2 == PageUtils.TIP_LIVE) {
      if (bean.strategyTipMap?.CHARGE?.code) {
        tipCode = bean.strategyTipMap?.CHARGE?.code
      } else if (bean.startTime == '0' || bean.endTime == '0') {
        tipCode = bean.showTip?.code ? bean.showTip?.code : bean.tip?.code
      } else {
        // todo:
      }
    } else if (bean.tip?.code || bean.showTip?.code) {
      tipCode = bean.showTip?.code ? bean.showTip?.code : bean.tip?.code
    } else {
    }
    return PageUtils.getTipImgUrl(tipCode)
  }

  // 获取左上角角标
  static getTopLeftTipImgUrl(bean: ProgrammeBean): string | undefined {
    return PageUtils.getTipImgUrl(bean?.tip2?.code)
  }

  static getTipImgUrl(tipCode: string | undefined, isSetBStyle?: boolean): string | undefined {
    // todo
    return undefined
  }

  /**
   * 获取评分的整数部分
   * @param score
   * @returns 整数部分
   */
  static getScoreInteger(score: string): string {
    if (!score) {
      return '';
    }

    let dotIndex: number = score.indexOf(".");
    dotIndex = dotIndex > 0 ? dotIndex : 0;
    return score.substring(0, dotIndex)
  }

  /**
   * 获取评分的小数部分
   * @param score
   * @returns 小数部分
   */
  static getScoreDecimal(score: string): string {
    if (!score) {
      return '';
    }
    let dotIndex: number = score.indexOf(".");
    dotIndex = dotIndex > 0 ? dotIndex : 0;
    return score.substring(dotIndex, score.length)
  }
}