base.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { ajaxGetSearchHistory, ajaxClearSearchHistory, ajaxSaveViewHistory } from '../api/search-history'
  2. class SearchHistoryBaseApi {
  3. constructor(config = {}) {
  4. // 初始化需要将type值传进来(标讯搜索:1 企业搜索:2,3 采购单位搜索: 4,5)必传
  5. this._getParams = config || (() => {})
  6. // 历史搜索列表
  7. this.searchHistoryList = []
  8. // 历史浏览记录列表
  9. this.browseHistoryList = []
  10. // 函数 Hooks
  11. this.getHistoryQuery = this.runQuery.bind(this)
  12. this.clearHistoryQuery = this.ajaxClearQuery.bind(this)
  13. this.saveViewHistoryQuery = this.ajaxSaveViewQuery.bind(this)
  14. }
  15. /**
  16. * 历史搜索、历史浏览查询 API 入口
  17. * @param params - 请求参数(可传可不传,不传会从this._getParams中取 传了会合并)
  18. * @returns {Promise<{success: boolean, search: [], browse: []}>}
  19. */
  20. async runQuery(params) {
  21. const query = Object.assign({}, params, this._getParams)
  22. const result = await this.ajaxQuery(query)
  23. if (result.success) {
  24. this.searchHistoryList = result.search
  25. this.browseHistoryList = result.browse
  26. } else {
  27. this.searchHistoryList = []
  28. this.browseHistoryList = []
  29. }
  30. return result
  31. }
  32. // 需要覆写的API处理逻辑
  33. async ajaxQuery(params) {
  34. return ajaxGetSearchHistory(params).then((res) => {
  35. let success = res?.error_code === 0
  36. if (res?.data?.browse) {
  37. // 格式化浏览历史数据(企业画像浏览记录跳转需要企业id, 存历史记录的时候保存格式为“企业名称_企业id”, 取的时候要分割开)
  38. res.data.browse = res.data.browse.map((item) => {
  39. if (item.indexOf('_') > -1) {
  40. const [name, id] = item.split('_')
  41. return {
  42. name,
  43. id
  44. }
  45. } else {
  46. return {
  47. name: item
  48. }
  49. }
  50. })
  51. }
  52. return {
  53. success: success,
  54. search: res?.data?.search ? res.data.search.reverse() : [],
  55. browse: res?.data?.browse ? res.data.browse.reverse() : []
  56. }
  57. })
  58. }
  59. /**
  60. * 清除搜索历史
  61. * @param params - 请求参数必传
  62. */
  63. async ajaxClearQuery(params) {
  64. const query = Object.assign({}, params)
  65. return ajaxClearSearchHistory(query).then((res) => {
  66. let success = res?.error_code === 0
  67. const { type } = this._getParams
  68. if (success && res.data && type) {
  69. // 清除成功更新取历史列表
  70. this.getHistoryQuery({ type: type })
  71. }
  72. return {
  73. success: success && res?.data
  74. }
  75. })
  76. }
  77. /**
  78. * 保存画像浏览记录
  79. * @param params - {type: buyer-采购单位画像 ent-企业画像, name: 采购单位画像时传名称,企业画像传 名称_企业id}
  80. */
  81. async ajaxSaveViewQuery(params) {
  82. const { type } = this._getParams
  83. const query = Object.assign({}, params)
  84. return ajaxSaveViewHistory(query).then((res) => {
  85. let success = res?.error_code === 0
  86. if (success && res.data && type) {
  87. // 保存成功更新历史列表
  88. this.getHistoryQuery({ type: type })
  89. }
  90. return {
  91. success: success && res?.data
  92. }
  93. })
  94. }
  95. }
  96. export default SearchHistoryBaseApi