FirstTabTopSearchComponent.ets 2.87 KB
/**
 * 首页顶部搜索导航栏
 * 竖向滚动实现方案
 * 方案一 使用动画 + 定时器
 * 方案二 使用容器组件Swiper(当前)
 */
import { SPHelper } from 'wdKit/Index'
import { WDRouterPage, WDRouterRule } from 'wdRouter'
import { TrackingButton, TrackConstants } from 'wdTracking/Index'
import SearcherAboutDataModel from '../../model/SearcherAboutDataModel'
import { Logger } from 'wdKit';
import { BottomNavDTO } from 'wdBean/Index'
import { ColorUtils } from '../../utils/ColorUtils'

const TAG = "FirstTabTopSearchComponent"

@Component
export struct FirstTabTopSearchComponent {
  @State searchTextData: string[] = []
  private swiperController: SwiperController = new SwiperController()
  navItem: BottomNavDTO = {} as BottomNavDTO

  async aboutToAppear() {
    this.getSearchHint()
  }

  getSearchHint() {
    SearcherAboutDataModel.getSearchHintData(getContext(this)).then((value) => {
      if (value != null) {
        this.searchTextData = value
      }
      this.setDefaultHitData()
    }).catch((err: Error) => {
      console.log(TAG, JSON.stringify(err))
      this.setDefaultHitData()
    })
  }

  setDefaultHitData() {
    if (this.searchTextData.length === 0) {
      this.searchTextData.push("搜索")
    }
  }

  build() {
    Row() {
      Image($r('app.media.icon_search'))
        .width(18)
        .height(18)
        .colorFilter(ColorUtils.getDrawingColorFilter(this.getBothColor("")))
        .margin({ right: '10lpx' })

      if (this.searchTextData != null && this.searchTextData.length > 0) {
        Swiper(this.swiperController) {
          ForEach(this.searchTextData, (item: string, index: number) => {
            Text(item)
              .fontWeight(400)
              .fontColor(this.getBothColor("#666666"))
              .fontSize($r('app.float.font_size_13'))
              .textAlign(TextAlign.Start)
              .maxLines(1)
              .textOverflow({ overflow: TextOverflow.Clip })
          })
        }
        .loop(true)
        .autoPlay(true)
        .interval(3000)
        .indicator(false)
        .vertical(true)
        .height(30)
        .enabled(false)
        .focusable(false)
      }
    }
    .height(30)
    .width(124)
    .padding({ left: 15 })
    .backgroundImage(this.navItem.searchUrl ? this.navItem.searchUrl :
    $r('app.media.background_search'))
    .backgroundImageSize(ImageSize.Cover)
    .onClick(() => {
      Logger.info(TAG, `搜索按钮点击: 新闻`);
      TrackingButton.searchClick(TrackConstants.PageName.Search, "NEWS")
      let params = { 'tabName': "NEWS" } as Record<string, string>
      WDRouterRule.jumpWithPage(WDRouterPage.searchPage, params)
    })
  }
  /**
   * 两侧文字图标颜色,搜索图标颜色
   * @returns
   */
  getBothColor(defaultColor: string): string {
    let bothColor = this.navItem.searchBothColor ? this.navItem.searchBothColor : defaultColor
    return bothColor
  }


}