SearchResultContentComponent.ets 11.1 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 { 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
        }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:ContentDTO = {
          appStyle: value.data.appStyle + "",
          cityCode: value.data.cityCode,
          coverSize: "",
          coverType: value.data.type == "5"?1:-1,
          coverUrl: this.searchType=="activity"?value.data.zhChannelPageImg: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: Number.parseInt(value.data.status),
          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: value.data.id,
          objectType: value.data.type,
          channelId: value.data.channelId,
          relId: value.data.relId,
          relType: value.data.relType,
          newsTitle: value.data.titleLiteral,
          publishTime: StringUtils.isNotEmpty(value.data.firstPublishTime)?value.data.firstPublishTime: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: {
            collectNum:value.data.collectNum ,
            commentNum:value.data.commentNum,
            contentId:value.data.id,
            contentType :Number.parseInt(value.data.type),
            likeNum: value.data.likeNum,
            readNum:Number.parseInt(value.data.readNum),
            shareNum:Number.parseInt(value.data.shareNum)
          } as InteractDataDTO,
          corner: '',
          rmhPlatform: 0,
          newTags: '',
          isSearch: true,
          publishTimestamp:"",
          bottomNavId:'',
          openType:'',
          extra:'',
          titleShow:value.data.type == "5"?1:0,
        }

        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
      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')
                  }
                }
              }
              .onClick(()=>{
                //TODO 跳转
              })
            }, (item: ContentDTO, index: number) => index.toString())

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