selectProduct.vue 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
<template>
  <div class="account-body" v-loading="loading" v-if="tabMenuStatus">
    <!-- <div class="account-title">{{ title }}</div> -->
    <el-tabs v-if="tabMenu.length != 0" v-model="tabName" @tab-click="handleClik">
      <template v-for="(item, index) in tabMenu">
        <el-tab-pane :label="item.accounttypeName" :name="item.accounttypeId + ''" v-if="item && item.accounttypeId" :key="index"></el-tab-pane>
      </template>
    </el-tabs>
    <div class="account-select-pro" v-for="(item, index) in tabMenu" :key="index" v-show="tabName == item.accounttypeId">
      <label>选择产品:</label>
      <el-checkbox-group v-model="item.productsArr" @change="selectPro">
        <el-checkbox v-for="(itm, idx) in item.products" :label="itm" :key="idx">{{ itm.productName }}</el-checkbox>
      </el-checkbox-group>
    </div>
    <div class="account-select-menu">选择产品权限:</div>
    <div class="menu-box">
      <div class="menu-box-panel">
        <div :key="index" v-for="(item, index) in tabMenu">
          <div class="menu-box-tab" v-show="tabName == item.accounttypeId">
            <div v-show="item.productsArr.indexOf(itm) != -1" class="tab-item text-hide" :class="{ 'select-checked': productTab == itm.productId }" @click="productClick(itm.productId)" v-for="(itm, idx) in item.products" :key="idx">
              {{ itm.productName }}
            </div>
          </div>
          <div v-show="tabName == item.accounttypeId" class="menu-box-tree" v-if="item.products">
            <div v-for="(itm, idx) in item.products" :key="idx">
              <div class="nodata" v-if="item.productsArr.length == 0 || itm.merchantProTreeResponses.length == 0">
                <img src="../../../assets/img/empty.png" alt="" />
                <div>
                  {{ item.productsArr.length == 0 ? '请先选择产品' : '暂无菜单权限' }}
                </div>
              </div>
              <div v-else>
                <el-tree v-show="productTab == itm.productId" :data="itm.merchantProTreeResponses" show-checkbox check-strictly check-on-click-node :expand-on-click-node="false" default-expand-all node-key="id" :class="'tree_' + item.accounttypeId + '_' + itm.productId" :ref="`tree_${item.accounttypeId}_${itm.productId}`" highlight-current :render-content="renderContent" :props="defaultProps" @check="clickDeal"></el-tree>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import util from '@/utils/util'
