ContentDetailRequest.ets 1.64 KB
import { Logger, ResourcesUtils } from 'wdKit';
import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork'
import { ContentDetailDTO } from 'wdBean'
import HashMap from '@ohos.util.HashMap';

const TAG = 'ContentDetailRequest';

const mock_switch = false;

export interface ContentDetailRequestParams {
  contentId: string
  relId: string
  relType: string
}

export class ContentDetailRequest {
  static getContentDetailDataMock(context: Context): Promise<ResponseDTO<ContentDetailDTO[]>> {
    Logger.info(TAG, `getContentDetailDataMock start`);
    return ResourcesUtils.getResourcesJson<ResponseDTO<ContentDetailDTO[]>>(context, 'content_detail.json')
  }

  /**
   * 现网-新闻内容详情域名
   */
  static readonly HOST2: string = "https://pdapis.pdnews.cn";
  /**
   * 新闻内容详情【get】接口
   */
  static readonly CONTENT_DETAIL_PATH: string = "/api/rmrb-bff-display-zh/content/zh/c/content/detail";

  static getContentDetailUrl(contentId: string, relId: string, relType: string) {
    let url = ContentDetailRequest.HOST2 + ContentDetailRequest.CONTENT_DETAIL_PATH
    url = url + "?&contentId=" + contentId
      + "&relId=" + relId
      + "&relType=" + relType;
    return url;
  }

  static getContentDetail(params: ContentDetailRequestParams): Promise<ResponseDTO<ContentDetailDTO[]>> {
    if (mock_switch) {
      return ContentDetailRequest.getContentDetailDataMock(getContext());
    }
    let url = ContentDetailRequest.getContentDetailUrl(params.contentId, params.relId, params.relType)
    let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
    return WDHttp.get<ResponseDTO<ContentDetailDTO[]>>(url, headers)
  }
}