SearchResultContentComponent.ets 21.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
import {
  CompDTO,
  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, StringUtils, UserDataLocal } from 'wdKit/Index'
import MinePageDatasModel from '../../model/MinePageDatasModel'
import SearcherAboutDataModel from '../../model/SearcherAboutDataModel'
import { CreatorDetailRequestItem } from '../../viewmodel/CreatorDetailRequestItem'
import { CreatorDetailResponseItem } from '../../viewmodel/CreatorDetailResponseItem';
import { FollowListDetailItem } from '../../viewmodel/FollowListDetailItem'
import { FollowListStatusRequestItem } from '../../viewmodel/FollowListStatusRequestItem'
import { QueryListIsFollowedItem } from '../../viewmodel/QueryListIsFollowedItem'
import { SearchResultContentData } from '../../viewmodel/SearchResultContentData'
import { SearchResultContentItem, SearchRmhDescription } from '../../viewmodel/SearchResultContentItem'
import { CardParser } from '../CardParser'
import { FollowChildComponent } from '../mine/follow/FollowChildComponent'
import { ListHasNoMoreDataUI } from '../reusable/ListHasNoMoreDataUI'
import { ActivityItemComponent } from './ActivityItemComponent'
import { SearchCreatorComponent } from './SearchCreatorComponent'
import { JSON } from '@kit.ArkTS'
import { MoreComponent } from '../cardview/MoreComponent'
import { Card9Component } from '../cardview/Card9Component'
import { WDRouterRule, WDRouterPage } from 'wdRouter/Index'
import { EmptyComponent, WDViewDefaultType } from '../view/EmptyComponent'

const TAG = "SearchResultContentComponent"

@Component
export struct SearchResultContentComponent {
  @State keywords: string = ""
  @State searchType: string = ""
  @State data: LazyDataSource<ContentDTO> = new LazyDataSource();
  tempList: ContentDTO[] = []
  @State data_rmh: SearchRmhDescription[] = []
  @State count: number = -1;
  @State isLoading: boolean = false
  @State hasMore: boolean = true
  curPageNum: number = 1;
  @State bean: FollowListDetailItem = new FollowListDetailItem("", "", "", "", "", "", "", "", "", -1, -1, "")
  scroller: Scroller = new Scroller()
  private scroller2: Scroller = new Scroller()
  @State ellipseW: number = 0
  @State moreWidth: number = 20
  @State listLeft: number = 0
  @State isEnd: boolean = false
  @Watch('updateData') @Prop sameSearch: number = 0
  @Prop isCurrentShow: boolean = false;

