VisitorCommentPage.ets 7.03 KB
import { TAG } from '@ohos/hypium/src/main/Constant';
import { ContentDTO } from 'wdBean/Index';
import { SpConstants } from 'wdConstant/Index'
import { DateTimeUtils, LazyDataSource, Logger, NetworkUtil, SPHelper, StringUtils} from 'wdKit/Index'
import { ProcessUtils } from 'wdRouter/Index';
import { VisitorCommentComponent } from '../components/mine/home/VisitorCommentComponent';
import { CustomPullToRefresh } from '../components/reusable/CustomPullToRefresh';
import { CustomTitleUI } from '../components/reusable/CustomTitleUI';
import { ListHasNoMoreDataUI } from '../components/reusable/ListHasNoMoreDataUI';
import { EmptyComponent } from '../components/view/EmptyComponent';
import MinePageDatasModel from '../model/MinePageDatasModel';
import { CommentListItem } from '../viewmodel/CommentListItem';

@Entry
@Component
struct VisitorCommentPage {
  @State data: LazyDataSource<CommentListItem> = new LazyDataSource();
  @State bottomSafeHeight: number = AppStorage.get<number>('bottomSafeHeight') || 0
  @State topSafeHeight: number = AppStorage.get<number>('topSafeHeight') || 0
  @State count: number = 0;
  @State isLoading: boolean = false
  @State hasMore: boolean = true
  curPageNum: number = 1;
  @State isGetRequest: boolean = false
  private scroller: Scroller = new Scroller();
  @State isConnectNetwork : boolean = NetworkUtil.isNetConnected()
  @State commentTime:string = ""

  aboutToAppear(): void {
    this.commentTime = SPHelper.default.getSync(SpConstants.FIRSTCOMMENTTIME, "") as string
    if (StringUtils.isNotEmpty(this.commentTime)) {
      this.getNewPageData(this.commentTime)
    }else{
      this.isGetRequest = true
    }
  }

  build() {
    Column(){
      Column() {
        //标题栏目
        CustomTitleUI({ titleName: "评论列表" })
        if (this.count == 0) {
          if (this.isGetRequest == true) {
            if(this.isConnectNetwork){
              EmptyComponent({ emptyType: 11 })
                .height('100%')
                .width('100%')
            }else{
              EmptyComponent({ emptyType: 1,emptyHeight:"100%" ,retry: () => {
                this.isConnectNetwork = NetworkUtil.isNetConnected()
                if(this.isConnectNetwork){
                  this.curPageNum = 1;
                  this.hasMore = true
                  this.isGetRequest = false
                  this.data.clear()

                  if (!this.isLoading) {
                    this.getNewPageData(this.commentTime)
                  }
                }
              }})
                .layoutWeight(1)
                .width('100%')
            }
          }
        } else {
          Stack(){
            Row()
              .width("100%")
              .height("100%")
              .backgroundColor($r('app.color.white'))
            CustomPullToRefresh({
              alldata:this.data,
              scroller:this.scroller,
              customList:()=>{
                this.ListLayout()
              },
              onRefresh:(resolve)=>{
                this.curPageNum = 1;
                this.hasMore = true
                this.isGetRequest = false
                this.data.clear()

                if (!this.isLoading){
                  this.getNewPageData(this.commentTime)
                  if(resolve) resolve('刷新成功')
                }
              },
              onLoadMore:(resolve)=> {
                if (!this.isLoading) {
                  this.isLoading = true
                  this.getNewPageData(this.commentTime)
                }
              }
            })
          }
        }
      }
      .backgroundColor($r('app.color.color_transparent'))
      .height('100%')
      .width('100%')
    }.width("100%")
    .height("100%")
    .backgroundColor(Color.White)
    .padding({top:px2vp(this.topSafeHeight),bottom:px2vp(this.bottomSafeHeight)})

  }

  @Builder ListLayout(){
    List({ space:6,scroller: this.scroller }) {
      LazyForEach(this.data, (item: CommentListItem, index: number) => {
        ListItem() {
          VisitorCommentComponent({
            data: item,
            isLastItem: index === this.data.totalCount() - 1
          })
        }
        .onClick(() => {
          let content = getParams(item)
          ProcessUtils.processPage(content)
        })
      }, (item: CommentListItem, index: number) => index.toString())

      //没有更多数据  显示提示
      if (!this.hasMore) {
        ListItem() {
          ListHasNoMoreDataUI()
        }.padding({bottom:px2vp(this.bottomSafeHeight) + 20})
      }
    }
    .cachedCount(4)
    .scrollBar(BarState.Off)
    .edgeEffect(EdgeEffect.None)
    .margin({ top: '23lpx'})
    .layoutWeight(1)
  }

  getNewPageData(commentTime:string) {
    this.isLoading = true
    if (this.hasMore) {
      MinePageDatasModel.visitorCommentListData( `${this.curPageNum}`, encodeURI(commentTime)).then((value) => {
        if (!this.data || value.list.length == 0) {
          this.hasMore = false
        } else {
          value.list.forEach((item) => {
            let commentContent = item.commentContent
            if (item.sensitiveShow === 0 && item.sensitiveExist === 1) {
              commentContent = item.commentContentSensitive
            }
            let parentCommentContent = ""
            if (item.parentCommentVo != null) {
              parentCommentContent = item.parentCommentVo.commentContent
            }
            let parentCommentUserName = ""
            if (item.parentCommentVo != null) {
              parentCommentUserName = item.parentCommentVo.fromUserName
            }
            let publishTime =
              DateTimeUtils.getCommentTime(DateTimeUtils.parseDate(item.createTime, DateTimeUtils.PATTERN_DATE_TIME_HYPHEN))

            this.data.push(new CommentListItem(item.fromUserHeader, item.fromUserName,item.fromUserType, item.targetTitle, publishTime,
              commentContent, item.likeNum, 0, item.id, item.targetId, item.targetType, item.targetRelId,
              item.targetRelObjectId, item.targetRelType, item.targetStatus, item.checkStatus, parentCommentContent,
              parentCommentUserName,item.h5Url))

          })
          this.data.notifyDataReload()
          this.count = this.data.totalCount()
          if (this.data.totalCount() < value.totalCount) {
            this.curPageNum++
          } else {
            this.hasMore = false
          }
        }
        this.isGetRequest = true
        this.isLoading = false
      }).catch((err: Error) => {
        Logger.error(TAG, "get " + JSON.stringify(err))
        this.isGetRequest = true
        this.isLoading = false
      })
    }
  }
}

function getParams(item: CommentListItem) : ContentDTO{
  let contentDTO = new ContentDTO();

  // contentDTO.objectType = item.targetType + ""
  // contentDTO.objectId = item.targetId + ""
  // contentDTO.newsTitle = item.targetTitle
  // contentDTO.channelId =
  // contentDTO.rmhPlatform =

  contentDTO.objectId = item.targetId;
  contentDTO.relType = item.targetRelType + "";
  contentDTO.relId = item.targetRelId;
  contentDTO.objectType = item.targetType + "";
  contentDTO.newsTitle = item.targetTitle

  return contentDTO
}