114 lines
2.7 KiB
JavaScript
114 lines
2.7 KiB
JavaScript
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;
|
|
}
|
|
|
|
const hasModule = hasModulePermission(moduleid);
|
|
|
|
if (!hasModule) {
|
|
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,
|
|
};
|
|
});
|