MinePageDatasModel.ets 15.9 KB

import MinePagePersonalFunctionsItem from '../viewmodel/MinePagePersonalFunctionsItem'
import MinePageCreatorFunctionsItem from '../viewmodel/MinePageCreatorFunctionsItem'
import MinePageMoreFunctionModel from '../viewmodel/MinePageMoreFunctionModel';
import { HttpUrlUtils } from '../network/HttpUrlUtils';
import HashMap from '@ohos.util.HashMap';
import { ResponseDTO, WDHttp } from 'wdNetwork';
import { MineAppointmentListItem } from '../viewmodel/MineAppointmentListItem';
import { Logger, ResourcesUtils } from 'wdKit';
import { MineFollowListDetailItem } from '../viewmodel/MineFollowListDetailItem';
import { FollowListDetailRequestItem } from '../viewmodel/FollowListDetailRequestItem';
import { FollowListItem } from '../viewmodel/FollowListItem';
import { MineFollowListItem } from '../viewmodel/MineFollowListItem';
import { QueryListIsFollowedItem } from '../viewmodel/QueryListIsFollowedItem';
import { MineCommentListDetailItem } from '../viewmodel/MineCommentListDetailItem';
import { FollowListStatusRequestItem } from '../viewmodel/FollowListStatusRequestItem';
const TAG = "MinePageDatasModel"

/**
 * 我的页面 所有数据 获取封装类
 */
class MinePageDatasModel{
  private static instance: MinePageDatasModel;
  personalData:MinePagePersonalFunctionsItem[] = []
  creatorData:MinePageCreatorFunctionsItem[] = []
  moreData:MinePageMoreFunctionModel[] = []

  private constructor() { }

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




  /**
   * 评论 关注 收藏 等7个数据
   * 包含名字和图标
   */
  getPersonalFunctionsData():MinePagePersonalFunctionsItem[]{
    if(this.personalData.length === 7){
      return this.personalData
    }
    this.personalData.push(new MinePagePersonalFunctionsItem("评论",$r('app.media.mine_comment_icon')))
    this.personalData.push(new MinePagePersonalFunctionsItem("关注",$r('app.media.mine_order_icon')))
    this.personalData.push(new MinePagePersonalFunctionsItem("收藏",$r('app.media.mine_collect_icon')))
    this.personalData.push(new MinePagePersonalFunctionsItem("历史",$r('app.media.mine_history_icon')))
    this.personalData.push(new MinePagePersonalFunctionsItem("消息",$r('app.media.mine_msg_icon')))
    this.personalData.push(new MinePagePersonalFunctionsItem("留言板",$r('app.media.mine_msgboard_icon')))
    this.personalData.push(new MinePagePersonalFunctionsItem("预约",$r('app.media.mine_order_icon')))
    return this.personalData
  }


  /**
   * 发布文章 发布视频 等4个数据
   * 包含名字和图标
   */
  getCreatorFunctionsData():MinePageCreatorFunctionsItem[]{
    if(this.creatorData.length === 4){
      return this.creatorData
    }
    this.creatorData.push(new MinePageCreatorFunctionsItem("发布文章",$r('app.media.mine_active_create_article')))
    this.creatorData.push(new MinePageCreatorFunctionsItem("发布视频",$r('app.media.mine_active_create_video')))
    this.creatorData.push(new MinePageCreatorFunctionsItem("发布动态",$r('app.media.mine_active_create_video')))
    this.creatorData.push(new MinePageCreatorFunctionsItem("发布图集",$r('app.media.mine_active_create_pics')))
    return this.creatorData
  }

  /**
   * 扫一扫 我的奖品 等5个数据
   * 包含名字和图标
   */
  getMoreFunctionsData():MinePageCreatorFunctionsItem[]{
    if(this.moreData.length === 5){
      return this.moreData
    }
    this.moreData.push(new MinePageMoreFunctionModel("扫一扫",$r('app.media.mine_scan')))
    this.moreData.push(new MinePageMoreFunctionModel("我的奖品",$r('app.media.mine_mygift')))
    this.moreData.push(new MinePageMoreFunctionModel("意见反馈",$r('app.media.mine_suggest')))
    this.moreData.push(new MinePageMoreFunctionModel("设置",$r('app.media.mine_setting')))
    this.moreData.push(new MinePageMoreFunctionModel("关于",$r('app.media.mine_about')))
    return this.moreData
  }

  /**
   * 扫一扫 我的奖品 等5个数据
   * 包含名字和图标
   */
  getSettingFunctionsData():MinePageCreatorFunctionsItem[]{
    if(this.moreData.length === 5){
      return this.moreData
    }
    this.moreData.push(new MinePageMoreFunctionModel("扫一扫",$r('app.media.mine_scan')))
    this.moreData.push(new MinePageMoreFunctionModel("我的奖品",$r('app.media.mine_mygift')))
    this.moreData.push(new MinePageMoreFunctionModel("意见反馈",$r('app.media.mine_suggest')))
    this.moreData.push(new MinePageMoreFunctionModel("设置",$r('app.media.mine_setting')))
    this.moreData.push(new MinePageMoreFunctionModel("关于",$r('app.media.mine_about')))
    return this.moreData
  }

  fetchAppointmentListData(pageSize:string,pageNum:string) {
    let url = HttpUrlUtils.getAppointmentListDataUrl()+ `?pageSize=${pageSize}&pageNum=${pageNum}`
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.get<ResponseDTO<MineAppointmentListItem>>(url, headers)
  };

