tb-menu-list.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>权限分配</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport"
  7. content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
  8. <!-- 所有的 css & js 资源 -->
  9. <link rel="stylesheet" href="../../static/kj/element-ui/theme-chalk/index.css">
  10. <link rel="stylesheet" href="../../static/sa.css">
  11. <script src="../../static/kj/vue.min.js"></script>
  12. <script src="../../static/kj/element-ui/index.js"></script>
  13. <script src="../../static/kj/jquery.min.js"></script>
  14. <script src="../../static/kj/layer/layer.js"></script>
  15. <script src="../../static/sa.js"></script>
  16. <style>
  17. body,
  18. .el-tree {
  19. background-color: #eee;
  20. }
  21. .el-tree-node {
  22. margin: 0.15em 0 !important;
  23. }
  24. /* 悬浮时颜色更深一点 */
  25. .el-tree-node__content:hover {
  26. background-color: #CFE8FC !important;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="vue-box sbot" style="display: none;" :style="'display: block;'">
  32. <!-- ------- 内容部分 ------- -->
  33. <div class="s-body">
  34. <!-- 表格 -->
  35. <div style="padding: 1em 2em;">
  36. <el-form>
  37. <div class="c-title">所有权限</div>
  38. <!-- 此扩展能递归渲染一个权限树,点击深层次节点,父级节点中没有被选中的节点会被自动选中,单独点击父节点,子节点会全部 选中/去选中 -->
  39. <el-tree ref="tree" :data="dataList" how-checkbox node-key="id" :default-expand-all="true"
  40. :default-checked-keys="selectList" :expand-on-click-node="false" :check-on-click-node="true"
  41. :check-strictly="true" @node-click="node_click" @check="node_click">
  42. <span class="custom-tree-node" slot-scope="s">
  43. <span style="color: #2D8CF0;"
  44. v-if="s.data.isShow == undefined || s.data.isShow == true">{{ s.data.name
  45. }}</span>
  46. <span style="color: #999;" v-if="s.data.isShow == false">{{ s.data.name }} </span>
  47. <span style="color: #999;" v-if="s.data.info">&emsp;———— {{s.data.info}} </span>
  48. </span>
  49. </el-tree>
  50. </el-form>
  51. </div>
  52. </div>
  53. </div>
  54. <script src="../../sa-frame/menu-list.js?v=32648732"></script>
  55. <script src="../../sa-frame/menu-list-sp.js"></script>
  56. <script src="../../sa-frame/index/admin-util.js"></script>
  57. <script>
  58. var roleId = sa.p('roleId');
  59. var app = new Vue({
  60. el: '.vue-box',
  61. data: {
  62. p: [],
  63. dataList: [], // 数据集合
  64. selectList: [], // 默认选中
  65. ywList: [], // 一维数组
  66. haveList: [] // 这个角色用的权限id,拷贝
  67. },
  68. methods: {
  69. // 保存
  70. ok: function (clickCount) {
  71. if (clickCount === undefined) {
  72. clickCount = 5;
  73. }
  74. // 判断是否改掉了关键权限
  75. var keys = this.$refs.tree.getCheckedKeys(); // 设置完拥有的id列表
  76. var rArr = ['1', '99', 'auth', 'role-list']; // 敏感菜单id列表
  77. var isR = false; // 是否给改掉了
  78. rArr.forEach(function (item) {
  79. // 只有原先有,现在没有,才会被这样判定
  80. if (this.haveList.indexOf(item) > -1 && keys.indexOf(item) == -1) {
  81. isR = true;
  82. }
  83. }.bind(this))
  84. // 提示
  85. if (isR) {
  86. var tipStr = '危险!系统检测到您取消了此角色的重要权限,这将导致与之关联的账号可能会无法正常使用后台,您无论如何都要这样设置吗?';
  87. tipStr += '<br/>为保证您不是误操作,您还需要继续点击按钮: ' + clickCount + '次'
  88. tipStr = '<b style="color: red;">' + tipStr + '</b>';
  89. sa.confirm(tipStr, function (res) {
  90. if (clickCount <= 1) {
  91. this.ok2();
  92. } else {
  93. clickCount--;
  94. this.ok(clickCount);
  95. }
  96. }.bind(this))
  97. } else {
  98. this.ok2();
  99. }
  100. },
  101. // 开始设置
  102. ok2: function () {
  103. var str = '';
  104. var keys = this.$refs.tree.getCheckedKeys();
  105. keys.forEach(function (ts) {
  106. str += 'code=' + ts + '&';
  107. })
  108. var url = '/SpRolePermission/updatePcodeByRid?roleId=' + roleId;
  109. sa.ajax(url, str, function (res) {
  110. sa.alert('设置成功', function () {
  111. sa.closeCurrIframe();
  112. });
  113. // 如果设置的角色与当前登录者的角色一致,则立即显示出来
  114. if (roleId == sa.$sys.getCurrUser().roleId) {
  115. top.sa_admin.initMenu(keys);
  116. }
  117. }.bind(this))
  118. },
  119. // 点击回调, 处理其子节点跟随父节点的选中
  120. node_click: function (node) {
  121. var is_select = this.$refs.tree.getCheckedKeys().indexOf(node.id) != -1; // 此节点现在是否被选中
  122. if (node.children) {
  123. node.children.forEach(function (item) {
  124. this.$refs.tree.setChecked(item.id, is_select);
  125. // 递归
  126. if (item.children) {
  127. this.node_click(item);
  128. }
  129. }.bind(this))
  130. }
  131. },
  132. // 全选/ 取消全选
  133. checkedAll: function () {
  134. // console.log(this.$refs.tree.getCheckedKeys().length);
  135. // console.log(this.ywList.length);
  136. if (this.$refs.tree.getCheckedKeys().length != this.ywList.length) {
  137. this.$refs['tree'].setCheckedNodes(this.ywList);
  138. } else {
  139. this.$refs['tree'].setCheckedNodes([]);
  140. }
  141. },
  142. filterMenu(menu_list, menuCode) {
  143. let oneList = menu_list.filter(item => menuCode.indexOf(item.id) !== -1)
  144. for (let i in oneList) {
  145. let one = oneList[i];
  146. let childrenList = one.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  147. for (let j in childrenList) {
  148. let two = childrenList[j];
  149. if (!two.childList) {
  150. continue;
  151. }
  152. let cList = two.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  153. for (let k in cList) {
  154. let three = cList[k];
  155. if (!three.childList) {
  156. continue;
  157. }
  158. let list = three.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  159. for (let l in list) {
  160. let item = list[l];
  161. if (!item.childList) {
  162. continue;
  163. }
  164. let itemList = item.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  165. item.childList = itemList;
  166. }
  167. three.childList = list;
  168. }
  169. two.childList = cList;
  170. }
  171. one.childList = childrenList;
  172. }
  173. return oneList;
  174. },
  175. getCurrPer() {
  176. sa.ajax('/TbInitPermission/getList', {
  177. pageSize: 200,
  178. pageNo: 1
  179. }, function (resp) {
  180. let cuList = resp.data.filter(obj => obj.type !== '0').map(obj => obj.code);
  181. console.log(cuList)
  182. let menu_list = this.filterMenu(menuList, cuList);
  183. this.handler(menu_list);
  184. }.bind(this))
  185. },
  186. handler(menu_list) {
  187. // 全部
  188. menu_list = sa_admin_code_util.arrayToTree(menu_list); // 一维转tree
  189. menu_list = sa_admin_code_util.refMenuList(menu_list); // 属性处理
  190. this.dataList = menu_list; // 数据
  191. this.ywList = sa_admin_code_util.treeToArray(this.dataList);
  192. // 拉取此 roleId 的
  193. sa.ajax('/SpRolePermission/getPcodeByRid?roleId=' + roleId, function (res) {
  194. this.selectList = res.data; // 选中的列表
  195. this.haveList = [].concat(this.selectList);
  196. }.bind(this))
  197. }
  198. },
  199. created: function () {
  200. this.getCurrPer();
  201. }
  202. })
  203. </script>
  204. </body>
  205. </html>