PageViewModel.ets
4.24 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
import { AppContext, Logger, ResourcesUtils } from 'wdKit';
import { ResponseDTO, } from 'wdNetwork';
import { PageRepository } from '../repository/PageRepository';
import http from '@ohos.net.http';
import { NavigationBodyDTO } from '../repository/bean/NavigationBodyDTO';
import { BottomNavDTO } from '../repository/bean/BottomNavDTO';
import { PageDTO } from '../repository/bean/PageDTO';
import { BaseViewModel } from './BaseViewModel';
import Router from '@system.router';
import router from '@ohos.router';
const TAG = 'PageViewModel';
/**
* mock数据开关,默认关。
* mock数据是本地json数据,可自行修改内容(‘entry\src\main\resources\rawfile\’目录)
*/
const mock_switch = false;
/**
* 处理返回后的数据
*/
export class PageViewModel extends BaseViewModel {
getLogTag() {
return TAG;
}
/**
* get Nav Data from Resource .
*
* @return BottomNavBean[] Nav Data List
*/
async getBottomNavData(): Promise<NavigationBodyDTO> {
Logger.info(TAG, `getBottomNavData start`);
if (mock_switch) {
return this.getBottomNavDataMock();
}
return this.getNavData();
}
async getBottomNavDataMock(): Promise<NavigationBodyDTO> {
Logger.info(TAG, `getBottomNavDataMock start`);
let compRes: ResponseDTO<NavigationBodyDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<NavigationBodyDTO>>('bottom_nav.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getBottomNavDataMock compRes bottomNavList is empty`);
return null
}
Logger.info(TAG, `getBottomNavDataMock getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
private getNavData(): Promise<NavigationBodyDTO> {
return new Promise<NavigationBodyDTO>((success, error) => {
Logger.info(TAG, `getNavData start`);
PageRepository.fetchNavigationDataApi().then((navResDTO: ResponseDTO<NavigationBodyDTO>) => {
if (this.isRespondsInvalid(navResDTO, 'getNavData')) {
error("page data invalid");
return
}
Logger.info(TAG, "getNavData then,navResDTO.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data
success(navigationBean);
}).catch((err: Error) => {
Logger.error(TAG, `fetchNavigationDataApi catch, error.name : ${err.name}, error.message:${err.message}`);
error(err);
})
})
}
/**
* Get PageDTO data.
*
* @return {GroupDTO} compRes.data
*/
private async getPageData1(): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list0.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getCompList compRes is empty`);
return {} as PageDTO
}
Logger.info(TAG, `getCompList getResourcesJson compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
/**
* Get Group data.
*
* @return {GroupDTO} compRes.data
* @deprecated
*/
private async getPageData2(): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list2.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getCompList compRes is empty`);
return {} as PageDTO
}
// router.push('')
Logger.info(TAG, `getCompList getResourcesJson compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
async getPageData(pageId: string, groupId: string, channelId: string): Promise<PageDTO> {
Logger.error(TAG, 'getPageData pageId: ' + pageId);
if (mock_switch) {
return this.getPageData1();
}
return new Promise<PageDTO>((success, error) => {
PageRepository.fetchPageData(pageId, groupId, channelId).then((resDTO: ResponseDTO<PageDTO>) => {
if (this.isRespondsInvalid(resDTO, 'getPageData')) {
error("page data invalid");
return
}
Logger.info(TAG, "getPageData then,resDTO.timeStamp:" + resDTO.timestamp);
success(resDTO.data);
}).catch((err: Error) => {
Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`);
error(err);
})
})
}
}
let pageViewModel = new PageViewModel();
export default pageViewModel as PageViewModel;