  getAppointmentListData(pageSize:string,pageNum:string,context?: Context): Promise<MineAppointmentListItem> {
    return new Promise<MineAppointmentListItem>((success, error) => {
      Logger.info(TAG, `getAppointmentList start`);
      this.fetchAppointmentListData(pageSize,pageNum).then((navResDTO: ResponseDTO<MineAppointmentListItem>) => {
        if (!navResDTO) {
          error("page data invalid");
          success(this.getAppointmentListDataLocal(context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        // error(err);
        success(this.getAppointmentListDataLocal(context))
      })
    })
  }

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

  /**
   * 关注频道详情
   * @param pageSize
   * @param pageNum
   * @param context
   * @returns
   */
  getFollowListDetailData(params:FollowListDetailRequestItem,context: Context): Promise<MineFollowListDetailItem> {
    return new Promise<MineFollowListDetailItem>((success, error) => {
      Logger.info(TAG, `getAppointmentList start`);
      this.fetchFollowListDetailData(params).then((navResDTO: ResponseDTO<MineFollowListDetailItem>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getFollowListDetailDataLocal(context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data as MineFollowListDetailItem
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        success(this.getFollowListDetailDataLocal(context))
      })
    })
  }

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

  fetchFollowListDetailData(object:FollowListDetailRequestItem) {
    let url = HttpUrlUtils.getFollowListDetailDataUrl()
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.post<ResponseDTO<MineFollowListDetailItem>>(url, object,headers)
  };

  /**
   * 关注频道列表
   * @returns
   */
  fetchFollowListData() {
    let url = HttpUrlUtils.getFollowListDataUrl()
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.get<ResponseDTO<FollowListItem[]>>(url, headers)
  };

  getFollowListData(context: Context): Promise<FollowListItem[]> {
    return new Promise<FollowListItem[]>((success, error) => {
      Logger.info(TAG, `getAppointmentList start`);
      this.fetchFollowListData().then((navResDTO: ResponseDTO<FollowListItem[]>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getFollowListDataLocal(context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data as FollowListItem[]
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        success(this.getFollowListDataLocal(context))
      })
    })
  }

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

  /**
   * 我的关注列表
   * @param params
   * @param context
   * @returns
   */
  getMineFollowListData(params:FollowListDetailRequestItem,context: Context): Promise<MineFollowListItem> {
    return new Promise<MineFollowListItem>((success, error) => {
      Logger.info(TAG, `getAppointmentList start`);
      this.fetchMineDetailFollowListData(params).then((navResDTO: ResponseDTO<MineFollowListItem>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getMineFollowListDataLocal(context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data as MineFollowListItem
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        success(this.getMineFollowListDataLocal(context))
      })
    })
  }

  fetchMineDetailFollowListData(object:FollowListDetailRequestItem) {
    let url = HttpUrlUtils.getMineFollowListDataUrl()+`?pageSize=${object.pageSize}&pageNum=${object.pageNum}`
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.get<ResponseDTO<MineFollowListItem>>(url, headers)
  };

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

  /**
   * 查询是否被关注 列表
   * @param params
   * @param context
   * @returns
   */
  getFollowListStatusData(params:FollowListStatusRequestItem,context: Context): Promise<QueryListIsFollowedItem[]> {
    return new Promise<QueryListIsFollowedItem[]>((success, error) => {
      Logger.info(TAG, `getAppointmentList start`);
      this.fetchFollowListStatusData(params).then((navResDTO: ResponseDTO<QueryListIsFollowedItem[]>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getFollowListStatusDataLocal(context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data as QueryListIsFollowedItem[]
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        success(this.getFollowListStatusDataLocal(context))
      })
    })
  }

  fetchFollowListStatusData(object:FollowListStatusRequestItem) {
    let url = HttpUrlUtils.getFollowListStatusDataUrl()
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.post<ResponseDTO<QueryListIsFollowedItem[]>>(url,object, headers)
  };

  async getFollowListStatusDataLocal(context: Context): Promise<QueryListIsFollowedItem[]> {
    Logger.info(TAG, `getMineFollowListDataLocal start`);
    let compRes: ResponseDTO<QueryListIsFollowedItem[]> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<QueryListIsFollowedItem[]>>('follow_list_id120_isfocus_data.json',context );
    if (!compRes || !compRes.data) {
      Logger.info(TAG, `getMineFollowListDataLocal compRes  is empty`);
      return []
    }
    Logger.info(TAG, `getMineFollowListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
    return compRes.data
  }


  /**
   * 我的评论列表
   * @param params
   * @param context
   * @returns
   */
  getMineCommentListData(params:FollowListDetailRequestItem,context: Context): Promise<MineCommentListDetailItem> {
    return new Promise<MineCommentListDetailItem>((success, error) => {
      Logger.info(TAG, `getAppointmentList start`);
      this.fetchMineCommentListData(params).then((navResDTO: ResponseDTO<MineCommentListDetailItem>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getMineCommentListDataLocal(context))
          return
        }
        Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data as MineCommentListDetailItem
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name},  error.message:${err.message}`);
        success(this.getMineCommentListDataLocal(context))
      })
    })
  }

  fetchMineCommentListData(object:FollowListDetailRequestItem) {
    let url = HttpUrlUtils.getMineCommentListDataUrl()+`?pageSize=${object.pageSize}&pageNum=${object.pageNum}`
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.get<ResponseDTO<MineCommentListDetailItem>>(url, headers)
  };

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

}

const minePageDatasModel = MinePageDatasModel.getInstance()
export default minePageDatasModel as MinePageDatasModel