wangyong_wd
1 import { CommonConstants, ViewType } from 'wdConstant'; 1 import { CommonConstants, ViewType } from 'wdConstant';
2 -import { CollectionUtils, Logger } from 'wdKit'; 2 +import { CollectionUtils, DateTimeUtils, 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 '../view/EmptyComponent'; 5 import { EmptyComponent } from '../view/EmptyComponent';
@@ -65,7 +65,9 @@ export struct PageComponent { @@ -65,7 +65,9 @@ export struct PageComponent {
65 CompParser({ compDTO: compDTO, compIndex: compIndex }); 65 CompParser({ compDTO: compDTO, compIndex: compIndex });
66 } 66 }
67 } 67 }
68 - }) 68 + },
  69 + (compDTO: CompDTO, compIndex: number) => compDTO.id + compIndex.toString() + this.pageModel.timestamp
  70 + )
69 71
70 // 加载更多 72 // 加载更多
71 ListItem() { 73 ListItem() {
@@ -136,6 +138,11 @@ export struct PageComponent { @@ -136,6 +138,11 @@ export struct PageComponent {
136 } else { 138 } else {
137 this.pageModel.hasMore = false; 139 this.pageModel.hasMore = false;
138 } 140 }
  141 + PageViewModel.getInteractData(pageDto.compList).then((data: CompDTO[]) => {
  142 + // 刷新,替换所有数据
  143 + this.pageModel.compList.replaceAll(...data)
  144 + this.pageModel.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
  145 + })
139 } else { 146 } else {
140 Logger.debug(TAG, 'aboutToAppear, data response page ' + this.pageId + ', comp list is empty.'); 147 Logger.debug(TAG, 'aboutToAppear, data response page ' + this.pageId + ', comp list is empty.');
141 this.pageModel.viewType = ViewType.EMPTY; 148 this.pageModel.viewType = ViewType.EMPTY;
@@ -35,6 +35,8 @@ export struct BigPicCardComponent { @@ -35,6 +35,8 @@ export struct BigPicCardComponent {
35 Column() { 35 Column() {
36 36
37 Column() { 37 Column() {
  38 + // TODO 测试代码
  39 + Text("likeNum " + this.contentDTO?.interactData?.likeNum)
38 //新闻标题 40 //新闻标题
39 Text(this.compDTO.operDataList[0].newsTitle) 41 Text(this.compDTO.operDataList[0].newsTitle)
40 .fontSize(17) 42 .fontSize(17)
1 /** 1 /**
2 * 批查接口查询互动相关数据,返回数据bean 2 * 批查接口查询互动相关数据,返回数据bean
3 */ 3 */
4 -export interface InteractDataDTO { 4 +export class InteractDataDTO {
5 collectNum: number; 5 collectNum: number;
6 commentNum: number; 6 commentNum: number;
7 contentId: string; 7 contentId: string;
@@ -4,6 +4,8 @@ import { RefreshConstants as Const, RefreshState } from './RefreshConstants'; @@ -4,6 +4,8 @@ import { RefreshConstants as Const, RefreshState } from './RefreshConstants';
4 import PageViewModel from '../viewmodel/PageViewModel'; 4 import PageViewModel from '../viewmodel/PageViewModel';
5 import { PageDTO } from '../repository/bean/PageDTO'; 5 import { PageDTO } from '../repository/bean/PageDTO';
6 import { touchMoveLoadMore, touchUpLoadMore } from './PullUpLoadMore'; 6 import { touchMoveLoadMore, touchUpLoadMore } from './PullUpLoadMore';
  7 +import { CompDTO } from '../repository/bean/CompDTO';
  8 +import { DateTimeUtils } from 'wdKit/src/main/ets/utils/DateTimeUtils';
7 9
8 export function listTouchEvent(pageModel: PageModel, event: TouchEvent) { 10 export function listTouchEvent(pageModel: PageModel, event: TouchEvent) {
9 switch (event.type) { 11 switch (event.type) {
@@ -73,6 +75,7 @@ export function touchUpPullRefresh(pageModel: PageModel) { @@ -73,6 +75,7 @@ export function touchUpPullRefresh(pageModel: PageModel) {
73 75
74 PageViewModel.getPageData(self) 76 PageViewModel.getPageData(self)
75 .then((data: PageDTO) => { 77 .then((data: PageDTO) => {
  78 + self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
76 if (data == null || data.compList == null || data.compList.length == 0) { 79 if (data == null || data.compList == null || data.compList.length == 0) {
77 self.hasMore = false; 80 self.hasMore = false;
78 } else { 81 } else {
@@ -84,6 +87,11 @@ export function touchUpPullRefresh(pageModel: PageModel) { @@ -84,6 +87,11 @@ export function touchUpPullRefresh(pageModel: PageModel) {
84 } 87 }
85 // 刷新,替换所有数据 88 // 刷新,替换所有数据
86 self.compList.replaceAll(...data.compList) 89 self.compList.replaceAll(...data.compList)
  90 + PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
  91 + // 刷新,替换所有数据
  92 + self.compList.replaceAll(...data)
  93 + self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
  94 + })
87 } 95 }
88 closeRefresh(self, true); 96 closeRefresh(self, true);
89 }).catch((err: string | Resource) => { 97 }).catch((err: string | Resource) => {
@@ -3,6 +3,8 @@ import PageModel from '../viewmodel/PageModel'; @@ -3,6 +3,8 @@ import PageModel from '../viewmodel/PageModel';
3 import { RefreshConstants as Const } from './RefreshConstants'; 3 import { RefreshConstants as Const } from './RefreshConstants';
4 import PageViewModel from '../viewmodel/PageViewModel'; 4 import PageViewModel from '../viewmodel/PageViewModel';
5 import { PageDTO } from '../repository/bean/PageDTO'; 5 import { PageDTO } from '../repository/bean/PageDTO';
  6 +import { CompDTO } from '../repository/bean/CompDTO';
  7 +import { DateTimeUtils } from 'wdKit';
6 8
7 export function touchMoveLoadMore(model: PageModel, event: TouchEvent) { 9 export function touchMoveLoadMore(model: PageModel, event: TouchEvent) {
8 // list size +1 10 // list size +1
@@ -38,7 +40,13 @@ export function touchUpLoadMore(model: PageModel) { @@ -38,7 +40,13 @@ export function touchUpLoadMore(model: PageModel) {
38 } else { 40 } else {
39 self.hasMore = false; 41 self.hasMore = false;
40 } 42 }
  43 + let sizeBefore = self.compList.size();
41 self.compList.push(...data.compList) 44 self.compList.push(...data.compList)
  45 + PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
  46 + // 刷新,替换所有数据
  47 + self.compList.updateItems(sizeBefore, data)
  48 + self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
  49 + })
42 } 50 }
43 }).catch((err: string | Resource) => { 51 }).catch((err: string | Resource) => {
44 promptAction.showToast({ message: err }); 52 promptAction.showToast({ message: err });
@@ -34,4 +34,6 @@ export default class PageModel { @@ -34,4 +34,6 @@ export default class PageModel {
34 isPullRefreshOperation = false; 34 isPullRefreshOperation = false;
35 isLoading: boolean = false; 35 isLoading: boolean = false;
36 isCanLoadMore: boolean = false; 36 isCanLoadMore: boolean = false;
  37 + // keyGenerator相关字符串,用于刷新list布局
  38 + timestamp: String = '1';
37 } 39 }
@@ -147,14 +147,6 @@ export class PageViewModel extends BaseViewModel { @@ -147,14 +147,6 @@ export class PageViewModel extends BaseViewModel {
147 return; 147 return;
148 } 148 }
149 success(resDTO.data); 149 success(resDTO.data);
150 - // TODO 打开同步请求互动数据,待优化为异步加载  
151 - if (CollectionUtils.isEmpty(resDTO.data.compList)) {  
152 - success(resDTO.data);  
153 - } else {  
154 - this.getInteractData(resDTO.data.compList).then(() => {  
155 - success(resDTO.data);  
156 - })  
157 - }  
158 }) 150 })
159 .catch((err: Error) => { 151 .catch((err: Error) => {
160 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`); 152 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`);
@@ -249,7 +241,8 @@ export class PageViewModel extends BaseViewModel { @@ -249,7 +241,8 @@ export class PageViewModel extends BaseViewModel {
249 } 241 }
250 if (id == content.objectId) { 242 if (id == content.objectId) {
251 content.interactData = interactData; 243 content.interactData = interactData;
252 - content.interactData.likeNum = 109; 244 + // TODO 测试代码,待删除
  245 + // content.interactData.likeNum = Math.floor(Math.random() * Math.floor(999));;
253 break outer; 246 break outer;
254 } 247 }
255 } 248 }
@@ -254,4 +254,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> { @@ -254,4 +254,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> {
254 sort(comparator: (firstValue: T, secondValue: T) => number): void { 254 sort(comparator: (firstValue: T, secondValue: T) => number): void {
255 this.dataArray.sort(comparator) 255 this.dataArray.sort(comparator)
256 } 256 }
  257 +
  258 + public updateItems(start: number, data: T[]): void {
  259 + // 从数组中的start位置开始删除dataArray.length个元素,并在同一位置插入((1个或多个))新元素。
  260 + this.dataArray.splice(start, this.dataArray.length, ...data);
  261 + this.notifyDataReload()
  262 + }
257 } 263 }
@@ -39,6 +39,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> { @@ -39,6 +39,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> {
39 return this.getData(this.totalCount() - 1); 39 return this.getData(this.totalCount() - 1);
40 } 40 }
41 41
  42 + public size(): number {
  43 + return this.dataArray.length
  44 + }
  45 +
42 // 获取所有数据 46 // 获取所有数据
43 public getDataArray(): T[] { 47 public getDataArray(): T[] {
44 return this.dataArray; 48 return this.dataArray;
@@ -276,4 +280,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> { @@ -276,4 +280,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> {
276 public sort(comparator: (firstValue: T, secondValue: T) => number): void { 280 public sort(comparator: (firstValue: T, secondValue: T) => number): void {
277 this.dataArray.sort(comparator) 281 this.dataArray.sort(comparator)
278 } 282 }
  283 +
  284 + public updateItems(start: number, data: T[]): void {
  285 + // 从数组中的start位置开始删除dataArray.length个元素,并在同一位置插入((1个或多个))新元素。
  286 + this.dataArray.splice(start, this.dataArray.length, ...data);
  287 + this.notifyDataReload()
  288 + }
279 } 289 }
@@ -59,7 +59,7 @@ export interface ContentDTO { @@ -59,7 +59,7 @@ export interface ContentDTO {
59 59
60 newsSummary: string; //appstyle:2 ,新闻详情 60 newsSummary: string; //appstyle:2 ,新闻详情
61 61
62 - // 二次请求接口,返回的数据,这里组装到content里;TODO 后续优化 62 + // 二次请求接口,返回的数据,这里组装到content里;
63 interactData:InteractDataDTO; 63 interactData:InteractDataDTO;
64 64
65 hasMore: number, 65 hasMore: number,
1 import { CommonConstants, ViewType } from 'wdConstant'; 1 import { CommonConstants, ViewType } from 'wdConstant';
2 -import { Logger } from 'wdKit'; 2 +import { Logger, DateTimeUtils } from 'wdKit';
3 import PageViewModel from '../../viewmodel/PageViewModel'; 3 import PageViewModel from '../../viewmodel/PageViewModel';
4 import { EmptyComponent } from '../view/EmptyComponent'; 4 import { EmptyComponent } from '../view/EmptyComponent';
5 import { ErrorComponent } from '../view/ErrorComponent'; 5 import { ErrorComponent } from '../view/ErrorComponent';
@@ -48,7 +48,8 @@ export struct PageComponent { @@ -48,7 +48,8 @@ export struct PageComponent {
48 48
49 } 49 }
50 50
51 - @Builder ListLayout() { 51 + @Builder
  52 + ListLayout() {
52 List() { 53 List() {
53 // 下拉刷新 54 // 下拉刷新
54 ListItem() { 55 ListItem() {
@@ -64,7 +65,9 @@ export struct PageComponent { @@ -64,7 +65,9 @@ export struct PageComponent {
64 CompParser({ compDTO: compDTO, compIndex: compIndex }); 65 CompParser({ compDTO: compDTO, compIndex: compIndex });
65 } 66 }
66 } 67 }
67 - }) 68 + },
  69 + (compDTO: CompDTO, compIndex: number) => compDTO.id + compIndex.toString() + this.pageModel.timestamp
  70 + )
68 71
69 // 加载更多 72 // 加载更多
70 ListItem() { 73 ListItem() {
@@ -79,7 +82,7 @@ export struct PageComponent { @@ -79,7 +82,7 @@ export struct PageComponent {
79 } 82 }
80 } 83 }
81 .scrollBar(BarState.Off) 84 .scrollBar(BarState.Off)
82 - .cachedCount(5) 85 + .cachedCount(8)
83 .height(CommonConstants.FULL_PARENT) 86 .height(CommonConstants.FULL_PARENT)
84 .onScrollIndex((start: number, end: number) => { 87 .onScrollIndex((start: number, end: number) => {
85 // Listen to the first index of the current list. 88 // Listen to the first index of the current list.
@@ -89,7 +92,8 @@ export struct PageComponent { @@ -89,7 +92,8 @@ export struct PageComponent {
89 }) 92 })
90 } 93 }
91 94
92 - @Builder LoadingLayout() { 95 + @Builder
  96 + LoadingLayout() {
93 CustomRefreshLoadLayout({ refreshBean: new RefreshLayoutBean(true, 97 CustomRefreshLoadLayout({ refreshBean: new RefreshLayoutBean(true,
94 $r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.pageModel.pullDownRefreshHeight) }) 98 $r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.pageModel.pullDownRefreshHeight) })
95 } 99 }
@@ -116,6 +120,7 @@ export struct PageComponent { @@ -116,6 +120,7 @@ export struct PageComponent {
116 this.pageModel.currentPage = 1; 120 this.pageModel.currentPage = 1;
117 let pageDto = await PageViewModel.getPageData(this.pageModel.pageId, this.pageModel.pageId, this.pageModel.channelId 121 let pageDto = await PageViewModel.getPageData(this.pageModel.pageId, this.pageModel.pageId, this.pageModel.channelId
118 , this.pageModel.currentPage, this.pageModel.pageSize, getContext(this)) 122 , this.pageModel.currentPage, this.pageModel.pageSize, getContext(this))
  123 + this.pageModel.timestamp = DateTimeUtils.getTimeStamp().toString()
119 if (pageDto && pageDto.compList && pageDto.compList.length > 0) { 124 if (pageDto && pageDto.compList && pageDto.compList.length > 0) {
120 this.pageModel.viewType = ViewType.LOADED; 125 this.pageModel.viewType = ViewType.LOADED;
121 this.pageModel.compList.push(...pageDto.compList) 126 this.pageModel.compList.push(...pageDto.compList)
@@ -125,6 +130,12 @@ export struct PageComponent { @@ -125,6 +130,12 @@ export struct PageComponent {
125 } else { 130 } else {
126 this.pageModel.hasMore = false; 131 this.pageModel.hasMore = false;
127 } 132 }
  133 + // 二次请求,批查互动数据
  134 + PageViewModel.getInteractData(pageDto.compList).then((data: CompDTO[]) => {
  135 + // 刷新,替换所有数据
  136 + this.pageModel.compList.replaceAll(...data)
  137 + this.pageModel.timestamp = DateTimeUtils.getTimeStamp().toString()
  138 + })
128 } else { 139 } else {
129 Logger.debug(TAG, 'aboutToAppear, data response page ' + this.pageId + ', comp list is empty.'); 140 Logger.debug(TAG, 'aboutToAppear, data response page ' + this.pageId + ', comp list is empty.');
130 this.pageModel.viewType = ViewType.EMPTY; 141 this.pageModel.viewType = ViewType.EMPTY;
@@ -32,7 +32,8 @@ export struct BigPicCardComponent { @@ -32,7 +32,8 @@ export struct BigPicCardComponent {
32 @Builder 32 @Builder
33 originalBuild() { 33 originalBuild() {
34 Column() { 34 Column() {
35 - 35 + // TODO 测试代码
  36 + // Text("likeNum " + this.contentDTO?.interactData?.likeNum)
36 Column() { 37 Column() {
37 //新闻标题 38 //新闻标题
38 Text(this.compDTO.operDataList[0].newsTitle) 39 Text(this.compDTO.operDataList[0].newsTitle)
1 import promptAction from '@ohos.promptAction'; 1 import promptAction from '@ohos.promptAction';
2 import { RefreshConstants as Const, RefreshState } from './RefreshConstants'; 2 import { RefreshConstants as Const, RefreshState } from './RefreshConstants';
3 import { touchMoveLoadMore, touchUpLoadMore } from './PullUpLoadMore'; 3 import { touchMoveLoadMore, touchUpLoadMore } from './PullUpLoadMore';
4 -import { PageDTO } from 'wdBean'; 4 +import { PageDTO, CompDTO } from 'wdBean';
5 import PageModel from '../viewmodel/PageModel'; 5 import PageModel from '../viewmodel/PageModel';
6 import PageViewModel from '../viewmodel/PageViewModel'; 6 import PageViewModel from '../viewmodel/PageViewModel';
  7 +import { DateTimeUtils } from 'wdKit';
7 8
8 export function listTouchEvent(pageModel: PageModel, event: TouchEvent) { 9 export function listTouchEvent(pageModel: PageModel, event: TouchEvent) {
9 switch (event.type) { 10 switch (event.type) {
@@ -73,6 +74,7 @@ export function touchUpPullRefresh(pageModel: PageModel) { @@ -73,6 +74,7 @@ export function touchUpPullRefresh(pageModel: PageModel) {
73 74
74 PageViewModel.getPageData(self.pageId, self.groupId, self.channelId, self.currentPage, self.pageSize, getContext()) 75 PageViewModel.getPageData(self.pageId, self.groupId, self.channelId, self.currentPage, self.pageSize, getContext())
75 .then((data: PageDTO) => { 76 .then((data: PageDTO) => {
  77 + self.timestamp = DateTimeUtils.getTimeStamp().toString()
76 if (data == null || data.compList == null || data.compList.length == 0) { 78 if (data == null || data.compList == null || data.compList.length == 0) {
77 self.hasMore = false; 79 self.hasMore = false;
78 } else { 80 } else {
@@ -84,6 +86,11 @@ export function touchUpPullRefresh(pageModel: PageModel) { @@ -84,6 +86,11 @@ export function touchUpPullRefresh(pageModel: PageModel) {
84 } 86 }
85 // 刷新,替换所有数据 87 // 刷新,替换所有数据
86 self.compList.replaceAll(...data.compList) 88 self.compList.replaceAll(...data.compList)
  89 + PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
  90 + // 刷新,替换所有数据
  91 + self.compList.replaceAll(...data)
  92 + self.timestamp = DateTimeUtils.getTimeStamp().toString()
  93 + })
87 } 94 }
88 closeRefresh(self, true); 95 closeRefresh(self, true);
89 }).catch((err: string | Resource) => { 96 }).catch((err: string | Resource) => {
@@ -2,7 +2,8 @@ import promptAction from '@ohos.promptAction'; @@ -2,7 +2,8 @@ import promptAction from '@ohos.promptAction';
2 import PageModel from '../viewmodel/PageModel'; 2 import PageModel from '../viewmodel/PageModel';
3 import { RefreshConstants as Const } from './RefreshConstants'; 3 import { RefreshConstants as Const } from './RefreshConstants';
4 import PageViewModel from '../viewmodel/PageViewModel'; 4 import PageViewModel from '../viewmodel/PageViewModel';
5 -import { PageDTO } from 'wdBean'; 5 +import { PageDTO,CompDTO } from 'wdBean';
  6 +import { DateTimeUtils } from 'wdKit';
6 7
7 export function touchMoveLoadMore(model: PageModel, event: TouchEvent) { 8 export function touchMoveLoadMore(model: PageModel, event: TouchEvent) {
8 // list size +1 9 // list size +1
@@ -29,6 +30,7 @@ export function touchUpLoadMore(model: PageModel) { @@ -29,6 +30,7 @@ export function touchUpLoadMore(model: PageModel) {
29 closeLoadMore(model); 30 closeLoadMore(model);
30 PageViewModel.getPageData(self.pageId, self.groupId, self.channelId, self.currentPage, self.pageSize, getContext()) 31 PageViewModel.getPageData(self.pageId, self.groupId, self.channelId, self.currentPage, self.pageSize, getContext())
31 .then((data: PageDTO) => { 32 .then((data: PageDTO) => {
  33 + self.timestamp = DateTimeUtils.getTimeStamp().toString()
32 if (data == null || data.compList == null || data.compList.length == 0) { 34 if (data == null || data.compList == null || data.compList.length == 0) {
33 self.hasMore = false; 35 self.hasMore = false;
34 } else { 36 } else {
@@ -38,7 +40,13 @@ export function touchUpLoadMore(model: PageModel) { @@ -38,7 +40,13 @@ export function touchUpLoadMore(model: PageModel) {
38 } else { 40 } else {
39 self.hasMore = false; 41 self.hasMore = false;
40 } 42 }
  43 + let sizeBefore:number = self.compList.size();
41 self.compList.push(...data.compList) 44 self.compList.push(...data.compList)
  45 + PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
  46 + // 刷新,替换所有数据
  47 + self.compList.updateItems(sizeBefore, data)
  48 + self.timestamp = DateTimeUtils.getTimeStamp().toString()
  49 + })
42 } 50 }
43 }).catch((err: string | Resource) => { 51 }).catch((err: string | Resource) => {
44 promptAction.showToast({ message: err }); 52 promptAction.showToast({ message: err });
@@ -32,4 +32,6 @@ export default class PageModel { @@ -32,4 +32,6 @@ export default class PageModel {
32 isPullRefreshOperation = false; 32 isPullRefreshOperation = false;
33 isLoading: boolean = false; 33 isLoading: boolean = false;
34 isCanLoadMore: boolean = false; 34 isCanLoadMore: boolean = false;
  35 + // keyGenerator相关字符串,用于刷新list布局
  36 + timestamp: String = '1';
35 } 37 }
@@ -11,11 +11,7 @@ const TAG = 'PageViewModel'; @@ -11,11 +11,7 @@ const TAG = 'PageViewModel';
11 * mock数据是本地json数据,可自行修改内容(‘entry\src\main\resources\rawfile\’目录) 11 * mock数据是本地json数据,可自行修改内容(‘entry\src\main\resources\rawfile\’目录)
12 */ 12 */
13 const mock_switch = false; 13 const mock_switch = false;
14 -/**  
15 - * 互动数据获取开关开关,默认开。  
16 - * TODO 后续需要优化掉,变为二次请求异步刷新  
17 - */  
18 -const interact_sync_switch = false; 14 +
19 /** 15 /**
20 * 处理返回后的数据 16 * 处理返回后的数据
21 */ 17 */
@@ -114,18 +110,7 @@ export class PageViewModel extends BaseViewModel { @@ -114,18 +110,7 @@ export class PageViewModel extends BaseViewModel {
114 return 110 return
115 } 111 }
116 Logger.info(TAG, "getNavData then,resDTO.timestamp:" + resDTO.timestamp); 112 Logger.info(TAG, "getNavData then,resDTO.timestamp:" + resDTO.timestamp);
117 - if (!interact_sync_switch) {  
118 - success(resDTO.data);  
119 - return;  
120 - }  
121 - // TODO 打开同步请求互动数据,待优化为异步加载  
122 - // if (CollectionUtils.isEmpty(resDTO.data.compList)) {  
123 - // success(resDTO.data);  
124 - // } else {  
125 - // this.getInteractData(resDTO.data.compList).then(() => {  
126 - // success(resDTO.data);  
127 - // })  
128 - // } 113 + success(resDTO.data);
129 }) 114 })
130 .catch((err: Error) => { 115 .catch((err: Error) => {
131 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`); 116 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`);
@@ -165,18 +150,7 @@ export class PageViewModel extends BaseViewModel { @@ -165,18 +150,7 @@ export class PageViewModel extends BaseViewModel {
165 return 150 return
166 } 151 }
167 Logger.info(TAG, "getNavData then,resDTO.timestamp:" + resDTO.timestamp); 152 Logger.info(TAG, "getNavData then,resDTO.timestamp:" + resDTO.timestamp);
168 - if (!interact_sync_switch) {  
169 - success(resDTO.data);  
170 - return;  
171 - }  
172 - // TODO 打开同步请求互动数据,待优化为异步加载  
173 - // if (CollectionUtils.isEmpty(resDTO.data.compList)) {  
174 - // success(resDTO.data);  
175 - // } else {  
176 - // this.getInteractData(resDTO.data.compList).then(() => {  
177 - // success(resDTO.data);  
178 - // })  
179 - // } 153 + success(resDTO.data);
180 }) 154 })
181 .catch((err: Error) => { 155 .catch((err: Error) => {
182 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`); 156 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`);
@@ -185,120 +159,123 @@ export class PageViewModel extends BaseViewModel { @@ -185,120 +159,123 @@ export class PageViewModel extends BaseViewModel {
185 }) 159 })
186 } 160 }
187 161
188 - // async getInteractData(compList: CompDTO[]) {  
189 - // let param: InteractParam = this.getInteractParams(compList);  
190 - // const SIZE = 20;  
191 - // // 批查接口,参数size限制20,这里截断分批查询,0,20;20,40...  
192 - // let count = Math.ceil(param.contentList.length / SIZE);  
193 - // if (count == 1) {  
194 - // let promise: Promise<InteractDataDTO[]> = this.createInteractDataPromise(param);  
195 - // promises.push(promise);  
196 - // } else {  
197 - // for (let i = 1;i <= count; i++) {  
198 - // // 将查询参数截断(参数限制20个),分批请求接口  
199 - // let subList = new Array<ContentBean>();  
200 - // let start = 0;  
201 - // let end = 0;  
202 - // if (i == count) {  
203 - // start = (i - 1) * SIZE;  
204 - // end = param.contentList.length;  
205 - // subList = CollectionUtils.getSubElements(param.contentList, start, end)  
206 - // } else {  
207 - // start = (i - 1) * SIZE;  
208 - // end = start + SIZE;  
209 - // subList = CollectionUtils.getSubElements(param.contentList, start, end)  
210 - // }  
211 - // let subParam: InteractParam = {} as InteractParam;  
212 - // subParam.contentList = subList;  
213 - // let promise: Promise<InteractDataDTO[]> = this.createInteractDataPromise(subParam);  
214 - // promises.push(promise);  
215 - // }  
216 - // }  
217 - //  
218 - // return new Promise<CompDTO[]>((success, error) => {  
219 - // Promise.all(promises).then((result) => {  
220 - // if (!CollectionUtils.isArray(result)) {  
221 - // success(compList);  
222 - // return;  
223 - // }  
224 - //  
225 - // let allInteractDataList = new Array();  
226 - // result.forEach((value: InteractDataDTO[]) => {  
227 - // if (value != null && value.length > 0) {  
228 - // allInteractDataList.push(...value);  
229 - // }  
230 - // })  
231 - // // 批查全部完成,统一设置到comp里  
232 - // this.resetInteract(allInteractDataList, compList);  
233 - // success(compList);  
234 - // })  
235 - // })  
236 - // }  
237 - //  
238 - // private createInteractDataPromise(param: InteractParam) {  
239 - // return new Promise<InteractDataDTO[]>((success, error) => {  
240 - // PageRepository.fetchInteractData(param).then((resDTO: ResponseDTO<InteractDataDTO[]>) => {  
241 - // if (this.isRespondsInvalid(resDTO, 'getInteractData')) {  
242 - // Logger.info(TAG, "getInteractData then,resDTO.timeStamp:" + resDTO.timestamp);  
243 - // success(null);  
244 - // return;  
245 - // }  
246 - // success(resDTO.data);  
247 - // }).catch((err: Error) => {  
248 - // Logger.error(TAG, `getInteractData catch, error.name : ${err.name}, error.message:${err.message}`);  
249 - // // 无论是否成功(暂不做重试),都回调结果,通知刷新数据  
250 - // success(null);  
251 - // })  
252 - // });  
253 - // }  
254 - //  
255 - // private resetInteract(interact: InteractDataDTO[], compList: CompDTO[]) {  
256 - // if (interact == null || interact.length == 0) {  
257 - // return  
258 - // }  
259 - // interact.forEach((interactData) => {  
260 - // let id = interactData.contentId;  
261 - // outer: for (let i = 0;i < compList.length; i++) {  
262 - // let comp = compList[i];  
263 - // if (comp == null || comp.operDataList == null || comp.operDataList.length == 0) {  
264 - // continue;  
265 - // }  
266 - // for (let j = 0;j < comp.operDataList.length; j++) {  
267 - // let content = comp.operDataList[j];  
268 - // if (content == null) {  
269 - // continue;  
270 - // }  
271 - // if (id == content.objectId) {  
272 - // content.interactData = interactData;  
273 - // break outer;  
274 - // }  
275 - // }  
276 - // }  
277 - // })  
278 - // }  
279 - //  
280 - // private getInteractParams(compList: CompDTO[]): InteractParam {  
281 - // if (compList == null || compList.length == 0) {  
282 - // return null;  
283 - // }  
284 - //  
285 - // let param: InteractParam = {} as InteractParam;  
286 - // param.contentList = new Array<ContentBean>();  
287 - // compList.forEach((value) => {  
288 - // let contentList = value.operDataList;  
289 - // if (contentList != null && contentList.length > 0) {  
290 - // contentList.forEach((v) => {  
291 - // if (StringUtils.isNotEmpty(v.objectId)) {  
292 - // let bean = {} as ContentBean;  
293 - // bean.contentId = v.objectId;  
294 - // bean.contentType = v.objectType;  
295 - // param.contentList.push(bean);  
296 - // }  
297 - // })  
298 - // }  
299 - // })  
300 - // return param;  
301 - // } 162 + async getInteractData(compList: CompDTO[]) {
  163 + let param: InteractParam = this.getInteractParams(compList);
  164 + const SIZE = 20;
  165 + // 批查接口,参数size限制20,这里截断分批查询,0,20;20,40...
  166 + let count = Math.ceil(param.contentList.length / SIZE);
  167 + let promises: Array<Promise<InteractDataDTO[]>> = new Array;
  168 + if (count == 1) {
  169 + let promise: Promise<InteractDataDTO[]> = this.createInteractDataPromise(param);
  170 + promises.push(promise);
  171 + } else {
  172 + for (let i = 1; i <= count; i++) {
  173 + // 将查询参数截断(参数限制20个),分批请求接口
  174 + let subList = new Array<ContentBean>();
  175 + let start = 0;
  176 + let end = 0;
  177 + if (i == count) {
  178 + start = (i - 1) * SIZE;
  179 + end = param.contentList.length;
  180 + subList = CollectionUtils.getSubElements(param.contentList, start, end)
  181 + } else {
  182 + start = (i - 1) * SIZE;
  183 + end = start + SIZE;
  184 + subList = CollectionUtils.getSubElements(param.contentList, start, end)
  185 + }
  186 + let subParam: InteractParam = {} as InteractParam;
  187 + subParam.contentList = subList;
  188 + let promise: Promise<InteractDataDTO[]> = this.createInteractDataPromise(subParam);
  189 + promises.push(promise);
  190 + }
  191 + }
  192 +
  193 + return new Promise<CompDTO[]>((success, error) => {
  194 + Promise.all(promises).then((result) => {
  195 + if (!CollectionUtils.isArray(result)) {
  196 + success(compList);
  197 + return;
  198 + }
  199 +
  200 + let allInteractDataList: Array<InteractDataDTO> = new Array();
  201 + result.forEach((value: InteractDataDTO[]) => {
  202 + if (value != null && value.length > 0) {
  203 + allInteractDataList.push(...value);
  204 + }
  205 + })
  206 + // 批查全部完成,统一设置到comp里
  207 + this.resetInteract(allInteractDataList, compList);
  208 + success(compList);
  209 + })
  210 + })
  211 + }
  212 +
  213 + private createInteractDataPromise(param: InteractParam) {
  214 + return new Promise<InteractDataDTO[]>((success, error) => {
  215 + PageRepository.fetchInteractData(param).then((resDTO: ResponseDTO<InteractDataDTO[]>) => {
  216 + if (!resDTO || !resDTO.data) {
  217 + Logger.info(TAG, "getInteractData then,resDTO.timeStamp:" + resDTO.timestamp);
  218 + success([]);
  219 + return;
  220 + }
  221 + success(resDTO.data);
  222 + }).catch((err: Error) => {
  223 + Logger.error(TAG, `getInteractData catch, error.name : ${err.name}, error.message:${err.message}`);
  224 + // 无论是否成功(暂不做重试),都回调结果,通知刷新数据
  225 + success([]);
  226 + })
  227 + });
  228 + }
  229 +
  230 + private resetInteract(interact: InteractDataDTO[], compList: CompDTO[]) {
  231 + if (interact == null || interact.length == 0) {
  232 + return
  233 + }
  234 + interact.forEach((interactData) => {
  235 + let id = interactData.contentId;
  236 + outer: for (let i = 0; i < compList.length; i++) {
  237 + let comp = compList[i];
  238 + if (comp == null || comp.operDataList == null || comp.operDataList.length == 0) {
  239 + continue;
  240 + }
  241 + for (let j = 0; j < comp.operDataList.length; j++) {
  242 + let content = comp.operDataList[j];
  243 + if (content == null) {
  244 + continue;
  245 + }
  246 + if (id == content.objectId) {
  247 + content.interactData = interactData;
  248 + // TODO 测试代码,待删除
  249 + // content.interactData.likeNum = Math.floor(Math.random() * Math.floor(999));;
  250 + break outer;
  251 + }
  252 + }
  253 + }
  254 + })
  255 + }
  256 +
  257 + private getInteractParams(compList: CompDTO[]): InteractParam {
  258 + if (compList == null || compList.length == 0) {
  259 + return {} as InteractParam;
  260 + }
  261 +
  262 + let param: InteractParam = {} as InteractParam;
  263 + param.contentList = new Array<ContentBean>();
  264 + compList.forEach((value) => {
  265 + let contentList = value.operDataList;
  266 + if (contentList != null && contentList.length > 0) {
  267 + contentList.forEach((v) => {
  268 + if (StringUtils.isNotEmpty(v.objectId)) {
  269 + let bean = {} as ContentBean;
  270 + bean.contentId = v.objectId;
  271 + bean.contentType = v.objectType;
  272 + param.contentList.push(bean);
  273 + }
  274 + })
  275 + }
  276 + })
  277 + return param;
  278 + }
302 } 279 }
303 280
304 281