fix event list sort

This commit is contained in:
Timi
2026-08-20 16:22:58 +08:00
parent f119e85798
commit a67ca759e4
3 changed files with 51 additions and 8 deletions
+41 -6
View File
@@ -4,33 +4,68 @@ import { getCurrentStoreId, withCurrentStoreId, withCurrentStorePage } from "@/u
const BASE_URI = "/gao/event";
const eventOrderMap: NonNullable<Page<EventItem>["orderMap"]> = {
sort: "ASC",
createdAt: "ASC"
};
type ListOptions = {
allStores?: boolean;
storeId?: string;
};
/**
* 为所有事件分页查询补充统一排序,避免后端未传排序参数时按数据库默认顺序返回。
*/
function withEventOrder(page: Page<EventItem>): Page<EventItem> {
return {
...page,
orderMap: {
...eventOrderMap,
...(page.orderMap || {})
}
};
}
/**
* 按事件排序值升序排列,缺少排序值的历史数据放到列表末尾。
*/
function sortEventList(list: EventItem[]): EventItem[] {
return [...list].sort((left, right) => {
const leftSort = left.sort ?? Number.MAX_SAFE_INTEGER;
const rightSort = right.sort ?? Number.MAX_SAFE_INTEGER;
if (leftSort !== rightSort) {
return leftSort - rightSort;
}
return (left.createdAt ?? Number.MAX_SAFE_INTEGER) - (right.createdAt ?? Number.MAX_SAFE_INTEGER);
});
}
async function list(req: Page<EventItem>, options?: ListOptions): Promise<PageResult<EventItem>> {
const orderedPage = withEventOrder(req);
if (options?.allStores) {
return await axios.post(`${BASE_URI}/list`, req);
return await axios.post(`${BASE_URI}/list`, orderedPage);
}
if (options?.storeId) {
return await axios.post(`${BASE_URI}/list`, {
...req,
...orderedPage,
equalsExample: {
...(req.equalsExample || {}),
...(orderedPage.equalsExample || {}),
storeId: options.storeId
}
});
}
return await axios.post(`${BASE_URI}/list`, withCurrentStorePage(req));
return await axios.post(`${BASE_URI}/list`, withCurrentStorePage(orderedPage));
}
async function listValid(): Promise<EventItem[]> {
return await axios.post(`${BASE_URI}/list/valid`, undefined, {
const list = await axios.post(`${BASE_URI}/list/valid`, undefined, {
params: { storeId: getCurrentStoreId() }
});
}) as unknown as EventItem[];
return sortEventList(list);
}
async function detail(id: string): Promise<EventItem> {
+5 -1
View File
@@ -130,7 +130,11 @@ const isLoading = ref(false);
const isNoMore = ref(false);
const page = reactive<Page<EventItem>>({
index: 0,
size: 15
size: 15,
orderMap: {
sort: "ASC",
createdAt: "ASC"
}
});
const pageResult = reactive<PageResult<EventItem>>({
total: 0,
+5 -1
View File
@@ -131,7 +131,11 @@ async function loadData(): Promise<void> {
try {
const result = await EventAPI.list({
index: 0,
size: 100
size: 100,
orderMap: {
sort: "ASC",
createdAt: "ASC"
}
});
eventList.value = result.list || [];
sortDirty.value = false;