PeopleShipMainComponent.ets 10.8 KB
import { PeopleShipRecommendComponent } from './PeopleShipRecommendComponent';
import { PeopleShipMainViewModel } from '../../viewmodel/PeopleShipMainViewModel';
import {  HttpUtils } from 'wdNetwork/Index';
import { Logger, DateTimeUtils, EmitterEventId, EmitterUtils } from 'wdKit';
import { RmhRecommendDTO, ContentDTO, AttentionBatchDTO, CreatorDTO } from 'wdBean';
import { ViewType } from 'wdConstant/src/main/ets/enum/ViewType';
import { channelSkeleton } from '../skeleton/channelSkeleton'
import { EmptyComponent } from '../view/EmptyComponent';
import { ErrorComponent } from '../view/ErrorComponent';
import { WDRouterPage, WDRouterRule } from 'wdRouter/Index'
import MinePageDatasModel from '../../model/MinePageDatasModel';
import { FollowListDetailRequestItem } from '../../viewmodel/FollowListDetailRequestItem';
import { FollowListDetailItem } from '../../viewmodel/FollowListDetailItem';
import { CustomPullToRefresh } from '../reusable/CustomPullToRefresh';
import PageModel from '../../viewmodel/PageModel';
import PageAdModel from '../../viewmodel/PageAdvModel';
import PageHelper from '../../viewmodel/PageHelper';
import { PeopleShipAttentionContentListTopComponent } from './PeopleShipAttentionContentListTopComponent'
import { CardParser } from '../CardParser'
import NoMoreLayout from '../page/NoMoreLayout';

@Preview
@Component
export struct PeopleShipMainComponent {
  @State private pageModel: PageModel = new PageModel();
  @State private pageAdvModel: PageAdModel = new PageAdModel();

  navIndex: number = 0;
  pageId: string = "";
  channelId: string = "";
  @Link @Watch('onChange') currentTopNavSelectedIndex: number
  private scroller: Scroller = new Scroller()
  private listScroller: Scroller = new Scroller()

  @State rmhList: RmhRecommendDTO[] = []
  @Provide rmhSelectedList: string[] = []
  @State viewType: ViewType = ViewType.LOADING
  // 一键关注处理
  @State @Watch('onOneFollowChange') oneKeyFollow: boolean = false
  //  已经关注的列表
  @State followList: FollowListDetailItem[] = []
  // 换一换
  @State @Watch('onChangeButtonClick') changeButton: boolean = false
  @State attentionList: ContentDTO[] = []
  @State private hasMore: boolean = true
  @State private currentPage: number = 1
  @State private isLoading: boolean = false
  @State private loadTime: string = ''

  // 页面展示监听
  @Consume @Watch('onPageShowChange') pageShow: number
  @State private pageAttentionChange: boolean = false

  build() {
    if (this.viewType == ViewType.LOADING) {
      this.LoadingLayout()
    } else if (this.viewType == ViewType.ERROR) {
      ErrorComponent()
    } else if (this.viewType == ViewType.EMPTY) {
      EmptyComponent()
    } else {
      if (this.followList.length == 0) {
        CustomPullToRefresh({
          alldata: this.rmhList,
          scroller: this.scroller,
          hasMore: false,
          customList: () => {
            this.ListLayout()
          },
          onRefresh: (resolve) => {
            this.currentPage = 1
            this.getData(resolve)
          },
        })
      }else {
        CustomPullToRefresh({
          alldata: this.attentionList,
          scroller: this.listScroller,
          hasMore: this.hasMore,
          customList: () => {
            this.ListAttentionLayout()
          },
          onRefresh: (resolve) => {
            this.currentPage = 1
            this.getData(resolve)
          },
          onLoadMore:(resolve)=> {
            if (this.hasMore === false) {
              if(resolve) {
                resolve('')
              }
              return
            }
            if(!this.isLoading && this.hasMore){
              //加载分页数据
              this.currentPage++;
              this.getAttentionContentListData(resolve)
            }else  {
              if(resolve)  {
                resolve('')
              }
            }
          },
        })
      }
    }
  }

