Browse Source

feat:搜索历史数据模型

yangfeng 1 year ago
parent
commit
af61bc1633

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


+ 17 - 0
data/data-models/modules/quick-search-history/model/index.js

@@ -0,0 +1,17 @@
+import { toRefs, reactive } from 'vue'
+import SearchHistoryBaseApi from '../plugins/base'
+
+function useSearchHistoryModel(config) {
+  const useApiModel = new SearchHistoryBaseApi(config)
+
+  const { getHistoryQuery, clearHistoryQuery } = useApiModel
+  const { searchHistoryList } = toRefs(reactive(useApiModel))
+
+  return {
+    searchHistoryList,
+    getHistoryQuery,
+    clearHistoryQuery
+  }
+}
+
+export default useSearchHistoryModel

+ 11 - 7
data/data-models/modules/quick-search/plugins/search-history.js → data/data-models/modules/quick-search-history/plugins/base.js

@@ -1,8 +1,8 @@
 import { ajaxGetSearchHistory, ajaxClearSearchHistory } from '../api/search-history'
 
-class SearchHistoryApi {
+class SearchHistoryBaseApi {
   constructor(config = {}) {
-    this.historyList = []
+    this.searchHistoryList = []
     this._getParams = config.getParams || (() => {})
     // 函数 Hooks
     this.getHistoryQuery = this.runQuery.bind(this)
@@ -16,13 +16,13 @@ class SearchHistoryApi {
    */
 
   async runQuery(params) {
-    this.historyList = []
+    this.searchHistoryList = []
     const query = Object.assign({}, params, this._getParams(params))
     const result = await this.ajaxQuery(query)
     if (result.success) {
-      this.historyList = result.list
+      this.searchHistoryList = result.list
     } else {
-      this.historyList = []
+      this.searchHistoryList = []
     }
     return result
   }
@@ -45,11 +45,15 @@ class SearchHistoryApi {
     return ajaxClearSearchHistory(query).then((res) => {
       let success = res?.error_code === 0
 
+      if (success && res.data) {
+        this.searchHistoryList = []
+      }
       return {
-        success: success
+        success: success,
+        list: []
       }
     })
   }
 }
 
-export default SearchHistoryApi
+export default SearchHistoryBaseApi