浏览代码

feat:搜索历史api模型

yangfeng 1 年之前
父节点
当前提交
0098d5de26

+ 22 - 0
data/data-models/modules/quick-search/api/search-history.js

@@ -0,0 +1,22 @@
+import { useRequest } from '../../../api'
+import qs from 'qs'
+
+// 获取搜索历史 
+// type: 1-标讯搜索历史记录  2-企业历史搜索 3-企业历史浏览 4-采购单位历史搜索 5-采购单位历史浏览
+export function ajaxGetSearchHistory(data) {
+  return useRequest({
+    url: `/publicapply/history/get`,
+    method: 'post',
+    data: qs.stringify(data)
+  })
+}
+
+// 清空搜索历史 
+// type: 1-标讯搜索历史记录  2-企业历史搜索 3-企业历史浏览 4-采购单位历史搜索 5-采购单位历史浏览
+export function ajaxClearSearchHistory(data) {
+  return useRequest({
+    url: `/publicapply/history/del`,
+    method: 'post',
+    data: qs.stringify(data)
+  })
+}

+ 55 - 0
data/data-models/modules/quick-search/plugins/search-history.js

@@ -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