PageViewModel.ets 4.21 KB
import { BottomNavBean, GroupDTO, NavigationBody } from 'wdBean';
import { Logger, ResourcesUtils } from 'wdKit';
import { ResponseDTO, WDHttp } from 'wdNetwork';
import { PageRepository } from '../repository/PageRepository';
import http from '@ohos.net.http';
import { BusinessError } from '@ohos.base';

const TAG = 'PageViewModel';

/**
 * 处理返回后的数据
 */
export class PageViewModel {
  /**
   * get Nav Data from Resource .
   *
   * @return BottomNavBean[] Nav Data List
   */
  static getBottomNavData(context: Context): BottomNavBean[] {
    Logger.info(TAG, `getBottomNavData start`);
    let compRes: ResponseDTO<NavigationBody> | null = ResourcesUtils.getResourcesJsonSync<ResponseDTO<NavigationBody>>(context, 'bottom_nav.json');
    if (!compRes || !compRes.data || !compRes.data.bottomNavList) {
      Logger.info(TAG, `getBottomNavData compRes bottomNavList is empty`);
      return []
    }
    Logger.info(TAG, `getBottomNavData getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
    return compRes.data.bottomNavList
  }

  static getNavData(url: string): Promise<NavigationBody> {
    return new Promise<NavigationBody>((success, error) => {
      Logger.info(TAG, `getNavData start`);
      PageRepository.fetchNavigationDataApi(url).then((navResDTO: WDHttp.ResponseDTO<NavigationBody>) => {
        if (!navResDTO) {
          Logger.error(TAG, 'getNavData then navResDTO is empty');
          error('navResDTO is empty');
          return
        }
        if (navResDTO.code != http.ResponseCode.OK) {
          Logger.error(TAG, `getNavData then code:${navResDTO.code}, message:${navResDTO.message}`);
          error('navResDTO Response Code is failure');
          return
        }
        if (!navResDTO.body?.bottomNavList) {
          error('navResDTO list is empty');
          return
        }
        // let navResStr = JSON.stringify(navResDTO);
        Logger.info(TAG, "getNavData then,navResDTO.timeStamp:" + navResDTO.timeStamp);
        let navigationBean = navResDTO.body
        success(navigationBean);
      }).catch((err: BusinessError) => {
        Logger.error(TAG, `fetchNavigationDataApi catch, error.code : ${err.code},  error.message:${err.message}`);
        error(err);
      })
    })
  }

  /**
   * Get Group data.
   *
   * @return {GroupDTO} compRes.data
   */
  static getGroupDTO(context: Context): GroupDTO {
    let compRes: ResponseDTO<GroupDTO> | null = ResourcesUtils.getResourcesJsonSync<ResponseDTO<GroupDTO>>(context, 'comp_list0.json');
    if (!compRes || !compRes.data) {
      Logger.info(TAG, `getCompList compRes is empty`);
      return {} as GroupDTO
    }
    Logger.info(TAG, `getCompList getResourcesJson compRes : ${JSON.stringify(compRes)}`);
    return compRes.data
  }

  /**
   * Get Group data.
   *
   * @return {GroupDTO} compRes.data
   */
  static getGroup2DTO(context: Context): GroupDTO {
    let compRes: ResponseDTO<GroupDTO> | null = ResourcesUtils.getResourcesJsonSync<ResponseDTO<GroupDTO>>(context, 'comp_list2.json');
    if (!compRes || !compRes.data) {
      Logger.info(TAG, `getCompList compRes is empty`);
      return {} as GroupDTO
    }
    Logger.info(TAG, `getCompList getResourcesJson compRes : ${JSON.stringify(compRes)}`);
    return compRes.data
  }


  static getPageData(url: string): Promise<GroupDTO> {
    return new Promise<GroupDTO>((success, error) => {
      PageRepository.fetchPageData(url).then((resDTO: WDHttp.ResponseDTO<GroupDTO>) => {
        if (!resDTO) {
          Logger.error(TAG, 'getPageData then resDTO is empty');
          error("page data is empty");
          return
        }
        if (resDTO.code != http.ResponseCode.OK || !resDTO.body) {
          Logger.error(TAG, `getPageData then code:${resDTO.code}, message:${resDTO.message}`);
          error(`get page data error code:${resDTO.code}, message:${resDTO.message}`);
          return
        }
        Logger.info(TAG, "getPageData then,resDTO.timeStamp:" + resDTO.timeStamp);
        success(resDTO.body);
      }).catch((err: BusinessError) => {
        Logger.error(TAG, `getPageData catch, error.code : ${err.code},  error.message:${err.message}`);
        error(err);
      })
    })
  }
}



let pageViewModel = new PageViewModel();

export default pageViewModel as PageViewModel;