  @Builder
  LoadingLayout() {
    channelSkeleton()
  }

  @Builder
  ListLayout() {
    Scroll(this.scroller) {
      // 推荐人民号
      PeopleShipRecommendComponent({
        rmhList: this.rmhList,
        oneKeyFollow: this.oneKeyFollow,
        changeButton: this.changeButton
      })
    }
    .edgeEffect(EdgeEffect.None)
    .scrollBar(BarState.Off)
    .width('100%')
    .height('100%')
  }

  @Builder
  ListAttentionLayout() {
    List({scroller: this.listScroller}){
      // 头部关注列表
      ListItem(){
        PeopleShipAttentionContentListTopComponent({
          followList: this.followList
        })
      }
      ForEach(this.attentionList, (item: ContentDTO) => {
        ListItem() {
          CardParser({ contentDTO: item })
        }.width("100%")
        .backgroundColor(Color.Transparent)

      }, (item: ContentDTO, index: number) =>  item.objectId + index.toString())

      // 加载更多
      ListItem() {
        if (!this.hasMore && !this.isLoading) {
          NoMoreLayout()
        }
      }
    }
    .edgeEffect(EdgeEffect.None)
    .scrollBar(BarState.Off)
    .width('100%')
    .height('100%')

    // PeopleShipAttentionContentListComponent({
    //   followList: this.followList,
    //   attentionList: this.attentionList
    // })
  }

  aboutToAppear() {
    if (this.navIndex == this.currentTopNavSelectedIndex && this.viewType == ViewType.LOADING) {
      this.getData()
    }
    // 有登录-退出登录-关注接口等问题
    EmitterUtils.receiveEvent(EmitterEventId.PEOPLE_SHIP_ATTENTION, () => {
      Logger.debug('PeopleShipMainComponent', 'PEOPLE_SHIP_ATTENTION')
      this.pageAttentionChange = true
    })
  }

  onChange() {
    if (this.navIndex === this.currentTopNavSelectedIndex && (this.viewType == ViewType.LOADING || this.pageAttentionChange)) {
      this.getData();
    }
  }

  private async getData(resolve?: (value: string | PromiseLike<string>) => void) {
    if (this.isLoading) {
      if (resolve) {
        resolve('已更新至最新')
      }
      return
    }
    this.pageAttentionChange = false
    this.isLoading = true
    this.getInitData()
    try {
      // 登录后获取
      if (HttpUtils.getUserId()) {
        // 获取用户关注人数
        let object = new FollowListDetailRequestItem(-1, 20, 1)
        let followInfo = await MinePageDatasModel.getMineFollowListData(object, getContext(this))
        Logger.debug('PeopleShipMainComponent', '获取页面信息' + `${JSON.stringify(followInfo)}`)

        if (followInfo.list.length == 0) {
          this.followList = []
          this.getRmhRecommendInfo(resolve)
        } else {
          this.followList = []
          // this.followList = followInfo.list
          this.followList.push(...followInfo.list)
          this.attentionList = []
          this.currentPage = 1
          this.loadTime = DateTimeUtils.getCurDate(DateTimeUtils.PATTERN_DATE_TIME_HYPHEN)
          this.getAttentionContentListData(resolve)
        }
      } else {
        this.followList = []
        this.getRmhRecommendInfo(resolve)
      }

    } catch (exception) {
      this.viewType = ViewType.ERROR
      this.isLoading = false
    }
  }

