index.js 1.28 KB
const DISPLAY = Object.freeze({
  NONE: '',
  DISABLED: 'disabled'
})

// 权限校验
function permission(ctx) {
  const {
    props: { fn, mode, display },
    parent
  } = ctx
  if (__includeFn(parent, fn)) {
    return mode
  } else if (display === DISPLAY.DISABLED) {
    ctx.data.attrs.disabled = true
    return mode
  }
  return DISPLAY.NONE
}

export function hasPermission(fn, display) {
  return __includeFn(this, fn) || display === DISPLAY.DISABLED
}
function __includeFn(ctx, fn) {
  const router = __getRoutes(ctx)
  return router && router.meta && router.meta.permissions && router.meta.permissions.includes(fn)
}
function __getRoutes(ctx) {
  const {
    $GLBUTTION_PERMISSIONS: { routes = [], routerBase }
  } = ctx
  return routes.find(route => location.pathname === `${routerBase}${route.path.split(':/')[0]}`)
}

// GLButton权限按钮组件
export default {
  functional: true,
  name: 'GLButton',
  props: {
    // fn权限对应的名字
    fn: {
      type: String
    },
    // 生成的组件类型
    mode: {
      type: String,
      default: 'el-button'
    },
    // 是否显示
    display: {
      type: String,
      default: ''
    }
  },
  render: function(createElement, context) {
    return createElement(permission(context), context.data, context.children)
  }
}