PageComponent.ets 8.53 KB
import { CommonConstants, ViewType } from 'wdConstant';
import { Logger } from 'wdKit';
import { EmptyComponent } from '../view/EmptyComponent';
import PageModel from '../../viewmodel/PageModel';
import { autoRefresh, listTouchEvent } from '../../utils/PullDownRefresh';
import LoadMoreLayout from './LoadMoreLayout';
import { CompParser } from '../CompParser';
import { CompDTO } from 'wdBean';
import PageHelper from '../../viewmodel/PageHelper';
import { channelSkeleton } from '../skeleton/channelSkeleton'
import { ProcessUtils } from 'wdRouter/Index';
import PageAdModel from '../../viewmodel/PageAdvModel';
import PageNoMoreLayout from './PageNoMoreLayout';
import { NoMoreBean } from './NoMoreBean';
import { RefreshLayoutBean } from '../refresh/RefreshLayoutBean';
import RefreshLayout from '../refresh/RefreshLayout';

const TAG = 'PageComponent';

@Component
export struct PageComponent {
  @State private pageModel: PageModel = new PageModel();
  @State private pageAdvModel: PageAdModel = new PageAdModel();
  navIndex: number = 0;
  pageId: string = "";
  channelId: string = "";
  @Link @Watch('onChange') currentTopNavSelectedIndex: number
  // 自动刷新通知
  @Prop @Watch('onAutoRefresh') autoRefresh: number = 0
  private listScroller: Scroller = new Scroller();
  needload: boolean = true;
  build() {
    Column() {
      if (this.pageModel.viewType == ViewType.LOADING) {
        this.LoadingLayout()
      } else if (this.pageModel.viewType == ViewType.LOADED) {
        this.ListLayout()
      } else if (this.pageModel.viewType == ViewType.EMPTY) {
        //缺省页
        EmptyComponent({
          emptyType: this.pageModel.emptyType,
          emptyButton: true,
          retry: () => {
            this.getData()
          }
        })
      }
    }
    .width(CommonConstants.FULL_PARENT)
    .height(CommonConstants.FULL_PARENT)
    .onTouch((event: TouchEvent | undefined) => {
      if (event) {
        if (this.pageModel.viewType === ViewType.LOADED) {
          listTouchEvent(this.pageModel, this.pageAdvModel, event);
        }
      }
    })

  }

  @Builder
  ListLayout() {

    RelativeContainer() {
      List({ scroller: this.listScroller }) {
        // 下拉刷新
        ListItem() {
          RefreshLayout({
            refreshBean: new RefreshLayoutBean(this.pageModel.isVisiblePullDown, this.pageModel.load,
              this.pageModel.offsetY)
          })
        }

        LazyForEach(this.pageModel.compList, (compDTO: CompDTO, compIndex: number) => {
          ListItem() {
            Column() {
              CompParser({ pageModel: this.pageModel, compDTO: compDTO, compIndex: compIndex });
            }
          }
        },
          (compDTO: CompDTO, compIndex: number) => compDTO.id + compIndex.toString() + this.pageModel.timestamp
        )

        // 加载更多
        ListItem() {
          if (this.pageModel.hasMore) {
            // LoadMoreLayout({
            //   refreshBean: new RefreshLayoutBean(this.pageModel.isVisiblePullUpLoad, this.pageModel.pullUpLoadImage,
            //     this.pageModel.pullUpLoadText, this.pageModel.pullUpLoadHeight)
            // })
          } else if (!this.pageModel.contentNeedScroll) {
            PageNoMoreLayout({ noMoreBean: new NoMoreBean(this.pageModel.pageInfo.baselineCopywriting) })
          }
        }
      }
      // comp自己处理分页,这里设置EdgeEffect.None
      .edgeEffect(this.pageModel.contentNeedScroll ? EdgeEffect.None : EdgeEffect.Spring)
      .scrollBar(BarState.Off)
      .cachedCount(8)
      .height(CommonConstants.FULL_PARENT)
      .onScrollIndex((start: number, end: number) => {
        // Listen to the first index of the current list.
        this.pageModel.startIndex = start;
        // 包含了 头尾item,判断时需要考虑+2
        this.pageModel.endIndex = end;
      })
      .id('page_list')

      // 挂角广告
      this.pageHornAd()

    }
  }

  @Builder
  LoadingLayout() {
    channelSkeleton()
    // CustomRefreshLoadLayout({
    //   refreshBean: new RefreshLayoutBean(true,
    //     $r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.pageModel.pullDownRefreshHeight)
    // })
  }

