SearchCreatorPage.ets 7.36 KB
import { Params } from 'wdBean/Index';
import { CustomTitleUI } from '../components/reusable/CustomTitleUI';
import { router } from '@kit.ArkUI';
import { FollowListDetailItem } from '../viewmodel/FollowListDetailItem';
import { LazyDataSource, Logger, SPHelper, StringUtils } from 'wdKit/Index';
import SearcherAboutDataModel from '../model/SearcherAboutDataModel';
import { CreatorDetailRequestItem } from '../viewmodel/CreatorDetailRequestItem';
import { FollowListStatusRequestItem } from '../viewmodel/FollowListStatusRequestItem';
import { QueryListIsFollowedItem } from '../viewmodel/QueryListIsFollowedItem';
import MinePageDatasModel from '../model/MinePageDatasModel';
import { ListHasNoMoreDataUI } from '../components/reusable/ListHasNoMoreDataUI';
import { FollowChildComponent } from '../components/mine/follow/FollowChildComponent';
import { SpConstants } from 'wdConstant/Index';

const TAG = "SearchCreatorPage"

@Entry
@Component
struct SearchCreatorPage {
  @State params: Params = router.getParams() as Params;
  @State keyword: string = '';
  @State searchType:string = ""
  @State data_temp: FollowListDetailItem[] = []
  @State data: LazyDataSource<FollowListDetailItem> = new LazyDataSource();
  @State count: number = -1;
  @State isLoading: boolean = false
  @State hasMore: boolean = true
  curPageNum: number = 1;
  @State topSafeHeight: number = AppStorage.get<number>('topSafeHeight') || 0
  @State bottomSafeHeight: number = AppStorage.get<number>('bottomSafeHeight') || 0

  onPageShow() {
    this.keyword = this.params?.pageID;
    this.searchType = "onlyRmh"
    this.getNewPageData()
  }

  getNewPageData() {
    this.isLoading = true
    if (this.hasMore) {
      SearcherAboutDataModel.getSearchResultListData("20",`${this.curPageNum}`,this.searchType,this.keyword,getContext(this)).then((result)=>{
        if (!this.data || result.list.length == 0){
          this.hasMore = false
          this.isLoading = false
        }else{
          this.data_temp = []
          result.list.forEach((data)=>{
            this.data_temp.push(new FollowListDetailItem("",data.data.creatorName,"0","",data.data.id,"0",data.data.userId,data.data.userType,data.data.userId,Number.parseInt(data.data.mainControl),-1,data.data.authIcon))
          })

          let request = new CreatorDetailRequestItem()

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

          SearcherAboutDataModel.getCreatorDetailListData(request).then((value)=>{
            if(value!=null && value.length>0){
              this.data_temp.forEach((data)=>{
                value.forEach((item)=>{
                  if(data.creatorId == item.creatorId){
                    data.headPhotoUrl = item.headPhotoUrl
                    if(item.fansNum>10000){
                      let temp = (item.fansNum/10000)+""
                      let index = temp.indexOf('.')
                      if(index != -1){
                        temp = temp.substring(0,index+2)
                      }else{
                        temp = temp
                      }
                      data.cnFansNum = temp + "万"
                    }else{
                      data.cnFansNum = item.fansNum + ""
                    }
                    data.introduction = item.introduction
                    data.mainControl = item.mainControl
                    data.banControl = item.banControl
                  }
                })
              })
            this.getFollowListStatus(result.totalCount)
            }
          }).catch((err:Error)=>{
            console.log(TAG,JSON.stringify(err))
            this.isLoading = false
            this.count = this.count===-1?0:this.count
          })
        }
      }).catch((err:Error)=>{
        console.log(TAG,JSON.stringify(err))
        this.isLoading = false
        this.count = this.count===-1?0:this.count
      })
    }
  }

  getFollowListStatus(totalCount:number){
    let isLogin = false
    let userid = SPHelper.default.getSync(SpConstants.USER_ID,"") as string
    if(StringUtils.isNotEmpty(userid)){
      isLogin = true
    }else{
      isLogin = false
    }

    if(isLogin){
      let status = new FollowListStatusRequestItem()
      this.data_temp.forEach((item)=>{
        status.creatorIds.push(new QueryListIsFollowedItem(item.creatorId))
      })

      MinePageDatasModel.getFollowListStatusData(status,getContext(this)).then((newValue)=>{
        newValue.forEach((item)=>{
          this.data_temp.forEach((list)=>{
            if (item.creatorId == list.creatorId) {
              list.status = item.status
            }
          })
        })

        this.data_temp.forEach((item)=>{
          this.data.push(new FollowListDetailItem(item.headPhotoUrl,item.cnUserName,item.cnFansNum,item.introduction,item.creatorId,item.status,item.attentionUserId,item.cnUserType,item.cnUserId,item.mainControl,item.banControl,item.authIcon))
        })

        this.data.notifyDataReload()

        this.count = this.data.totalCount()
        if (this.data.totalCount() < totalCount) {
          this.curPageNum++
        }else {
          this.hasMore = false
        }

        this.isLoading = false
      }).catch((err:Error)=>{
        Logger.error(TAG,"请求失败" + JSON.stringify(err))
        this.isLoading = false
        this.count = this.count===-1?0:this.count
      })
    }else{
      this.data_temp.forEach((item)=>{
        this.data.push(new FollowListDetailItem(item.headPhotoUrl,item.cnUserName,item.cnFansNum,item.introduction,item.creatorId,"0",item.attentionUserId,item.cnUserType,item.cnUserId,item.mainControl,item.banControl,item.authIcon))
      })

      this.data.notifyDataReload()

      this.count = this.data.totalCount()
      if (this.data.totalCount() < totalCount) {
        this.curPageNum++
      }else {
        this.hasMore = false
      }

      this.isLoading = false
    }
  }

  build() {
    Column(){
      Column() {
        CustomTitleUI({ titleName: "全部结果" })
        Divider()
          .width('100%')
          .height('1lpx')
          .color($r('app.color.color_F5F5F5'))
          .strokeWidth('1lpx')

        Column(){
          if(this.count === 0){
            ListHasNoMoreDataUI({style:2})
              .height('100%')
          }else{
            List({ space: 3 }) {
              LazyForEach(this.data, (item: FollowListDetailItem, index: number = 0) => {
                ListItem() {
                  FollowChildComponent({data: item,type:1})
                }
                .onClick(() => {
                })
              }, (item: FollowListDetailItem, index: number) => index.toString())

              //没有更多数据  显示提示
              if(!this.hasMore){
                ListItem(){
                  ListHasNoMoreDataUI()
                }
              }
            }.cachedCount(5)
            .padding({left:'31lpx',right:'31lpx'})
            .layoutWeight(1)
            .scrollBar(BarState.Off)
            .onReachEnd(()=>{
              Logger.debug(TAG,"触底了");
              if(!this.isLoading){
                this.isLoading = true
                //加载分页数据
                this.getNewPageData()
              }
            })
          }
        }.layoutWeight(1)
        .width('100%')
      }
      .width('100%')
      .height('100%')
    }.width("100%")
    .height("100%")
    .backgroundColor($r('app.color.color_transparent'))
    .padding({top:px2vp(this.topSafeHeight)})

  }
}