SearcherAboutDataModel.ets
5.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
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';
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.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)
}
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
}
}
const searcherAboutDataModel = SearcherAboutDataModel.getInstance()
export default searcherAboutDataModel as SearcherAboutDataModel