d2-auth-btn.js 2.01 KB
export default {
  name: 'AuthButton',
  props: {
    label: {
      type: String,
      default: 'Button'
    },
    size: {
      type: String,
      default: 'mini'
    },
    type: {
      type: String,
      default: 'text'
    },
    loading: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: null
    },
    accept: {
      type: String,
      default: null
    },
    mapping: {
      type: Object,
      default: null
    },
    data: Object,
  },
  functional: true, // 开启函数化组件 
  render: (h, ctx) => {
    const _this = ctx.parent //获取 vue  this 实例
    /**
     * 获取传入参数 
     * type 非必填  button 类型  默认 text ----- route 中不标明 视为默认值 text 
     * data  点击按钮时 需要得到的参数  比如 table 中的 当前行数据 row
     * mapping 非必填  但: 当列表中的操作选项 根据 某个字段 变化的话 必填 且 必须 注明 field: 字段名
     */
    const { type , data , mapping } = ctx.props 
    /**
     * 判断一个按钮(btn)是否存在与当前行(data) 的映射关系(mapping)中
     * @param {*} btn 
     */
    const filter = btn => mapping ? mapping[data[mapping['field']]].includes(btn.label):true 
    /**
     * 拿到当前模块(页面) 所拥有按钮列表 过滤type属性 与 映射关系 返回需要显示的按钮列表
     */
    const curBtns = _this.$store.getters['d2admin/auth/curBtns'].filter(btn => {
      if(type === 'text' || !type){
        return (btn.type === 'text' || !btn.type) && filter(btn)
      }
      return btn.type === type && filter(btn)
    })
    const __aBtns = []
    /**
     * 循环最终显示的按钮列表 
     * 并渲染到页面
     */
    curBtns.forEach(btn =>
      __aBtns.push(h('el-button', {
        props: {
          type
        },
        on: {
          click: ()=>_this[btn.handle](data)
        }
      }, [btn.label])))
    return h('div', {}, __aBtns)
  }
}