UNIVPLMDataIntegration/Web/src/directive/authDirective.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-06-15 13:02:35 +08:00
import type { App } from 'vue';
import { useUserInfo } from '/@/stores/userInfo';
import { judgementSameArr } from '/@/utils/arrayOperation';
/**
*
* @directive v-auth="xxx"
* @directive v-auths="[xxx,xxx]"
* @directive v-auth-all="[xxx,xxx]"
*/
export function authDirective(app: App) {
// 单个权限验证v-auth="xxx"
app.directive('auth', {
mounted(el, binding) {
const stores = useUserInfo();
2024-07-22 02:54:41 +08:00
if (!stores.userInfos.authApiList.some((v: string) => v === binding.value)) el.parentNode.removeChild(el);
2024-06-15 13:02:35 +08:00
},
});
// 多个权限验证满足一个则显示v-auths="[xxx,xxx]"
app.directive('auths', {
mounted(el, binding) {
let flag = false;
const stores = useUserInfo();
2024-07-22 02:54:41 +08:00
stores.userInfos.authApiList.map((val: string) => {
2024-06-15 13:02:35 +08:00
binding.value.map((v: string) => {
if (val === v) flag = true;
});
});
if (!flag) el.parentNode.removeChild(el);
},
});
// 多个权限验证全部满足则显示v-auth-all="[xxx,xxx]"
app.directive('auth-all', {
mounted(el, binding) {
const stores = useUserInfo();
2024-07-22 02:54:41 +08:00
const flag = judgementSameArr(binding.value, stores.userInfos.authApiList);
2024-06-15 13:02:35 +08:00
if (!flag) el.parentNode.removeChild(el);
},
});
}