  // 获取人民号号主推荐
  private async getRmhRecommendInfo(resolve?: (value: string | PromiseLike<string>) => void) {
    try {
      // 获取页面信息
      this.rmhList = await PeopleShipMainViewModel.getPeopleRemRecommendInfo()
      this.rmhSelectedList = []
      this.rmhList.forEach((item: RmhRecommendDTO) => {
        this.rmhSelectedList.push(item.creatorId)
      })
      this.viewType = ViewType.LOADED
      this.changeButton = false
      this.isLoading = false
      Logger.debug('PeopleShipMainComponent', 'getRmhRecommendInfo' + `${JSON.stringify(this.rmhList)}`)
      if (resolve) {
        resolve('已更新至最新')
      }
    } catch (exception) {
      if (resolve) {
        resolve('')
      }
      this.viewType = ViewType.ERROR
      this.changeButton = false
      this.isLoading = false
    }
  }

  // 获取关注
  private async getAttentionContentListData(resolve?: (value: string | PromiseLike<string>) => void) {
    if (this.isLoading && this.currentPage != 1) {
      if (resolve) {
        resolve('')
      }
      return
    }
    this.isLoading = true
    try {
      // 获取列表数据
      let listData = await PeopleShipMainViewModel.getAttentionContentListInfo(this.currentPage, 20, this.loadTime)
      Logger.debug('PeopleShipMainComponent', '获取页面信息' + `${JSON.stringify(listData)}`)

      if (resolve ) {
        if (this.currentPage == 1) {
          resolve('已更新至最新')
        }else  {
          resolve('')
        }
      }
      if (listData && listData.list && listData.list.length > 0) {
        if (listData.list.length === 20) {
          this.hasMore = true;
        } else {
          this.hasMore = false;
        }
        if (this.currentPage == 1) {
          this.attentionList = []
        }
        this.attentionList.push(...listData.list)
      }
      this.viewType = ViewType.LOADED
      this.isLoading = false
    } catch (exception) {
      this.viewType = ViewType.ERROR
      this.isLoading = false
    }
  }

  // 说是首页必须要调用
  async getInitData() {
    Logger.debug('PeopleShipMainComponent', `getData id: ${this.pageId} , ${this.channelId} , navIndex: ${this.currentTopNavSelectedIndex}`);
    this.pageModel.pageId = this.pageId;
    this.pageModel.groupId = this.pageId;
    this.pageModel.channelId = this.channelId;
    this.pageModel.currentPage = 1;
    PageHelper.getInitData(this.pageModel, this.pageAdvModel)
  }

  // 点击一键关注
  onOneFollowChange() {
    if (this.oneKeyFollow) {
      if (this.rmhSelectedList.length > 0) {
        if (!HttpUtils.getUserId()) {
          this.oneKeyFollow = false
          WDRouterRule.jumpWithPage(WDRouterPage.loginPage)
          return
        }
        //调用接口
        this.attentionBatchPort()
      } else {
        this.oneKeyFollow = false
      }
    }
  }

  // 一键关注接口
  private async attentionBatchPort() {
    try {
      // 获取列表数据
      let objects = new AttentionBatchDTO()
      this.rmhList.forEach((element: RmhRecommendDTO) => {
        if ( this.rmhSelectedList.indexOf(element.creatorId) != -1) {
          const creator = new CreatorDTO(element.userType, element.userId, element.creatorId)
          objects.creators.push(creator)
        }
      })
      Logger.debug('PeopleShipMainComponent', `一键关注接口参数: ${JSON.stringify(objects)}`);
      let batchInfo = await PeopleShipMainViewModel.getAttentionBatchInfo(objects)
      if (batchInfo.code === 0 || batchInfo.code.toString() === "0") {
        this.getData()
      }
    } catch (exception) {

    }
  }

  // 点击换一换
  onChangeButtonClick() {
    if (this.changeButton) {
      this.getRmhRecommendInfo()
    }
  }

  // 页面展示监听
  onPageShowChange() {
    if (this.navIndex === this.currentTopNavSelectedIndex && this.pageAttentionChange ) {
      this.getData()
    }
  }

}