处理列表

This commit is contained in:
李瑞
2025-09-13 17:49:59 +08:00
parent aef84e76cb
commit ba7d904134
15 changed files with 226 additions and 194 deletions

View File

@@ -6,6 +6,7 @@ import {
getSearchHistory,
clearHistory,
searchSuggestion,
getGamesCount,
} from "../services/listApi";
import {
ListActions,
@@ -14,12 +15,10 @@ import {
IPayload,
} from "../../types/list/types";
import dateRangeUtils from '@/utils/dateRange'
// 完整的 Store 类型
type TennisStore = ListState & ListActions;
const toDate = dateRangeUtils?.formatDate(new Date())
const defaultDateRange: [string, string] = [dayjs().format('YYYY-MM-DD'), dayjs().add(1, 'M').format('YYYY-MM-DD')]
const defaultFilterOptions: IFilterOptions = {
@@ -100,6 +99,8 @@ export const useListStore = create<TennisStore>()((set, get) => ({
],
// 球局数量
gamesNum: 0,
// 是否还有更多数据
isHasMoreData: true,
// 页面滚动距离顶部距离 是否大于0
isScrollTop: false,
// 搜索历史数据
@@ -143,22 +144,31 @@ export const useListStore = create<TennisStore>()((set, get) => ({
lat: state?.location?.latitude,
lng: state?.location?.longitude,
};
console.log('===列表参数params', params)
return params;
},
// 设置搜索的列表结果
setListData: (payload: IPayload) => {
setListData: (payload: IPayload & { isAppend?: boolean }) => {
const { isSearchResult } = get();
const { error, data, loading, gamesNum } = payload;
const { error, data, loading, count, isAppend = false } = payload;
const saveKey = isSearchResult ? "searchResultData" : "matches";
const saveData = { error, loading, gamesNum, [saveKey]: data };
console.log('===saveData', saveData)
set({...saveData});
const isHasMoreData = count > 0;
if (isAppend) {
// 追加数据到现有数组
const currentData = get()[saveKey] || [];
const newData = [...currentData, ...(data || [])];
const saveData = { error, loading, count, isHasMoreData, [saveKey]: newData };
set({...saveData});
} else {
// 替换整个数组
const saveData = { error, loading, count, isHasMoreData, [saveKey]: data };
set({...saveData});
}
},
// 获取列表数据(常规搜索)
fetchMatches: async (params, isFirstLoad = false) => {
fetchMatches: async (params, isFirstLoad = false, isAppend = false) => {
set({ loading: true, error: null });
const { getSearchParams, setListData, distanceQuickFilter } = get();
@@ -191,70 +201,45 @@ export const useListStore = create<TennisStore>()((set, get) => ({
error: "-1",
data: [],
loading: false,
gamesNum: 0,
count: 0,
isAppend,
});
return Promise.reject(new Error('获取数据失败'));
}
const { count, rows } = data;
setListData({
error: '',
data: rows || [],
loading: false,
gamesNum: count,
count,
isAppend,
});
return Promise.resolve();
} catch (error) {
setListData({
error: "-1",
data: [],
loading: false,
gamesNum: 0,
count: 0,
isAppend,
});
return Promise.reject(error);
}
},
// 获取列表数据(智能筛选)
// getIntegrateListData: async (params) => {
// set({ loading: true, error: null });
// const { getSearchParams, setListData } = get();
// try {
// const searchParams = getSearchParams() || {};
// const reqParams = {
// ...(searchParams || {}),
// ...params,
// };
// reqParams.order = "";
// console.log("===getGamesIntegrateList 获取列表数据参数:", reqParams);
// const resData = (await getGamesIntegrateList(reqParams)) || {};
// const { data = {}, code } = resData;
// if (code !== 0) {
// setListData({
// error: '-1',
// data: [],
// loading: false,
// gamesNum: 0,
// });
// }
// const { count, rows } = data;
// setListData({
// // recommendList: rows || [],
// error: null,
// data: rows || [],
// loading: false,
// gamesNum: count,
// });
// } catch (error) {
// setListData({
// error: null,
// matches: [],
// loading: false,
// gamesNum: 0,
// });
// }
// },
// 获取列表数据
getMatchesData: () => {
getMatchesData: async () => {
const { fetchMatches } = get();
fetchMatches({}, true); // 第一次进入页面,传入 isFirstLoad = true
return await fetchMatches({}, true); // 第一次进入页面,传入 isFirstLoad = true
},
// 获取球局数量
fetchGetGamesCount: async () => {
const { getSearchParams } = get();
const params = getSearchParams() || {};
const resData = (await getGamesCount(params)) || {};
const gamesNum = resData?.data?.count || 0;
set({ gamesNum });
},
// 获取历史搜索数据
@@ -307,7 +292,7 @@ export const useListStore = create<TennisStore>()((set, get) => ({
// 更新综合筛选项
updateFilterOptions: (payload: Record<string, any>) => {
const { filterOptions: preFilterOptions, getMatchesData } = get() || {};
const { filterOptions: preFilterOptions, fetchGetGamesCount } = get() || {};
const filterOptions = { ...preFilterOptions, ...payload };
const filterCount = Object.values(filterOptions).filter(Boolean).length;
console.log("===更新综合筛选项", filterOptions, filterCount);
@@ -316,42 +301,48 @@ export const useListStore = create<TennisStore>()((set, get) => ({
filterCount,
pageOption: defaultPageOption,
});
// 重新搜索数据
getMatchesData();
// 获取球局数量
fetchGetGamesCount();
},
// 清空综合筛选选项
clearFilterOptions: () => {
const { getMatchesData } = get() || {};
const { getMatchesData, fetchGetGamesCount } = get() || {};
set({
filterOptions: defaultFilterOptions,
filterCount: 0,
pageOption: defaultPageOption,
});
getMatchesData();
fetchGetGamesCount();
},
// 加载更多数据
loadMoreMatches: () => {
const { pageOption, getMatchesData } = get() || {};
const { pageOption, fetchMatches, isHasMoreData } = get() || {};
if (!isHasMoreData) {
return;
}
set({
pageOption: {
page: pageOption?.page + 1,
pageSize: 20,
},
});
getMatchesData();
// 加载更多时追加数据到现有数组
fetchMatches({}, false, true);
},
// 初始化搜索条件 重新搜索
initialFilterSearch: () => {
const { getMatchesData } = get();
initialFilterSearch: async () => {
const { getMatchesData, fetchGetGamesCount } = get();
set({
distanceQuickFilter: defaultDistanceQuickFilter,
filterOptions: defaultFilterOptions,
pageOption: defaultPageOption,
});
getMatchesData();
fetchGetGamesCount();
return await getMatchesData();
},
// 更新store数据