base.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { ajaxGetSearchHistory, ajaxClearSearchHistory, ajaxSaveViewHistory } from '../api/search-history'
  2. class SearchHistoryBaseApi {
  3. constructor(config = {}) {
  4. // 搜索历史type
  5. this.searchType = [1, 2, 4]
  6. // 浏览历史type
  7. this.browseType = [3, 5]
  8. // 历史搜索列表
  9. this.searchHistoryList = []
  10. // 历史浏览记录列表
  11. this.browseHistoryList = []
  12. this._getParams = config.getParams || (() => {})
  13. // 函数 Hooks
  14. this.getHistoryQuery = this.runQuery.bind(this)
  15. this.clearHistoryQuery = this.ajaxClearQuery.bind(this)
  16. this.saveViewHistoryQuery = this.ajaxSaveViewQuery.bind(this)
  17. }
  18. /**
  19. * 统一查询 API 入口
  20. * @param params - 请求参数
  21. * @returns {Promise<{success: boolean, search: [], browse: []}>}
  22. */
  23. async runQuery(params) {
  24. this.searchHistoryList = []
  25. const query = Object.assign({}, params, this._getParams(params))
  26. const result = await this.ajaxQuery(query)
  27. if (result.success) {
  28. this.searchHistoryList = result.search
  29. this.browseHistoryList = result.browse
  30. } else {
  31. this.searchHistoryList = []
  32. this.browseHistoryList = []
  33. }
  34. return result
  35. }
  36. // 需要覆写的API处理逻辑
  37. async ajaxQuery(params) {
  38. return ajaxGetSearchHistory(params).then((res) => {
  39. let success = res?.error_code === 0
  40. return {
  41. success: success,
  42. search: res?.data?.search ? res.data.search.reverse() : [],
  43. browse: res?.data?.browse ? res.data.browse.reverse() : []
  44. }
  45. })
  46. }
  47. // 清除搜索历史
  48. async ajaxClearQuery(params) {
  49. const query = Object.assign({}, params, this._getParams(params))
  50. return ajaxClearSearchHistory(query).then((res) => {
  51. let success = res?.error_code === 0
  52. if (success && res.data) {
  53. // 清除成功再次获取历史列表
  54. this.getHistoryQuery(params)
  55. }
  56. return {
  57. success: success && res?.data
  58. }
  59. })
  60. }
  61. // 保存企业浏览记录
  62. async ajaxSaveViewQuery(params) {
  63. const query = Object.assign({}, params, this._getParams(params))
  64. return ajaxSaveViewHistory(query).then((res) => {
  65. let success = res?.error_code === 0
  66. return {
  67. success: success && res?.data
  68. }
  69. })
  70. }
  71. }
  72. export default SearchHistoryBaseApi