You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
8.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <template>
  2. <div>
  3. <div style="margin:2px 2px 5px 2px;display: flex;justify-content:space-between;">
  4. <div>
  5. <el-input placeholder="输入关键字进行过滤" v-model="filterText" size="small" />
  6. </div>
  7. <div v-show="isRole && checkPagePriv(pagePriv.privs,'提交')">
  8. <el-button @click="btnSubmit" size="small">提交</el-button>
  9. </div>
  10. </div>
  11. <div style="overflow: scroll;height:450px;">
  12. <el-tree :data="menuInfoView" :props="customerOrg.treeprops"
  13. node-key="id" :filter-node-method="filterNode" show-checkbox
  14. default-expand-all
  15. :default-expanded-keys="customerOrg.defaultExpandedKeys"
  16. @check-change="handleCheckChange" highlight-current ref="customerOrgTree"/>
  17. </div>
  18. </div>
  19. </template>
  20. <script>
  21. import { mapState, mapMutations } from "vuex";
  22. import { getapi, postapi, putapi, deletapi } from "@/api/api";
  23. import { getPagePriv,checkPagePriv, deepCopy,tcdate,arrayExistObj } from "../../utlis/proFunc";
  24. import { getTreeNode, getTreeAllChildIdsByNode, getTreeAllChildIdsById, getTreePids } from "../../utlis/tree";
  25. export default {
  26. components: {},
  27. props:["params"], // params = { opra: role/user 用户/角色, idVal: roleId/userId}
  28. data() {
  29. return {
  30. pagePriv:{
  31. routeUrlorPageName:'MenuInfoSet', //当前页面归属路由或归属页面权限名称
  32. privs:[] // 页面权限
  33. },
  34. menuInfoView:[], //显示菜单
  35. menuInfoSet:[], //设置菜单
  36. checkedKeys:[], //勾选的菜单节点ID
  37. filterText:''
  38. };
  39. },
  40. computed: {
  41. ...mapState(["window", "dialogWin", "dataTransOpts", "customerOrg" ]),
  42. // 是否角色
  43. isRole(){
  44. let ret = true
  45. if(this.params && this.params.opra == "user"){
  46. ret = false
  47. }
  48. return ret
  49. },
  50. },
  51. //创建组件后
  52. created() {
  53. //获取用户当前页面的权限
  54. let userPriv = window.sessionStorage.getItem('userPriv')
  55. if(userPriv) this.pagePriv.privs = deepCopy(getPagePriv(this.pagePriv.routeUrlorPageName))
  56. },
  57. //挂载组件完成
  58. mounted() {
  59. },
  60. methods: {
  61. ...mapMutations(["setData"]),checkPagePriv,
  62. // 初始化数据,获取菜单列表树信息 及角色的授权菜单
  63. initData(roleOrUser_Id) {
  64. getapi("/api/app/menuinfo/getmenuinfotreelist").then((res) => {
  65. if(res.code != -1){
  66. this.menuInfoView = res.data
  67. tcdate(this.menuInfoView);
  68. // 如果不是角色,则不允许编辑权限
  69. if(!this.isRole){
  70. this.setTreeDisabled(this.menuInfoView,"treeChildren")
  71. }
  72. if(this.isRole){
  73. return getapi(`/api/app/rolemenuinfo/getrolemenuinfolist?RoleId=${roleOrUser_Id}`) //
  74. }else{
  75. return getapi(`/api/app/menuinfo/getmymenuinfolistinuser?UserId=${roleOrUser_Id}`)
  76. }
  77. }
  78. }).then(res =>{
  79. if(res && res.code != -1){
  80. // 勾选赋值
  81. this.menuInfoSet = res.data
  82. this.setCheckedKeys()
  83. }
  84. }).catch(err =>{
  85. console.log(err)
  86. });
  87. },
  88. // 用户只允查看权限,暂不允许修权限
  89. setTreeDisabled(tree,childrenNodeName){
  90. tree.forEach(e => {
  91. e.disabled = true
  92. if(e[childrenNodeName]) this.setTreeDisabled(e[childrenNodeName],childrenNodeName)
  93. });
  94. },
  95. // 初始设置菜单勾选
  96. setCheckedKeys(){
  97. this.checkedKeys = []
  98. if(!this.isRole){
  99. this.menuInfoSet.forEach(e => {
  100. e.menuInfoId = e.id
  101. });
  102. }
  103. this.menuInfoSet.forEach(e => {
  104. // 无下级节点时,勾选;有下级节点时,所有下级节点全部勾选时,才勾选
  105. let node = getTreeNode(this.menuInfoView, "treeChildren", "id", e.menuInfoId)
  106. if(node.treeChildren){
  107. //获取所有下级节点值
  108. let menuInfoIds = getTreeAllChildIdsByNode(node, "treeChildren", "id")
  109. let count = 0
  110. menuInfoIds.forEach(item => {
  111. let lfind = arrayExistObj(this.menuInfoSet,"menuInfoId",item)
  112. if(lfind > -1) count++
  113. });
  114. if(menuInfoIds.length == count) this.checkedKeys.push(e.menuInfoId)
  115. }else{
  116. this.checkedKeys.push(e.menuInfoId)
  117. }
  118. });
  119. this.$refs["customerOrgTree"].setCheckedKeys(this.checkedKeys);
  120. },
  121. //提交按钮(保存角色权限)
  122. btnSubmit(){
  123. let body = {
  124. roleId:this.dataTransOpts.tableS.adp_roles.id,
  125. menuInfoIds:[]
  126. }
  127. this.menuInfoSet.forEach(e => {
  128. if(e.menuInfoId) body.menuInfoIds.push(e.menuInfoId)
  129. });
  130. postapi('/api/app/rolemenuinfo/setrolemenuinfo',body).then(res =>{
  131. if(res.code != -1){
  132. console.log('操作成功!')
  133. this.dialogWin.MenuPageSet = false
  134. }
  135. })
  136. },
  137. //勾选节点
  138. handleCheckChange(data, checked, indeterminate) {
  139. console.log('data, checked, indeterminate,this.menuInfoSet',data, checked, indeterminate,this.menuInfoSet);
  140. let lfind = 0,count = 0
  141. if(checked){
  142. // 获取所有上级节点
  143. let pids = getTreePids(this.menuInfoView, "treeChildren", "parentId", "id", data.id)
  144. pids.push(data.id) //添加自身节点
  145. pids.forEach(e => {
  146. if(e){
  147. lfind = arrayExistObj(this.menuInfoSet,'menuInfoId',e)
  148. if(lfind == -1) this.menuInfoSet.push({ menuInfoId:e })
  149. }
  150. });
  151. }else if(!indeterminate){
  152. // 取消授权时
  153. lfind = arrayExistObj(this.menuInfoSet,'menuInfoId',data.id)
  154. if(lfind > -1) this.menuInfoSet.splice(lfind,1)
  155. // 如果有下级节点,则取消所有下级节点的授权
  156. // let node = getTreeNode(this.menuInfoView, "treeChildren", "id", data.id)
  157. // if(node.treeChildren){
  158. // let childs = getTreeAllChildIdsByNode(node, "treeChildren", "id")
  159. // childs.forEach(id => {
  160. // lfind = arrayExistObj(this.menuInfoSet,'menuInfoId',id)
  161. // if(lfind > -1) this.menuInfoSet.splice(lfind,1)
  162. // });
  163. // }
  164. // 所有平级节点都被取消时,上级节点也应取消
  165. let parentIds = getTreePids(this.menuInfoView, "treeChildren", "parentId", "id", data.id)
  166. parentIds.forEach(e => {
  167. if(e){
  168. let pNode = getTreeNode(this.menuInfoView, "treeChildren", "id", e)
  169. count = pNode["treeChildren"].length
  170. pNode["treeChildren"].forEach(item => {
  171. lfind = arrayExistObj(this.menuInfoSet,'menuInfoId',item.id)
  172. if(lfind == -1) count--
  173. });
  174. if(count == 0){
  175. lfind = arrayExistObj(this.menuInfoSet,'menuInfoId',e)
  176. if(lfind > -1) this.menuInfoSet.splice(lfind,1)
  177. }
  178. }
  179. });
  180. }
  181. console.log('this.menuInfoSet',this.menuInfoSet)
  182. },
  183. //树过滤
  184. filterNode(value, data) {
  185. //console.log(value,data)
  186. if (!value) return true;
  187. return data['displayName'].indexOf(value) !== -1 || data['simpleCode'].indexOf(value.toUpperCase()) !== -1;
  188. }
  189. },
  190. watch: {
  191. // 刷新菜单
  192. "dataTransOpts.refresh.role_menu_info.M":{
  193. immediate:true,
  194. handler(newVal, oldVal) {
  195. let roleOrUser_Id
  196. if(this.isRole){
  197. roleOrUser_Id = this.dataTransOpts.tableS.adp_roles.id
  198. }else{
  199. roleOrUser_Id = this.dataTransOpts.tableS.adp_users.id
  200. }
  201. console.log(`watch ${this.isRole ? '角色':'用户'} 权限 newVal:${newVal} oldVal:${oldVal} registerCheckId: ${roleOrUser_Id}`);
  202. this.initData(roleOrUser_Id)
  203. }
  204. },
  205. "customerOrg.treeCurrentNodekey"(newVal,oldVal){
  206. //console.log('watch:customerOrg.treeCurrentNodekey',newVal,oldVal)
  207. if(newVal && newVal != oldVal){
  208. this.$nextTick(() => {
  209. this.$refs['customerOrgTree'].setCurrentKey(newVal);
  210. })
  211. }
  212. },
  213. "filterText"(newVal,oldVal){ //过滤菜单
  214. this.$refs['customerOrgTree'].filter(newVal);
  215. }
  216. },
  217. };
  218. </script>
  219. <style scoped>
  220. @import "../../assets/css/global.css";
  221. @import "../../assets/css/global_table.css";
  222. </style>