zhangbo1_wd

页面布局里拆除comp解析逻辑;新增tab页面数据请求逻辑,避免初始化而大量接口请求

  1 +import { CommonConstants, CompStyle } from 'wdConstant';
  2 +import { CompDTO } from '../repository/bean/CompDTO';
  3 +import { BannerComponent } from './BannerComponent';
  4 +import { LabelComponent } from './LabelComponent';
  5 +import { TitleAbbrComponent } from './TitleAbbrComponent';
  6 +import { TitleAllComponent } from './TitleAllComponent';
  7 +
  8 +/**
  9 + * comp适配器.
  10 + */
  11 +@Component
  12 +export struct CompParser {
  13 + compDTO: CompDTO = {} as CompDTO;
  14 + compIndex: number = 0;
  15 +
  16 + build() {
  17 + this.componentBuilder(this.compDTO, this.compIndex);
  18 + }
  19 +
  20 + @Builder
  21 + componentBuilder(compDTO: CompDTO, compIndex: number) {
  22 + if (compDTO.compStyle === CompStyle.Label_03) {
  23 + LabelComponent({ compDTO: compDTO })
  24 + } else if (compDTO.compStyle === CompStyle.Title_Abbr_01) {
  25 + TitleAbbrComponent({ compDTO: compDTO })
  26 + } else if (compDTO.compStyle === CompStyle.Title_All_01) {
  27 + TitleAllComponent({ compDTO: compDTO })
  28 + } else if (compDTO.compStyle === CompStyle.Carousel_Layout_01) {
  29 + BannerComponent({ compDTO: compDTO })
  30 + } else {
  31 + // todo:组件未实现 / Component Not Implemented
  32 + Text(compDTO.compStyle)
  33 + .width(CommonConstants.FULL_PARENT)
  34 + .padding(10)
  35 + // .backgroundColor(Color.Brown) // 展示本页未实现的compStyle
  36 + }
  37 + }
  38 +}
1 -import { CommonConstants, CompStyle, ViewType } from 'wdConstant';  
2 -import { LazyDataSource, Logger } from 'wdKit'; 1 +import { CommonConstants, ViewType } from 'wdConstant';
  2 +import { Logger } from 'wdKit';
3 import { CompDTO } from '../repository/bean/CompDTO'; 3 import { CompDTO } from '../repository/bean/CompDTO';
4 import PageViewModel from '../viewmodel/PageViewModel'; 4 import PageViewModel from '../viewmodel/PageViewModel';
5 import { EmptyComponent } from './EmptyComponent'; 5 import { EmptyComponent } from './EmptyComponent';
6 import { ErrorComponent } from './ErrorComponent'; 6 import { ErrorComponent } from './ErrorComponent';
7 -import { LabelComponent } from './LabelComponent';  
8 -import { TitleAbbrComponent } from './TitleAbbrComponent';  
9 -import { TitleAllComponent } from './TitleAllComponent';  
10 -import { BannerComponent } from './BannerComponent'  
11 import PageModel from '../viewmodel/PageModel'; 7 import PageModel from '../viewmodel/PageModel';
12 import { listTouchEvent } from '../utils/PullDownRefresh'; 8 import { listTouchEvent } from '../utils/PullDownRefresh';
13 import RefreshLayout from './page/RefreshLayout'; 9 import RefreshLayout from './page/RefreshLayout';
@@ -15,6 +11,7 @@ import { RefreshLayoutBean } from './page/RefreshLayoutBean'; @@ -15,6 +11,7 @@ import { RefreshLayoutBean } from './page/RefreshLayoutBean';
15 import NoMoreLayout from './page/NoMoreLayout'; 11 import NoMoreLayout from './page/NoMoreLayout';
16 import LoadMoreLayout from './page/LoadMoreLayout'; 12 import LoadMoreLayout from './page/LoadMoreLayout';
17 import CustomRefreshLoadLayout from './page/CustomRefreshLoadLayout'; 13 import CustomRefreshLoadLayout from './page/CustomRefreshLoadLayout';
  14 +import { CompParser } from './CompParser';