  updateData() {
    if (this.isCurrentShow) {
      this.data_rmh = []

      this.data.clearAllData()

      this.curPageNum = 1
      this.hasMore = true
      this.isLoading = false
      this.getNewSearchResultData()
    }

  }

  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) => {
        console.log('SearcherAboutDataModel', JSON.stringify(value))
        if (!this.data || value.list.length == 0) {
          this.hasMore = false
          this.isLoading = false
          this.count = this.count === -1 ? 0 : this.count
        } 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 + ""

                      if (data_temp.length === 1) {
                        this.bean.headPhotoUrl = item.headPhotoUrl.split("?")[0]
                        this.bean.cnUserName = item.userName
                        this.bean.creatorId = item.creatorId
                        this.bean.authIcon = item.authIcon

                        if (value[0].fansNum > 10000) {
                          let temp = (value[0].fansNum / 10000) + ""
                          let index = temp.indexOf('.')
                          if (index != -1) {
                            temp = temp.substring(0, index + 2)
                          } else {
                            temp = temp
                          }
                          this.bean.cnFansNum = temp + "万"
                        } else {
                          this.bean.cnFansNum = item.fansNum + ""
                        }
                        let regex: RegExp = new RegExp('\n', 'g')
                        this.bean.introduction = item.introduction.replace(regex, '')

                        this.bean.mainControl = item.mainControl
                        this.bean.banControl = item.banControl
                        this.bean.cnUserType = item.userType
                        this.bean.cnUserId = item.userId
                      }
                    }
                  })
                })
              }
              data_temp.forEach((data) => {
                this.data_rmh.push(data)
              })

              if (this.data_rmh.length > 0) {
                this.count = 1
              }
              //只有一条创作者,获取 创作者信息
              if (this.data_rmh.length === 1) {
                if (StringUtils.isNotEmpty(UserDataLocal.getUserId())) {
                  //查询是否被关注
                  let status = new FollowListStatusRequestItem()
                  status.creatorIds.push(new QueryListIsFollowedItem(this.data_rmh[0].creatorId))
                  MinePageDatasModel.getFollowListStatusData(status, getContext(this)).then((newValue) => {
                    this.bean.status = newValue[0].status
                  }).catch((err: Error) => {
                    console.log(TAG, "请求失败")
                  })
                } else {
                  this.bean.status = ""
                }
              }
            }).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)
      })
    })

    if (data.contentList.length > 0) {

      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 + ""
            }
          })
        })
        // 批量号主信息
        let creatorIdList: string[] = []
        resultData.list.forEach((value: SearchResultContentItem) => {
          creatorIdList.push(value.data.creatorId)
        })
        SearcherAboutDataModel.getCreatorDetailListData({ creatorIdList: creatorIdList }).then((rem) => {
          resultData.list.forEach((value: SearchResultContentItem) => {
            const landscape = value.data.landscape
            let photos: FullColumnImgUrlDTO[] = []
            // if (value.data.appStyle === 4) {
            value.data.appStyleImages.split("&&").forEach((value) => {
              let resizeParams = MinePageDatasModel.extractResizeParams(value)
              if (resizeParams && resizeParams.length === 2) {
                photos.push(
                  {
                    fullUrl: value,
                    weight: resizeParams[0],
                    height: resizeParams[1],
                    landscape: Number(landscape),
                  } as FullColumnImgUrlDTO
                )
              }
            })
            // }
            let contentDTO = this.dataTransform(rem, value, photos);
            if (value.data.type != "13") {
              this.data.push(contentDTO)
              if (value.data.sameContentList != null && value.data.sameContentList.length > 0) {
                let contentDTO2 = new ContentDTO();
                contentDTO2.sameContentListJson = JSON.stringify(value.data.sameContentList)
                contentDTO2.sameContentListSize = value.data.sameContentList.length
                this.data.push(contentDTO2)
              }
            }
          })

          this.tempList = []
          this.data.notifyDataReload()
          this.count = this.data.totalCount()
          if (this.count < resultData.totalCount) {
            this.curPageNum++
          } else {
            this.hasMore = false
          }
          this.isLoading = false
        }).catch((err: Error) => {
          console.log(TAG, JSON.stringify(err))
        })
      }).catch((err: Error) => {
        console.log(TAG, "请求失败")
        this.isLoading = false
        this.count = this.count === -1 ? 0 : this.count
      })
    } else {
      this.hasMore = false
    }
  }

  build() {
    Column() {
      if (this.count == 0) {

        EmptyComponent({
          emptyType: WDViewDefaultType.WDViewDefaultType_NoSearchResult,
          emptyButton: false,
        }).height('70%')

      } else {
        List({ scroller: this.scroller2 }) {
          if (this.data_rmh != null && this.data_rmh.length > 0) {
            if (this.data_rmh.length === 1) {
              ListItem() {
                FollowChildComponent({ data: this.bean, type: 1 })
              }.padding({ left: "31lpx", right: "31lpx" })
            } else {
              ListItem() {
                Column() {
                  this.SearchListUI()
                }
              }
            }
          }
          LazyForEach(this.data, (item: ContentDTO, index: number) => {
            ListItem() {
              Column() {
                if (this.searchType == "activity") {
                  ActivityItemComponent({ contentDTO: item })
                } else if (item.sameContentListSize > 0) {
                  MoreComponent({ contentDTO: item })
                } else if (item.appStyle == "9") {
                  Column() {
                    Card9Component({
                      compDTO: new CompDTO,
                      contentDTO: item,
                      pageId: "",
                      pageName: ""
                    })
                    Divider().strokeWidth(5).color('#f5f5f5').padding({ left: 0, right: 0 })
                  }
                } else {
                  if (this.data?.get(index + 1)?.sameContentListSize > 0 && index !== 0) {
                    Divider()
                      .width('100%')
                      .color($r('app.color.color_F5F5F5'))
                      .strokeWidth(4)
                  }
                  CardParser({ compDTO: new CompDTO, contentDTO: item })
                }
                if (index != this.data.totalCount() - 1) {
                  Divider().strokeWidth(1).color('#f5f5f5').padding({ left: 16, right: 16 })

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

          //没有更多数据  显示提示
          if (!this.hasMore && (this.data.totalCount() > 0 || (this.data_rmh != null && this.data_rmh.length > 0))) {
            ListItem() {
              ListHasNoMoreDataUI()
            }
          }
        }
        .cachedCount(5)
        .edgeEffect(EdgeEffect.None)
        .scrollBar(BarState.Off)
        .height('100%')
        .onReachEnd(() => {
          console.log(TAG, "触底了");
          if (!this.isLoading) {
            this.getNewSearchResultData()
          }
        })
      }
    }
    .backgroundColor($r('app.color.white'))
    .height('100%')
    .width('100%')
  }

  @Builder
  SearchListUI() {
    List({ initialIndex: 0, space: '8lpx', scroller: this.scroller }) {
      ListItemGroup() {
        ForEach(this.data_rmh, (item: SearchRmhDescription, index: number) => {
          ListItem() {
            SearchCreatorComponent({ item: item })
          }
          .width('150lpx')
          .height('100%')
        })
      }.offset({ left: this.listLeft })

      ListItem() {
        if (this.data_rmh.length === 10) {
          this.itemEnd()
        }
      }.height('100%')
      .margin({ left: '23lpx' })
    }
    .edgeEffect(EdgeEffect.None)
    .scrollBar(BarState.Off)
    .listDirection(Axis.Horizontal)
    .width('100%')
    .height('219lpx')
    .onReachEnd(() => {
      this.isEnd = true
      console.log(TAG, 'is end')
    })
    .onScrollFrameBegin((offset: number, state: ScrollState) => {
      console.log(TAG, 'offset', offset)
      if (!this.scroller.isAtEnd()) {
        this.isEnd = false
      }
      if (this.ellipseW > 0) {
        return { offsetRemain: 0 }
      }
      return { offsetRemain: offset }
    })
    .parallelGesture(
      PanGesture({ direction: PanDirection.Horizontal, distance: 1 })
        .onActionStart((event: GestureEvent) => {
          console.info(TAG, 'Pan start')
        })
        .onActionUpdate((event: GestureEvent) => {
          ///小于10个不展示滑动
          if (this.data_rmh.length < 10) {
            return;
          }
          if (event && this.isEnd) {
            // console.log('event.offsetX',event.offsetX)
            this.listLeft = event.offsetX < -60 ? -60 : event.offsetX > 0 ? 0 : event.offsetX
            this.ellipseW = (-this.listLeft) / 1.5

            console.log(TAG, "this.ellipseW==>" + this.ellipseW)
          }
        })
        .onActionEnd((event: GestureEvent) => {
          if (this.data_rmh.length < 10) {
            return;
          }
          console.info(TAG, 'Pan end')
          this.listLeft = 0
          // this.moreWidth = 20

          if (event.offsetX < 0 && this.ellipseW >= 20) {
            console.log(TAG, '跳转')
            let params: Params = {
              pageID: this.keywords
            }
            WDRouterRule.jumpWithPage(WDRouterPage.searchCreatorPage, params)
          }
          this.ellipseW = 0
        })
    )

    Divider()
      .width('100%')
      .height('12lpx')
      .color($r('app.color.color_F5F5F5'))
      .strokeWidth('12lpx')
  }

  @Builder
  itemEnd() {
    Row() {
      Ellipse()
        .width(2 * this.ellipseW)
        .height('100%')
        .fill('rgb(240,235,238)')
        .position({ left: -this.ellipseW, top: 0 })

      Column() {
        Text(this.ellipseW === 0 ? '' : this.ellipseW < 20 ? '查看更多' : '松手查看')
          .width('19lpx')
          .fontSize('19lpx')
          .fontWeight('400lpx')
          .lineHeight('27lpx')
          .fontColor("#9E9E9E")
      }
      .height('100%')
      .width(this.moreWidth)
      .backgroundColor("#EDEDED")
      .justifyContent(FlexAlign.Center)
    }
    .height('100%')
  }

  private dataTransform(rem: CreatorDetailResponseItem[], value: SearchResultContentItem,
    photos: FullColumnImgUrlDTO[]): ContentDTO {
    let rmhInfo = this.getRmhInfo(rem, value)
    console.log('获取photos', JSON.stringify(photos))
    console.log('获取value2', JSON.stringify(value))
    let liveType = value.data?.liveType;
    let seoTags = value.data?.seoTags
    let cornerMark = value.data?.cornerMark
    let contentDTO = new ContentDTO();
    contentDTO.liveType = liveType ? liveType : ""
    contentDTO.seoTags = seoTags ? seoTags : ""
    contentDTO.cornerMark = cornerMark ? cornerMark : ""
    contentDTO.appStyle = value.data.appStyle + ""
    contentDTO.cityCode = value.data.cityCode
    contentDTO.coverSize = ""
    contentDTO.coverType = value.data.type == "5" ? 1 : -1
    contentDTO.coverUrl =
      this.searchType == "activity" ? value.data.zhChannelPageImg : value.data.appStyleImages.split("&&")[0];
    contentDTO.description = value.data.description
    contentDTO.districtCode = value.data.districtCode
    contentDTO.endTime = value.data.endTime
    contentDTO.hImageUrl = ""
    contentDTO.heatValue = ""
    contentDTO.innerUrl = ""
    contentDTO.landscape = Number.parseInt(value.data.landscape)
    contentDTO.linkUrl = value.data.linkUrl
    contentDTO.openLikes = Number.parseInt(value.data.openLikes)
    contentDTO.openUrl = ""
    contentDTO.pageId = value.data.pageId
    contentDTO.programAuth = ""
    contentDTO.programId = ""
    contentDTO.programName = ""
    contentDTO.programSource = -1
    contentDTO.programType = Number.parseInt(value.data.status)
    contentDTO.provinceCode = value.data.provinceCode
    contentDTO.showTitleEd = value.data.showTitleEd
    contentDTO.showTitleIng = value.data.showTitleIng
    contentDTO.showTitleNo = value.data.showTitleNo
    contentDTO.startTime = value.data.startTime
    contentDTO.subType = ""
    contentDTO.subtitle = ""
    contentDTO.title = value.data.title
    contentDTO.vImageUrl = ""
    contentDTO.screenType = ""
    contentDTO.source = StringUtils.isEmpty(value.data.creatorName) ? value.data.sourceName : value.data.creatorName
    contentDTO.objectId = value.data.id
    contentDTO.objectType = value.data.type
    contentDTO.channelId = value.data.channelId
    contentDTO.relId = value.data.relId
    contentDTO.relType = value.data.relType
    contentDTO.newsTitle = value.data.titleLiteral;
    contentDTO.publishTime =
      StringUtils.isNotEmpty(value.data.firstPublishTime) ? value.data.firstPublishTime : value.data.publishTime
    contentDTO.visitorComment = -1
    contentDTO.fullColumnImgUrls = photos
    contentDTO.newsSummary = ""
    contentDTO.hasMore = -1
    contentDTO.slideShows = []
    contentDTO.voiceInfo = {} as VoiceInfoDTO
    contentDTO.tagWord = -1
    contentDTO.isSelect = true
    contentDTO.rmhInfo = {} as RmhInfoDTO
    contentDTO.photoNum = StringUtils.isEmpty(value.data.picCount) ? 0 : Number(value.data.picCount)
    contentDTO.liveInfo = {} as LiveInfoDTO;
    contentDTO.videoInfo = {
      videoDuration: Number.parseInt(value.data.duration)
    } as VideoInfoDTO;

    let interact = new InteractDataDTO()
    interact.collectNum = value.data.collectNum
    interact.commentNum = value.data.commentNum
    interact.contentId = value.data.id
    interact.contentType = Number.parseInt(value.data.type)
    interact.likeNum = value.data.likeNum
    interact.readNum = Number.parseInt(value.data.readNum)
    interact.shareNum = Number.parseInt(value.data.shareNum)
    contentDTO.interactData = interact
    contentDTO.corner = ''
    contentDTO.rmhPlatform = 0
    contentDTO.newTags = ''
    contentDTO.isSearch = true
    contentDTO.publishTimestamp = ""
    contentDTO.bottomNavId = '';
    contentDTO.openType = '';
    contentDTO.extra = '';
    contentDTO.titleShow = value.data.titleShow == "1" ? 0 : 1
    contentDTO.rmhInfo = rmhInfo
    contentDTO.shareFlag = value.data.shareFlag
    contentDTO.contentText = value.data.contentText
    contentDTO.author = value.data.author
    return contentDTO;
  }

  // 搜索数据转化rmhInfo
  private getRmhInfo(rem: CreatorDetailResponseItem[], value: SearchResultContentItem) {
    let obj = value.data
    let rmhInfo: RmhInfoDTO = {
      rmhHeadUrl: obj.headerPhotoUrl,
      rmhName: obj.creatorName,
      rmhId: obj.creatorId,
      authIcon: obj.authIcon,
      authTitle: obj.authTitle,
      authTitle2: '',
      banControl: 0,
      cnIsAttention: 0,
      cnAttention: 0,
      cnlsComment: 0,
      cnlsLike: 0,
      cnMainControl: 0,
      cnShareControl: 0,
      cnIsComment: 0,
      cnIsLike: 0,
      posterShareControl: 0,
      rmhDesc: obj.introduction,
      userId: obj.userId,
      userType: obj.userType,
      honoraryIcon: '',
      rmhPlatform: 0
    }
    if (rem.length > 0) {
      rem.forEach(item => {
        if (item.creatorId === obj.creatorId) {
          rmhInfo = {
            rmhHeadUrl: item.headPhotoUrl,
            rmhName: item.userName,
            rmhId: item.creatorId,
            authIcon: item.authIcon,
            authTitle: item.authTitle,
            authTitle2: '',
            banControl: 0,
            cnIsAttention: item.isAttention,
            cnAttention: 0,
            cnlsComment: 0,
            cnlsLike: 0,
            cnMainControl: 0,
            cnShareControl: 0,
            cnIsComment: 0,
            cnIsLike: 0,
            posterShareControl: 0,
            rmhDesc: item.introduction,
            userId: item.userId,
            userType: item.userType,
            honoraryIcon: '',
            rmhPlatform: 0
          }
        }
      })
    }
    return rmhInfo
  }
}