newAuthManage.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <div class="authManage_container">
  3. <div class="auth_cont_top">
  4. <div>
  5. <span>{{$t('SystemManage.RoleManage.role_name')}}:</span>
  6. <span>{{Role.RoleName}}</span>
  7. <!-- <el-select v-model="role" placeholder="请选择角色" style="width:360px;marginLeft:30px;">
  8. <el-option v-for="item in roleList" :key="item.RoleId" :label="item.RoleName" :value="item.RoleId">
  9. </el-option>
  10. </el-select> -->
  11. </div>
  12. <el-button style="margin-left:auto;marginRight:50px;width:140px;" @click="$router.back()" v-if="!isLook">{{$t('Dialog.cancel_btn')}}
  13. </el-button>
  14. <el-button type="primary" style="marginRight:50px;width:140px;" @click="saveAuth" v-if="!isLook">{{$t('Dialog.confirm_save_btn')}}
  15. </el-button>
  16. </div>
  17. <div class="auth_bot">
  18. <div class="menu_auth_cont">
  19. <h3 style="color:#333;fontSize:14px;">{{$t('SystemManage.RoleManage.permission_set')}}</h3>
  20. <div class="auth-wrap">
  21. <el-tree ref="checkboxTree" :data="authList" :props="{label:'Name',children:'Children',disabled:'Disabled'}"
  22. :default-expand-all="false" show-checkbox node-key="MenuId"
  23. :default-checked-keys="defaultCheckedKeys">
  24. </el-tree>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import {departInterence} from '@/api/api.js';
  32. export default {
  33. name: '',
  34. components: {},
  35. data() {
  36. return {
  37. role: '', //角色
  38. roleList: [], //角色列表
  39. authList: [], //权限列表
  40. defaultCheckedKeys: [],
  41. loading: null, //loading
  42. isLook: false, //是否仅查看
  43. Role:{},//角色
  44. checkList:[],//角色应勾选的list
  45. };
  46. },
  47. watch: {
  48. // 监测角色改变对应的权限也改变
  49. role() {
  50. this.loading = this.$loading({
  51. lock: true,
  52. target: '.authManage_container',
  53. text: '切换角色中...',
  54. spinner: 'el-icon-loading',
  55. background: 'rgba(255, 255, 255, 0.8)'
  56. });
  57. this.getAuthList();
  58. }
  59. },
  60. methods: {
  61. /* 获取已有角色列表 */
  62. getRoles() {
  63. departInterence.getRole().then(res => {
  64. if (res.Ret === 200) {
  65. this.roleList = res.Data.List||[];
  66. if (res.Data.List.length && !this.role) {
  67. this.role = res.Data.List[0].RoleId
  68. }
  69. this.Role = this.roleList.find(i=>i.RoleId===this.role)||{}
  70. }
  71. })
  72. },
  73. /* 根据角色id获取已有权限 */
  74. getAuthList() {
  75. departInterence.getRoleAuth({
  76. RoleId: Number(this.role)
  77. }).then(res => {
  78. if (res.Ret !== 200) return
  79. this.loading && this.loading.close()
  80. if (!res.Data) return
  81. const {
  82. List,
  83. ChoiceList = [],
  84. HalfChoiceList = []
  85. } = res.Data
  86. this.authList = List || []
  87. this.checkList = ChoiceList
  88. /* this.defaultCheckedKeys = ChoiceList.filter((item) => !HalfChoiceList.some((halfItem) =>
  89. item === halfItem)) */
  90. const type = this.isLook?'all':''
  91. this.$nextTick(()=>{
  92. this.authList.forEach(l=>{
  93. this.formatTree(l,type)
  94. })
  95. this.defaultCheckedKeys=this.checkList
  96. })
  97. })
  98. },
  99. // 保存
  100. saveAuth() {
  101. const keys = this.$refs.checkboxTree.getCheckedKeys()
  102. const halfKeys = this.$refs.checkboxTree.getHalfCheckedKeys()
  103. if(!keys.length&&!halfKeys.length){
  104. this.$message.warning(this.$t('SystemManage.RoleManage.set_permission_msg01'))
  105. return
  106. }
  107. const ChoiceList = Array.from(new Set([...keys,...halfKeys]))
  108. departInterence.saveRoleAuth({
  109. RoleId:Number(this.role),
  110. MenuIds:ChoiceList,
  111. HalfMenuIds:halfKeys
  112. }).then(res=>{
  113. if(res.Ret!==200) return
  114. this.$message.success(this.$t('SystemManage.RoleManage.set_permission_success'))
  115. // this.$router.back()
  116. })
  117. },
  118. formatTree(data,type){
  119. if(/* data.MenuType===2|| */type==='all'){
  120. data.Disabled = true
  121. }
  122. //非叶子节点递归
  123. if(data.Children && data.Children.length){
  124. data.Children = data.Children.map(i=>{
  125. return this.formatTree(i,type)
  126. })
  127. }
  128. if(!data.Children||data.Children&&data.Children.length===0){
  129. //叶子节点向上检查MenuId
  130. this.checkDataList(data)
  131. }
  132. return data
  133. },
  134. //根据MenuId找到对应节点
  135. findTreeNode(MenuId){
  136. return this.$refs.checkboxTree.getNode(MenuId)
  137. },
  138. checkDataList(data){
  139. //获取data的MenuId 和 checkList对比
  140. //如果MenuId不在checkList里,检查data.ParentId在不在checkList里,若在,则从checkList里去除
  141. if(!this.checkList.includes(data.MenuId)&&this.checkList.includes(data.ParentId)){
  142. const index = this.checkList.indexOf(data.ParentId)
  143. index!==-1&&this.checkList.splice(index,1)
  144. console.log('应该去除的节点',data.ParentId)
  145. }
  146. //向上检查MenuId
  147. const parentNode = this.findTreeNode(data.ParentId)
  148. if(parentNode){
  149. this.checkDataList(parentNode.data)
  150. }
  151. }
  152. },
  153. created() {
  154. if (this.$route.query.id) {
  155. this.role = Number(this.$route.query.id);
  156. //this.isLook = true;
  157. }
  158. },
  159. mounted() {
  160. this.getRoles();
  161. },
  162. }
  163. </script>
  164. <style lang='scss'>
  165. .authManage_container {
  166. * {
  167. box-sizing: border-box;
  168. }
  169. .auth_cont_top {
  170. font-size: 16px;
  171. color: #333;
  172. display: flex;
  173. // justify-content: space-between;
  174. align-items: center;
  175. border: 1px solid #ECECEC;
  176. padding: 20px 30px;
  177. background: #fff;
  178. border-radius: 4px;
  179. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05);
  180. justify-content: space-between;
  181. }
  182. .auth_bot {
  183. min-height: calc(100vh - 224px);
  184. padding: 30px;
  185. background: #fff;
  186. margin-top: 20px;
  187. position: relative;
  188. border: 1px solid #ECECEC;
  189. border-radius: 4px;
  190. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05);
  191. .el-tree {
  192. border-top: 1px solid #E5E7ED;
  193. border-left: 1px solid #E5E7ED;
  194. border-right: 1px solid #E5E7ED;
  195. width: 98%;
  196. margin-bottom: 40px;
  197. .el-tree-node__label {
  198. margin: 10px;
  199. }
  200. .el-tree-node__content {
  201. min-width: 200px;
  202. width: 200px;
  203. white-space: normal;
  204. box-sizing: border-box;
  205. }
  206. .el-tree-node {
  207. .el-tree-node {
  208. .el-tree-node__children {
  209. width: 100%;
  210. }
  211. .el-tree-node {
  212. &:not(:first-child) {
  213. /* .el-tree-node__content{
  214. border-right: 1px solid #E5E7ED;
  215. } */
  216. border-top: 1px solid #E5E7ED;
  217. }
  218. .el-tree-node__content {
  219. border-right: 1px solid #E5E7ED;
  220. }
  221. }
  222. }
  223. }
  224. .el-tree-node__content {
  225. padding: 5px 10px !important;
  226. height: auto;
  227. .el-tree-node__expand-icon.el-icon-caret-right {
  228. //display: none;
  229. }
  230. }
  231. >.el-tree-node {
  232. padding: 0 !important;
  233. display: flex;
  234. border-bottom: 1px solid #E5E7ED;
  235. >.el-tree-node__children {
  236. width: 100%;
  237. >.el-tree-node {
  238. &:not(:first-child) {
  239. border-top: 1px solid #E5E7ED;
  240. }
  241. >.el-tree-node__content {
  242. border-left: 1px solid #E5E7ED;
  243. border-right: 1px solid #E5E7ED;
  244. }
  245. }
  246. }
  247. }
  248. .el-tree-node__children {
  249. display: flex;
  250. flex-direction: column;
  251. .el-tree-node {
  252. display: flex;
  253. flex:1;
  254. padding: 0px !important;
  255. .el-tree-node__content {
  256. border-bottom: none;
  257. .custom-tree-node {
  258. height: 24px;
  259. display: flex;
  260. align-items: center;
  261. .tree-btn {
  262. margin-left: 10px;
  263. display: none;
  264. }
  265. .el-button {
  266. padding: 0px !important;
  267. border-radius: 4px;
  268. background: #363554;
  269. color: #ffffff;
  270. }
  271. }
  272. }
  273. .el-tree-node__children {
  274. .el-tree-node {
  275. &:not(:first-child) {
  276. .el-tree-node__content {
  277. //border-left: none;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. .auth-wrap{
  286. margin-top: 20px;
  287. }
  288. }
  289. }
  290. </style>