121 lines
3.3 KiB
TypeScript
121 lines
3.3 KiB
TypeScript
import { axios } from "timi-web-api";
|
|
import type { Page, PageResult } from "timi-web-api";
|
|
import type { EventItem, EventOptionGroup } from "@/types/EventItem";
|
|
import { getCurrentStoreId, withCurrentStoreId, withCurrentStorePage } from "@/utils/store";
|
|
|
|
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`, orderedPage);
|
|
}
|
|
|
|
if (options?.storeId) {
|
|
return await axios.post(`${BASE_URI}/list`, {
|
|
...orderedPage,
|
|
equalsExample: {
|
|
...(orderedPage.equalsExample || {}),
|
|
storeId: options.storeId
|
|
}
|
|
});
|
|
}
|
|
|
|
return await axios.post(`${BASE_URI}/list`, withCurrentStorePage(orderedPage));
|
|
}
|
|
|
|
async function listValid(storeId?: string): Promise<EventItem[]> {
|
|
const list = await axios.post(`${BASE_URI}/list/valid`, undefined, {
|
|
params: { storeId: storeId || getCurrentStoreId() }
|
|
}) as unknown as EventItem[];
|
|
return sortEventList(list);
|
|
}
|
|
|
|
/**
|
|
* 查询登记记录筛选使用的事件,包含已删除事件。
|
|
* 该接口由后端使用 event.create 权限保护,仅用于历史记录回溯。
|
|
*/
|
|
async function listHistory(): Promise<PageResult<EventItem>> {
|
|
return await axios.post(`${BASE_URI}/list/history`, withCurrentStorePage(withEventOrder({ index: 0, size: 1000 })));
|
|
}
|
|
|
|
async function detail(id: string): Promise<EventItem> {
|
|
return await axios.post(`${BASE_URI}/detail`, undefined, {
|
|
params: { id }
|
|
});
|
|
}
|
|
|
|
async function create(event: EventItem): Promise<void> {
|
|
return await axios.post(`${BASE_URI}/create`, withCurrentStoreId(event));
|
|
}
|
|
|
|
async function update(event: EventItem): Promise<void> {
|
|
return await axios.post(`${BASE_URI}/update`, withCurrentStoreId(event));
|
|
}
|
|
|
|
async function updateOptionGroupList(eventId: string, optionGroupList: EventOptionGroup[]): Promise<void> {
|
|
await axios.post(`${BASE_URI}/options/update`, optionGroupList, {
|
|
params: { id: eventId }
|
|
});
|
|
}
|
|
|
|
async function sort(idList: string[]): Promise<void> {
|
|
return await axios.post(`${BASE_URI}/sort`, idList);
|
|
}
|
|
|
|
async function remove(id: string): Promise<void> {
|
|
return await axios.post(`${BASE_URI}/delete`, undefined, {
|
|
params: { id }
|
|
});
|
|
}
|
|
|
|
export default {
|
|
list,
|
|
listValid,
|
|
listHistory,
|
|
detail,
|
|
create,
|
|
update,
|
|
updateOptionGroupList,
|
|
sort,
|
|
remove
|
|
};
|