add.html 20.3 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
<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8" />
    <meta content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="广联帐户管理中心-员工管理" />
    <meta name="author" content="GLSX Web Team" />
    <meta name="keywords" itemprop="keywords" content="广联,帐户管理,员工管理" />
    <link rel="stylesheet" type="text/css" href="/css/main.css" />
    <title>角色管理</title>
    <style>
      .show {
        display: inline-block;
      }

      .hide {
        display: none;
      }
      .tabmutiple-content {
        margin: 0 20px;
      }
      .edit-box {
        padding: 20px 20px;
        color: #333333;
        background-color: #ffffff;
        margin: 15px auto;
      }
      .edit-title {
        height: 48px;
        line-height: 48px;
        padding: 0 10px;
        margin-bottom: 15px;
        font-size: 16px;
        font-weight: 600;
        background: #f7f7f7;
      }
    </style>
  </head>

  <body>
    <div id="app">
      <main class="bg-white">
        <div class="sub-bg-white edit-box">
          <form class="layui-form add" lay-filter="roleForm" action="">
            <div class="layui-form-item edit-title" style="position: relative;">
              <div id="roleTitle" class="layui-input-inline"></div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label required">所属商户:</label>
              <div class="layui-input-block" id="merchantBox">
                <!-- <div id="merchantSelect" class="hide">
                    <select id="merchantName" name="merchantId" lay-filter="merchants" lay-verify="required" autocomplete="off" placeholder="请选择商户名称" required></select>
                </div> -->
                <div class="hide" id="merchantIdText" style="line-height: 36px;"></div>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label required">角色名称:</label>
              <div class="layui-input-block">
                <input type="text" name="roleName" maxlength="15" lay-verify="required" placeholder="请输入角色名称" class="layui-input" />
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">角色说明:</label>
              <div class="layui-input-block">
                <input type="text" name="roleDescription" maxlength="50" placeholder="请输入角色说明" class="layui-input" />
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label required">角色权限:</label>
              <div class="layui-input-block" id="accountType">
                <!-- <input lay-filter="switch" type="checkbox" name="merchantId" value="1" title="引流渠道商" />
                    <input lay-filter="switch" type="checkbox" name="merchantId" value="2" title="服务商/合作商" /> -->
              </div>
            </div>
            <div class="layui-tab tabmutiple-content" lay-filter="roleAuth">
              <ul class="layui-tab-title"></ul>
              <div class="layui-tab-content"></div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn hide" id="draft">存草稿</button>
                <button class="layui-btn" lay-submit lay-filter="roleSubmit">确定</button>
                <button type="reset" class="layui-btn " id="return">取消</button>
              </div>
            </div>
          </form>
        </div>
      </main>
    </div>
    <script src="https://www.layuicdn.com/layui/layui.js"></script>
    <script src="/js/load.js"></script>
    <script>
      layui.use(['form', 'element', 'request', 'laytpl', 'xmSelect', 'gtree', 'helper'], function() {
        // var merchantId = qs('merchantId')
        var roleid = qs('id')
        var element = layui.element
        var form = layui.form
        var arr_box = []
        var $ = layui.$
        var request = layui.request
        var merchantId = ''
        if (roleid) {
          $('#roleTitle').append('编辑角色')
          getById(roleid)
          $('#merchantIdText').text(qs('merchantName'))
          $('#merchantIdText').removeClass('hide')
          $('#merchantIdText').addClass('show')
        } else {
          $('#roleTitle').append('新增角色')
          $('#draft').removeClass('hide')
          $('#draft').addClass('show')
          var str = '<div id="merchantSelect" class="hide"><select id="merchantName" name="merchantId" lay-filter="merchants" lay-verify="required" autocomplete="off" placeholder="请选择商户名称" required></select></div>'
          $('#merchantBox').append(str)
          findAllMerchant()
          $('#merchantSelect').removeClass('hide')
          $('#merchantSelect').addClass('show')
          // findMenuFunTreeByMerId()
        }
        var merchantName = $('#merchantName')
        // start
        var laytpl = layui.laytpl
        var xmSelect = layui.xmSelect
        var tree = layui.gtree
        var helper = layui.helper
        var check_arr = []
        var requestList = []
        var templateData = {}
        var selectEl = {}
        var treeData = {}
        var menuFunTreeData = [] // 商户的菜单权限树数据
        var active = {
          /**
           *  第一级tab添加
           *  item: {name: 选项卡名称, value: 选项卡值}
           *  roleAuth: 选项卡过滤器的名称 lay-filter绑定的名字
           *  content:  一个放置模板的视图容器,用于呈现渲染结果 带有id的div
           *  id:选项卡标题的lay-id属性值
           *  需要引入laytpl 模块
           *  rolesAuth:模板的id
           *  view 放置模板的视图容器的dom
           *  templateData:用于渲染模板的数据
           *  productList:复选下拉框的list,修改过的数据{name: '', value: ''}
           *  products:对应的菜单下的产品list,包含了对应的权限树,原始数据,需作为参数传入treeTabAdd中,在实例化tree时传入的数据使用
           */
          tabAdd: function(item) {
            // 渠道的id,一层选项卡的id
            var id = item.value
            // roleAuth 一层选项卡过滤器的名称
            element.tabAdd('roleAuth', {
              title: item.title,
              // 放置模板视图的上一级div
              content: '<div id="role' + id + '"></div>',
              // 选项卡标题的lay-id属性值
              id: id
            })
            var getTpl =
              '<div id="select{{d.id}}"></div> <div class="layui-tab layui-tab-brief" lay-filter="treeTab{{d.id}}"><ul class="layui-tab-title"></ul> <div class="layui-tab-content"></div></div>'
            // var getTpl = tpl//rolesAuth.innerHTML, // 模板内容
            view = document.getElementById('role' + id)
            templateData.id = id // 渲染模版的数据
            laytpl(getTpl).render(templateData, function(html) {
              view.innerHTML = html
            })
            var productList = []
            var products = []
            $.each(menuFunTreeData, function(index, item) {
              if (item.accounttypeId == id) {
                products = item.products
                $.each(item.products, function(idx, i) {
                  productList.push({
                    name: i.productName,
                    value: i.productId
                  })
                })
              }
            })
            selectEl[id] = xmSelect.render({
              el: '#select' + id,
              data: productList,
              on: function(data) {
                //arr:  当前多选已选中的数据
                var arr = data.arr
                //change, 此次选择变化的数据,数组
                var change = data.change
                //isAdd, 此次操作是新增还是删除
                var isAdd = data.isAdd
                var filterName = 'treeTab' + id
                if (isAdd) {
                  active.treeTabAdd(change[0], id, products)
                } else {
                  var productId = 'tab_' + id + '_' + change[0].value
                  active.tabDelete(filterName, productId)
                  delete treeData[id][change[0].value]
                }
                if (arr.length != 0) {
                  var productId = 'tab_' + id + '_' + arr[0].value
                  active.tabChange(filterName, productId)
                }
              }
            })
            console.log(selectEl[id])
          },
          // tab删除
          tabDelete: function(name, id) {
            element.tabDelete(name, id)
          },
          // tab Change
          tabChange: function(name, id) {
            element.tabChange(name, id)
          },
          /**
           * 第二级选项卡
           * treeTabAdd 产品的权限添加
           * item: {name: 选项卡名称, value: 选项卡值}
           * id 第一级选项卡的item.value
           * filterName: 过滤器的名称 lay-filter绑定的值
           * treeData:渲染模板的数据,和用来存放对应树实例的对象,
           * products:产品list数组
           * defaultList:编辑时传入,默认勾选中的数组。(举例:[1,2,3])
           */
          treeTabAdd: function(item, id, products, defaultList) {
            console.log(defaultList)
            var productId = 'tab_' + id + '_' + item.value
            var filterName = 'treeTab' + id // 过滤器的名称 lay-filter
            element.tabAdd(filterName, {
              title: item.name,
              content: '<div id="treeTab' + productId + '"></div>', // 放置模板视图的div
              id: productId
            })
            var getTreeTpl = treeAuth.innerHTML, // 获取的模板的id的内容
              treeView = document.getElementById('treeTab' + productId) // 放置模板视图的div
            treeData[id] = treeData[id] || {}
            treeData[id][item.value] = treeData[id][item.value] || {}
            treeData[id][item.value].id = productId // 渲染模版的数据
            treeData[id][item.value].productId = item.value
            laytpl(getTreeTpl).render(treeData[id][item.value], function(html) {
              treeView.innerHTML = html
            })
            var treeListData = []
            $.each(products, function(index, i) {
              if (item.value == i.productId) {
                treeListData = i.merchantProTreeResponses
              }
            })
            var treedom = 'tree_' + productId
            if (!roleid) {
              treeData[id][item.value].treeObj = tree.create(treedom, treeListData)
              treeData[id][item.value].treeObj.checkAllNodes(true)
            } else {
              treeData[id][item.value].treeObj = tree.create(treedom, treeListData)
              if (defaultList) treeData[id][item.value].treeObj.checkNodes(defaultList)
              // helper.defaultChecked(treeData[id][item.value].treeObj, defaultList)
            }
          }
        }

        // 复选框的change事件
        form.on('checkbox(switch)', function(data) {
          if (data.elem.checked) {
            var obj = { title: data.elem.title, value: data.elem.value }
            check_arr.push({
              accounttypeId: data.elem.value
            })
            active.tabAdd(obj)
          } else {
            for (var i in check_arr) {
              var id = check_arr[i].accounttypeId
              if (check_arr[i].accounttypeId == data.elem.value) {
                active.tabDelete('roleAuth', id)
                check_arr.splice(i, 1)
              }
            }
          }
          if (check_arr.length != 0) {
            active.tabChange('roleAuth', check_arr[0].accounttypeId)
          }
        })
        // end

        // 表单提交
        form.on('submit(roleSubmit)', function(data) {
          if (Object.keys(treeData).length == 0) {
            layer.msg('请选择角色权限')
            return false
          }
          getAuthList()
          insert(0, data.field)
          return false
        })

        // select 选择触发
        form.on('select(merchants)', function (data) {
          console.log(data.value)
          merchantId = data.value
          findMenuFunTreeByMerId(merchantId)
        })

        // 存草稿
        $('#draft').on('click', function() {
          var formVal = form.val('roleForm')
          if (formVal.roleName || formVal.roleDescription || Object.keys(treeData).length != 0) {
            console.log('baocun')
            insert(1, formVal)
          } else {
            layer.msg('至少填一项')
          }
          return false
        })

        // 返回上个页面
        $('#return').on('click', function() {
          window.history.back()
        })

        // 调用新增接口
        function insert(delFlag, formVal) {
          var otherParams = {
            merchantId: merchantId,
            delFlag: delFlag
          }
          if (requestList.length != 0) {
            otherParams.requestList = JSON.stringify(requestList)
          } else {
            otherParams.requestList = JSON.stringify([])
          }
          if (roleid) {
            otherParams.id = roleid
            otherParams.method = 'org.ore.api.role.update'
          } else {
            otherParams.method = 'org.ore.api.role.insert'
          }
          var params = helper.clone(true, formVal, otherParams)
          delete params.select
          request(params, function(res) {
            if (res.code == '0') {
              layer.msg('操作成功', {
                icon: 1
              })
              window.history.back()
            }
          })
        }

        // 查询商户的菜单权限树
        function findMenuFunTreeByMerId(merchantId) {
          var params = {
            method: 'cn.com.glsx.merchants.merchant.findMenuFunTreeByMerId',
            id: merchantId
          }
          request(params, function(res) {
            if (res.code == '0') {
              // callback(res.data)
              menuFunTreeData = res.data
              var accountType = $('#accountType')
              accountType.empty()
              $.each(menuFunTreeData, function(index, item) {
                console.log(item)
                var str = $('<input lay-filter="switch" type="checkbox" name="accounttypeId" value="' + item.accounttypeId + '" title="' + item.accounttypeName + '" />')
                accountType.append(str)
                if (roleid) {
                  console.log(check_arr)
                  // 角色权限的check
                  for (var i = 0; i < check_arr.length; i++) {
                    if (check_arr[i].accounttypeId == item.accounttypeId) {
                      var productData = check_arr[i]
                      str.attr('checked', true)
                      var obj = { title: item.accounttypeName, value: item.accounttypeId }
                      active.tabAdd(obj)
                      var selectArr = []
                      var menuData = []
                      if (productData.productList) {
                        for (var j = 0; j < productData.productList.length; j++) {
                        menuData = productData.productList[j]
                        selectArr.push(menuData.productId)
                        active.treeTabAdd({
                            name: menuData.productName,
                            value: menuData.productId
                          }, item.accounttypeId, item.products, menuData.idList
                        )}
                        selectEl[item.accounttypeId].setValue(selectArr)
                        var filterName = 'treeTab' + item.accounttypeId
                        var productId = 'tab_' + item.accounttypeId + '_' + item.products[0].productId
                        console.log(filterName, productId)
                        active.tabChange(filterName, productId)
                      }
                    }
                  }
                  if (check_arr.length != 0) {
                    active.tabChange('roleAuth', check_arr[0].accounttypeId)
                  }
                }
              })
              form.render('checkbox')
            }
          })
        }

        // 根据id获取角色详情
        function getById(id) {
          var params = {
            method: 'org.ore.api.role.getById',
            id: id
          }
          request(params, function(res) {
            if (res.code == '0') {
              var data = res.data
              var accounttypeId = ''
              if (data.responseList) {
                check_arr = data.responseList
              }
              merchantId = data.merchantId
              findMenuFunTreeByMerId(data.merchantId)
              form.val('roleForm', {
                roleName: data.roleName,
                roleDescription: data.roleDescription,
                merchantId: data.merchantId
              })
            }
          })
        }

        // 获取角色权限数据requestList
        function getAuthList() {
          requestList = []
          for (var key in treeData) {
            var accountObj = treeData[key]
            var productList = []
            for (var i in accountObj) {
              var productObj = accountObj[i]
              var menuIdList = []
              var functionIdList = []
              var functionIdLists = []
              var functionValueIdLists = []
              var nodes = productObj.treeObj.getCheckedNodes(true)
              $.each(nodes, function(index, item) {
                if (item.logo == 'function') {
                  functionIdLists.push(item)
                } else if (item.logo == 'functionValue') {
                  functionValueIdLists.push(item)
                } else {
                  if (item.logo != 'neglect') {
                    menuIdList.push(item.id)
                  }
                }
              })
              $.each(functionValueIdLists, function(idx, m) {
                // m.parentId
                var findIdx = -1
                $.each(functionIdList, function(idn, n) {
                  if (n.id == m.parentId) {
                    findIdx = idn
                  }
                })
                if (findIdx != -1) {
                  functionIdList[findIdx].functionValue += ';' + m.id + ':' + m.label
                } else {
                  functionIdList.push({
                    id: m.parentId,
                    menuId: m.superParentId,
                    functionValue: m.id + ':' + m.label
                  })
                }
              })
              $.each(functionIdLists, function(idx, m) {
                // console.log(m)
                functionIdList.push({
                  id: m.parentId,
                  menuId: m.superParentId
                })
              })
              productList.push({
                productId: productObj.productId,
                functionIdList: functionIdList,
                menuIdList: menuIdList
              })
            }
            requestList.push({
              accountTypeId: key,
              productList: productList
            })
          }
        }

        // 查询所有商户
        function findAllMerchant() {
          var params = { method: 'cn.com.glsx.merchants.menu.findAllMerchant' }
          request(params, function(result) {
            if (result.code == '0') {
              console.log(result)
              var list = result.data
              list.unshift('')
              $.each(list, function(index, item) {
                merchantName.append(new Option(item.merchantName, item.id))
              })
              form.render('select')
            }
          })
        }
      })
    </script>
    <script type="text/html" id="rolesAuth">
      `<div id="select{{d.id}}"></div>
      <div class="layui-tab layui-tab-brief" lay-filter="treeTab{{d.id}}">
        <ul class="layui-tab-title">
        </ul>
        <div class="layui-tab-content">
        </div>
      </div>`
    </script>
    <script type="text/html" id="treeAuth">
      <!-- <button class="layui-btn" data-type="{{d.id}}" id="checkedAll">全选</button>
      <button class="layui-btn" id="cancelCheckedAll">取消全选</button> -->
      <ul id="tree_{{d.id}}" class="ztree wrapper-tree"></ul>
    </script>
  </body>
</html>