create.vue 12.9 KB
<template>
  <div class="role-panel">
    <div class="role-create-page scrm-box-f">
      <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' && accountLevel != 4" label="所属商户" prop="merchantId">
            <el-select size="small" style="width: 40%" v-model="ruleForm.merchantId" clearable filterable placeholder="请选择" @change="selectMerchant">
              <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-if="pageType == 'create' && accountLevel == 4">
            <el-input size="small" disabled style="width: 40%" v-model="ruleForm.merchantName"></el-input>
          </el-form-item>
          <el-form-item label="所属商户" prop="merchantName" v-if="pageType == 'edit'">
            <el-input size="small" disabled style="width: 40%" v-model="ruleForm.merchantName"></el-input>
          </el-form-item>
          <el-form-item label="角色名称" prop="roleName">
            <el-input size="small" maxlength="20" style="width: 40%;padding-right:50px" v-model="ruleForm.roleName" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item label="角色说明" prop="roleDesc">
            <el-input size="small" maxlength="50" style="width: 40%;padding-right:50px" v-model="ruleForm.roleDesc" placeholder="请输入角色说明"></el-input>
          </el-form-item>
          <!-- <el-form-item label="角色权限" prop="roleRoot">
          <div class="custom_element">
            <template v-for="(item, index) in accountListSelect">
              <div :key="index" class="wrap">
                <AutoButton @getBtnStatus="selectCurAccount(item, index)" :typeStatus="item.select" :btnLabel="item.accounttypeName"></AutoButton>
              </div>
            </template>
          </div>
        </el-form-item> -->
          <el-row>
            <el-col :span="16">
              <div data-v-995a4ff0="" class="el-form-item is-required">
                <label for="roleName" class="el-form-item__label" style="width: 100px">角色权限</label>
                <div class="el-form-item__content" style="margin-left: 100px">
                  <!-- <div data-v-995a4ff0="" class="el-input el-input--small">
                    <auto-button v-for="(item, index) in accountDetail.accountTypes" :key="index" :btnLabel="item.accounttypeName" :btnValue="item.accounttypeId" :stopClick="nextStatus" @getBtnStatus="getBtnStatus"></auto-button>
                  </div> -->
                  <div class="custom_element">
                    <template v-for="(item, index) in accountListSelect">
                      <div :key="index" class="wrap">
                        <AutoButton @getBtnStatus="selectCurAccount(item, index)" :typeStatus="item.select" :btnLabel="item.accounttypeName"></AutoButton>
                      </div>
                    </template>
                  </div>
                  <div class="el-form-item__error" v-if="roleRootRules">请选择角色权限</div>
                </div>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="scrm-box-f" style="margin-top: 10px">
      <SelectProduct ref="productFrom" :roleType="true" title="角色权限"></SelectProduct>
      <!-- <div style="height: 84px"></div> -->
      <div
        class="fixed_bottom"
        :style="{
          left: footerLeft + 'px',
          width: footerWidth + 'px'
        }"
      >
        <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>
  </div>
</template>

