auth

138 阅读1分钟

登陆拿到权限列表:

  • rbac.permission:[] -> localstorage.phoenix_user_auths:[],
  • is_administrator-> antd-pro-authority['user'] or antd-pro-authority['admin']

菜单路由动态渲染-layouts-BasicLayout-SiderMenu- BaseMenu,

/**
 * 获得菜单子节点权限
 * @memberof SiderMenu
 */
getNavMenuItems = (menusData, parent) => {
  if (!menusData) {
    return [];
  }
  return menusData
    .filter(item => checkAuth(item.meta && item.meta.authKey)) - 判断权限
    .filter(item => item.name && !item.hideInMenu)
    .map(item => this.getSubMenuOrItem(item, parent))
    .filter(item => item);
};

getRouterAuthority = (pathname, routeData) => {
  let routeAuthority;
  const getAuthority = (key, routes) => {
    routes.map(route => {
      if (route.path && pathToRegexp(route.path).test(key)) {
        // routeAuthority = route.authority;
        if (route.meta && !checkAuth(route.meta.authKey)) {
          routeAuthority = ['noAuthority']; // 从地址栏输入地址,无权限-403
        }
      } else if (route.routes) {
        routeAuthority = getAuthority(key, route.routes);
      }
      return route;
    });
    return routeAuthority;
  };
  return getAuthority(pathname, routeData);
};

checkAuth

export function checkAuth(authKey, userAuths) {
  if (!authKey) {
    return true;
  }
  const _userAuths = userAuths || getAuths() || []; // 数组-权限节点keys
  const _userAuthority = getAuthority() || [];
  if (_userAuthority.indexOf('admin') >= 0) {
    return true;
  }
  return _userAuths.indexOf(authKey) >= 0;
}

内容403- layouts-BasicLayout

<Content className={styles.content} style={contentStyle}>
  <Authorized authority={routerConfig} noMatch={<Exception403 />}>
    {children}
  </Authorized>
</Content>
const Authorized = ({ children, authority = [], path, noMatch = null, noUpdatePwd = null }) => {
  const childrenRender = typeof children === 'undefined' ? null : children;
  if (!checkSignin()) {
    return noMatch; // Redirect to ='/user/login'
  }
  if (getUser().is_set_pwd && path !== '/user/update-pwd') {
    return noUpdatePwd; //Redirect to = '/user/update-pwd'
  }

  if (authority.indexOf('noAuthority') > -1) {
    return <Redirect to="/exception/403" />;
  }

  return childrenRender;
};

export default Authorized;
```。


note:vue3-https://www.bilibili.com/video/BV1dS4y1y7vd/?p=1&vd_source=0b0f475fa6611873c106289f514d9d9e

https://www.bilibili.com/video/BV1Yt4y1p7Z9?p=2&vd_source=0b0f475fa6611873c106289f514d9d9e

openApi 地址:http://dishaxy.dishait.cn/shopadminapi
shopAdmin-b站地址:https://www.bilibili.com/video/BV1Yt4y1p7Z9/?p=14&spm_id_from=pageDriver&vd_source=0b0f475fa6611873c106289f514d9d9e

vue---useFetch:https://juejin.cn/post/7211033231058042938