20260726225220
This commit is contained in:
1 parent
e3c7c6f8da
commit
a8478d60ed
170 files changed
+6786
-1678
No files matched your search
@@ -1,18 +1,20 @@
|
||||
import { computed, ref } from "vue";
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { defineStore } from "pinia";
|
||||
import { loadDataApi } from "@/services/api";
|
||||
import { useAuthStore } from "./auth";
|
||||
|
||||
export const usePermissionStore = defineStore("permissions", () => {
|
||||
const loadedUserId = ref("");
|
||||
const loadingPromise = ref(null);
|
||||
const authStore = useAuthStore();
|
||||
const loadedPrincipalKey = ref("");
|
||||
const moduleCodes = ref([]);
|
||||
const powerCodes = ref([]);
|
||||
let loadingPromise = null;
|
||||
let loadingPrincipalKey = "";
|
||||
let loadVersion = 0;
|
||||
|
||||
const moduleSet = computed(() => new Set(moduleCodes.value));
|
||||
const powerSet = computed(() => new Set(powerCodes.value));
|
||||
const isG3soft = computed(() => {
|
||||
const authStore = useAuthStore();
|
||||
return (
|
||||
String(authStore.user?.account || "")
|
||||
.trim()
|
||||
@@ -21,25 +23,33 @@ export const usePermissionStore = defineStore("permissions", () => {
|
||||
});
|
||||
|
||||
async function load() {
|
||||
const authStore = useAuthStore();
|
||||
const userId = String(authStore.user?.id || "");
|
||||
const principalKey = `${String(authStore.orgId || "")}:${userId}`;
|
||||
if (isG3soft.value) {
|
||||
loadedUserId.value = `g3soft:${userId}`;
|
||||
const elevatedKey = `g3soft:${principalKey}`;
|
||||
if (loadedPrincipalKey.value === elevatedKey) return;
|
||||
loadVersion++;
|
||||
loadingPromise = null;
|
||||
loadingPrincipalKey = "";
|
||||
loadedPrincipalKey.value = elevatedKey;
|
||||
moduleCodes.value = [];
|
||||
powerCodes.value = [];
|
||||
return;
|
||||
}
|
||||
if (!/^\d+$/.test(userId)) throw new Error("登录用户 ID 无效,请重新登录");
|
||||
if (loadedUserId.value === userId) return;
|
||||
if (loadingPromise.value) return loadingPromise.value;
|
||||
if (loadedPrincipalKey.value === principalKey) return;
|
||||
if (loadingPromise && loadingPrincipalKey === principalKey) return loadingPromise;
|
||||
|
||||
loadingPromise.value = Promise.all([
|
||||
loadDataApi("b_module", "b_canuse = 1", "b_id ASC"),
|
||||
const requestVersion = ++loadVersion;
|
||||
loadingPrincipalKey = principalKey;
|
||||
const request = Promise.all([
|
||||
loadDataApi("s_module", "b_canuse = 1", "b_id ASC"),
|
||||
loadDataApi("b_user_module", `b_user_id = ${userId}`, "b_id ASC"),
|
||||
loadDataApi("b_user_power", `b_user_id = ${userId}`, "b_id ASC"),
|
||||
loadDataApi("b_module_power", "b_canuse = 1", "b_id ASC"),
|
||||
loadDataApi("s_module_power", "b_canuse = 1", "b_id ASC"),
|
||||
])
|
||||
.then(([modules, userModules, userPowers, powers]) => {
|
||||
if (requestVersion !== loadVersion) return;
|
||||
const moduleCodeById = new Map(
|
||||
modules.data.map((module) => [String(module.b_id), module.b_code]),
|
||||
);
|
||||
@@ -55,12 +65,15 @@ export const usePermissionStore = defineStore("permissions", () => {
|
||||
return moduleCode ? `${moduleCode}.${power.b_code}` : "";
|
||||
})
|
||||
.filter(Boolean);
|
||||
loadedUserId.value = userId;
|
||||
loadedPrincipalKey.value = principalKey;
|
||||
})
|
||||
.finally(() => {
|
||||
loadingPromise.value = null;
|
||||
if (requestVersion !== loadVersion) return;
|
||||
loadingPromise = null;
|
||||
loadingPrincipalKey = "";
|
||||
});
|
||||
return loadingPromise.value;
|
||||
loadingPromise = request;
|
||||
return request;
|
||||
}
|
||||
|
||||
function canModule(moduleCode) {
|
||||
@@ -74,10 +87,18 @@ export const usePermissionStore = defineStore("permissions", () => {
|
||||
}
|
||||
|
||||
function clear() {
|
||||
loadedUserId.value = "";
|
||||
loadVersion++;
|
||||
loadingPromise = null;
|
||||
loadingPrincipalKey = "";
|
||||
loadedPrincipalKey.value = "";
|
||||
moduleCodes.value = [];
|
||||
powerCodes.value = [];
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [authStore.orgId, authStore.user?.id, authStore.user?.account, authStore.token],
|
||||
clear,
|
||||
);
|
||||
|
||||
return { load, canModule, canPower, clear, moduleCodes, powerCodes, isG3soft };
|
||||
});
|
||||
Reference in new issue
Block a user