export default {
  name: 'product',
  props: {
    title: {
      type: String
    },
    roleType: {
      type: Boolean
    }
  },
  data() {
    return {
      loading: false,
      tabMenuStatus: false,
      treeHeight: 450,
      tabMenu: [],
      tabMenu_r: [],
      tabMenu_x: [],
      tabMenu_s: [],
      accountList: [],
      selectIndex: 0,
      productId: 0,
      products: [],
      productsArr: [],
      tabName: '',
      productTab: '',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      accountTypes: []
    }
  },
  watch: {
    tabMenu: {
      handler() {
        this.tabMenuStatus = this.tabMenu.some(item => item)
      },
      deep: true
    }
  },
  methods: {
    handleClik(tab, event) {
      const tabId = tab.name
      const productTab = this.tabMenu.find(i => i.accounttypeId == tabId)
      this.productTab = productTab.productsArr[0] ? productTab.productsArr[0].productId : ''
    },
    selectPro(data) {
      this.$parent.roleRootRules = false
      this.$parent.editStatus = true
      this.productTab = data.length != 0 ? data[data.length - 1].productId : ''
      const { tabName, tabMenu, productTab } = this
      // 设置全选
      var merchantProTreeResponses = tabMenu.find(i => i.accounttypeId == tabName).products.find(j => j.productId == productTab)
      var arr = merchantProTreeResponses ? merchantProTreeResponses.merchantProTreeResponses : ''
      this.$refs[`tree_${tabName}_${productTab}`] ? this.$refs[`tree_${tabName}_${productTab}`][0].setCheckedKeys(this.getAllId(arr)) : ''
      this.$nextTick(() => {
        this.addStyle()
      })
    },
    productClick(productId) {
      this.productTab = productId
    },
    findMenuFunTree(id, status) {
      let menu = util.deepClone(this.tabMenu)
      let tabMenu = util.deepClone(this.tabMenu_r)
      let menuSelect = tabMenu.filter(item => item.accounttypeId == id)
      let idx = tabMenu.findIndex(item => item.accounttypeId == id)
      this.tabMenu_r.forEach((item, index) => {
        if (menu.length == 0) {
          this.tabMenu[index] = ''
          return
        }
        menu.forEach(itm => {
          if (itm.accounttypeId != item.accounttypeId) {
            if (!this.tabMenu[index]) {
              this.tabMenu[index] = ''
            }
          }
          if (itm.accounttypeId == item.accounttypeId) {
            if (!this.tabMenu[index]) {
              this.tabMenu[index] = item
            }
          }
        })
      })
      if (status) {
        this.$set(this.tabMenu, idx, menuSelect[0])
        this.tabName = this.tabMenu[idx] ? this.tabMenu[idx].accounttypeId + '' : ''
      } else {
        this.$set(this.tabMenu, idx, '')
        let accounttypeId = this.tabMenu.find(item => item)
        this.tabName = accounttypeId ? accounttypeId.accounttypeId + '' : ''
      }
      this.tabMenuStatus = this.tabMenu.some(item => item)
    },
    // 获取当前的所有权限
    async findMenuFunTreeByMerId(params) {
      let tabMenu = await this.$apis.store.findMenuFunTreeByMerId(params)
      tabMenu = tabMenu.map(item => {
        return {
          productsArr: [],
          products: item.products,
          accounttypeName: item.accounttypeName,
          accounttypeId: item.accounttypeId
        }
      })
      this.tabMenu_r = util.deepClone(tabMenu)
      this.accountTypes = tabMenu.map(item => {
        return {
          products: null,
          accounttypeId: item.accounttypeId
        }
      })
      this.tabName = this.tabMenu_r[0].accounttypeId
      return this.tabMenu_r
    },
    // 获取当前的所有权限
    async accountTypeSearchRequest(params, arr) {
      let account = ''
      let tabMenu = await this.$apis.store.findMenuFunTreeByMerId(params)
      this.accountList = tabMenu.map(item => {
        return {
          productsArr: [],
          products: item.products,
          accounttypeName: item.accounttypeName,
          accounttypeId: item.accounttypeId
          // merchantProTreeResponses: item.merchantProTreeResponses,
          // productId: ''
        }
      })
      this.$nextTick(() => {
        this.addStyle()
      })
      this.accountTypes = tabMenu.map(item => {
        return {
          products: null,
          accounttypeId: item.accounttypeId
        }
      })
      this.tabMenu_s = util.deepClone(this.accountList)
      // this.tabName = this.tabMenu[0].accounttypeId
      this.findPermissionsByMerId(arr)
      this.tabMenu_r = util.deepClone(this.accountList)
      return this.accountList
    },
    // 获取已选择的权限
    findPermissionsByMerId(arr) {
      let accounttypeId = []
      arr.forEach(item => {
        accounttypeId.push(item.accounttypeId)
        this.accountList.forEach((itm, index) => {
          if (accounttypeId.includes(itm.accounttypeId)) {
            this.tabMenu[index] = itm
            if (!item.productList) {
              return
            }
            item.productList.forEach(i => {
              itm.products.forEach(j => {
                if (i.productId == j.productId) {
                  itm.productsArr.push(j)
                  this.$nextTick(() => {
                    setTimeout(() => {
                      this.addStyle()
                      this.$refs[`tree_${itm.accounttypeId}_${j.productId}`] ? this.$refs[`tree_${itm.accounttypeId}_${j.productId}`][0].setCheckedKeys(i.idList) : ''
                    }, 300)
                  })
                }
              })
            })
          } else {
            this.tabMenu[index] = ''
          }
        })
      })
      this.tabMenuStatus = this.tabMenu.some(item => item)
      let tabMenu = this.tabMenu.filter((item, index) => {
        return item && item.accounttypeId && this.tabMenu[index].productsArr.length > 0
      })
      setTimeout(() => {
        this.tabName = tabMenu[0].accounttypeId
        this.productTab = tabMenu[0].productsArr[0].productId
      }, 100)
    },
    resetView() {
      this.tabMenu = []
      this.accountList = util.deepClone(this.tabMenu_s)
      this.tabMenu_x.forEach(item => {
        this.accountList.forEach(itm => {
          if (item.accounttypeId == itm.accounttypeId) {
            this.tabMenu.push(itm)
            item.products.forEach(i => {
              itm.products.forEach(j => {
                if (i.productId == j.productId) {
                  itm.productsArr.push(j)
                  this.$nextTick(() => {
                    this.addStyle()
                    this.$refs[`tree_${itm.accounttypeId}_${j.productId}`] ? this.$refs[`tree_${itm.accounttypeId}_${j.productId}`][0].setCheckedKeys(i.menuIds) : ''
                  })
                }
              })
            })
          }
        })
      })
    },
    // 渲染树结构
    renderContent(h, v) {
      if (v.data.logo == 'functionValue') {
        return (
          <div class="custom-tree-node fun-tree-node">
            <span>{v.node.label}</span>
          </div>
        )
      } else {
        return (
          <div class="custom-tree-node">
            <span>{v.node.label}</span>
          </div>
        )
      }
    },
    addStyle() {
      const nodes = document.getElementsByClassName('fun-tree-node')
      for (let i = 0; i < nodes.length; i++) {
        const parentNode = nodes[i].parentNode
        parentNode.style.float = 'left'
      }
    },
    clickDeal(currentObj, treeStatus) {
      this.$parent.editStatus = true
      // 用于:父子节点严格互不关联时,父节点勾选变化时通知子节点同步变化,实现单向关联。
      let selected = treeStatus.checkedKeys.indexOf(currentObj.id) // -1未选中
      // 选中
      if (selected !== -1) {
        // 子节点只要被选中父节点就被选中
        this.selectedParent(currentObj)
        // 统一处理子节点为相同的勾选状态
        this.uniteChildSame(currentObj, true)
      } else {
        // 未选中 处理子节点全部未选中
        console.log(currentObj)
        if (currentObj.children !== null) {
          if (currentObj.children.length !== 0) {
            this.uniteChildSame(currentObj, false)
          }
          //放开时为当子节点全为未选中时父节点也变为未选中状态   注释后就是父节点不和子节点强制绑定
          // this.uniteParentSame(currentObj.id,treeStatus)//当子节点全为未选中时父节点也变为未选中状态
        }
        // else {
        //     this.uniteParentSame(currentObj.id,treeStatus)
        // }
      }
    },
    uniteParentSame(id, treeStatus) {
      //当子节点全为未选中时父节点也变为未选中状态
      const tree_ref = `tree_${this.tabName}_${this.productTab}`
      let node = this.$refs[tree_ref][0].getNode(id) //获取当前节点的节点树
      if (node.parent !== null && node.parent !== undefined) {
        let parentNode = node.parent.data //获取当前节点的父节点树
        if (parentNode != undefined) {
          //判断父节点是否存在
          for (let i = 0; i < parentNode.children.length; i++) {
            let checkedKeys = this.$refs[tree_ref][0].getCheckedKeys()
            let hafCheckedKeys = this.$refs[tree_ref][0].getHalfCheckedKeys()
            let selectNodes = checkedKeys.concat(hafCheckedKeys) //获取已选择树节点
            let selected = selectNodes.indexOf(parentNode.children[i].id) // -1当前节点的同级节点是否全部未选中
            if (selected !== -1) {
              return
            }
          }
        } else {
          return
        }
        this.$refs[tree_ref][0].setChecked(parentNode.id, false) //修改节点为未选择
        if (node.level > 2) {
          //判断是否是最上级节点
          this.uniteParentSame(parentNode.id, treeStatus)
        }
      } else {
        return
      }
    },
    // 统一处理子节点为相同的勾选状态
    uniteChildSame(treeList, isSelected) {
      const tree_ref = `tree_${this.tabName}_${this.productTab}`
      this.$refs[tree_ref][0].setChecked(treeList.id, isSelected)
      if (treeList.children !== null) {
        for (let i = 0; i < treeList.children.length; i++) {
          this.uniteChildSame(treeList.children[i], isSelected)
        }
      }
    },
    // 统一处理父节点为选中
    selectedParent(currentObj) {
      const tree_ref = `tree_${this.tabName}_${this.productTab}`
      let currentNode = this.$refs[tree_ref][0].getNode(currentObj)
      if (currentNode.parent.key !== undefined) {
        this.$refs[tree_ref][0].setChecked(currentNode.parent, true)
        this.selectedParent(currentNode.parent)
      }
    },
    // 获取所有节点的id
    getAllId(data, arr = []) {
      if (data) {
        data.forEach(item => {
          arr.push(item.id)
          if (item.children && item.children.length != 0) {
            item.children.forEach(i => {
              arr.push(i.id)
              this.getAllId(i.children, arr)
            })
          }
        })
      }
      return arr
    },
    getSelectInfo() {
      this.tabMenu = this.tabMenu.filter(item => item)
      this.accountTypes = this.tabMenu.map(item => {
        let products = []
        let productList = []
        item.productsArr.forEach(itm => {
          const checkedNodes = this.$refs[`tree_${item.accounttypeId}_${itm.productId}`] ? this.$refs[`tree_${item.accounttypeId}_${itm.productId}`][0].getCheckedNodes() : []
          const menuIdList = checkedNodes
            // .filter((item) => item.logo === "menu")
            .filter(item => item.logo !== 'function' && item.logo !== 'functionValue' && item.logo !== 'neglect')
            .map(i => i.id) //   全选菜单idlist
          // const partSelectList = halfCheckedNodes.filter(item => item.logo === 'menu').map(i => i.id) // 半选菜单idlist
          const functionIdList = checkedNodes.filter(item => item.logo === 'function')
          const functionValueIdLists = checkedNodes.filter(item => item.logo === 'functionValue')
          var list = []
          /** { menuId, id, functionValue} */
          if (functionValueIdLists.length !== 0) {
            functionValueIdLists.forEach((j, index) => {
              if (list.map(item => item.id).includes(j.parentId)) {
                const obj = list.find(k => k.id === j.parentId)
                obj.functionValue += ';' + j.id + ':' + j.label
              } else {
                list.push({
                  id: j.parentId,
                  menuId: j.superParentId,
                  functionValue: j.id + ':' + j.label
                })
              }
            })
          }
          if (functionIdList.length !== 0) {
            functionIdList.forEach(i => {
              list.push({
                id: i.parentId,
                menuId: i.superParentId
              })
            })
          }
          if (this.roleType) {
            productList.push({
              productId: itm.productId,
              functionIdList: list,
              // functionValue: list,
              menuIdList: menuIdList
            })
          } else {
            products.push({
              productId: itm.productId,
              functionIds: list,
              // functionValue: list,
              menuIds: menuIdList
            })
          }
        })
        if (this.roleType) {
          return {
            accounttypeId: item.accounttypeId,
            productList
          }
        } else {
          return {
            accounttypeId: item.accounttypeId,
            products
          }
        }
      })
      return this.accountTypes
    }
  },
  mounted() {
    // this.accountTypeSearchRequest();
    // this.findPermissionsByMerId()
  }
}
</script>

