SearcherAboutDataModel.ets
13.6 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
320
321
322
323
324
325
326
import { Logger, ResourcesUtils, SPHelper, UserDataLocal } from 'wdKit';
import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork';
import HashMap from '@ohos.util.HashMap';
import { SearchHistoryItem } from '../viewmodel/SearchHistoryItem';
import { SearchHotContentItem } from '../viewmodel/SearchHotContentItem';
import { SearchResultCountItem } from '../viewmodel/SearchResultCountItem';
import { SearchResultContentData } from '../viewmodel/SearchResultContentData';
import { contentListParams, InteractDataDTO } from 'wdBean/Index';
const TAG = "SearcherAboutDataModel"
/**
* 我的页面 所有数据 获取封装类
*/
class SearcherAboutDataModel{
private static instance: SearcherAboutDataModel;
public searchHistoryData:SearchHistoryItem[] = []
public SEARCH_HISTORY_KEY:string = "SEARCH_HISTORY_KEY" + UserDataLocal.userId
private constructor() { }
/**
* 单例模式
* @returns
*/
public static getInstance(): SearcherAboutDataModel {
if (!SearcherAboutDataModel.instance) {
SearcherAboutDataModel.instance = new SearcherAboutDataModel();
}
return SearcherAboutDataModel.instance;
}
/**
* 插入搜索记录(单个)
*/
public async putSearchHistoryData(content:string){
let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
this.searchHistoryData = JSON.parse(history)
this.searchHistoryData.forEach((element,index) => {
if (element.searchContent == content) {
this.searchHistoryData.splice(index,1)
}
});
this.searchHistoryData.splice(0,0,new SearchHistoryItem(content))
await SPHelper.default.saveSync(this.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData));
}
/**
* 删除搜索记录(所有)
*/
public async delSearchHistoryData(){
SPHelper.default.deleteSync(this.SEARCH_HISTORY_KEY)
this.searchHistoryData = []
}
/**
* 删除搜索记录(单个)
*/
public async delSearchSingleHistoryData(index:number){
if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
this.searchHistoryData.splice(index,1)
}else{
let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
this.searchHistoryData = JSON.parse(history)
this.searchHistoryData.splice(index,1)
}
SPHelper.default.saveSync(this.SEARCH_HISTORY_KEY, JSON.stringify(this.searchHistoryData))
}
/**
* 查询搜索记录(所有)
*/
public getSearchHistoryData() : SearchHistoryItem[] {
if(this.searchHistoryData!=null && this.searchHistoryData.length>0){
if(this.searchHistoryData.length>10){
this.searchHistoryData.splice(10,this.searchHistoryData.length - 10)
}
return this.searchHistoryData
}
let history = SPHelper.default.getSync(this.SEARCH_HISTORY_KEY,"[]") as string
this.searchHistoryData = JSON.parse(history)
if(this.searchHistoryData.length>10){
this.searchHistoryData.splice(10,this.searchHistoryData.length - 10)
}
// this.putSearchHistoryData("大家")
// this.putSearchHistoryData("人民")
return this.searchHistoryData
}
/**
* 首页 搜索提示滚动内容
*/
getSearchHintData(context: Context): Promise<string[]> {
return new Promise<string[]>((success, error) => {
Logger.info(TAG, `getSearchHintData start`);
this.fetchSearchHintData().then((navResDTO: ResponseDTO<string[]>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.getSearchHintDataLocal(context))
return
}
Logger.info(TAG, "getSearchHintData then,SearchHintDataResDTO.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data as string[]
success(navigationBean);
}).catch((err: Error) => {
Logger.error(TAG, `fetchSearchHintData catch, error.name : ${err.name}, error.message:${err.message}`);
success(this.getSearchHintDataLocal(context))
})
})
}
fetchSearchHintData() {
let url = HttpUrlUtils.getSearchHintDataUrl()
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<string[]>>(url, headers)
};
async getSearchHintDataLocal(context: Context): Promise<string[]> {
Logger.info(TAG, `getSearchHintDataLocal start`);
let compRes: ResponseDTO<string[]> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<string[]>>(context,'search_hint_data.json' );
if (!compRes || !compRes.data) {
Logger.info(TAG, `getSearchHintDataLocal compRes is empty`);
return []
}
Logger.info(TAG, `getSearchHintDataLocal compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
/**
* 搜索主页 热词
*/
getSearchHotsData(context: Context): Promise<SearchHotContentItem[]> {
return new Promise<SearchHotContentItem[]>((success, error) => {
Logger.info(TAG, `getSearchHintData start`);
this.fetchSearchHotsData().then((navResDTO: ResponseDTO<SearchHotContentItem[]>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.getSearchHotsDataLocal(context))
return
}
Logger.info(TAG, "getSearchHotsData then,getSearchHotsData.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data as SearchHotContentItem[]
success(navigationBean);
}).catch((err: Error) => {
Logger.error(TAG, `getSearchHotsData catch, error.name : ${err.name}, error.message:${err.message}`);
success(this.getSearchHotsDataLocal(context))
})
})
}
fetchSearchHotsData() {
let url = HttpUrlUtils.getSearchHotsDataUrl()
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<SearchHotContentItem[]>>(url, headers)
};
async getSearchHotsDataLocal(context: Context): Promise<SearchHotContentItem[]> {
Logger.info(TAG, `getSearchHotsDataLocal start`);
let compRes: ResponseDTO<SearchHotContentItem[]> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<SearchHotContentItem[]>>(context,'search_hots_data.json' ,);
if (!compRes || !compRes.data) {
Logger.info(TAG, `getSearchHotsDataLocal compRes is empty`);
return []
}
Logger.info(TAG, `getSearchHotsDataLocal compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
/**
* 搜索 联想词
*/
getRelatedSearchContentData(keyword:string,context: Context): Promise<string[]> {
return new Promise<string[]>((success, error) => {
Logger.info(TAG, `getSearchHintData start`);
this.fetchRelatedSearchContentData(keyword).then((navResDTO: ResponseDTO<string[]>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.getRelatedSearchContentDataLocal(context))
return
}
Logger.info(TAG, "getSearchHintData then,SearchHintDataResDTO.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data as string[]
success(navigationBean);
}).catch((err: Error) => {
Logger.error(TAG, `fetchSearchHintData catch, error.name : ${err.name}, error.message:${err.message}`);
success(this.getRelatedSearchContentDataLocal(context))
})
})
}
fetchRelatedSearchContentData(keyword:string) {
let url = HttpUrlUtils.getRelatedSearchContentDataUrl()+ keyword
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<string[]>>(url, headers)
};
async getRelatedSearchContentDataLocal(context: Context): Promise<string[]> {
Logger.info(TAG, `getSearchHintDataLocal start`);
let compRes: ResponseDTO<string[]> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<string[]>>(context,'search_related_data_nimen.json' );
if (!compRes || !compRes.data) {
Logger.info(TAG, `getSearchHintDataLocal compRes is empty`);
return []
}
Logger.info(TAG, `getSearchHintDataLocal compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
/**
* 搜索结果 展示tab数量
*/
getSearchResultCountData(keyword:string,context: Context): Promise<SearchResultCountItem> {
return new Promise<SearchResultCountItem>((success, error) => {
Logger.info(TAG, `getSearchResultCountData start`);
this.fetchSearchResultCountData(keyword).then((navResDTO: ResponseDTO<SearchResultCountItem>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.getSearchResultCountDataLocal(context))
return
}
Logger.info(TAG, "getSearchResultCountData then,SearchHintDataResDTO.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data as SearchResultCountItem
success(navigationBean);
}).catch((err: Error) => {
Logger.error(TAG, `getSearchResultCountData catch, error.name : ${err.name}, error.message:${err.message}`);
success(this.getSearchResultCountDataLocal(context))
})
})
}
fetchSearchResultCountData(keyword:string) {
let url = HttpUrlUtils.getSearchResultCountDataUrl() + keyword
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<SearchResultCountItem>>(url, headers)
};
async getSearchResultCountDataLocal(context: Context): Promise<SearchResultCountItem> {
Logger.info(TAG, `getSearchResultCountDataLocal start`);
let compRes: ResponseDTO<SearchResultCountItem> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<SearchResultCountItem>>(context,'search_result_count_data.json' );
if (!compRes || !compRes.data) {
Logger.info(TAG, `getSearchResultCountDataLocal compRes is empty`);
return new SearchResultCountItem()
}
Logger.info(TAG, `getSearchResultCountDataLocal compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
/**
* 搜索结果 展示列表
*/
getSearchResultListData(pageSize:string,pageNum:string,searchType:string,keyword:string,context: Context): Promise<SearchResultContentData> {
return new Promise<SearchResultContentData>((success, error) => {
Logger.info(TAG, `getSearchResultListData start`);
this.fetchSearchResultListData(pageSize,pageNum,searchType,keyword).then((navResDTO: ResponseDTO<SearchResultContentData>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.getSearchResultListDataLocal(context))
return
}
Logger.info(TAG, "getSearchResultListData then,SearchResultListResDTO.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data as SearchResultContentData
success(navigationBean);
}).catch((err: Error) => {
Logger.error(TAG, `getSearchResultListData catch, error.name : ${err.name}, error.message:${err.message}`);
success(this.getSearchResultListDataLocal(context))
})
})
}
fetchSearchResultListData(pageSize:string,pageNum:string,searchType:string,keyword:string) {
let url = HttpUrlUtils.getSearchResultListDataUrl() + `?pageSize=${pageSize}&pageNum=${pageNum}&searchType=${searchType}&keyword=${keyword}`
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<SearchResultContentData>>(url, headers)
};
async getSearchResultListDataLocal(context: Context): Promise<SearchResultContentData> {
Logger.info(TAG, `getSearchResultListDataLocal start`);
let compRes: ResponseDTO<SearchResultContentData> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<SearchResultContentData>>(context,'search_result_list_data.json' );
if (!compRes || !compRes.data) {
Logger.info(TAG, `getSearchResultListDataLocal compRes is empty`);
return new SearchResultContentData()
}
Logger.info(TAG, `getSearchResultListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
/**
* 搜索结果 展示列表(交互详情 评论收藏点赞分享数量)
*/
getInteractListData(data : contentListParams,context: Context): Promise<InteractDataDTO[]> {
return new Promise<InteractDataDTO[]>((success, error) => {
Logger.info(TAG, `getInteractListData start`);
this.fetchInteractListData(data).then((navResDTO: ResponseDTO<InteractDataDTO[]>) => {
if (!navResDTO || navResDTO.code != 0) {
success(this.getInteractListDataLocal(context))
return
}
Logger.info(TAG, "getInteractListData then,SearchResultListResDTO.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data as InteractDataDTO[]
success(navigationBean);
}).catch((err: Error) => {
Logger.error(TAG, `getInteractListData catch, error.name : ${err.name}, error.message:${err.message}`);
success(this.getInteractListDataLocal(context))
})
})
}
fetchInteractListData(data : contentListParams) {
let url = HttpUrlUtils.getInteractListDataUrl()
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.post<ResponseDTO<InteractDataDTO[]>>(url,data, headers)
};
async getInteractListDataLocal(context: Context): Promise<InteractDataDTO[]> {
Logger.info(TAG, `getInteractListDataLocal start`);
let compRes: ResponseDTO<InteractDataDTO[]> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<InteractDataDTO[]>>(context,'search_result_interact_list_data.json' );
if (!compRes || !compRes.data) {
Logger.info(TAG, `getInteractListDataLocal compRes is empty`);
return []
}
Logger.info(TAG, `getInteractListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
}
const searcherAboutDataModel = SearcherAboutDataModel.getInstance()
export default searcherAboutDataModel as SearcherAboutDataModel