MyCollectionViewModel.ets 4.45 KB
import { MyCollectionListModel } from '../model/MyCollectionModel';
import HashMap from '@ohos.util.HashMap';
import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork';
import { Logger, ResourcesUtils } from 'wdKit';
import { PageDTO } from 'wdBean';

const TAG = "MyCollectionViewModel"

class MyCollectionViewModel {
  private static instance:MyCollectionViewModel

  /**
   * 单例模式
   * @returns
   */
  public static getInstance(): MyCollectionViewModel {
    if (!MyCollectionViewModel.instance) {
      MyCollectionViewModel.instance = new MyCollectionViewModel();
    }
    return MyCollectionViewModel.instance;
  }

  BaseGetRequest(type:number,tagId:string,pageNum:string){
    let url = HttpUrlUtils.getMyCollectionListDataUrl()+ `?type=${type}&operateTag=${1}&pageSize=${20}&pageNum=${pageNum}`
    if (tagId.length > 0) {
      url = url + `&tagId=${tagId}`
    }
    let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders()
    return WDHttp.get<ResponseDTO<MyCollectionListModel>>(url, headers)
  }

  async getAppointmentListDataLocal(context: Context): Promise<MyCollectionListModel> {
    Logger.info(TAG, `getBottomNavDataMock start`);
    let compRes: ResponseDTO<MyCollectionListModel> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<MyCollectionListModel>>(context,'browsingHistory_list_data.json' );
    if (!compRes || !compRes.data) {
      Logger.info(TAG, `getAppointmentListDataLocal compRes  is empty`);
      return new MyCollectionListModel()
    }
    Logger.info(TAG, `getAppointmentListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
    return compRes.data
  }

  //Type 1 收藏 2 浏览历史
  //tagId 收藏界面 标签筛选
  fetchMyCollectList(type:number,tagId:string,pageNum:string,context: Context):Promise<MyCollectionListModel>{
    return new Promise<MyCollectionListModel>((success,error) => {
      this.BaseGetRequest(type,tagId,pageNum).then((navResDTO: ResponseDTO<MyCollectionListModel>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getAppointmentListDataLocal(context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let listData =  navResDTO.data as MyCollectionListModel
        success(listData)
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        error("page data invalid");
      })
    })
  }


  newBaseGetRequest(type:number,tagId:string,pageNum:number){
    let url = HttpUrlUtils.getMyCollectionListDataUrl()+ `?type=${type}&operateTag=${1}&pageSize=${20}&pageNum=${pageNum.toString()}`
    if (tagId.length > 0) {
      url = url + `&tagId=${tagId}`
    }
    let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders()
    return WDHttp.get<ResponseDTO<PageDTO>>(url, headers)
  }

  newFetchMyCollectList(type:number,tagId:string,pageNum:number,context: Context):Promise<PageDTO>{
    return new Promise<PageDTO>((success,error) => {
      success(this.newGetAppointmentListDataLocal(type,context))
      return
      this.newBaseGetRequest(type,tagId,pageNum).then((navResDTO: ResponseDTO<PageDTO>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.newGetAppointmentListDataLocal(type,context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let listData =  navResDTO.data as PageDTO
        success(listData)
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        error("page data invalid");
      })
    })
  }

  async newGetAppointmentListDataLocal(type:number, context: Context): Promise<PageDTO> {
    Logger.info(TAG, `getBottomNavDataMock start`);
    let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>(context,type == 1?'MyCollection_list_data.json':'browsingHistory_list_data.json');
    if (!compRes || !compRes.data) {
      Logger.info(TAG, `getAppointmentListDataLocal compRes  is empty`);
      return {} as PageDTO
    }
    Logger.info(TAG, `getAppointmentListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
    return compRes.data
  }
}

const collectionViewModel = MyCollectionViewModel.getInstance();

export default collectionViewModel as MyCollectionViewModel