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

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)
  }

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