adminOperation.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div class='m-container'>
  3. <h2>
  4. <el-page-header @back="onCancel"
  5. :content="(pageType == 'create' ? '添加' : '修改') + '角色'"></el-page-header>
  6. </h2>
  7. <div class="m-core">
  8. <el-form ref="form"
  9. label-width="120px"
  10. style="width: 500px">
  11. <el-form-item label="角色名称"
  12. prop="roleName">
  13. <el-input v-model="result.roleName"></el-input>
  14. </el-form-item>
  15. <el-form-item label="角色描述">
  16. <el-input type="textarea"
  17. v-model="result.roleDesc"></el-input>
  18. </el-form-item>
  19. <el-form-item label="基本权限">
  20. <el-checkbox :indeterminate="isIndeterminate"
  21. @change="onCheckAll"
  22. v-model="checkAll">全选</el-checkbox>
  23. <el-tree :data="data"
  24. show-checkbox
  25. node-key="id"
  26. @check="onTreeCheck"
  27. ref="tree"
  28. accordion
  29. highlight-current
  30. :default-checked-keys="result.menuIds"
  31. :props="defaultProps">
  32. <div slot-scope="{ node, data }">
  33. {{ node.label }}
  34. <el-tag v-if="data.type == 1"
  35. size="mini"
  36. effect="dark">按钮</el-tag>
  37. </div>
  38. </el-tree>
  39. </el-form-item>
  40. <el-form-item>
  41. <el-button @click="onSubmit"
  42. type="primary">立即{{ pageType == "create" ? '创建' : '修改' }}</el-button>
  43. <el-button @click="onReSet('form')">重置</el-button>
  44. </el-form-item>
  45. </el-form>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. import { getUserRole } from '@/api/systemManage'
  51. import store from '@/store'
  52. import { getSilder } from '@/api/silder'
  53. import { roleGetMenus, getRoleInfo, roleUpdate, roleAdd } from '@/api/systemManage'
  54. export default {
  55. data () {
  56. return {
  57. organId: null,
  58. pageType: this.$route.query.type,
  59. id: this.$route.query.id,
  60. page: this.$route.query.page,
  61. isIndeterminate: false,
  62. data: [],
  63. defaultProps: {
  64. children: 'children',
  65. label: 'label'
  66. },
  67. result: {
  68. roleName: null,
  69. roleDesc: null,
  70. },
  71. checkAll: false,
  72. splice: [],
  73. allChildIds: [], // 所有子编号
  74. slideCount: 0
  75. }
  76. },
  77. mounted () {
  78. this.lookSilder()
  79. },
  80. methods: {
  81. onSubmit () {
  82. let tempIds = this.$refs.tree.getCheckedKeys()
  83. let halfIds = this.$refs.tree.getHalfCheckedKeys()
  84. let allIds = [...tempIds, ...halfIds]
  85. if (this.pageType == 'update') {
  86. roleUpdate({
  87. id: this.id,
  88. organId: this.organId,
  89. roleDesc: this.result.roleDesc,
  90. roleName: this.result.roleName,
  91. menuIds: allIds
  92. }).then(res => {
  93. this.messageTips('修改', res)
  94. })
  95. } else if (this.pageType == 'create') {
  96. roleAdd({
  97. organId: this.organId,
  98. roleDesc: this.result.roleDesc,
  99. roleName: this.result.roleName,
  100. menuIds: allIds
  101. }).then(res => {
  102. this.messageTips('添加', res)
  103. })
  104. }
  105. },
  106. messageTips (title, res) {
  107. if (res.code == 200) {
  108. this.$message.success('修改成功')
  109. this.$router.push({ path: '/specialSetup/adminManager', query: { page: this.page } })
  110. } else {
  111. this.$message.error(res.msg)
  112. }
  113. },
  114. async lookSilder () {
  115. let silderList = await getSilder({ hid: 0 })
  116. let tempData = []
  117. if (silderList.code == 200) {
  118. tempData = this.setTableData(silderList.data)
  119. this.data = tempData
  120. }
  121. if (this.pageType == 'update') {
  122. let roleInfo = await getRoleInfo({ id: this.id })
  123. if (roleInfo.code == 200) {
  124. let roleData = roleInfo.data
  125. // 是否是全部选中
  126. this.checkAll = roleData.menuIds.length >= this.slideCount
  127. // 是否是半选
  128. this.isIndeterminate = roleData.menuIds.length > 0 && roleData.menuIds.length < this.slideCount
  129. let tSplice = this.getParent(roleData.menuIds, tempData)
  130. roleData.menuIds = tSplice
  131. this.result = roleData
  132. }
  133. }
  134. },
  135. onTreeCheck () {
  136. let checkTree = this.$refs.tree.getCheckedKeys()
  137. this.checkAll = checkTree.length >= this.slideCount
  138. this.isIndeterminate = checkTree.length > 0 && checkTree.length < this.slideCount
  139. },
  140. onCheckAll (val) {
  141. if (val) {
  142. // 先去掉半选
  143. this.isIndeterminate = false
  144. this.$refs.tree.setCheckedNodes(this.data)
  145. } else {
  146. this.$refs.tree.setCheckedNodes([])
  147. }
  148. },
  149. //递归获取到所有的为子级的ID
  150. getParent (checkIds, data) {
  151. let removeIds = JSON.parse(JSON.stringify(checkIds))
  152. this.getAllChildIds(data)
  153. let tempAllChildIds = this.allChildIds
  154. for (let i = checkIds.length; i > 0; i--) {
  155. if (!tempAllChildIds.includes(checkIds[i - 1])) {
  156. removeIds.splice(i - 1, 1)
  157. }
  158. }
  159. return removeIds
  160. },
  161. getAllChildIds (data) {
  162. // 获取所有最子集编号
  163. let child = this.allChildIds
  164. let tempList = []
  165. data.forEach((item, index) => {
  166. let temp = []
  167. if (item.children && item.children.length > 0) {
  168. temp = this.getAllChildIds(item.children)
  169. } else {
  170. child.push(item.id)
  171. }
  172. })
  173. },
  174. setTableData (result) {
  175. let list = []
  176. list = result.map(res => {
  177. let tempList = {}
  178. tempList = {
  179. id: res.id,
  180. name: res.name,
  181. label: res.name,
  182. type: res.type,
  183. path: res.path,
  184. permission: res.permission,
  185. icon: res.icon,
  186. parentId: res.parentId
  187. }
  188. this.slideCount++
  189. if (res.sysMenus && res.sysMenus.length > 0) {
  190. tempList.children = this.setTableData(res.sysMenus)
  191. }
  192. return tempList
  193. })
  194. return list
  195. },
  196. onReSet () {
  197. this.$refs.tree.setCheckedNodes([])
  198. this.result = {
  199. roleName: null,
  200. roleDesc: null,
  201. }
  202. this.checkAll = false
  203. },
  204. onCancel () {
  205. this.$router.push({
  206. path: '/specialSetup/adminManager',
  207. query: {
  208. page: this.$route.query.page
  209. }
  210. })
  211. }
  212. }
  213. }
  214. </script>
  215. <style lang="scss" scoped>
  216. .el-button--primary {
  217. background: #14928a;
  218. border-color: #14928a;
  219. color: #fff;
  220. &:hover,
  221. &:active,
  222. &:focus {
  223. background: #14928a;
  224. border-color: #14928a;
  225. color: #fff;
  226. }
  227. }
  228. .el-row {
  229. margin-top: 40px;
  230. }
  231. .el-col {
  232. display: flex;
  233. align-items: center;
  234. margin-bottom: 20px;
  235. justify-content: flex-end;
  236. margin-right: 50%;
  237. }
  238. .el-input-group {
  239. width: 200px;
  240. margin: 0 20px;
  241. }
  242. /deep/.el-tree-node__content {
  243. height: 40px !important;
  244. }
  245. </style>