import { ajaxGetSearchHistory, ajaxClearSearchHistory, ajaxSaveViewHistory } from '../api/search-history' class SearchHistoryBaseApi { constructor(config = {}) { // 搜索历史type this.searchType = [1, 2, 4] // 浏览历史type this.browseType = [3, 5] // 历史搜索列表 this.searchHistoryList = [] // 历史浏览记录列表 this.browseHistoryList = [] this._getParams = config.getParams || (() => {}) // 函数 Hooks this.getHistoryQuery = this.runQuery.bind(this) this.clearHistoryQuery = this.ajaxClearQuery.bind(this) this.saveViewHistoryQuery = this.ajaxSaveViewQuery.bind(this) } /** * 统一查询 API 入口 * @param params - 请求参数 * @returns {Promise<{success: boolean, search: [], browse: []}>} */ async runQuery(params) { this.searchHistoryList = [] const query = Object.assign({}, params, this._getParams(params)) const result = await this.ajaxQuery(query) if (result.success) { this.searchHistoryList = result.search this.browseHistoryList = result.browse } else { this.searchHistoryList = [] this.browseHistoryList = [] } return result } // 需要覆写的API处理逻辑 async ajaxQuery(params) { return ajaxGetSearchHistory(params).then((res) => { let success = res?.error_code === 0 return { success: success, search: res?.data?.search ? res.data.search.reverse() : [], browse: res?.data?.browse ? res.data.browse.reverse() : [] } }) } // 清除搜索历史 async ajaxClearQuery(params) { const query = Object.assign({}, params, this._getParams(params)) return ajaxClearSearchHistory(query).then((res) => { let success = res?.error_code === 0 if (success && res.data) { // 清除成功再次获取历史列表 this.getHistoryQuery(params) } return { success: success && res?.data } }) } // 保存企业浏览记录 async ajaxSaveViewQuery(params) { const query = Object.assign({}, params, this._getParams(params)) return ajaxSaveViewHistory(query).then((res) => { let success = res?.error_code === 0 return { success: success && res?.data } }) } } export default SearchHistoryBaseApi