SearcherAboutDataModel.ets 5.9 KB

import { Logger, ResourcesUtils, SPHelper, UserDataLocal } from 'wdKit';
import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork';
import HashMap from '@ohos.util.HashMap';
import { SearchHistoryItem } from '../viewmodel/SearchHistoryItem';
import { SearchHotContentItem } from '../viewmodel/SearchHotContentItem';

const TAG = "SearcherAboutDataModel"

/**
 * 我的页面 所有数据 获取封装类
 */
class SearcherAboutDataModel{
  private static instance: SearcherAboutDataModel;
  public searchHistoryData:SearchHistoryItem[] = []
  public SEARCH_HISTORY_KEY:string = "SEARCH_HISTORY_KEY" + UserDataLocal.userId

  private constructor() { }

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

  /**
   * 插入搜索记录(单个)
   */
  public async putSearchHistoryData(content:string){
    let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
    this.searchHistoryData = JSON.parse(history)
    this.searchHistoryData.splice(0,0,new SearchHistoryItem(content))
    await SPHelper.default.saveSync(this.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData));
  }

  /**
   * 删除搜索记录(所有)
   */
  public async delSearchHistoryData(){
    SPHelper.default.deleteSync(this.SEARCH_HISTORY_KEY)
    this.searchHistoryData = []
  }
  /**
   * 删除搜索记录(单个)
   */
  public async delSearchSingleHistoryData(index:number){
    if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
      this.searchHistoryData.splice(index,1)
    }else{
      let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
      this.searchHistoryData = JSON.parse(history)
      this.searchHistoryData.splice(index,1)
    }
    SPHelper.default.saveSync(this.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData))
  }

  /**
   * 查询搜索记录(所有)
   */
  public getSearchHistoryData() : SearchHistoryItem[] {
    if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
      if(this.searchHistoryData.length>10){
        this.searchHistoryData.splice(10,this.searchHistoryData.length - 10)
      }
      return this.searchHistoryData
    }
    let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string

    this.searchHistoryData = JSON.parse(history)
    if(this.searchHistoryData.length>10){
      this.searchHistoryData.splice(10,this.searchHistoryData.length - 10)
    }

    return  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.getCommonHeaders();
    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[]>>(context,'search_hint_data.json' );
    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.getCommonHeaders();
    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[]>>(context,'search_hots_data.json' ,);
    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