create.vue 7.24 KB
<template>
  <div class="role-create-page">
    <div class="top_title_widget">
      <span class="title">{{ pageName }}</span>
    </div>
    <div class="main_widget">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item v-if="pageType == 'create'" label="所属商户" prop="merchantId">
          <el-select size="small" style="width: 40%" v-model="ruleForm.merchantId" :filter-method="filterList" clearable filterable placeholder="请选择" @change="selectMerchant">
            <el-option v-for="item in list" :key="item.id" :label="item.merchantName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属商户" prop="merchantName" v-if="pageType == 'edit'">
          <el-input disabled style="width: 40%" v-model="ruleForm.merchantName"></el-input>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleName">
          <el-input style="width: 40%" v-model="ruleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色说明" prop="roleDesc">
          <el-input style="width: 40%" v-model="ruleForm.roleDesc"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" prop="roleRoot">
          <div class="custom_element">
            <template v-for="(item, index) in accountList">
              <div :key="index" class="wrap">
                <AutoButton @getBtnStatus="selectCurAccount(item, index)" :typeStatus="item.select" :btnLabel="item.accounttypeName"></AutoButton>
              </div>
            </template>
          </div>
        </el-form-item>
      </el-form>
      <SelectProduct ref="productFrom" :roleType="true" :parentId="41712" :accountlevel="2" :pId="41713"></SelectProduct>
    </div>
    <div style="height: 84px"></div>
    <div class="fixed_bottom">
      <el-button v-if="pageType == 'create'" type="primary" @click="toCreateRole(1)">草稿</el-button>
      <div class="wrap_driver">
        <el-button type="primary" @click="toCreateRole(0)">确定</el-button>
      </div>
      <el-button type="primary" @click="toBack">取消</el-button>
    </div>
  </div>
</template>

<script>
import AutoButton from '../../components/auto-button/index.vue'
import SelectProduct from '../account/components/selectProduct.vue'
export default {
  name: '',
  components: {
    AutoButton,
    SelectProduct
  },
  data() {
    return {
      ruleForm: {
        merchantId: '',
        roleName: '',
        roleDesc: ''
      },
      rules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        merchantId: [{ required: true, message: '请选择所属商户', trigger: 'change' }],
        roleRoot: [{ required: true, message: '请选择角色权限', trigger: 'change' }]
      },
      rootList: [],
      curRootIndex: 0,
      pageName: '新增角色',
      roleInfo: null,
      pageType: 'create',
      accountList: [],
      //商户
      list: [],
      // tree数据
      treeDataList: []
    }
  },
  created() {
    let id = Number(this.$route.params.id);
    if (!id) {
      this.pageName = '新增角色'
      this.pageType ='create'
      this.getMerchantName()
    } else {
      this.pageName = '修改角色'
      this.pageType ='edit'
      this.getRoleData(id)
      this.editId = id
    }
  },
  methods: {
    // 获取角色信息
    getRoleData(id) {
      this.$apis.role.roleInfoData({ id }).then(res => {
        this.roleInfo = res
        this.ruleForm = {
          merchantName: res.merchantName,
          roleName: res.roleName,
          roleDesc: res.roleDescription,
          merchantId:res.merchantId
        }
        this.getMerchantTree(id, res.responseList)
      })
    },
    // 获取商户树
    getMerchantTree(id, list) {
      this.$apis.role.roleTreeDataById({ id }).then(res => {
        for (let i = 0; i < list.length; i++) {
          for (let j = 0; j < res.length; j++) {
            if (res[j].accounttypeId == list[i].accounttypeId) {
              res[j].select = true
            }
          }
        }
        this.accountList = res
      })
    },
    getMerchantName() {
      this.$apis.role.merchantDataList({ source: 'glsx' }).then(res => {
        this.merchantList = res
        this.filterList()
      })
    },
    // 过滤数据
    filterList(query = '') {
      let arr = this.merchantList.filter(item => {
        return item.merchantName.includes(query)
      })
      if (arr.length > 100) {
        this.list = arr.slice(0, 100)
      } else {
        this.list = arr
      }
    },
    // 选择商户----获取权限
    selectMerchant(id) {
      this.$apis.role.merchantRootById({ id }).then(res => {
        this.accountList = res.map(item => {
          item.select = false
          return item
        })
      })
    },
    // 选择商户权限
    selectCurAccount(item, index) {
      this.accountList[index].select = !item.select
      this.curRootIndex = 0
      if (this.accountList[index].select) {
        this.rootList.push(item)
      } else {
        let arr = this.rootList.filter(el => {
          return item.accounttypeId != el.accounttypeId
        })
        this.rootList = arr
      }
    },
  
  
    // 确定按钮---处理参数---提交数据---创建为0---草稿为1
    toCreateRole(delFlag) {
      let { merchantId, roleDesc, roleName } = this.ruleForm
      if(!merchantId){
        this.$message.error("请选择所属商户");
        return
      }
      if(!roleName){
         this.$message.error("请输入角色名称");
        return
      }
      if (this.$refs.productFrom.getSelectInfo().length == 0) {
        this.$message.error("请选择产品");
        return;
      }
       let accountTypes = JSON.stringify(this.$refs.productFrom.getSelectInfo())
      let para = {
        merchantId: merchantId,
        roleName: roleName,
        roleDescription: roleDesc,
        delFlag,
        requestList: accountTypes,
      }
      if(this.pageType=='edit'){
        para.id = this.editId
        this.updateRole(para)
      }else{
        this.insertRole(para)
      }
    },
    insertRole(para) {
      this.$apis.role.createRole(para).then(res => {
        if (res) {
          this.toBack()
        }
      })
    },
    updateRole(para) {
      this.$apis.role.updateRole(para).then(res => {
        if (res) {
          this.toBack()
        }
      })
    },
    toBack() {
       this.$router.push("/role/index");
    }
  }
}
</script>
<style lang="scss" scoped>
.role-create-page {
  height: 100%;
  width: 100%;
  background: #ffffff;
}
.top_title_widget {
  width: 100%;
  height: 58px;
  box-sizing: border-box;
  padding: 0 20px;
  border-bottom: 1px solid #d8d8d8;
  display: flex;
  align-items: center;
  .title {
    font-size: 16px;
    font-weight: 500;
    color: #424242;
  }
}
.main_widget {
  box-sizing: border-box;
  padding: 32px 0;
  background: #ffffff;
}
.fixed_bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  // width: calc(100% - 60px);
  width: 100%;
  height: 64px;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  .wrap_driver {
    margin: 0 40px;
  }
}
// 角色权限按钮样式
.custom_element {
  display: flex;
  align-items: center;
  .wrap {
    margin-right: 15px;
  }
}

</style>