18 15
19 const TAG = 'PageComponent'; 16 const TAG = 'PageComponent';
20 17
@@ -24,11 +21,10 @@ export struct PageComponent { @@ -24,11 +21,10 @@ export struct PageComponent {
24 // Group数据及子组件数据 21 // Group数据及子组件数据
25 // @State compList: LazyDataSource<CompDTO> = new LazyDataSource(); 22 // @State compList: LazyDataSource<CompDTO> = new LazyDataSource();
26 @State private pageModel: PageModel = new PageModel(); 23 @State private pageModel: PageModel = new PageModel();
27 - currentTopNavSelectedIndex: number = 0; 24 + navIndex: number = 0;
28 pageId: string = ""; 25 pageId: string = "";
29 channelId: string = ""; 26 channelId: string = "";
30 -  
31 - // @Link @Watch('onChange') paIndex:number 27 + @Link @Watch('onChange') currentTopNavSelectedIndex: number
32 28
33 build() { 29 build() {
34 Column() { 30 Column() {
@@ -68,7 +64,7 @@ export struct PageComponent { @@ -68,7 +64,7 @@ export struct PageComponent {
68 LazyForEach(this.pageModel.compList, (compDTO: CompDTO, compIndex: number) => { 64 LazyForEach(this.pageModel.compList, (compDTO: CompDTO, compIndex: number) => {
69 ListItem() { 65 ListItem() {
70 Column() { 66 Column() {
71 - this.componentBuilder(compDTO, compIndex) 67 + CompParser({ compDTO: compDTO, compIndex: compIndex });
72 } 68 }
73 } 69 }
74 }) 70 })
@@ -100,27 +96,21 @@ export struct PageComponent { @@ -100,27 +96,21 @@ export struct PageComponent {
100 $r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.pageModel.pullDownRefreshHeight) }) 96 $r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.pageModel.pullDownRefreshHeight) })
101 } 97 }
102 98
103 - @Builder  
104 - componentBuilder(compDTO: CompDTO, compIndex: number) {  
105 - if (compDTO.compStyle === CompStyle.Label_03) {  
106 - LabelComponent({ compDTO: compDTO })  
107 - } else if (compDTO.compStyle === CompStyle.Title_Abbr_01) {  
108 - TitleAbbrComponent({ compDTO: compDTO })  
109 - } else if (compDTO.compStyle === CompStyle.Title_All_01) {  
110 - TitleAllComponent({ compDTO: compDTO })  
111 - } else if (compDTO.compStyle === CompStyle.Carousel_Layout_01) {  
112 - BannerComponent({ compDTO: compDTO })  
113 - } else {  
114 - // todo:组件未实现 / Component Not Implemented  
115 - Text(compDTO.compStyle)  
116 - .width(CommonConstants.FULL_PARENT)  
117 - .padding(10)  
118 - // .backgroundColor(Color.Brown) // 展示本页未实现的compStyle 99 + async aboutToAppear() {
  100 + // 选中tab,才请求数据。拦截大量接口请求
  101 + if (this.navIndex === this.currentTopNavSelectedIndex) {
  102 + this.getData();
119 } 103 }
120 } 104 }
121 105
122 - async aboutToAppear() {  
123 - Logger.info(TAG, `aboutToAppear id: ${this.pageId} , ${this.channelId} , navIndex: ${this.currentTopNavSelectedIndex}`); 106 + onChange() {
  107 + if (this.navIndex === this.currentTopNavSelectedIndex) {
  108 + this.getData();
  109 + }
  110 + }
  111 +
  112 + async getData() {
  113 + Logger.info(TAG, `getData id: ${this.pageId} , ${this.channelId} , navIndex: ${this.currentTopNavSelectedIndex}`);
124 this.pageModel.pageId = this.pageId; 114 this.pageModel.pageId = this.pageId;
125 this.pageModel.groupId = this.pageId; 115 this.pageModel.groupId = this.pageId;
126 this.pageModel.channelId = this.channelId; 116 this.pageModel.channelId = this.channelId;
@@ -21,7 +21,12 @@ export struct TopNavigationComponent { @@ -21,7 +21,12 @@ export struct TopNavigationComponent {
21 Tabs() { 21 Tabs() {
22 ForEach(this.topNavList, (navItem: TopNavDTO, index: number) => { 22 ForEach(this.topNavList, (navItem: TopNavDTO, index: number) => {
23 TabContent() { 23 TabContent() {
24 - PageComponent({ currentTopNavSelectedIndex: index, pageId: navItem.pageId, channelId: navItem.channelId }) 24 + PageComponent({
  25 + currentTopNavSelectedIndex: $currentTopNavSelectedIndex,
  26 + navIndex: index,
  27 + pageId: navItem.pageId,
  28 + channelId: navItem.channelId
  29 + })
25 } 30 }
26 .tabBar(this.tabBarBuilder(navItem, index)) 31 .tabBar(this.tabBarBuilder(navItem, index))
27 }, (navItem: TopNavDTO) => JSON.stringify(navItem)); 32 }, (navItem: TopNavDTO) => JSON.stringify(navItem));