addOrEdit.vue 14.2 KB
<template>
  <div>
    <el-dialog :title="dialogTitle" :visible.sync="groupVisible" width="50%" :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分群名称" prop="crowdGroupName" size="small">
          <el-input v-model="ruleForm.crowdGroupName" placeholder="请输入分群名称" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="分群描述" size="small">
          <el-input v-model="ruleForm.crowdGroupDesc" placeholder="请输入分群描述" maxlength="100"></el-input>
        </el-form-item>
        <!-- <el-form-item label="概览是否展示" size="small">
          <el-radio-group v-model="ruleForm.isDisplay">
            <el-radio label="1">是</el-radio>

            <el-radio label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择展示位" size="small" v-show="ruleForm.expand == 1">
          <el-select v-model="ruleForm.expandNum" placeholder="请选择">
            <el-option v-for="item in 12" :key="item" :label="`${item}号展示位`" :value="item"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="选择人群" size="small">
          <div class="group-tree-container">
            <div class="group-tree-panel" :style="{ height: heightBox + 'px' }">
              <template v-for="(item, index) in crowdUnitList">
                <div :key="item" v-if="item.selectGroup.length != item.children.length">
                  <span class="el-tree-node__expand-icon el-icon-caret-right" :class="{ expanded: item.checked }" @click.self="handleExpand(index)"></span>
                  <el-checkbox :indeterminate="item.isIndeterminateGroup" v-model="item.gruopCheckAll" @change="handleGroup(item.gruopCheckAll, index)">{{ item.crowdName }}</el-checkbox>
                  <el-checkbox-group :style="{ 'max-height': item.checked ? heightBox - 70 + 'px' : '0px' }" v-model="item.groupArr" @change="handleCheckedGroup(item.groupArr, index)" v-show="item.checked">
                    <template v-for="crowd in item.children">
                      <el-checkbox v-if="isChecked(crowd, item)" :label="crowd" :key="crowd.crowdId">{{ crowd.crowdName }}</el-checkbox>
                    </template>
                  </el-checkbox-group>
                </div>
              </template>
            </div>
            <div class="group-tree-operation">
              <div :class="[{ 'group-tree-btn': true, active: rightSelectNum }]" @click="handleRight"><i class="el-icon-arrow-right"></i></div>
              <div :class="[{ 'group-tree-btn': true, active: leftSelectNum }]" @click="handleLeft"><i class="el-icon-arrow-left"></i></div>
            </div>
            <div class="group-tree-panel" :style="{ height: heightBox + 'px' }">
              <template v-for="(item, index) in crowdUnitList">
                <div :key="item" v-if="item.selectGroup.length > 0">
                  <span class="el-tree-node__expand-icon el-icon-caret-right" :class="{ expanded: item.checked_right }" @click.self="handleExpandChecked(index)"></span>
                  <el-checkbox :indeterminate="item.isIndeterminateGroup_right" v-model="item.gruopCheckAll_right" @change="handleGroupChecked(item.gruopCheckAll_right, index)">{{ item.crowdName }}</el-checkbox>
                  <el-checkbox-group :style="{ 'max-height': item.checked_right ? heightBox - 70 + 'px' : '0px' }" v-model="item.groupArr_right" @change="handleCheckedGroupChecked(item.groupArr_right, index)" v-show="item.checked_right">
                    <template v-for="crowd in item.children">
                      <el-checkbox v-if="!isChecked(crowd, item)" :label="crowd" :key="crowd.crowdId">{{ crowd.crowdName }}</el-checkbox>
                    </template>
                  </el-checkbox-group>
                </div>
              </template>
            </div>
            <div class="el-form-item__error" v-show="crowdStatus">
              请选择人群
            </div>
            <!-- <div class="group-tree-panel" :style="{ height: heightBox + 'px' }">
              <el-tree @node-expand="nodeExpand" accordion :data="crowdUnitList" :props="defaultProps" show-checkbox></el-tree>
            </div> -->
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="groupVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="handleSelect">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'addGrouping',
  data() {
    return {
      heightBox: 0,
      rightSelectNum: 0,
      leftSelectNum: 0,
      groupVisible: false,
      crowdStatus: false,
      dialogTitle: '新建分群',
      ruleForm: {},
      rules: {
        crowdGroupName: [{ required: true, message: '请输入分群名称', trigger: 'blur' }]
      },
      crowdUnitList: [],
      crowdUnitListReset: []
    }
  },
  watch: {
    crowdUnitList: {
      handler(val) {
        this.crowdStatus = false
      },
      deep: true
    }
  },
  props: {
    // dialogTitle: {
    //   type: String,
    //   default: '新建分群'
    // }
  },
  mounted() {
    let clientHeight = document.documentElement.clientHeight
    this.heightBox = clientHeight - 630
    this.getCrowdList()
  },
  methods: {
    async getCrowdList() {
      const commonParam = { pageSize: 1000, pageNum: 1, merchantId: this.$globalState.session.merchantId }
      const params1 = {
        req: {
          crowdType: 1,
          ...commonParam
        }
      }
      const params2 = {
        req: {
          ...commonParam,
          crowdType: 2
        }
      }
      const crowdUnitList1 = this.$apis.crowdManage.getCrowdList(params1)
      const crowdUnitList2 = this.$apis.crowdManage.getCrowdList(params2)
      const crowdUnitList = await Promise.all([crowdUnitList1, crowdUnitList2])
      /*
        groupArr: 勾选的人群
        checked: 是否展开
        isIndeterminateGroup:树节点选中
        gruopCheckAll: 全选
        selectGroup:右边树的选中数据
      */
      this.crowdUnitList = [
        { crowdName: '推荐人群', children: crowdUnitList[0].data.crowdUnitList, groupArr: [], groupArr_right: [], checked: false, checked_right: false, isIndeterminateGroup: false, isIndeterminateGroup_right: false, gruopCheckAll: false, gruopCheckAll_right: false, selectGroup: [] },
        { crowdName: '自定义人群', children: crowdUnitList[1].data.crowdUnitList, groupArr: [], groupArr_right: [], checked: false, checked_right: false, isIndeterminateGroup: false, isIndeterminateGroup_right: false, gruopCheckAll: false, gruopCheckAll_right: false, selectGroup: [] }
      ]
      this.crowdUnitListReset = this.$common.utils.deepClone(this.crowdUnitList)
    },
    //判断是否选中
    isChecked(crowd, item) {
      return !item.selectGroup.includes(crowd)
    },
    //推送右侧
    handleRight() {
      if (this.rightSelectNum == 0) {
        return
      }
      this.crowdUnitList.forEach(group => {
        group.selectGroup = group.selectGroup.concat(group.groupArr)
        group.groupArr = []
        group.gruopCheckAll = false
        group.gruopCheckAll_right = false
        if (group.groupArr_right.length) {
          group.isIndeterminateGroup_right = true
        }
        group.isIndeterminateGroup = false
        this.rightSelectNum = 0
      })
    },
    //推送左侧
    handleLeft() {
      if (this.leftSelectNum == 0) {
        return
      }
      this.crowdUnitList.forEach(group => {
        group.selectGroup = this.getReduceGroup(group.selectGroup, group.groupArr_right, 'crowdId')
        group.groupArr_right = []
        group.gruopCheckAll = false
        group.gruopCheckAll_right = false
        if (group.groupArr.length) {
          group.isIndeterminateGroup = true
        }
        group.isIndeterminateGroup_right = false
        this.leftSelectNum = 0
      })
    },
    handleSelect() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          let crowdArr = []
          this.crowdUnitList.forEach(crowd => {
            Object.assign(crowdArr, crowd.selectGroup)
          })
          if (crowdArr.length == 0) {
            this.crowdStatus = true
            return
          }
          const crowdIdList = []
          crowdArr.forEach(arr => {
            crowdIdList.push(arr.crowdId)
          })
          const params = {
            req: {
              merchantId: this.$globalState.session.merchantId,
              crowdGroupType: 2,
              crowdIdList
            }
          }
          Object.assign(params.req, this.ruleForm)
          let crowdGroup
          if (this.ruleForm.crowdGroupId) {
            crowdGroup = 'editCrowdGroup'
          } else {
            crowdGroup = 'addCrowdGroup'
          }
          this.$apis.crowdManage[crowdGroup](params).then(res => {
            this.groupVisible = false
            this.$parent.searchTable()
          })
        }
      })
    },
    init(row) {
      this.rightSelectNum = 0
      this.leftSelectNum = 0
      this.crowdUnitList = this.$common.utils.deepClone(this.crowdUnitListReset)
      this.ruleForm = {
        isDisplay: 2
      }

      if (row) {
        this.ruleForm.crowdGroupId = row.crowdGroupId
        this.dialogTitle = '编辑分群'
        this.queryCrowdGroupDetailsByCrowdGroupId(row)
      } else {
        this.dialogTitle = '新建分群'
      }
      this.groupVisible = true
    },
    queryCrowdGroupDetailsByCrowdGroupId(row) {
      const { crowdGroupName, crowdGroupDesc, crowdGroupId, crowdUnitList } = row
      this.ruleForm = {
        crowdGroupName,
        crowdGroupDesc,
        crowdGroupId
      }
      const arr1 = [],
        arr2 = []
      const crowdId1 = [],
        crowdId2 = []
      this.crowdUnitList[0].children.forEach(item => {
        crowdId1.push(item.crowdId)
      })
      this.crowdUnitList[1].children.forEach(item => {
        crowdId2.push(item.crowdId)
      })
      crowdUnitList.forEach(item => {
        if (crowdId1.includes(item.crowdId)) {
          arr1.push(item.crowdId)
        }
        if (crowdId2.includes(item.crowdId)) {
          arr2.push(item.crowdId)
        }
      })
      console.log(arr1, arr2)
      this.setTreeChecked(arr1, arr2)
    },
    setTreeChecked(arr1, arr2) {
      this.crowdUnitList[0].children.forEach(item => {
        if (arr1.includes(item.crowdId)) {
          this.crowdUnitList[0].selectGroup.push(item)
        }
      })
      this.crowdUnitList[1].children.forEach(item => {
        if (arr2.includes(item.crowdId)) {
          this.crowdUnitList[1].selectGroup.push(item)
        }
      })
    },
    //左侧展开
    handleExpand(index) {
      this.crowdUnitList.forEach((item, idx) => {
        if (idx == index) {
          item.checked = !item.checked
        } else {
          item.checked = false
        }
      })
    },
    //右侧展开
    handleExpandChecked(index) {
      this.crowdUnitList.forEach((item, idx) => {
        if (idx == index) {
          item.checked_right = !item.checked_right
        } else {
          item.checked_right = false
        }
      })
    },
    getReduceGroup(arr1, arr2, identification) {
      const selectGroupId = []
      arr2.forEach(group => {
        selectGroupId.push(group[identification])
      })
      return arr1.filter(child => !selectGroupId.includes(child[identification]))
    },
    //左侧全选
    handleGroup(val, index) {
      const surplusArr = this.getReduceGroup(this.crowdUnitList[index].children, this.crowdUnitList[index].selectGroup, 'crowdId')
      this.crowdUnitList[index].groupArr = val ? surplusArr : []
      this.crowdUnitList[index].isIndeterminateGroup = false
      this.rightShow()
    },
    //左侧单选
    handleCheckedGroup(value, index) {
      const surplusArr = this.getReduceGroup(this.crowdUnitList[index].children, this.crowdUnitList[index].selectGroup, 'crowdId')
      let checkedCount = value.length
      this.crowdUnitList[index].gruopCheckAll = checkedCount === surplusArr.length
      this.crowdUnitList[index].isIndeterminateGroup = checkedCount > 0 && checkedCount < surplusArr.length
      this.rightShow()
    },
    //按钮判断
    rightShow() {
      let selectNum = 0
      this.crowdUnitList.forEach(item => {
        selectNum += item.groupArr.length
      })
      this.rightSelectNum = selectNum
    },
    //按钮判断
    leftShow() {
      let selectNum = 0
      this.crowdUnitList.forEach(item => {
        selectNum += item.groupArr_right.length
      })
      this.leftSelectNum = selectNum
    },
    //右侧全选
    handleGroupChecked(val, index) {
      this.crowdUnitList[index].groupArr_right = val ? this.crowdUnitList[index].selectGroup : []
      this.crowdUnitList[index].isIndeterminateGroup_right = false
      this.leftShow()
    },
    //右侧全选
    handleCheckedGroupChecked(value, index) {
      let checkedCount = value.length
      this.crowdUnitList[index].gruopCheckAll_right = checkedCount === this.crowdUnitList[index].selectGroup.length
      this.crowdUnitList[index].isIndeterminateGroup_right = checkedCount > 0 && checkedCount < this.crowdUnitList[index].selectGroup.length
      this.leftShow()
    }
  }
}
</script>

<style lang="scss" scoped>
.group-tree-container {
  display: flex;
  .group-tree-operation {
    width: 32px;
    margin: 0px 16px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .group-tree-btn {
      width: 32px;
      height: 32px;
      background: #f7f7f7;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      text-align: center;
      line-height: 32px;
      cursor: pointer;
      &.active {
        background: #5850ff;
        color: #ffffff;
      }
      &:first-child {
        margin-bottom: 16px;
      }
    }
  }
  .group-tree-panel {
    width: 50%;
    border-radius: 2px;
    padding: 8px;
    border: 1px solid #eeeeee;
    .el-tree-node__expand-icon {
      font-size: 16px;
      color: #000000;
    }
    /deep/ {
      .el-checkbox-group {
        margin-left: 30px;
        overflow-y: auto;
        animation: all ease-in 0.5s;
        .el-checkbox {
          display: block;
        }
      }
    }
  }
}
</style>