add store
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { Toast } from "tdesign-mobile-vue";
|
||||
import StoreAPI from "@/api/StoreAPI";
|
||||
import type { Store } from "@/types/Store";
|
||||
import { getCurrentStoreId, readStoredCurrentStoreId, setCurrentStoreId } from "@/utils/store";
|
||||
|
||||
export const useCurrentStoreStore = defineStore("currentStore", () => {
|
||||
const storeList = ref<Store[]>([]);
|
||||
const currentStoreId = ref(readStoredCurrentStoreId());
|
||||
const isLoading = ref(false);
|
||||
|
||||
const currentStore = computed(() => storeList.value.find(item => item.id === currentStoreId.value));
|
||||
const currentStoreName = computed(() => currentStore.value?.name || "默认门店");
|
||||
|
||||
async function loadStoreList(): Promise<void> {
|
||||
if (isLoading.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const result = await StoreAPI.list({
|
||||
index: 0,
|
||||
size: 1000,
|
||||
equalsExample: {
|
||||
status: "ACTIVE"
|
||||
},
|
||||
orderMap: {
|
||||
sort: "ASC",
|
||||
createdAt: "DESC"
|
||||
}
|
||||
});
|
||||
storeList.value = result.list || [];
|
||||
normalizeCurrentStoreId();
|
||||
} catch {
|
||||
Toast.error("门店列表加载失败");
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function selectStore(storeId: string): void {
|
||||
if (!storeId) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentStoreId.value = storeId;
|
||||
setCurrentStoreId(storeId);
|
||||
}
|
||||
|
||||
function normalizeCurrentStoreId(): void {
|
||||
const storedStoreId = getCurrentStoreId();
|
||||
if (storeList.value.some(item => item.id === storedStoreId)) {
|
||||
selectStore(storedStoreId);
|
||||
return;
|
||||
}
|
||||
|
||||
const firstStoreId = storeList.value[0]?.id;
|
||||
if (firstStoreId) {
|
||||
selectStore(firstStoreId);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
storeList,
|
||||
currentStoreId,
|
||||
currentStore,
|
||||
currentStoreName,
|
||||
isLoading,
|
||||
loadStoreList,
|
||||
selectStore
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user