add store
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import type { StoreBusinessDayOpenStatus, StoreStatus } from "@/types/Store";
|
||||
import { permissionCodes } from "@/utils/permission";
|
||||
import { type Page, useUserStore } from "timi-web";
|
||||
|
||||
export const defaultStoreId = "00000000-0000-0000-0000-000000000001";
|
||||
export const currentStoreIdStorageKey = "gao-customer-h5:current-store-id";
|
||||
|
||||
type LoginUserWithStore = {
|
||||
storeId?: string;
|
||||
user?: {
|
||||
storeId?: string;
|
||||
};
|
||||
managedUser?: {
|
||||
storeId?: string;
|
||||
};
|
||||
gaoUser?: {
|
||||
storeId?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export function getCurrentStoreId(): string {
|
||||
const userStore = useUserStore();
|
||||
if (userStore.hasPermissions(permissionCodes.store.read)) {
|
||||
return readStoredCurrentStoreId() || getLoginUserStoreId() || defaultStoreId;
|
||||
}
|
||||
return getLoginUserStoreId() || defaultStoreId;
|
||||
}
|
||||
|
||||
export function setCurrentStoreId(storeId: string): void {
|
||||
localStorage.setItem(currentStoreIdStorageKey, storeId);
|
||||
}
|
||||
|
||||
export function readStoredCurrentStoreId(): string {
|
||||
return localStorage.getItem(currentStoreIdStorageKey) || "";
|
||||
}
|
||||
|
||||
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?: StoreBusinessDayOpenStatus): string {
|
||||
const map: Record<StoreBusinessDayOpenStatus, string> = {
|
||||
OPEN: "营业",
|
||||
CLOSED: "休息"
|
||||
};
|
||||
return status ? map[status] : "未配置";
|
||||
}
|
||||
|
||||
export function getBusinessDayStatusTheme(status?: StoreBusinessDayOpenStatus): "success" | "warning" | "default" {
|
||||
if (status === "OPEN") {
|
||||
return "success";
|
||||
}
|
||||
if (status === "CLOSED") {
|
||||
return "warning";
|
||||
}
|
||||
return "default";
|
||||
}
|
||||
Reference in New Issue
Block a user