DataPermissionModal.vue 7.1 KB
<template>
  <a-drawer
    :title="title"
    :width="drawerWidth"
    @close="handleCancel"
    :visible="visible"
    :confirmLoading="confirmLoading">

    <div :style="{width: '100%',border: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',}">
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="form" :model="model" :rules="validatorRules">

          <a-form-model-item label="资源类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-radio-group v-model="model.type">
              <a-radio v-if="model.type === 0" :value="0">标识</a-radio>
              <a-radio v-if="model.type === 1" :value="1">权限</a-radio>
            </a-radio-group>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :label="label"
            prop="name"
            hasFeedback>
            <a-input placeholder="请输入数据名称" v-model="model.name" :readOnly="disableSubmit" />
          </a-form-model-item>

          <a-form-model-item
            v-show="model.type !== 0"
            label="上级权限"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :validate-status="validateStatus"
            :hasFeedback="true"
            :required="true">
            <span slot="help">{{ validateStatus === 'error' ? '请选择上级权限' : '&nbsp;&nbsp;' }}</span>
            <a-tree-select
              style="width:100%"
              :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
              :treeData="treeData"
              v-model="model.parentId"
              placeholder="请选择父级权限"
              :disabled="disableSubmit"
              @change="handleParentIdChange">
            </a-tree-select>
          </a-form-model-item>

          <a-form-model-item
            v-show="show"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="perms"
            label="授权标识">
            <a-input placeholder="请输入授权标识, 如: user:list" v-model="model.perms" :readOnly="disableSubmit" />
          </a-form-model-item>

          <a-form-model-item
            v-show="show && model.type !== 0"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="状态">
            <j-dict-select-tag v-model="model.status" placeholder="请选择状态" :type="'radio'" dictCode="valid_status" />
          </a-form-model-item>

          <a-form-model-item
            v-show="show"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="sortNo"
            label="排序">
            <a-input-number placeholder="请输入权限排序" v-model="model.sortNo" style="width: 200px"
                            :readOnly="disableSubmit" />
          </a-form-model-item>
        </a-form-model>
      </a-spin>
      <a-row :style="{textAlign:'right'}">
        <a-button :style="{marginRight: '8px'}" @click="handleCancel">
          关闭
        </a-button>
        <a-button :disabled="disableSubmit" @click="handleOk" type="primary">确定</a-button>
      </a-row>
    </div>
  </a-drawer>
</template>

<script>
import { addDataPermission, duplicateCheck, editDataPermission, queryDataTreeList } from '@/api/api'
import Icons from './icon/Icons'

export default {
  name: 'DataPermissionModal',
  components: { Icons },
  data() {
    return {
      drawerWidth: 700,
      treeData: [],
      title: '操作',
      visible: false,
      disableSubmit: false,
      model: {},
      show: true,//根据权限类型,动态显示隐藏表单元素
      label: '数据名称',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      iconChooseVisible: false,
      validateStatus: ''
    }
  },
  computed: {
    validatorRules: function() {
      return {
        name: [{ required: true, message: '请输入权限标题!' }],
        perms: [{ required: true, message: '请输入授权标识!' }, { validator: this.validatePerms }]
      }
    }
  },
  created() {
  },
  methods: {
    loadTree() {
      let that = this
      queryDataTreeList().then((res) => {
        if (res.success) {
          that.treeData = []
          let treeList = res.result.treeList
          for (const element of treeList) {
            let temp = element
            temp.isLeaf = temp.leaf
            that.treeData.push(temp)
          }
        }
      })
    },
    add() {
      //初始化默认值
      this.edit({ status: '1', sortNo: 1.0, type: 0 })
    },
    edit(record) {
      this.resetScreenSize() // 调用此方法,根据屏幕宽度自适应调整抽屉的宽度
      this.model = { ...record }
      debugger;
      if (this.model.type === 0) {
        // 如果资源类型是标识,则没有状态
        this.model.status = null
      }
      this.visible = true
      this.loadTree()
    },
    close() {
      this.$emit('close')
      this.disableSubmit = false
      this.visible = false
      this.$refs.form.resetFields()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.model.type === 1 && !this.model.parentId) {
            that.validateStatus = 'error'
            that.$message.error('请检查你填的类型以及信息是否正确!')
            return
          } else {
            that.validateStatus = 'success'
          }
          that.confirmLoading = true
          let obj
          if (!this.model.id) {
            obj = addDataPermission(this.model)
          } else {
            obj = editDataPermission(this.model)
          }
          obj.then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validatePerms(rule, value, callback) {
      if (value && value.length > 0) {
        //校验授权标识是否存在
        let params = {
          tableName: 'sys_data_permission',
          fieldName: 'perms',
          fieldVal: value,
          dataId: this.model.id
        }
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback('授权标识已存在!')
          }
        })
      } else {
        callback()
      }
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth
      } else {
        this.drawerWidth = 700
      }
    },
    handleParentIdChange(value) {
      if (!value) {
        this.validateStatus = 'error'
      } else {
        this.validateStatus = 'success'
      }
    }
  }
}
</script>

<style scoped>

</style>