|
@@ -0,0 +1,55 @@
|
|
|
+import { ajaxGetSearchHistory, ajaxClearSearchHistory } from '../api/search-history'
|
|
|
+
|
|
|
+class SearchHistoryApi {
|
|
|
+ constructor(config = {}) {
|
|
|
+ this.historyList = []
|
|
|
+ this._getParams = config.getParams || (() => {})
|
|
|
+ // 函数 Hooks
|
|
|
+ this.getHistoryQuery = this.runQuery.bind(this)
|
|
|
+ this.clearHistoryQuery = this.ajaxClearQuery.bind(this)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统一查询 API 入口
|
|
|
+ * @param params - 请求参数
|
|
|
+ * @returns {Promise<{success: boolean, list: []}>}
|
|
|
+ */
|
|
|
+
|
|
|
+ async runQuery(params) {
|
|
|
+ this.historyList = []
|
|
|
+ const query = Object.assign({}, params, this._getParams(params))
|
|
|
+ const result = await this.ajaxQuery(query)
|
|
|
+ if (result.success) {
|
|
|
+ this.historyList = result.list
|
|
|
+ } else {
|
|
|
+ this.historyList = []
|
|
|
+ }
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ // 需要覆写的API处理逻辑
|
|
|
+ async ajaxQuery(params) {
|
|
|
+ return ajaxGetSearchHistory(params).then((res) => {
|
|
|
+ let success = res?.error_code === 0
|
|
|
+
|
|
|
+ return {
|
|
|
+ success: success,
|
|
|
+ list: res.data.reverse() || []
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除搜索历史
|
|
|
+ async ajaxClearQuery(params) {
|
|
|
+ const query = Object.assign({}, params, this._getParams(params))
|
|
|
+ return ajaxClearSearchHistory(query).then((res) => {
|
|
|
+ let success = res?.error_code === 0
|
|
|
+
|
|
|
+ return {
|
|
|
+ success: success
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default SearchHistoryApi
|