SearchResultContentComponent.ets 5.6 KB
import { ContentDTO, FullColumnImgUrlDTO, InteractDataDTO, RmhInfoDTO, VideoInfoDTO } from 'wdBean/Index'
import { LiveInfoDTO } from 'wdBean/src/main/ets/bean/detail/LiveInfoDTO'
import { VoiceInfoDTO } from 'wdBean/src/main/ets/bean/detail/VoiceInfoDTO'
import { LazyDataSource, StringUtils } from 'wdKit/Index'
import SearcherAboutDataModel from '../../model/SearcherAboutDataModel'
import { CardParser } from '../CardParser'
import { ListHasNoMoreDataUI } from '../reusable/ListHasNoMoreDataUI'

const TAG = "SearchResultContentComponent"

@Component
export struct SearchResultContentComponent{
  @State keywords:string = ""
  @State searchType:string = ""
  @State data: LazyDataSource<ContentDTO> = new LazyDataSource();
  @State count:number = 0;
  @State isLoading:boolean = false
  @State hasMore:boolean = true
  curPageNum:number = 1;


  aboutToAppear(): void {
    if(this.searchType == "全部"){
      this.searchType = "all"
    }else if(this.searchType == "精选"){
      this.searchType = "cms"
    }else if(this.searchType == "人民号"){
      this.searchType = "rmh"
    }else if(this.searchType == "视频"){
      this.searchType = "video"
    }else if(this.searchType == "活动"){
      this.searchType = "activity"
    }

    this.keywords = encodeURI(this.keywords)
    this.getNewSearchResultData()
  }

  getNewSearchResultData(){
    this.isLoading = true
    if(this.hasMore){
      SearcherAboutDataModel.getSearchResultListData("20",`${this.curPageNum}`,this.searchType,this.keywords,getContext(this)).then((value)=>{
        if (!this.data || value.list.length == 0){
          this.hasMore = false
        }else{
          value.list.forEach((value)=>{

            let photos:FullColumnImgUrlDTO[] = []
            if(value.data.appStyle === 4){
              value.data.appStyleImages.split("&&").forEach((value)=>{
                photos.push({url:value} as FullColumnImgUrlDTO)
              })
            }

            //TODO 48 个赋值
            let contentDTO:ContentDTO = {
              appStyle:value.data.appStyle+"",
              cityCode:value.data.cityCode,
              coverSize:"",
              coverType:-1,
              coverUrl:value.data.appStyleImages.split("&&")[0],
              description:value.data.description,
              districtCode:value.data.districtCode,
              endTime:value.data.endTime,
              hImageUrl:"",
              heatValue:"",
              innerUrl:"",
              landscape:Number.parseInt(value.data.landscape),
              // lengthTime:null,
              linkUrl:value.data.linkUrl,
              openLikes:Number.parseInt(value.data.openLikes),
              openUrl:"",
              pageId:value.data.pageId,
              programAuth:"",
              programId:"",
              programName:"",
              programSource:-1,
              programType:-1,
              provinceCode:value.data.provinceCode,
              showTitleEd:value.data.showTitleEd,
              showTitleIng:value.data.showTitleIng,
              showTitleNo:value.data.showTitleNo,
              startTime:value.data.startTime,
              subType:"",
              subtitle:"",
              title:value.data.title,
              vImageUrl:"",
              screenType:"",
              source:StringUtils.isEmpty(value.data.creatorName)?value.data.sourceName:value.data.creatorName,
              objectId:"",
              objectType:value.data.type,
              channelId:value.data.channelId,
              relId:value.data.relId,
              relType:value.data.relType,
              newsTitle:value.data.titleLiteral,// 有颜色,目前没做
              publishTime:value.data.publishTime,
              visitorComment:-1,
              fullColumnImgUrls:photos,
              newsSummary:"",
              hasMore:-1,
              slideShows:[],
              voiceInfo:{} as VoiceInfoDTO,
              tagWord:-1,
              isSelect:true,
              rmhInfo:{} as RmhInfoDTO,
              photoNum:-1,
              liveInfo:{} as LiveInfoDTO,
              videoInfo:{
                videoDuration:Number.parseInt(value.data.duration)
              } as VideoInfoDTO,
              interactData:{} as InteractDataDTO
            }

            this.data.push(contentDTO)
          })
          this.data.notifyDataReload()
          this.count = this.data.totalCount()
          if (this.data.totalCount() < value.totalCount) {
            this.curPageNum++
          }else {
            this.hasMore = false
          }
        }
      }).catch((err:Error)=>{
        console.log(TAG,JSON.stringify(err))
      })
    }
    this.isLoading = false
  }


  build() {
    Column() {
      if(this.count == 0){
        ListHasNoMoreDataUI({style:2})
      }else{
        //List
        List({ space: '6lpx' }) {
          LazyForEach(this.data, (item: ContentDTO, index: number) => {
            ListItem() {
              CardParser({contentDTO:item})
            }
            .onClick(()=>{
              //TODO 跳转
            })
          }, (item: ContentDTO, index: number) => index.toString())

          //没有更多数据  显示提示
          if(!this.hasMore){
            ListItem(){
              ListHasNoMoreDataUI()
            }
          }
        }.cachedCount(4)
        .scrollBar(BarState.Off)
        .margin({top:'23lpx',left:'23lpx',right:'23lpx'})
        .layoutWeight(1)
        .onReachEnd(()=>{
          console.log(TAG,"触底了");
          if(!this.isLoading){
            //加载分页数据
            this.getNewSearchResultData()
          }
        })
      }
    }
    .backgroundColor($r('app.color.white'))
    .height('100%')
    .width('100%')
  }
}