  /**
   *  页面挂角广告
   */
  @Builder
  pageHornAd() {

    if (this.pageAdvModel.isShowAds) {
      if (this.pageAdvModel.pageCornerAdv.matInfo != null) {
        // 广告中心的挂角广告
        this.drawPageCornerAdvView(1, 1 == this.pageAdvModel.isRightAdv)
      } else if (this.pageAdvModel.pageCornerContentInfo.advert != null) {
        // 展现中心的挂角广告业务
        this.drawPageCornerAdvView(2, 1 == this.pageAdvModel.isRightAdv)
      }
    }

  }

  /**
   * 绘制页面挂角
   *
   * @param type  1:广告中心的挂角广告;2:展现中心的挂角广告
   * @param isRightCorne   true:右挂角;false:左挂角
   */
  @Builder
  drawPageCornerAdvView(type: number, isRightCorne: boolean) {
    // 页面左挂角
    Image(type === 1 ? this.pageAdvModel.pageCornerAdv.matInfo.matImageUrl[0]
      : this.pageAdvModel.pageCornerContentInfo.advert.displayUrl)
      .width($r('app.float.vp_80'))
      .height($r('app.float.vp_80'))
      .id("left_iv")
      .alignRules({
        bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
        left: { anchor: isRightCorne ? "" : '__container__', align: HorizontalAlign.Start },
        right: { anchor: isRightCorne ? '__container__' : "", align: HorizontalAlign.End },
      })
      .margin({
        bottom: "65vp",
        left: isRightCorne ? 0 : $r('app.float.card_comp_pagePadding_lf'),
        right: isRightCorne ? $r('app.float.card_comp_pagePadding_lf') : 0,

      })
      .onClick(() => {
        if (type === 1) {
          // 广告业务跳转
          ProcessUtils.openAdvDetail(this.pageAdvModel.pageCornerAdv.matInfo);
        } else {
          // 展现中心的业务跳转
          ProcessUtils.advJumpMainPage(this.pageAdvModel.pageCornerContentInfo.advert)
        }
      })
    // 关闭按钮
    Image($r('app.media.icon_adv_horn_close'))
      .id('left_close')
      .width($r('app.float.vp_16'))
      .alignRules({
        top: { anchor: 'left_iv', align: VerticalAlign.Top },
        left: { anchor: isRightCorne ? '' : 'left_iv', align: HorizontalAlign.Start },
        right: { anchor: isRightCorne ? 'left_iv' : '', align: HorizontalAlign.End },
      })
      .offset({
        x: isRightCorne ? 10 : -10,
        y: isRightCorne ? -10 : -10
      })
      .onClick(() => {
        // 关闭挂角广告
        this.pageAdvModel.isShowAds = false;

      })

    if (type == 1) {
      Text($r('app.string.comp_advertisement'))
        .width($r('app.float.vp_28'))
        .height($r('app.float.vp_16'))
        .fontSize($r('app.float.font_size_10'))
        .fontColor(Color.White)
        .id('left_tag')
        .alignRules({
          bottom: { anchor: 'left_iv', align: VerticalAlign.Bottom },
          left: { anchor: isRightCorne ? '' : 'left_iv', align: HorizontalAlign.Start },
          right: { anchor: isRightCorne ? 'left_iv' : '', align: HorizontalAlign.End },
        })
        .textAlign(TextAlign.Center)
        .backgroundColor($r('app.color.res_color_general_000000_30'))
        .borderRadius({
          topLeft: $r('app.float.vp_2'),
          topRight: $r('app.float.vp_2'),
          bottomLeft: $r('app.float.vp_2'),
          bottomRight: $r('app.float.vp_2')
        })
    }

  }

  async aboutToAppear() {
    // 选中tab,才请求数据。拦截大量接口请求
    if (this.navIndex === this.currentTopNavSelectedIndex) {
      this.getData();
      this.needload = false;
    }
  }

  onChange() {
    Logger.info(TAG, `onChangezz id: ${this.pageId} , ${this.channelId} , ${this.navIndex} , navIndex: ${this.currentTopNavSelectedIndex}`);
    if (this.navIndex === this.currentTopNavSelectedIndex) {
      if(this.needload){
        this.getData();
      }
      this.needload = false;
    }
  }

  onAutoRefresh() {
    if (this.navIndex != this.currentTopNavSelectedIndex) {
      return
    }
    // 当前页面,自动刷新数据
    Logger.debug(TAG, 'page onAutoRefresh ' + this.autoRefresh)
    this.listScroller.scrollToIndex(0)
    autoRefresh(this.pageModel, this.pageAdvModel)
  }

  async getData() {
    Logger.info(TAG, `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;
    this.pageModel.pageTotalCompSize = 0;
    PageHelper.getInitData(this.pageModel, this.pageAdvModel)
  }
}