Files
gao-customer-h5/src/utils/store.ts
T
2026-09-12 15:07:43 +08:00

106 lines
2.8 KiB
TypeScript

import type { StoreBusinessDayStatus, StoreStatus } from "@/types/Store";
import { permissionCodes } from "@/utils/permission";
import type { Page } from "timi-web-api/types";
import { useUserStore } from "timi-web-api/store";
export const currentStoreIdStorageKey = "gao-customer-h5:current-store-id";
export const loginStoreIdStorageKey = "gao-customer-h5:login-store-id";
type LoginUserWithStore = {
storeId?: string;
user?: {
storeId?: string;
};
managedUser?: {
storeId?: string;
};
gaoUser?: {
storeId?: string;
};
};
export function getCurrentStoreId(): string {
const userStore = useUserStore();
const loginStoreId = readLoginStoreId();
if (userStore.hasPermissions(permissionCodes.store.read)) {
return readStoredCurrentStoreId() || loginStoreId || getLoginUserStoreId();
}
return loginStoreId || getLoginUserStoreId();
}
export function setCurrentStoreId(storeId: string): void {
localStorage.setItem(currentStoreIdStorageKey, storeId);
}
export function setLoginStoreId(storeId: string): void {
localStorage.setItem(loginStoreIdStorageKey, storeId);
}
export function readStoredCurrentStoreId(): string {
return localStorage.getItem(currentStoreIdStorageKey) || "";
}
export function readLoginStoreId(): string {
return localStorage.getItem(loginStoreIdStorageKey) || "";
}
export function withCurrentStoreId<T extends { storeId?: string }>(data: T): T {
return {
...data,
storeId: data.storeId || getCurrentStoreId()
};
}
export function withCurrentStorePage<T extends { storeId?: string }>(page: Page<T>): Page<T> {
return {
...page,
equalsExample: withCurrentStoreId(page.equalsExample || {} as Partial<T>) as Partial<T>
};
}
function getLoginUserStoreId(): string {
const userStore = useUserStore();
const loginUser = userStore.loginUser as LoginUserWithStore;
return loginUser.storeId || loginUser.user?.storeId || loginUser.managedUser?.storeId || loginUser.gaoUser?.storeId || "";
}
export function getStoreStatusText(status?: StoreStatus): string {
const map: Record<StoreStatus, string> = {
ACTIVE: "启用",
INACTIVE: "停用",
DELETED: "已删除"
};
return status ? map[status] : "未知";
}
export function getStoreStatusTheme(status?: StoreStatus): "success" | "warning" | "danger" | "default" {
if (status === "ACTIVE") {
return "success";
}
if (status === "INACTIVE") {
return "warning";
}
if (status === "DELETED") {
return "danger";
}
return "default";
}
export function getBusinessDayStatusText(status?: StoreBusinessDayStatus): string {
const map: Record<StoreBusinessDayStatus, string> = {
OPEN: "营业",
CLOSED: "休息"
};
return status ? map[status] : "未配置";
}
export function getBusinessDayStatusTheme(status?: StoreBusinessDayStatus): "success" | "warning" | "default" {
if (status === "OPEN") {
return "success";
}
if (status === "CLOSED") {
return "warning";
}
return "default";
}