createRole.vue 6.13 KB
<!--
 * @Description: 
 * @Author: huyue
 * @Date: 2022-06-10 15:36:06
 * @LastEditors: huyue
 * @LastEditTime: 2022-06-22 17:22:54
-->
<template>
  <div>
    <GLDialog :width="'1080px'" :visible="roleVisible" :close-on-click-modal="false" @close="handleClose" title="创建角色" :showClose="true">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="154px" class="demo-ruleForm">
        <el-form-item class="aa" v-if="$globalState.session.accountLevel != 4" label="所属商户" prop="merchantId">
          <el-select :size="size" style="width: 40%" v-model="ruleForm.merchantId" clearable filterable placeholder="请选择" @change="merchantChange">
            <el-option v-for="item in merchantList" :key="item.id" :label="item.merchantName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属商户:" prop="merchantName" v-else>
          <span>{{ $globalState.session.merchantName }}</span>
        </el-form-item>
        <el-form-item label="角色名称:" prop="roleName">
          <el-input :size="size" maxlength="20" style="width: 40%; padding-right: 50px" @input="changeInput" v-model="ruleForm.roleName" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色说明:" prop="roleDesc">
          <el-input :size="size" maxlength="50" style="width: 40%; padding-right: 50px" @input="changeInput" v-model="ruleForm.roleDesc" placeholder="请输入角色说明"></el-input>
        </el-form-item>
        <el-form-item label="角色权限:" prop="roleAuth">
          <el-checkbox-group v-model="ruleForm.roleAuth">
            <el-checkbox v-for="(itm, idx) in accountListSelect" :label="itm.accounttypeName" @change="selectCurAccount(itm, idx)" :key="idx">{{ itm.accounttypeName }}</el-checkbox>
          </el-checkbox-group>
          <SelectProduct v-if="roleVisible" ref="productFrom" :roleType="true" title="角色权限"></SelectProduct>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button :size="size" @click="handleClose">取消</el-button>
        <el-button :size="size" type="primary" @click="toCreateRole">提交</el-button>
      </div>
    </GLDialog>
  </div>
</template>

<script>
import SelectProduct from './selectProduct'
export default {
  name: 'CreateRole',
  components: {
    SelectProduct
  },
  data() {
    return {
      roleVisible: false,
      ruleForm: {
        merchantId: '',
        roleName: '',
        roleDesc: '',
        roleAuth: []
      },
      rules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        merchantId: [{ required: true, message: '请选择所属商户', trigger: 'change' }],
        roleAuth: [{ required: true, message: '请选择角色权限', trigger: 'change' }]
      },
      //商户下拉框
      merchantList: [],
      accountListSelect: [],
      rootList: []
    }
  },
  methods: {
    init() {
      this.roleVisible = true
      if (this.$refs.ruleForm) this.$refs.ruleForm.resetFields()
      this.getMerchantName()
    },
    handleClose() {
      this.roleVisible = false
    },
    getMerchantName() {
      this.$apis.role.merchantDataList({ source: 'glsx' }).then(res => {
        this.merchantList = res
        if (this.$globalState.session.accountLevel == 4) {
          this.ruleForm.merchantId = this.merchantList[0].id
          this.selectMerchant(this.merchantList[0].id)
        }
        // this.filterList()
      })
    },
    // 选择商户----获取权限
    async selectMerchant(id, list) {
      let res = await this.$refs.productFrom.findMenuFunTreeByMerId({ id })
      this.accountListSelect = res.map(item => {
        item.select = false
        return item
      })
    },
    // 选择商户权限
    selectCurAccount(item, index) {
      this.isChangeEdit = true
      this.roleRootRules = false
      this.accountListSelect[index].select = !item.select
      this.curRootIndex = 0
      if (this.accountListSelect[index].select) {
        this.rootList.push(item)
      } else {
        let arr = this.rootList.filter(el => {
          return item.accounttypeId != el.accounttypeId
        })
        this.rootList = arr
      }
      let accounttypeId = []
      this.rootList.forEach(item => {
        accounttypeId.push(item.accounttypeId)
      })
      this.accounttypeIds = accounttypeId
      this.$refs.productFrom.findMenuFunTree(item.accounttypeId, item.select)
    },
    toCreateRole() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let { merchantId, roleDesc, roleName } = this.ruleForm
          let product = this.$refs.productFrom.getSelectInfo()
          if (product && product.every(item => item.productList.length == 0)) {
            this.$message({
              type: 'error',
              message: '请至少选择一个产品',
              customClass: 'message-z'
            })
            return
          }
          let accountTypes = JSON.stringify(this.$refs.productFrom.getSelectInfo())
          let para = {
            merchantId: merchantId,
            roleName: roleName,
            roleDescription: roleDesc,
            delFlag: 0,
            requestList: accountTypes
          }
          this.$apis.role.createRole(para).then(res => {
            if (res.code == -1) {
              this.$message.error(res.message)
              return
            }
            this.$message.success('操作成功')
            this.$emit('updataRole', true)
            this.roleVisible = !res
            this.ruleForm = {
              merchantId: '',
              roleName: '',
              roleDesc: '',
              roleAuth: ''
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
/deep/ {
  .el-dialog__body {
    padding: 20px;
  }
  .el-form-item {
    display: inline-block;
    width: 50%;
    vertical-align: text-top;
    margin-bottom: 20px !important;
    .el-input {
      width: 360px !important;
    }
    &:last-child {
      width: 100%;
    }
    .el-form-item__label,
    .el-form-item__content {
      line-height: 32px;
    }
  }
}
</style>