SearchResultContentComponent.ets 12 KB
import { ContentDTO,
  contentListParams,
  FullColumnImgUrlDTO, InteractDataDTO,
  Params,
  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, Logger, StringUtils, ToastUtils } from 'wdKit/Index'
import { WDRouterPage, WDRouterRule } from 'wdRouter/Index'
import SearcherAboutDataModel from '../../model/SearcherAboutDataModel'
import { CreatorDetailRequestItem } from '../../viewmodel/CreatorDetailRequestItem'
import { SearchResultContentData } from '../../viewmodel/SearchResultContentData'
import { SearchResultContentItem, SearchRmhDescription } from '../../viewmodel/SearchResultContentItem'
import { CardParser } from '../CardParser'
import { ListHasNoMoreDataUI } from '../reusable/ListHasNoMoreDataUI'
import { ActivityItemComponent } from './ActivityItemComponent'
import { SearchCreatorComponent } from './SearchCreatorComponent'

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 = -1;
  @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
          this.count = this.count===-1?0:this.count
        }else{
          if(value.list[0].dataList!=null){
            let data_temp: SearchRmhDescription[] = []
            data_temp = value.list[0].dataList

            //TODO 查询创作者详情接口
            let request = new CreatorDetailRequestItem()

            data_temp.forEach((data)=>{
              request.creatorIdList.push(data.creatorId)
            })

            SearcherAboutDataModel.getCreatorDetailListData(request).then((value)=>{
              if(value!=null && value.length>0){
                data_temp.forEach((data)=>{
                  value.forEach((item)=>{
                    if(data.creatorId == item.creatorId){
                      data.headerPhotoUrl = item.headPhotoUrl.split("?")[0]
                      data.mainControl = item.mainControl+""
                    }
                  })
                })
              }
              data_temp.forEach((data)=>{
                this.data_rmh.push(data)
              })

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

  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)
          })
        }

        let contentDTO = this.dataTransform(value,photos);
        if(contentDTO.appStyle != "13"){
          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

      if(this.count === 0 && resultData.list.length > 0){
        this.count = -1
        if(!this.isLoading){
          //加载分页数据
          this.getNewSearchResultData()
        }
      }else if(this.count <= 20 && resultData.list.length > 0){
        if(!this.isLoading){
          //加载分页数据
          this.getNewSearchResultData()
        }
      }
    }).catch((err:Error)=>{
      console.log(TAG,"请求失败")
      this.isLoading = false
      this.count = this.count===-1?0:this.count
    })
  }

  build() {
    Column() {
      if(this.count == 0){
        ListHasNoMoreDataUI({style:2})
      }else{
        Column(){
          if (this.data_rmh!=null && this.data_rmh.length > 0) {
            //List
            List({space:'8lpx'}) {
              ForEach(this.data_rmh, (item: SearchRmhDescription, index: number) => {
                ListItem() {
                  SearchCreatorComponent({item:item})
                }.onClick(()=>{
                  //TODO 跳转
                })
                .width('150lpx')
                .height('100%')
              })

              ListItem(){
                Column(){
                  Text("查看更多")
                    .width('19lpx')
                    .fontSize('19lpx')
                    .fontWeight('400lpx')
                    .lineHeight('27lpx')
                    .fontColor($r('app.color.color_9E9E9E'))
                }.borderRadius({topLeft:'4lpx',bottomLeft:'4lpx'})
                .height('180lpx')
                .width('77lpx')
                .backgroundColor($r('app.color.color_EDEDED'))
                .justifyContent(FlexAlign.Center)

              }.height('100%')
              .margin({left:'23lpx'})
              .onClick(()=>{
                let params: Params = {
                  pageID: this.keywords
                }
                WDRouterRule.jumpWithPage(WDRouterPage.searchCreatorPage,params)
              })
            }
            .cachedCount(6)
            .edgeEffect(EdgeEffect.Spring)
            .scrollBar(BarState.Off)
            .listDirection(Axis.Horizontal)
            .width('100%')
            .height('219lpx')

            Divider()
              .width('100%')
              .height('12lpx')
              .color($r('app.color.color_F5F5F5'))
              .strokeWidth('12lpx')
          }
          //List
          List({ space: '6lpx' }) {
            LazyForEach(this.data, (item: ContentDTO, index: number) => {
              ListItem() {
                Column(){
                  if(this.searchType == "activity"){
                    ActivityItemComponent({contentDTO:item})
                  }else{
                    CardParser({contentDTO:item})
                  }
                  if(index != this.data.totalCount()-1 ){
                    Divider()
                      .width('100%')
                      .height('1lpx')
                      .color($r('app.color.color_F5F5F5'))
                      .strokeWidth('1lpx')
                  }
                }
              }
            }, (item: ContentDTO, index: number) => index.toString())

            //没有更多数据  显示提示
            if(!this.hasMore && this.data.totalCount() > 0){
              ListItem(){
                ListHasNoMoreDataUI()
              }
            }
          }.cachedCount(6)
          .edgeEffect(EdgeEffect.None)
          .scrollBar(BarState.Off)
          .layoutWeight(1)
          .onReachEnd(()=>{
            console.log(TAG,"触底了");
            if(!this.isLoading){
              //加载分页数据
              this.getNewSearchResultData()
            }
          })
        }.layoutWeight(1)
      }
    }
    .backgroundColor($r('app.color.white'))
    .layoutWeight(1)
    .width('100%')
  }

  private dataTransform(value: SearchResultContentItem, photos: FullColumnImgUrlDTO[]): ContentDTO {
    let contentDTO = new ContentDTO();
    contentDTO.appStyle = value.data.appStyle + ""
    contentDTO.cityCode = value.data.cityCode
    contentDTO.coverSize = ""
    contentDTO.coverType = value.data.type == "5" ? 1 : -1
    contentDTO.coverUrl =
      this.searchType == "activity" ? value.data.zhChannelPageImg : value.data.appStyleImages.split("&&")[0];
    contentDTO.description = value.data.description
    contentDTO.districtCode = value.data.districtCode
    contentDTO.endTime = value.data.endTime
    contentDTO.hImageUrl = ""
    contentDTO.heatValue = ""
    contentDTO.innerUrl = ""
    contentDTO.landscape = Number.parseInt(value.data.landscape)
    contentDTO.linkUrl = value.data.linkUrl
    contentDTO.openLikes = Number.parseInt(value.data.openLikes)
    contentDTO.openUrl = ""
    contentDTO.pageId = value.data.pageId
    contentDTO.programAuth = ""
    contentDTO.programId = ""
    contentDTO.programName = ""
    contentDTO.programSource = -1
    contentDTO.programType = Number.parseInt(value.data.status)
    contentDTO.provinceCode = value.data.provinceCode
    contentDTO.showTitleEd = value.data.showTitleEd
    contentDTO.showTitleIng = value.data.showTitleIng
    contentDTO.showTitleNo = value.data.showTitleNo
    contentDTO.startTime = value.data.startTime
    contentDTO.subType = ""
    contentDTO.subtitle = ""
    contentDTO.title = value.data.title
    contentDTO.vImageUrl = ""
    contentDTO.screenType = ""
    contentDTO.source = StringUtils.isEmpty(value.data.creatorName) ? value.data.sourceName : value.data.creatorName
    contentDTO.objectId = value.data.id
    contentDTO.objectType = value.data.type
    contentDTO.channelId = value.data.channelId
    contentDTO.relId = value.data.relId
    contentDTO.relType = value.data.relType
    contentDTO.newsTitle = value.data.titleLiteral;
    contentDTO.publishTime =
      StringUtils.isNotEmpty(value.data.firstPublishTime) ? value.data.firstPublishTime : value.data.publishTime
    contentDTO.visitorComment = -1
    contentDTO.fullColumnImgUrls = photos
    contentDTO.newsSummary = ""
    contentDTO.hasMore = -1
    contentDTO.slideShows = []
    contentDTO.voiceInfo = {} as VoiceInfoDTO
    contentDTO.tagWord = -1
    contentDTO.isSelect = true
    contentDTO.rmhInfo = {} as RmhInfoDTO
    contentDTO.photoNum = -1
    contentDTO.liveInfo = {} as LiveInfoDTO;
    contentDTO.videoInfo = {
      videoDuration: Number.parseInt(value.data.duration)
    } as VideoInfoDTO;

    let interact = new InteractDataDTO()
    interact.collectNum = value.data.collectNum
    interact.commentNum = value.data.commentNum
    interact.contentId = value.data.id
    interact.contentType = Number.parseInt(value.data.type)
    interact.likeNum = value.data.likeNum
    interact.readNum = Number.parseInt(value.data.readNum)
    interact.shareNum = Number.parseInt(value.data.shareNum)
    contentDTO.interactData = interact
    contentDTO.corner = ''
    contentDTO.rmhPlatform = 0
    contentDTO.newTags = ''
    contentDTO.isSearch = true
    contentDTO.publishTimestamp = ""
    contentDTO.bottomNavId = '';
    contentDTO.openType = '';
    contentDTO.extra = '';
    contentDTO.titleShow = value.data.type == "5" ? 1 : 0;

    return contentDTO;
  }
}