<style scoped lang="scss">
.account-body {
  border: 1px solid #e5e5e5;
  border-radius: 4px;
  /deep/ {
    .el-checkbox:last-of-type {
      margin-right: 8px;
    }
    .el-tabs {
      border-bottom: 1px solid #e5e5e5;
      margin-bottom: 20px;
    }
    .el-tabs__header {
      margin: 0;
      padding: 0 30px;
    }
    .el-tabs__nav-wrap::after {
      content: none;
    }
    .el-form-item {
      margin-bottom: 0 !important;
    }
  }
  .account-title {
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    line-height: 16px;
    letter-spacing: 1px;
    margin-bottom: 30px;
  }
  .account-select-pro {
    padding-left: 20px;
    font-size: 14px;
    font-weight: 500;
    color: #333333;
    line-height: 20px;
    display: flex;
    flex-wrap: wrap;
    // margin-top: 12px;
    label {
      margin-right: 20px;
    }
  }
  .account-select-menu {
    font-size: 14px;
    font-weight: 500;
    color: #333333;
    line-height: 20px;
    margin-top: 6px;
    padding-left: 20px;
  }
  .menu-box {
    position: relative;
    background: #fff;
    border-radius: 4px;
    padding: 8px 20px 16px;
    .tab-item {
      // width: 180px;
      background: #fafafa;
      border-radius: 4px 4px 0px 0px;
      border: 1px solid rgba(229, 229, 229, 1);
      border-bottom: none;
      font-size: 14px;
      line-height: 14px;
      font-weight: 400;
      color: #37383a;
      text-align: center;
      padding: 12px 20px;
      margin-right: 1px;
      cursor: pointer;
      &.select-checked {
        position: relative;
        color: #5850ff;
        background-color: #fff;
        transform: scaleY(1.05);
      }
    }
    .menu-box-tab {
      display: flex;
    }
    .menu-box-tree {
      background: #ffffff;
      border: 1px solid rgba(229, 229, 229, 1);
      border-radius: 0px 4px 4px 4px;
      height: 305px;
      overflow: auto;
    }
  }
}
.nodata {
  position: absolute;
  top: 50%;
  left: 50%;
  text-align: center;
  transform: translate(-50%, -50%);
  img {
    width: 64px;
    height: 41px;
    margin-bottom: 12px;
  }
}
</style>