PageComponent.ets
10.9 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
import { CommonConstants, ViewType } from 'wdConstant';
import { Logger } from 'wdKit';
import { EmptyComponent } from '../view/EmptyComponent';
import PageModel from '../../viewmodel/PageModel';
import { autoRefresh, onActionEnd, onActionStart, onActionUpdate } from '../../utils/PullDownRefresh';
import LoadMoreLayout from './LoadMoreLayout';
import { CompParser } from '../CompParser';
import { CompDTO, PageTrackBean } 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';
import { GrayManageModel } from '../../viewmodel/GrayManageModel';
const TAG = 'PageComponent';
@Component
export struct PageComponent {
@State private pageModel: PageModel = new PageModel();
@State private pageAdvModel: PageAdModel = new PageAdModel();
@State timer: number = -1
navIndex: number = 0;
pageId: string = "";
channelId: string = "";
isMourning: boolean = false // 是否国殇
@Link @Watch('onChange') currentTopNavSelectedIndex: number
// 自动刷新通知
@Prop @Watch('onAutoRefresh') autoRefresh: number = 0
private listScroller: Scroller = new Scroller();
private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Up | PanDirection.Down })
needload: boolean = true;
// page相关埋点数据,各组件需要,自行取用
// @Provide 暂不能用Provide、Consume,(别的用到组件样式的页面,都需要加provide,否则会crash,暂不用这种方案。待优化)
/**
* @deprecated
*/
private pageTrackBean: PageTrackBean = new PageTrackBean()
// 国殇灰度管理
GrayManage: SubscribedAbstractProperty<GrayManageModel> = AppStorage.link<GrayManageModel>('GrayManage')
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()
}
})
.backgroundColor(Color.White)
.margin({
left: 6,
right: 6
})
.borderRadius(4)
.grayscale(this.isMourning ? 1 : 0)
}
}
.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);
// }
// }
// })
// 对接新的下拉刷新手势,替换touch事件
.parallelGesture(
PanGesture(this.panOption)
.onActionStart((event?: GestureEvent) => {
onActionStart(this.pageModel, this.pageAdvModel, event)
})
.onActionUpdate((event?: GestureEvent) => {
onActionUpdate(this.pageModel, this.pageAdvModel, event)
})
.onActionEnd(() => {
onActionEnd(this.pageModel, this.pageAdvModel)
})
)
}
@Builder
ListLayout() {
RelativeContainer() {
List({ scroller: this.listScroller }) {
// 下拉刷新
ListItem() {
RefreshLayout({
refreshBean: new RefreshLayoutBean(this.pageModel.isVisiblePullDown, this.pageModel.load,
this.pageModel.offsetY)
})
}
.grayscale(this.isMourning ? 1 : 0)
LazyForEach(this.pageModel.compList, (compDTO: CompDTO, compIndex: number) => {
ListItem() {
Column() {
CompParser(
{
pageModel: this.pageModel,
nextCompDTO: compIndex === this.pageModel.compList.getDataArray().length - 1 ? new CompDTO() : this.pageModel.compList.get(compIndex + 1) as CompDTO,
compDTO: compDTO,
compIndex: compIndex,
pageId: this.pageId
}
)
}
}
.grayscale(this.isMourning && compIndex < 8 ? 1 : 0)
},
(compDTO: CompDTO, compIndex: number) => JSON.stringify(compDTO))
// 加载更多
ListItem() {
if (this.pageModel.hasMore) {
LoadMoreLayout({ isVisible: this.pageModel.isVisiblePullUpLoad })
} else {
PageNoMoreLayout({ noMoreBean: new NoMoreBean(this.pageModel.pageInfo.baselineCopywriting) })
}
}
.grayscale(this.isMourning ? 1 : 0)
}
.edgeEffect(EdgeEffect.None)
.scrollBar(BarState.Off)
.cachedCount(5)
.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()
.margin({
left: 6,
right: 6
})
.borderRadius(4)
.backgroundColor(Color.White)
.grayscale(this.isMourning ? 1 : 0)
// 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.pageAdvModel.pageCornerAdv.matInfo.linkType != '2') {
// 广告中心的挂角广告
this.drawPageCornerAdvView(1, 1 == this.pageAdvModel.isRightAdv)
} else if (this.pageAdvModel.pageCornerContentInfo.advert != null && this.pageAdvModel.pageCornerContentInfo.advert.objectType != '6') {
// 展现中心的挂角广告业务
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)
}
})
.grayscale(this.GrayManage.get().isMourning() ? 1 : 0)
// 关闭按钮
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;
})
.grayscale(this.GrayManage.get().isMourning() ? 1 : 0)
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')
})
.grayscale(this.GrayManage.get().isMourning() ? 1 : 0)
}
}
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() {
if (this.timer) {
clearTimeout(this.timer)
}
this.timer = setTimeout(() => {
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.getInitCacheData(this.pageModel, this.pageAdvModel).then(() => {
this.pageTrackBean.pageId = this.pageId
this.pageTrackBean.pageName = this.pageModel.pageInfo.name
})
PageHelper.getInitData(this.pageModel, this.pageAdvModel).then(() => {
this.pageTrackBean.pageId = this.pageId
this.pageTrackBean.pageName = this.pageModel.pageInfo.name
})
}, 100)
}
}