20260720172840
This commit is contained in:
1 parent
cda7f16d3a
commit
94207c77ff
3117 files changed
+201407
-1236
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import { computed, ref } 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 moduleCodes = ref([]);
|
||||
const powerCodes = ref([]);
|
||||
|
||||
const moduleSet = computed(() => new Set(moduleCodes.value));
|
||||
const powerSet = computed(() => new Set(powerCodes.value));
|
||||
|
||||
async function load() {
|
||||
const authStore = useAuthStore();
|
||||
const userId = String(authStore.user?.id || "");
|
||||
if (!/^\d+$/.test(userId)) throw new Error("登录用户 ID 无效,请重新登录");
|
||||
if (loadedUserId.value === userId) return;
|
||||
if (loadingPromise.value) return loadingPromise.value;
|
||||
|
||||
loadingPromise.value = Promise.all([
|
||||
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"),
|
||||
])
|
||||
.then(([userModules, userPowers, powers]) => {
|
||||
const powerById = new Map(powers.data.map((power) => [String(power.b_id), power]));
|
||||
moduleCodes.value = userModules.data.map((item) => item.b_module_id);
|
||||
powerCodes.value = userPowers.data
|
||||
.map((item) => powerById.get(String(item.b_power_id)))
|
||||
.filter(Boolean)
|
||||
.map((power) => `${power.b_module_id}.${power.b_code}`);
|
||||
loadedUserId.value = userId;
|
||||
})
|
||||
.finally(() => {
|
||||
loadingPromise.value = null;
|
||||
});
|
||||
return loadingPromise.value;
|
||||
}
|
||||
|
||||
function canModule(moduleCode) {
|
||||
return !moduleCode || moduleSet.value.has(moduleCode);
|
||||
}
|
||||
|
||||
function canPower(moduleCode, powerCode) {
|
||||
return canModule(moduleCode) && powerSet.value.has(`${moduleCode}.${powerCode}`);
|
||||
}
|
||||
|
||||
function clear() {
|
||||
loadedUserId.value = "";
|
||||
moduleCodes.value = [];
|
||||
powerCodes.value = [];
|
||||
}
|
||||
|
||||
return { load, canModule, canPower, clear, moduleCodes, powerCodes };
|
||||
});
|
||||
Reference in new issue
Block a user