import { ajaxGetSearchHistory, ajaxClearSearchHistory, ajaxSaveViewHistory } from '../api/search-history' class SearchHistoryBaseApi { constructor(config = {}) { // 初始化需要将type值传进来(标讯搜索:1 企业搜索:2,3 采购单位搜索: 4,5)必传 this._getParams = config || (() => {}) // 历史搜索列表 this.searchHistoryList = [] // 历史浏览记录列表 this.browseHistoryList = [] // 函数 Hooks this.getHistoryQuery = this.runQuery.bind(this) this.clearHistoryQuery = this.ajaxClearQuery.bind(this) this.saveViewHistoryQuery = this.ajaxSaveViewQuery.bind(this) } /** * 历史搜索、历史浏览查询 API 入口 * @param params - 请求参数(可传可不传,不传会从this._getParams中取 传了会合并) * @returns {Promise<{success: boolean, search: [], browse: []}>} */ async runQuery(params) { const query = Object.assign({}, params, this._getParams) 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 if (res?.data?.browse) { // 格式化浏览历史数据(企业画像浏览记录跳转需要企业id, 存历史记录的时候保存格式为“企业名称_企业id”, 取的时候要分割开) res.data.browse = res.data.browse.map((item) => { if (item.indexOf('_') > -1) { const [name, id] = item.split('_') return { name, id } } else { return { name: item } } }) } return { success: success, search: res?.data?.search ? res.data.search.reverse() : [], browse: res?.data?.browse ? res.data.browse.reverse() : [] } }) } /** * 清除搜索历史 * @param params - 请求参数必传 */ async ajaxClearQuery(params) { const query = Object.assign({}, params) return ajaxClearSearchHistory(query).then((res) => { let success = res?.error_code === 0 const { type } = this._getParams if (success && res.data && type) { // 清除成功更新取历史列表 this.getHistoryQuery({ type: type }) } return { success: success && res?.data } }) } /** * 保存画像浏览记录 * @param params - {type: buyer-采购单位画像 ent-企业画像, name: 采购单位画像时传名称,企业画像传 名称_企业id} */ async ajaxSaveViewQuery(params) { const { type } = this._getParams const query = Object.assign({}, params) return ajaxSaveViewHistory(query).then((res) => { let success = res?.error_code === 0 if (success && res.data && type) { // 保存成功更新历史列表 this.getHistoryQuery({ type: type }) } return { success: success && res?.data } }) } } export default SearchHistoryBaseApi