adminOperation.vue 8.1 KB

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