adminOperation.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.trim="result.roleName"></el-input>
  14. </el-form-item>
  15. <el-form-item label="角色描述">
  16. <el-input type="textarea"
  17. v-model.trim="result.roleDesc"></el-input>
  18. </el-form-item>
  19. <el-form-item label="基本权限">
  20. <el-checkbox :indeterminate="isIndeterminate"
  21. @change="onCheckAll"
  22. v-model.trim="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. name: 'adminOperation',
  56. data () {
  57. return {
  58. organId: null,
  59. pageType: null,
  60. id: null,
  61. page: null,
  62. isIndeterminate: false,
  63. data: [],
  64. defaultProps: {
  65. children: 'children',
  66. label: 'label'
  67. },
  68. result: {
  69. roleName: null,
  70. roleDesc: null,
  71. },
  72. checkAll: false,
  73. splice: [],
  74. allChildIds: [], // 所有子编号
  75. slideCount: 0
  76. }
  77. },
  78. mounted () {
  79. // this.onReSet()
  80. // this.init()
  81. },
  82. activated () {
  83. this.onReSet()
  84. this.init()
  85. },
  86. methods: {
  87. init () {
  88. this.pageType = this.$route.query.type
  89. this.id = this.$route.query.id
  90. this.page = this.$route.query.page
  91. if (this.pageType == 'create') {
  92. }
  93. this.lookSilder()
  94. },
  95. onSubmit () {
  96. let tempIds = this.$refs.tree.getCheckedKeys()
  97. let halfIds = this.$refs.tree.getHalfCheckedKeys()
  98. let allIds = [...tempIds, ...halfIds]
  99. if (this.pageType == 'update') {
  100. roleUpdate({
  101. id: this.id,
  102. organId: this.organId,
  103. roleDesc: this.result.roleDesc,
  104. roleName: this.result.roleName,
  105. menuIds: allIds
  106. }).then(res => {
  107. this.messageTips('修改', res)
  108. })
  109. } else if (this.pageType == 'create') {
  110. roleAdd({
  111. organId: this.organId,
  112. roleDesc: this.result.roleDesc,
  113. roleName: this.result.roleName,
  114. menuIds: allIds
  115. }).then(res => {
  116. this.messageTips('添加', res)
  117. })
  118. }
  119. },
  120. messageTips (title, res) {
  121. if (res.code == 200) {
  122. this.$message.success('修改成功')
  123. this.$router.push({ path: '/specialSetup/adminManager', query: { page: this.page } })
  124. } else {
  125. this.$message.error(res.msg)
  126. }
  127. },
  128. async lookSilder () {
  129. let silderList = await getSilder({ hid: 0 })
  130. let tempData = []
  131. if (silderList.code == 200) {
  132. tempData = this.setTableData(silderList.data)
  133. this.data = tempData
  134. }
  135. // console.log(this.pageType)
  136. if (this.pageType == 'update') {
  137. let roleInfo = await getRoleInfo({ id: this.id })
  138. if (roleInfo.code == 200) {
  139. let roleData = roleInfo.data
  140. console.log(roleData)
  141. // 是否是全部选中
  142. this.checkAll = roleData.menuIds.length >= this.slideCount
  143. // 是否是半选
  144. this.isIndeterminate = roleData.menuIds.length > 0 && roleData.menuIds.length < this.slideCount
  145. let tSplice = this.getParent(roleData.menuIds, tempData)
  146. roleData.menuIds = tSplice
  147. this.result = roleData
  148. }
  149. } else {
  150. this.onReSet()
  151. }
  152. },
  153. onTreeCheck () {
  154. let checkTree = this.$refs.tree.getCheckedKeys()
  155. this.checkAll = checkTree.length >= this.slideCount
  156. this.isIndeterminate = checkTree.length > 0 && checkTree.length < this.slideCount
  157. },
  158. onCheckAll (val) {
  159. if (val) {
  160. // 先去掉半选
  161. this.isIndeterminate = false
  162. this.$refs.tree.setCheckedNodes(this.data)
  163. } else {
  164. this.$refs.tree.setCheckedNodes([])
  165. }
  166. },
  167. //递归获取到所有的为子级的ID
  168. getParent (checkIds, data) {
  169. let removeIds = JSON.parse(JSON.stringify(checkIds))
  170. this.getAllChildIds(data)
  171. let tempAllChildIds = this.allChildIds
  172. for (let i = checkIds.length; i > 0; i--) {
  173. if (!tempAllChildIds.includes(checkIds[i - 1])) {
  174. removeIds.splice(i - 1, 1)
  175. }
  176. }
  177. return removeIds
  178. },
  179. getAllChildIds (data) {
  180. // 获取所有最子集编号
  181. let child = this.allChildIds
  182. let tempList = []
  183. data.forEach((item, index) => {
  184. let temp = []
  185. if (item.children && item.children.length > 0) {
  186. temp = this.getAllChildIds(item.children)
  187. } else {
  188. child.push(item.id)
  189. }
  190. })
  191. },
  192. setTableData (result) {
  193. let list = []
  194. list = result.map(res => {
  195. let tempList = {}
  196. tempList = {
  197. id: res.id,
  198. name: res.name,
  199. label: res.name,
  200. type: res.type,
  201. path: res.path,
  202. permission: res.permission,
  203. icon: res.icon,
  204. parentId: res.parentId
  205. }
  206. this.slideCount++
  207. if (res.sysMenus && res.sysMenus.length > 0) {
  208. tempList.children = this.setTableData(res.sysMenus)
  209. }
  210. return tempList
  211. })
  212. return list
  213. },
  214. onReSet () {
  215. this.$refs.tree.setCheckedNodes([])
  216. this.result = {
  217. roleName: null,
  218. roleDesc: null,
  219. }
  220. this.checkAll = false
  221. },
  222. onCancel () {
  223. this.$router.push({
  224. path: '/specialSetup/adminManager',
  225. query: {
  226. page: this.$route.query.page
  227. }
  228. })
  229. }
  230. }
  231. }
  232. </script>
  233. <style lang="scss" scoped>
  234. .el-button--primary {
  235. background: #14928a;
  236. border-color: #14928a;
  237. color: #fff;
  238. &:hover,
  239. &:active,
  240. &:focus {
  241. background: #14928a;
  242. border-color: #14928a;
  243. color: #fff;
  244. }
  245. }
  246. .el-row {
  247. margin-top: 40px;
  248. }
  249. .el-col {
  250. display: flex;
  251. align-items: center;
  252. margin-bottom: 20px;
  253. justify-content: flex-end;
  254. margin-right: 50%;
  255. }
  256. .el-input-group {
  257. width: 200px;
  258. margin: 0 20px;
  259. }
  260. /deep/.el-tree-node__content {
  261. height: 40px !important;
  262. }
  263. </style>