89 lines
3.7 KiB
TypeScript
89 lines
3.7 KiB
TypeScript
import { useUserStore } from "timi-web-api";
|
|
|
|
import EventRecordAPI from "@/api/EventRecordAPI";
|
|
import FulfillmentAPI from "@/api/FulfillmentAPI";
|
|
import RecycleAPI from "@/api/RecycleAPI";
|
|
import SalesOrderAPI from "@/api/SalesOrderAPI";
|
|
import { pendingNoticeEvents } from "@/utils/pendingNoticeEvents";
|
|
import { permissionCodes } from "@/utils/permission";
|
|
import { getCurrentStoreId } from "@/utils/store";
|
|
import { useCurrentStoreStore } from "./currentStoreStore";
|
|
|
|
export const usePendingNoticeStore = defineStore("pendingNotice", () => {
|
|
const userStore = useUserStore();
|
|
const currentStoreStore = useCurrentStoreStore();
|
|
const pendingCounts = reactive({ fulfillment: 0, stock: 0, fund: 0, event: 0, recycle: 0 });
|
|
let pendingRequestId = 0;
|
|
const pendingNoticeList = computed(() => {
|
|
const notices: { content: string; name: string; query: Record<string, string> }[] = [];
|
|
if (pendingCounts.fulfillment) {
|
|
notices.push({ content: `有 ${pendingCounts.fulfillment} 条履约待核销`, name: "OrderFulfillmentList", query: { status: "PENDING" } });
|
|
}
|
|
if (pendingCounts.recycle) {
|
|
notices.push({ content: `有 ${pendingCounts.recycle} 条回收兑换待核销`, name: "RecycleExchangeList", query: { status: "PENDING" } });
|
|
}
|
|
if (pendingCounts.fund) {
|
|
notices.push({ content: `有 ${pendingCounts.fund} 条资金流水待核销`, name: "FundRecordList", query: { status: "PENDING" } });
|
|
}
|
|
if (pendingCounts.stock) {
|
|
notices.push({ content: `有 ${pendingCounts.stock} 条履约等待库存`, name: "OrderFulfillmentList", query: { status: "WAITING_STOCK" } });
|
|
}
|
|
if (pendingCounts.event) {
|
|
notices.push({ content: `有 ${pendingCounts.event} 条事件待核销`, name: "EventRecordList", query: { verificationStatus: "PENDING", dateType: "all" } });
|
|
}
|
|
return notices;
|
|
});
|
|
|
|
async function refreshPendingNotices(): Promise<void> {
|
|
const requestId = ++pendingRequestId;
|
|
const tasks: Promise<void>[] = [];
|
|
const load = async (key: keyof typeof pendingCounts, request: () => Promise<{ total?: number }>): Promise<void> => {
|
|
try {
|
|
const result = await request();
|
|
if (requestId === pendingRequestId) {
|
|
pendingCounts[key] = result.total || 0;
|
|
}
|
|
} catch (error) {
|
|
if (requestId === pendingRequestId) {
|
|
console.warn("待处理通知加载失败", error);
|
|
}
|
|
}
|
|
};
|
|
if (userStore.hasPermissions(permissionCodes.orderFulfillment.read)) {
|
|
tasks.push(load("fulfillment", () => FulfillmentAPI.list({ index: 0, size: 1, status: "PENDING" })));
|
|
tasks.push(load("stock", () => FulfillmentAPI.list({ index: 0, size: 1, status: "WAITING_STOCK" })));
|
|
}
|
|
if (userStore.hasPermissions(permissionCodes.fundRecord.read)) {
|
|
tasks.push(load("fund", () => SalesOrderAPI.fundRecordList({ index: 0, size: 1, status: "PENDING" })));
|
|
}
|
|
if (userStore.hasPermissions(permissionCodes.eventRecord.read)) {
|
|
tasks.push(load("event", () => EventRecordAPI.list({ index: 0, size: 1, verificationStatus: "PENDING" })));
|
|
}
|
|
if (userStore.hasPermissions(permissionCodes.recycleExchange.query)) {
|
|
tasks.push(load("recycle", () => RecycleAPI.page({ index: 0, size: 1, status: "PENDING" })));
|
|
}
|
|
await Promise.all(tasks);
|
|
}
|
|
|
|
watch(
|
|
() => [currentStoreStore.currentStoreId, userStore.loginUser.user?.id || userStore.loginUser.id || ""],
|
|
([, userId]) => {
|
|
pendingRequestId++;
|
|
Object.assign(pendingCounts, { fulfillment: 0, stock: 0, fund: 0, event: 0, recycle: 0 });
|
|
if (userId) {
|
|
void refreshPendingNotices();
|
|
}
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
|
|
const unregister = pendingNoticeEvents.register("changed", storeId => {
|
|
if (!storeId || storeId === getCurrentStoreId()) {
|
|
void refreshPendingNotices();
|
|
}
|
|
});
|
|
onScopeDispose(unregister);
|
|
|
|
return { pendingNoticeList };
|
|
});
|