SearchResultContentComponent.ets 8.38 KB
import { ContentDTO,
  contentListParams,
  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 { SearchResultContentData } from '../../viewmodel/SearchResultContentData'
import { SearchRmhDescription } from '../../viewmodel/SearchResultContentItem'
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 data_rmh: SearchRmhDescription[] = []
  @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
          this.isLoading = false
        }else{
          if(value.list[0].dataList!=null){
            this.data_rmh = value.list[0].dataList
            //TODO 查询创作者详情接口

          }
          this.getInteractData(value)
        }
      }).catch((err:Error)=>{
        console.log(TAG,JSON.stringify(err))
        this.isLoading = false
      })
    }
  }

  getInteractData(resultData:SearchResultContentData){
    if(resultData.list[0].dataList!=null){
      resultData.list.splice(0,1)
    }

    let data : contentListParams = {
      contentList: []
    }
    resultData.list.forEach((item)=>{
      data.contentList.push({
        contentId: item.data.id + '',
        contentType: Number.parseInt(item.data.type)
      })
    })

    SearcherAboutDataModel.getInteractListData(data,getContext(this)).then((newValue)=>{
      newValue.forEach((item)=>{
        resultData.list.forEach((data)=>{
          if (item.contentId == data.data.id) {
            data.data.collectNum = item.collectNum+""
            data.data.commentNum = item.commentNum+""
            data.data.likeNum = item.likeNum+""
            data.data.readNum = item.readNum+""
            data.data.shareNum = item.shareNum+""
          }
        })
      })

      resultData.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,
          corner: '',
          rmhPlatform: 0,
          newTags: '',
          isSearch: true
        }

        this.data.push(contentDTO)
      })
      this.data.notifyDataReload()
      this.count = this.data.totalCount()
      if (this.data.totalCount() < resultData.totalCount) {
        this.curPageNum++
      }else {
        this.hasMore = false
      }
      this.isLoading = false
    }).catch((err:Error)=>{
      console.log(TAG,"请求失败")
      this.isLoading = false
    })
  }

  build() {
    Column() {
      if(this.count == 0){
        ListHasNoMoreDataUI({style:2})
      }else{
        Column(){
          if (this.data_rmh!=null && this.data_rmh.length > 0) {
            //List
            List() {
              ForEach(this.data_rmh, (item: SearchRmhDescription, index: number) => {
                ListItem() {
                  Column(){
                    Image($r('app.media.default_head'))
                      .width('84lpx')
                      .height('84lpx')
                      .margin({bottom:'15lpx'})
                    Text(item.creatorName)
                      .fontSize('20lpx')
                  }.alignItems(HorizontalAlign.Center)

                }.onClick(()=>{
                  //TODO 跳转
                })
                .width('150lpx')
              })
            }
            .cachedCount(6)
            .edgeEffect(EdgeEffect.None)
            .scrollBar(BarState.Off)
            .listDirection(Axis.Horizontal)
            .width('100%')
            .height('150lpx')
            .onReachEnd(()=>{
              if(!this.isLoading){
                //进入更多关注页
              }
            })
          }
          //List
          List({ space: '6lpx' }) {
            LazyForEach(this.data, (item: ContentDTO, index: number) => {
              ListItem() {
                Column(){
                  CardParser({contentDTO:item})
                  if(index != this.data.totalCount()-1 ){
                    Divider()
                      .width('100%')
                      .height('1lpx')
                      .color($r('app.color.color_F5F5F5'))
                      .strokeWidth('1lpx')
                  }
                }
              }
              .onClick(()=>{
                //TODO 跳转
              })
            }, (item: ContentDTO, index: number) => index.toString())

            //没有更多数据  显示提示
            if(!this.hasMore){
              ListItem(){
                ListHasNoMoreDataUI()
              }
            }
          }.cachedCount(6)
          .edgeEffect(EdgeEffect.None)
          .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%')
  }
}