d2-auth-btn.js 4.05 KB
export default {
  name: 'AuthButton',
  props: {
    label: {
      type: String,
      default: ''
    },
    size: {
      type: String,
      default: 'mini'
    },
    // type: {
    //   type: String,
    //   default: 'text'
    // },
    category: {
      type: String,
      default: 'operation'
    },
    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
    },
    showBtnIds: {
      type: Array,
      //  props类型为Object/Array 使用函数方法解决
      default: () => []
    },
    data: Object
  },
  functional: true, // 开启函数化组件
  render: (h, ctx) => {
    const _this = ctx.parent // 获取 vue  this 实例
    // console.log(ctx);
    /**
     * 获取传入参数
     * category 非必填  button 类别  暂时两种 1 表格操作:operation ,2 Button 按钮: action  默认 operation ----- route 中不标明 视为默认值 operation
     * data  点击按钮时 需要得到的参数  比如 table 中的 当前行数据 row
     * mapping 非必填  但: 当列表中的操作选项 根据 某个字段 变化的话 必填 且 必须 注明 field: 字段名
     * label 当传入label 字段时 标识明确指定渲染该label 的按钮
     * size 按钮样式,默认mini
     * showBtnIds 按需显示按钮
     */
    const { category, data, mapping, label, size, showBtnIds } = ctx.props
    let curBtns = []
    /**
     * 判断一个按钮(btn)是否存在与当前行(data) 的映射关系(mapping)中
     * @param {*} btn
     */
    const filter = btn =>
      mapping ? mapping[data[mapping.field]].includes(btn.label) : true
    const $store = _this.$store.getters['d2admin/auth/curBtns']
    if (!$store) return h('div')
    if (label) {
      // 指定label
      const __item = $store.find(btn => btn.label === label && filter(btn))
      __item && curBtns.push(__item)
    } else {
      /**
       * 拿到当前模块(页面) 所拥有按钮列表 过滤type属性 与 映射关系 返回需要显示的按钮列表
       */
      curBtns = $store.filter(btn => {
        if (category === 'operation') {
          return (btn.category === 'operation' || !btn.category) && filter(btn)
        } else if (!category) {
          // 未指定类别
          return !btn.category && filter(btn)
        }
        return btn.category === category && filter(btn)
      })
    }
    if (curBtns.length === 0) return h('div')
    const __aBtns = []
    /**
     * 循环最终显示的按钮列表
     * 并渲染到页面
     */
    curBtns = showBtnIds.length
      ? curBtns.filter(item => showBtnIds.includes(item.authId))
      : curBtns
    curBtns.forEach(btn => {
      // var judgeRes = data ? (data.yqMerchantId === _this.$store.state.d2admin.user.info.detail.merchantId) : true
      // var isShow = !btn.judgeShow ? true : judgeRes
      // if ((btn.category === 'Button') || (data && isShow)) {
      __aBtns.push(
        h(
          'el-button',
          {
            props: {
              type: btn.type || (btn.category === 'Button' ? '' : 'text'),
              icon: btn.icon ? `el-icon-${btn.icon}` : '',
              size: size,
              disabled: btn.ruleObj
                ? btn.ruleObj.hasOwnProperty('disabledStatusList')
                  ? btn.ruleObj.disabledStatusList.includes(
                    data[btn.ruleObj.attr]
                  )
                  : false
                : false
            },
            style: {
              color: btn.color
            },
            on: {
              click: e => btn.handle && _this[btn.handle](data, e)
            }
          },
          [btn.ruleObj ? btn.ruleObj.rule[data[btn.ruleObj.attr]] : btn.label]
        )
      )
      // }
    })
    return h('span', {}, [...__aBtns, ctx.children])
  }
}