SearcherAboutDataModel.ets 6.1 KB

import { Logger, ResourcesUtils } from 'wdKit';
import { ResponseDTO, WDHttp } from 'wdNetwork';
import HashMap from '@ohos.util.HashMap';
import { HttpUrlUtils } from '../network/HttpUrlUtils';
import { SearchHistoryItem } from '../viewmodel/SearchHistoryItem';
import { SearchHotContentItem } from '../viewmodel/SearchHotContentItem';
const TAG = "SearcherAboutDataModel"

/**
 * 我的页面 所有数据 获取封装类
 */
class SearcherAboutDataModel{
  private static instance: SearcherAboutDataModel;
  searchHistoryData:SearchHistoryItem[] = []

  private constructor() { }

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

  /**
   * 静态搜索历史记录
   */
  getSearchHistoryData():SearchHistoryItem[]{
    if(this.searchHistoryData.length > 0){
      return this.searchHistoryData
    }
    this.searchHistoryData.push(new SearchHistoryItem("评论"))
    this.searchHistoryData.push(new SearchHistoryItem("关注关注"))
    this.searchHistoryData.push(new SearchHistoryItem("收藏收藏收藏"))
    this.searchHistoryData.push(new SearchHistoryItem("历史"))
    this.searchHistoryData.push(new SearchHistoryItem("消息消息消息消息消息"))
    this.searchHistoryData.push(new SearchHistoryItem("留言板留言板留言板留言板留言板"))
    this.searchHistoryData.push(new SearchHistoryItem("预约"))
    return this.searchHistoryData
  }

  // async putSearchHistoryData(content:string){
  //   let history = await SPHelper.default.get(SearcherAboutDataModel.SEARCH_HISTORY_KEY, '[]') as string;
  //   this.searchHistoryData = JSON.parse(history)
  //   this.searchHistoryData.push(new SearchHistoryItem(content))
  //   await SPHelper.default.save(SearcherAboutDataModel.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData));
  // }

  //  getSearchHistoryData():Promise<SearchHistoryItem[]>{
  //   return new Promise<SearchHistoryItem[]>(async (success, error) => {
  //     if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
  //       success(this.searchHistoryData)
  //       return
  //     }
  //     try {
  //       let history = await SPHelper.default.get(SearcherAboutDataModel.SEARCH_HISTORY_KEY, '') as string;
  //       console.log('ycg',history);
  //     }catch (error){
  //       console.log('ycg',"1111");
  //     }
  //
  //     this.searchHistoryData = JSON.parse(history)
  //     this.searchHistoryData.push(new SearchHistoryItem("评论"))
  //     this.searchHistoryData.push(new SearchHistoryItem("关注关注"))
  //     this.searchHistoryData.push(new SearchHistoryItem("收藏收藏收藏"))
  //
  //     success(this.searchHistoryData)
  //   })
  // }


  /**
   * 首页 搜索提示滚动内容
   */
  getSearchHintData(context: Context): Promise<string[]> {
    return new Promise<string[]>((success, error) => {
      Logger.info(TAG, `getSearchHintData start`);
      this.fetchSearchHintData().then((navResDTO: ResponseDTO<string[]>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getSearchHintDataLocal(context))
          return
        }
        Logger.info(TAG, "getSearchHintData then,SearchHintDataResDTO.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data as string[]
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `fetchSearchHintData catch, error.name : ${err.name},  error.message:${err.message}`);
        success(this.getSearchHintDataLocal(context))
      })
    })
  }

  fetchSearchHintData() {
    let url = HttpUrlUtils.getSearchHintDataUrl()
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.get<ResponseDTO<string[]>>(url, headers)
  };

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

  /**
   * 搜索主页 热词
   */
  getSearchHotsData(context: Context): Promise<SearchHotContentItem[]> {
    return new Promise<SearchHotContentItem[]>((success, error) => {
      Logger.info(TAG, `getSearchHintData start`);
      this.fetchSearchHotsData().then((navResDTO: ResponseDTO<SearchHotContentItem[]>) => {
        if (!navResDTO || navResDTO.code != 0) {
          success(this.getSearchHotsDataLocal(context))
          return
        }
        Logger.info(TAG, "getSearchHotsData then,getSearchHotsData.timeStamp:" + navResDTO.timestamp);
        let navigationBean = navResDTO.data as SearchHotContentItem[]
        success(navigationBean);
      }).catch((err: Error) => {
        Logger.error(TAG, `getSearchHotsData catch, error.name : ${err.name},  error.message:${err.message}`);
        success(this.getSearchHotsDataLocal(context))
      })
    })
  }

  fetchSearchHotsData() {
    let url = HttpUrlUtils.getSearchHotsDataUrl()
    let headers: HashMap<string, string> = HttpUrlUtils.getYcgCommonHeaders();
    return WDHttp.get<ResponseDTO<SearchHotContentItem[]>>(url, headers)
  };

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




}

const searcherAboutDataModel = SearcherAboutDataModel.getInstance()
export default searcherAboutDataModel as SearcherAboutDataModel