import { Session } from '@/utils/storage';
import { CACHE_KEY } from '@/constants/index';

/**
 * 权限校验指令
 * 使用方法：v-hasPermi="['system:user:add']"
 */
export default {
    mounted(el: HTMLElement, binding: any) {
        const { value } = binding;
        const perms: string[] = Session.get(CACHE_KEY.PERMS) || [];

        if (value && value instanceof Array && value.length > 0) {
            const hasPermissions = perms.some(permission => {
                return (value as string[]).includes(permission);
            });

            if (!hasPermissions) {
                // 如果没有权限，则移除元素
                el.parentNode && el.parentNode.removeChild(el);
            }
        } else {
            console.error(`[v-hasPermi]: 请设置操作权限标签值, 如 v-hasPermi="['system:user:add']"`);
        }
    }
};
