MyCollectionViewModel.ets
4.45 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
import { MyCollectionListModel } from '../model/MyCollectionModel';
import HashMap from '@ohos.util.HashMap';
import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork';
import { Logger, ResourcesUtils } from 'wdKit';
import { PageDTO } from 'wdBean';
const TAG = "MyCollectionViewModel"
class MyCollectionViewModel {
private static instance:MyCollectionViewModel
/**
* 单例模式
* @returns
*/
public static getInstance(): MyCollectionViewModel {
if (!MyCollectionViewModel.instance) {
MyCollectionViewModel.instance = new MyCollectionViewModel();
}
return MyCollectionViewModel.instance;
}
BaseGetRequest(type:number,tagId:string,pageNum:string){
let url = HttpUrlUtils.getMyCollectionListDataUrl()+ `?type=${type}&operateTag=${1}&pageSize=${20}&pageNum=${pageNum}`
if (tagId.length > 0) {
url = url + `&tagId=${tagId}`
}
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders()
return WDHttp.get<ResponseDTO<MyCollectionListModel>>(url, headers)
}
async getAppointmentListDataLocal(context: Context): Promise<MyCollectionListModel> {
Logger.info(TAG, `getBottomNavDataMock start`);
let compRes: ResponseDTO<MyCollectionListModel> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<MyCollectionListModel>>(context,'browsingHistory_list_data.json' );
if (!compRes || !compRes.data) {
Logger.info(TAG, `getAppointmentListDataLocal compRes is empty`);
return new MyCollectionListModel()
}
Logger.info(TAG, `getAppointmentListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
//Type 1 收藏 2 浏览历史
//tagId 收藏界面 标签筛选
fetchMyCollectList(type:number,tagId:string,pageNum:string,context: Context):Promise<MyCollectionListModel>{
return new Promise<MyCollectionListModel>((success,error) => {
this.BaseGetRequest(type,tagId,pageNum).then((navResDTO: ResponseDTO<MyCollectionListModel>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.getAppointmentListDataLocal(context))
return
}
Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
let listData = navResDTO.data as MyCollectionListModel
success(listData)
}).catch((err: Error) => {
Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name}, error.message:${err.message}`);
error("page data invalid");
})
})
}
newBaseGetRequest(type:number,tagId:string,pageNum:number){
let url = HttpUrlUtils.getMyCollectionListDataUrl()+ `?type=${type}&operateTag=${1}&pageSize=${20}&pageNum=${pageNum.toString()}`
if (tagId.length > 0) {
url = url + `&tagId=${tagId}`
}
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders()
return WDHttp.get<ResponseDTO<PageDTO>>(url, headers)
}
newFetchMyCollectList(type:number,tagId:string,pageNum:number,context: Context):Promise<PageDTO>{
return new Promise<PageDTO>((success,error) => {
success(this.newGetAppointmentListDataLocal(type,context))
return
this.newBaseGetRequest(type,tagId,pageNum).then((navResDTO: ResponseDTO<PageDTO>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.newGetAppointmentListDataLocal(type,context))
return
}
Logger.info(TAG, "getAppointmentList then,AppointmentResDTO.timeStamp:" + navResDTO.timestamp);
let listData = navResDTO.data as PageDTO
success(listData)
}).catch((err: Error) => {
Logger.error(TAG, `fetchAppointmentListDataApi catch, error.name : ${err.name}, error.message:${err.message}`);
error("page data invalid");
})
})
}
async newGetAppointmentListDataLocal(type:number, context: Context): Promise<PageDTO> {
Logger.info(TAG, `getBottomNavDataMock start`);
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>(context,type == 1?'MyCollection_list_data.json':'browsingHistory_list_data.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getAppointmentListDataLocal compRes is empty`);
return {} as PageDTO
}
Logger.info(TAG, `getAppointmentListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
}
const collectionViewModel = MyCollectionViewModel.getInstance();
export default collectionViewModel as MyCollectionViewModel