look.vue 10.4 KB
<template>
  <div class="role-create-page">
    <div class="top_title_widget">
      <span class="title">角色基本信息</span>
      <span class="store_title">{{ roleInfo.merchantName }}</span>
    </div>
    <div class="info_widget">
      <div class="info_line_wrap">
        <div class="info_line_label">角色名称:</div>
        <div class="infl_line_con">{{ roleInfo.roleName }}</div>
      </div>
      <div class="info_line_wrap">
        <div class="info_line_label">角色号:</div>
        <div class="infl_line_con">{{ roleInfo.roleCode }}</div>
      </div>
      <div class="info_line_wrap">
        <div class="info_line_label">角色说明:</div>
        <div class="infl_line_con">{{ roleInfo.roleDescription || '-' }}</div>
      </div>
    </div>
    <div class="main_widget">
      <div class="main_title role-border-b">角色权限</div>
      <div class="wrapper role-border">
        <div class="menu_line_one role-border-b" :style="rootList.length ? '' : 'padding:0'">
          <div v-for="(item, index) in rootList" :key="index" class="line_element" :class="curRootIndex == index ? 'line_element_active' : ''" @click="selectRootItem(index)">
            {{ item.accounttypeName }}
            <div class="tab_line" v-show="curRootIndex == index"></div>
          </div>
        </div>
        <div class="main_body">
          <div class="menu_line_two">
            <div v-for="(item, index) in productList" :key="index" class="line_element_two role-border" :class="curProductIndex == index ? 'line_element_two_active' : ''" @click="selectProductItem(index)">{{ item.productName }}</div>
          </div>
          <div class="tree_widget role-border">
            <el-tree :render-content="renderContent" v-if="productList.length" default-expand-all :data="newTreeList" :props="defaultProps"></el-tree>
            <div v-if="productList.length == 0" class="no_data_icon">
              <div class="nodata_wrap">
                <img class="icon" src="../../assets/img/empty.png" />
                <span class="text">暂无数据</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      rootList: [],
      curRootIndex: 0,
      productList: [],
      curProductIndex: 0,
      roleInfo: {
        roleName: ''
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      newTreeList: []
    }
  },
  created() {
    let id = this.$route.params.id
    this.getRoleData(id)
    this.getMerchantTree(id)
  },
  methods: {
    // 获取角色信息
    getRoleData(id) {
      this.$apis.role.roleInfoData({ id }).then(res => {
        this.roleInfo = res
      })
    },
    // 获取商户树
    getMerchantTree(id) {
      this.$apis.role.roleTreeDataById({ id }).then(res => {
        this.rootList = res
        this.productList = this.rootList[0].products || []
        if (this.productList.length) {
          this.newTreeList = this.productList[0].merchantProTreeResponses || []
          this.handlerTreeDom()
        }
      })
    },
    selectRootItem(index) {
      this.curRootIndex = index
      this.productList = this.rootList[index].products || []
      this.curProductIndex = 0
      if (this.productList.length) {
        this.newTreeList = this.productList[0].merchantProTreeResponses || []
        this.handlerTreeDom()
      }
    },
    selectProductItem(index) {
      this.curProductIndex = index
      let productList = this.productList
      this.newTreeList = productList[index].merchantProTreeResponses || []
      this.handlerTreeDom()
    },
    toBack() {
      history.go(-1)
    },
    // 递归:处理某个值
    traverse(arr = [], count = 0) {
      count++
      arr.map(item => {
        if (item.children && item.children instanceof Array) {
          item.perms = false
          this.traverse(item.children, count)
        } else if (!item.children && count > 2) {
          item.perms = true
        }
        item.count = count
        return item
      })
      return arr
    },
    // 继续处理已经处理完的树
    toContiuneTree(arr = [], count = 0, maxLevel = 0) {
      count++
      arr.map(item => {
        if (item.children && item.children instanceof Array) {
          item.perms = false
          this.toContiuneTree(item.children, count, maxLevel)
        } else if (!item.children && count > maxLevel - 1) {
          item.perms = true
        }
        item.count = count
        return item
      })
      return arr
    },
    // 调用此方法去重构树节点
    handlerTreeDom() {
      this.newTreeList = this.traverse(this.newTreeList)
      let temp = this.flattenDeep(this.newTreeList)
      let maxval = Math.max.apply(
        Math,
        temp.map(item => {
          return item.count
        })
      )
      this.newTreeList = this.toContiuneTree(this.newTreeList, 0, maxval)
      this.maxval = maxval - 1
      this.$nextTick(() => {
        this.changeTreeClass(maxval)
      })
    },
    // 处理树横向
    renderContent(h, { node, data }) {
      let className = ''
      if (data.perms && data.count > this.maxval) {
        className = 'especially'
      }
      return <div class={className}>{data.label}</div>
    },
    // 改变tree节点样式
    changeTreeClass(maxval) {
      let piefix = (maxval - 1) * 20
      var classDomList = document.getElementsByClassName('especially')
      for (var i = 0; i < classDomList.length; i++) {
        classDomList[i].parentNode.style.cssText = 'float:left'
        // classDomList[i].parentNode.className = 'el-tree-node__content option-wrapper'
        classDomList[i].parentNode.parentNode.parentNode.style.marginLeft = `${piefix}px`
      }
    },
    flattenDeep(arr = [], childs = 'children', pid = 0) {
      return arr.reduce((flat, item) => {
        item.pid = pid
        return flat.concat(item, item[childs] ? this.flattenDeep(item[childs], childs, item.id) : [])
      }, [])
    }
  }
}
</script>
<style lang="scss" scoped>
.tab_line {
  width: 100%;
  height: 2px;
  background: #5850ff;
  border-radius: 4px;
  position: absolute;
  left: 50%;
  top: 38px;
  transform: translate(-50%, 0);
}
.role-create-page {
  height: 100%;
  width: 100%;
}
.top_title_widget {
  width: 100%;
  height: 58px;
  box-sizing: border-box;
  padding: 0 20px;
  border-bottom: 1px solid #d8d8d8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #ffffff;
  .title {
    font-size: 16px;
    font-weight: 600;
    color: #424242;
  }
  .store_title {
    font-size: 14px;
    font-weight: 500;
    color: #333333;
  }
}
.info_widget {
  background: #ffffff;
  height: 158px;
  box-sizing: border-box;
  padding: 20px 0 0 52px;
  // box-shadow: 0px 10px 20px 0px rgba(88, 80, 255, 0.1);
  .info_line_wrap {
    display: flex;
    align-items: center;
    margin-bottom: 24px;
    .info_line_label {
      width: 78px;
      font-size: 14px;
      font-weight: 400;
      color: #333333;
      text-align: right;
    }
    .infl_line_con {
      font-size: 14px;
      font-weight: 400;
      color: #666666;
    }
  }
}
.main_widget {
  margin-top: 20px;
  width: 100%;
  min-height: calc(100% - 300px);
  background: #ffffff;
  // box-shadow: 0px 10px 20px 0px rgba(88, 80, 255, 0.1);
  padding-bottom: 20px;
  .main_title {
    width: 100%;
    box-sizing: border-box;
    padding: 20px;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    // border-bottom: 1px solid #e5e5e5;
  }
  .wrapper {
    // border: 1px solid #e5e5e5;
    box-sizing: border-box;
    margin: 20px 20px;
    .menu_line_one {
      width: 100%;
      height: 48px;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      padding: 0px 10px;
      // border-bottom: 1px solid #e5e5e5;
      .line_element {
        position: relative;
        height: 32px;
        text-align: center;
        line-height: 32px;
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        margin: 0 20px;
        cursor: pointer;
      }
      .line_element_active {
        background: #ffffff;
        border-radius: 2px;
        color: #5850ff;
      }
    }
    .main_body {
      border-radius: 0 0 4px 4px;
      box-sizing: border-box;
      margin: 20px;
      .menu_line_two {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        .line_element_two {
          // width: 111px;
          padding: 0 38px;
          height: 40px;
          text-align: center;
          line-height: 40px;
          background: #fafafa;
          border-radius: 4px 4px 0px 0px;
          // border: 1px solid #e5e5e5;
          border-bottom: none;
          margin-right: 2px;
          color: #37383a;
          font-size: 14px;
          font-weight: 400;
          cursor: pointer;
        }
        .line_element_two_active {
          color: #5850ff;
          background-color: #ffffff;
        }
      }
      .tree_widget {
        width: 100%;
        // height: calc(100% - 40px);
        background: #ffffff;
        // border: 1px solid #e5e5e5;
        border-radius: 0px 4px 4px 4px;
        // height: 200px;
        // overflow: auto;
      }
      .tree_widget /deep/ .el-tree-node__expand-icon {
        // content: "\e6d9";
        color: #5850ff;
      }
      .tree_widget /deep/ .el-tree-node__expand-icon.is-leaf {
        color: transparent;
      }
      .no_data_icon {
        height: 150px;
        display: flex;
        align-items: center;
        justify-content: center;
        .nodata_wrap {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          .icon {
            width: 64px;
            height: 41px;
          }
          .text {
            font-size: 14px;
            font-weight: 400;
            color: #262626;
            margin-top: 10px;
          }
        }
      }
    }
  }
}
.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;
  }
}
.footer-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  width: 100%;
  bottom: 0px;
  height: 64px;
  background: #ffffff;
  z-index: 999;
  // box-shadow: 0px -1px 1px 0px rgba(88, 80, 255, 0.1);
}
/deep/ .el-tree {
  height: 640px;
  overflow: auto;
  padding: 0 20px;
  font-size: 14px;
}
</style>