menu-setup.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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" show-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. <!-- ------- 底部按钮 ------- -->
  54. <div class="s-foot">
  55. <el-button type="success" @click="checkedAll()">全选</el-button>
  56. <el-button type="primary" @click="ok()">确定</el-button>
  57. <el-button @click="sa.closeCurrIframe()">取消</el-button>
  58. </div>
  59. </div>
  60. <script src="../../sa-frame/menu-list.js?v=32648732"></script>
  61. <script src="../../sa-frame/menu-list-sp.js"></script>
  62. <script src="../../sa-frame/index/admin-util.js"></script>
  63. <script>
  64. var roleId = sa.p('roleId');
  65. var app = new Vue({
  66. el: '.vue-box',
  67. data: {
  68. p: [],
  69. dataList: [], // 数据集合
  70. selectList: [], // 默认选中
  71. ywList: [], // 一维数组
  72. haveList: [] // 这个角色用的权限id,拷贝
  73. },
  74. methods: {
  75. // 保存
  76. ok: function (clickCount) {
  77. if (clickCount === undefined) {
  78. clickCount = 5;
  79. }
  80. // 判断是否改掉了关键权限
  81. var keys = this.$refs.tree.getCheckedKeys(); // 设置完拥有的id列表
  82. var rArr = ['1', '99', 'auth', 'role-list']; // 敏感菜单id列表
  83. var isR = false; // 是否给改掉了
  84. rArr.forEach(function (item) {
  85. // 只有原先有,现在没有,才会被这样判定
  86. if (this.haveList.indexOf(item) > -1 && keys.indexOf(item) == -1) {
  87. isR = true;
  88. }
  89. }.bind(this))
  90. // 提示
  91. if (isR) {
  92. var tipStr = '危险!系统检测到您取消了此角色的重要权限,这将导致与之关联的账号可能会无法正常使用后台,您无论如何都要这样设置吗?';
  93. tipStr += '<br/>为保证您不是误操作,您还需要继续点击按钮: ' + clickCount + '次'
  94. tipStr = '<b style="color: red;">' + tipStr + '</b>';
  95. sa.confirm(tipStr, function (res) {
  96. if (clickCount <= 1) {
  97. this.ok2();
  98. } else {
  99. clickCount--;
  100. this.ok(clickCount);
  101. }
  102. }.bind(this))
  103. } else {
  104. this.ok2();
  105. }
  106. },
  107. // 开始设置
  108. ok2: function () {
  109. var str = '';
  110. var keys = this.$refs.tree.getCheckedKeys();
  111. keys.forEach(function (ts) {
  112. str += 'code=' + ts + '&';
  113. })
  114. var url = '/SpRolePermission/updatePcodeByRid?roleId=' + roleId;
  115. sa.ajax(url, str, function (res) {
  116. sa.alert('设置成功', function () {
  117. sa.closeCurrIframe();
  118. });
  119. // 如果设置的角色与当前登录者的角色一致,则立即显示出来
  120. if (roleId == sa.$sys.getCurrUser().roleId) {
  121. top.sa_admin.initMenu(keys);
  122. }
  123. }.bind(this))
  124. },
  125. // 点击回调, 处理其子节点跟随父节点的选中
  126. node_click: function (node) {
  127. var is_select = this.$refs.tree.getCheckedKeys().indexOf(node.id) != -1; // 此节点现在是否被选中
  128. if (node.children) {
  129. node.children.forEach(function (item) {
  130. this.$refs.tree.setChecked(item.id, is_select);
  131. // 递归
  132. if (item.children) {
  133. this.node_click(item);
  134. }
  135. }.bind(this))
  136. }
  137. },
  138. // 全选/ 取消全选
  139. checkedAll: function () {
  140. // console.log(this.$refs.tree.getCheckedKeys().length);
  141. // console.log(this.ywList.length);
  142. if (this.$refs.tree.getCheckedKeys().length != this.ywList.length) {
  143. this.$refs['tree'].setCheckedNodes(this.ywList);
  144. } else {
  145. this.$refs['tree'].setCheckedNodes([]);
  146. }
  147. },
  148. filterMenu(menu_list, menuCode) {
  149. let oneList = menu_list.filter(item => menuCode.indexOf(item.id) !== -1)
  150. for (let i in oneList) {
  151. let one = oneList[i];
  152. let childrenList = one.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  153. for (let j in childrenList) {
  154. let two = childrenList[j];
  155. if (!two.childList) {
  156. continue;
  157. }
  158. let cList = two.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  159. for (let k in cList) {
  160. let three = cList[k];
  161. if (!three.childList) {
  162. continue;
  163. }
  164. let list = three.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  165. for (let l in list) {
  166. let item = list[l];
  167. if (!item.childList) {
  168. continue;
  169. }
  170. let itemList = item.childList.filter(item => menuCode.indexOf(item.id) !== -1);
  171. item.childList = itemList;
  172. }
  173. three.childList = list;
  174. }
  175. two.childList = cList;
  176. }
  177. one.childList = childrenList;
  178. }
  179. return oneList;
  180. },
  181. getCurrPer() {
  182. sa.ajax('/SpRolePermission/getCustomerPcCode', function (resp) {
  183. let cuList = resp.data;
  184. let menu_list = this.filterMenu(menuList, cuList);
  185. this.handler(menu_list);
  186. }.bind(this))
  187. },
  188. handler(menu_list) {
  189. // 全部
  190. menu_list = sa_admin_code_util.arrayToTree(menu_list); // 一维转tree
  191. menu_list = sa_admin_code_util.refMenuList(menu_list); // 属性处理
  192. this.dataList = menu_list; // 数据
  193. this.ywList = sa_admin_code_util.treeToArray(this.dataList);
  194. // 拉取此 roleId 的
  195. sa.ajax('/SpRolePermission/getPcodeByRid?roleId=' + roleId, function (res) {
  196. this.selectList = res.data; // 选中的列表
  197. this.haveList = [].concat(this.selectList);
  198. }.bind(this))
  199. }
  200. },
  201. created: function () {
  202. this.getCurrPer();
  203. }
  204. })
  205. </script>
  206. </body>
  207. </html>