<script>
import AutoButton from '../../components/auto-button/index.vue'
import SelectProduct from './components/selectProduct.vue'
export default {
  name: '',

  components: {
    AutoButton,
    SelectProduct
  },

  data() {
    return {
      roleRootRules: false,
      accounttypeIds: [],
      ruleForm: {
        merchantId: '',
        roleName: '',
        roleDesc: ''
      },
      rules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        merchantId: [{ required: true, message: '请选择所属商户', trigger: 'change' }]
      },
      rootList: [],
      curRootIndex: 0,
      pageName: '新增角色',
      roleInfo: null,
      pageType: 'create',
      accountListSelect: [],
      //商户
      merchantList: [],
      // tree数据
      treeDataList: [],
      accountLevel: 1
    }
  },

  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.selectMerchant(res.merchantId,res.responseList)
        // this.getMerchantTree(id, res.responseList)
        this.$refs.productFrom.accountTypeSearchRequest({ id: res.merchantId }, res.responseList)
        this.rootList = res.responseList || []
        // if (res.delFlag == 1) {
        //   this.selectMerchant(res.merchantId,res.responseList)
        // }
      })
    },
    // 获取商户树
    getMerchantTree(id, list) {
      this.$apis.role.roleTreeDataById({ id }).then(res => {
        console.log(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.accountListSelect = res
        console.log(this.accountListSelect)
      })
    },
    getMerchantName() {
      this.$apis.role.merchantDataList({ source: 'glsx' }).then(res => {
        this.merchantList = res
        if (this.accountLevel == 4) {
          this.ruleForm.merchantId = this.merchantList[0].id
          this.selectMerchant(this.merchantList[0].id)
        }
        // 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
    //   }
    // },
    // 选择商户----获取权限
    async selectMerchant(id,list) {
      let res = await this.$refs.productFrom.findMenuFunTreeByMerId({ id })
      this.accountListSelect = res.map(item => {
        item.select = false
        return item
      })
      if(this.pageType=='edit'){
         this.accountListSelect =  this.accountListSelect.map(item=>{
           list.forEach(key=>{
             if( item.accounttypeId==key.accounttypeId){
               item.select = true
             }
           })
           return item
         })
      }
      // this.rootList = this.accountListSelect
    },
    // 选择商户权限
    selectCurAccount(item, index) {
      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)
      })
      console.log(this.rootList, accounttypeId, '99999')
      this.accounttypeIds = accounttypeId
      this.$refs.productFrom.findMenuFunTree(item.accounttypeId, item.select)
    },

    // 确定按钮---处理参数---提交数据---创建为0---草稿为1
    toCreateRole(delFlag) {
      let { merchantId, roleDesc, roleName } = this.ruleForm
      if (delFlag == 1) {
        if (!merchantId) {
          this.$refs.ruleForm.validateField('merchantId')
          return
        }

        if (!roleName && this.$refs.productFrom.getSelectInfo().length == 0) {
          this.$message.error('角色和权限至少选择一项')
          return
        }
        // this.roleRootRules = false
        // if (this.$refs.productFrom.getSelectInfo().length == 0) {
        //   this.roleRootRules = true
        //   return
        // }
        // if (this.$refs.productFrom.productTab === '') {
        //   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)
        }
      } else {
        this.$refs.ruleForm.validate(valid => {
          if (valid) {
            let accountTypes = JSON.stringify(this.$refs.productFrom.getSelectInfo())
            this.roleRootRules = false
            if (this.$refs.productFrom.getSelectInfo().length == 0) {
              this.roleRootRules = true
              return
            }
            let product = this.$refs.productFrom.getSelectInfo()
            if (product && product.every(item => item.productList.length == 0)) {
              this.$message.error('请选择产品')
              return
            }
              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)
              }
            // }
          }
        })
        // console.log('外面', this.$refs.productFrom.getSelectInfo().length, this.$refs.productFrom.productTab)
        // this.roleRootRules = false
        // if (this.$refs.productFrom.getSelectInfo().length == 0) {
        //   this.roleRootRules = true
        //   return
        // }
        // if (this.$refs.productFrom.productTab === '') {
        //   this.$message.error('请至少选择一项产品')
        //   return
        // }
      }
    },
    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')
    }
  },

  async created() {
    const { session } = this.$globalState
    this.accountLevel = session.accountLevel
    if (this.accountLevel == 4) {
      this.ruleForm.merchantName = session.merchantName
    }
    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
      // document.getElementsByClassName('el-breadcrumb__item')[3].innerText='修改角色'
      // document.getElementById('tab-/account/role/create').innerHTML='修改角色'
    }
  },

  mounted() {
    this.footerLeft = document.querySelector('.base-container-right').offsetLeft
    this.footerWidth = document.documentElement.clientWidth - this.footerLeft
    // if(this.accountLevel==4){
    //   this.selectMerchant(this.ruleForm.merchantId)
    // }
  }
}
</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;
  width: 100%;
  bottom: 0px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  z-index: 999;
  box-shadow: 0px -1px 1px 0px rgba(88, 80, 255, 0.1);
  .wrap_driver {
    margin: 0 40px;
  }
}
// 角色权限按钮样式
.custom_element {
  display: flex;
  align-items: center;
  .wrap {
    margin-right: 15px;
  }
}
</style>