u
This commit is contained in:
1 parent
11bcef03e1
commit
a4e59dd5d1
39 files changed
+2333
-459
No files matched your search
@@ -0,0 +1,101 @@
|
||||
import { loadDataApi } from "@/request/api";
|
||||
import { useUserStore } from "./user";
|
||||
import { defineStore } from "pinia";
|
||||
import { ref, computed } from "vue";
|
||||
import { useRoute } from "vue-router";
|
||||
|
||||
export const usePermissionStore = defineStore("permission", () => {
|
||||
const route = useRoute();
|
||||
const userStore = useUserStore();
|
||||
|
||||
const modulePermissions = ref([]);
|
||||
const actionPermissions = ref([]);
|
||||
const isInitialized = ref(false);
|
||||
|
||||
function reset() {
|
||||
modulePermissions.value = [];
|
||||
actionPermissions.value = [];
|
||||
isInitialized.value = false;
|
||||
}
|
||||
|
||||
async function init() {
|
||||
if (isInitialized.value || !userStore.isLogined()) {
|
||||
// 如果已初始化或者未登录,直接跳过
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const moduleResponse = await loadDataApi(
|
||||
"b_User_Qx",
|
||||
`father='${userStore.userInfo.userid}'`,
|
||||
"",
|
||||
["ModuleID", "QxType", "ControlType", "ShareType"],
|
||||
);
|
||||
|
||||
if (moduleResponse.success) {
|
||||
modulePermissions.value = moduleResponse.data || [];
|
||||
} else {
|
||||
throw new Error(moduleResponse.message || "查询可用账号失败");
|
||||
}
|
||||
|
||||
const powerResponse = await loadDataApi(
|
||||
"b_User_power",
|
||||
`UserID='${userStore.userInfo.userid}'`,
|
||||
"",
|
||||
["ModuleID", "b_canuse", "b_caption", "b_ivalue"],
|
||||
);
|
||||
|
||||
if (powerResponse.success) {
|
||||
actionPermissions.value = powerResponse.data || [];
|
||||
} else {
|
||||
throw new Error(powerResponse.message || "查询可用账号失败");
|
||||
}
|
||||
|
||||
isInitialized.value = true;
|
||||
} catch (error) {
|
||||
console.error("初始化权限失败", error);
|
||||
}
|
||||
}
|
||||
|
||||
function hasModulePermission(moduleid) {
|
||||
if (userStore.userInfo.userid === "g3soft") {
|
||||
return true;
|
||||
}
|
||||
|
||||
const module = modulePermissions.value.find((item) => item.ModuleID === moduleid);
|
||||
|
||||
if (!module) return false;
|
||||
if (module.QxType === "禁用") return false;
|
||||
|
||||
// 其他情况允许
|
||||
return true;
|
||||
}
|
||||
|
||||
function hasActionPermission(id) {
|
||||
if (userStore.userInfo.userid === "g3soft") {
|
||||
return true;
|
||||
}
|
||||
|
||||
const moduleid = route.meta.moduleId;
|
||||
if (!moduleid) return false;
|
||||
|
||||
if (!hasModulePermission(moduleid)) return false;
|
||||
|
||||
const actions = actionPermissions.value.filter((item) => item.ModuleID === moduleid);
|
||||
|
||||
const action = actions.find((item) => Number(item.b_ivalue) === Number(id));
|
||||
|
||||
if (!action) return false;
|
||||
if (action.b_canuse === "0") return false;
|
||||
|
||||
// 其他情况允许
|
||||
return true;
|
||||
}
|
||||
|
||||
return {
|
||||
init,
|
||||
reset,
|
||||
modulePermissions,
|
||||
hasModulePermission,
|
||||
hasActionPermission,
|
||||
};
|
||||
});
|
||||
Reference in new issue
Block a user