update user detail
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
import { Toast } from "tdesign-mobile-vue";
|
||||
import { useUserStore } from "timi-web";
|
||||
import StoreAPI from "@/api/StoreAPI";
|
||||
import type { Store } from "@/types/Store";
|
||||
import { getCurrentStoreId, readStoredCurrentStoreId, setCurrentStoreId } from "@/utils/store";
|
||||
|
||||
export const useCurrentStoreStore = defineStore("currentStore", () => {
|
||||
const userStore = useUserStore();
|
||||
const storeList = ref<Store[]>([]);
|
||||
const currentStoreId = ref(readStoredCurrentStoreId());
|
||||
const loginStore = ref<Store>();
|
||||
const loginStoreUserId = ref("");
|
||||
const isLoading = ref(false);
|
||||
let loginStoreRequest: Promise<Store> | undefined;
|
||||
let loginStoreRequestUserId = "";
|
||||
|
||||
const currentStore = computed(() => storeList.value.find(item => item.id === currentStoreId.value));
|
||||
const currentStoreName = computed(() => currentStore.value?.name || "默认门店");
|
||||
@@ -38,6 +44,36 @@ export const useCurrentStoreStore = defineStore("currentStore", () => {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadLoginStore(): Promise<Store> {
|
||||
const loginUserId = userStore.loginUser.user?.id || userStore.loginUser.id || "";
|
||||
if (loginStore.value?.id && loginStoreUserId.value === loginUserId) {
|
||||
return loginStore.value;
|
||||
}
|
||||
|
||||
if (!loginStoreRequest || loginStoreRequestUserId !== loginUserId) {
|
||||
loginStoreRequestUserId = loginUserId;
|
||||
loginStoreRequest = loadLoginStoreRequest(loginUserId);
|
||||
}
|
||||
const request = loginStoreRequest;
|
||||
try {
|
||||
return await request;
|
||||
} finally {
|
||||
if (loginStoreRequest === request) {
|
||||
loginStoreRequest = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function loadLoginStoreRequest(loginUserId: string): Promise<Store> {
|
||||
const result = await StoreAPI.loginStore();
|
||||
loginStore.value = result;
|
||||
loginStoreUserId.value = loginUserId;
|
||||
if (result.id) {
|
||||
selectStore(result.id);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function selectStore(storeId: string): void {
|
||||
if (!storeId) {
|
||||
return;
|
||||
@@ -64,10 +100,11 @@ export const useCurrentStoreStore = defineStore("currentStore", () => {
|
||||
storeList,
|
||||
currentStoreId,
|
||||
currentStore,
|
||||
loginStore,
|
||||
currentStoreName,
|
||||
isLoading,
|
||||
loadStoreList,
|
||||
loadLoginStore,
|
||||
selectStore
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user