adminOperation.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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:212px"
  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. ref="tree"
  37. accordion
  38. highlight-current
  39. :default-checked-keys="result.menuIds"
  40. :filter-node-method="seachRole"
  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. allChildIds: [], // 所有子编号
  85. slideCount: 0,
  86. seachRoleValue: '' //权限搜索字段
  87. }
  88. },
  89. mounted () {
  90. this.onReSet()
  91. this.init()
  92. },
  93. activated () {
  94. this.onReSet()
  95. this.init()
  96. },
  97. methods: {
  98. init () {
  99. this.pageType = this.$route.query.type
  100. this.id = this.$route.query.id
  101. this.page = this.$route.query.page
  102. if (this.pageType == 'create') {
  103. }
  104. this.lookSilder()
  105. },
  106. onSubmit () {
  107. let tempIds = this.$refs.tree.getCheckedKeys()
  108. let halfIds = this.$refs.tree.getHalfCheckedKeys()
  109. let allIds = [...tempIds, ...halfIds]
  110. if (this.pageType == 'update') {
  111. roleUpdate({
  112. id: this.id,
  113. organId: this.organId,
  114. roleDesc: this.result.roleDesc,
  115. roleName: this.result.roleName,
  116. menuIds: allIds
  117. }).then(res => {
  118. this.messageTips('修改', res)
  119. })
  120. } else if (this.pageType == 'create') {
  121. roleAdd({
  122. organId: this.organId,
  123. roleDesc: this.result.roleDesc,
  124. roleName: this.result.roleName,
  125. menuIds: allIds
  126. }).then(res => {
  127. this.messageTips('添加', res)
  128. })
  129. }
  130. },
  131. messageTips (title, res) {
  132. if (res.code == 200) {
  133. this.$message.success('修改成功')
  134. this.$router.push({ path: '/specialSetup/adminManager', query: { page: this.page } })
  135. } else {
  136. this.$message.error(res.msg)
  137. }
  138. },
  139. async lookSilder () {
  140. let silderList = await getSilder({ hid: 0 })
  141. let tempData = []
  142. if (silderList.code == 200) {
  143. tempData = this.setTableData(silderList.data)
  144. this.data = tempData
  145. console.log(this.data)
  146. }
  147. // console.log(this.pageType)
  148. if (this.pageType == 'update') {
  149. let roleInfo = await getRoleInfo({ id: this.id })
  150. if (roleInfo.code == 200) {
  151. let roleData = roleInfo.data
  152. // 是否是全部选中
  153. this.checkAll = roleData.menuIds.length >= this.slideCount
  154. // 是否是半选
  155. this.isIndeterminate = roleData.menuIds.length > 0 && roleData.menuIds.length < this.slideCount
  156. let tSplice = this.getParent(roleData.menuIds, tempData)
  157. roleData.menuIds = tSplice
  158. this.result = roleData
  159. }
  160. } else {
  161. this.onReSet()
  162. }
  163. },
  164. onTreeCheck () {
  165. let checkTree = this.$refs.tree.getCheckedKeys()
  166. this.checkAll = checkTree.length >= this.slideCount
  167. this.isIndeterminate = checkTree.length > 0 && checkTree.length < this.slideCount
  168. },
  169. onCheckAll (val) {
  170. if (val) {
  171. // 先去掉半选
  172. this.isIndeterminate = false
  173. this.$refs.tree.setCheckedNodes(this.data)
  174. } else {
  175. this.$refs.tree.setCheckedNodes([])
  176. }
  177. },
  178. //递归获取到所有的为子级的ID
  179. getParent (checkIds, data) {
  180. let removeIds = JSON.parse(JSON.stringify(checkIds))
  181. this.getAllChildIds(data)
  182. let tempAllChildIds = this.allChildIds
  183. for (let i = checkIds.length; i > 0; i--) {
  184. if (!tempAllChildIds.includes(checkIds[i - 1])) {
  185. removeIds.splice(i - 1, 1)
  186. }
  187. }
  188. return removeIds
  189. },
  190. getAllChildIds (data) {
  191. // 获取所有最子集编号
  192. let child = this.allChildIds
  193. let tempList = []
  194. data.forEach((item, index) => {
  195. let temp = []
  196. if (item.children && item.children.length > 0) {
  197. temp = this.getAllChildIds(item.children)
  198. } else {
  199. child.push(item.id)
  200. }
  201. })
  202. },
  203. setTableData (result) {
  204. let list = []
  205. list = result.map(res => {
  206. let tempList = {}
  207. tempList = {
  208. id: res.id,
  209. name: res.name,
  210. label: res.name,
  211. type: res.type,
  212. path: res.path,
  213. permission: res.permission,
  214. icon: res.icon,
  215. parentId: res.parentId
  216. }
  217. this.slideCount++
  218. if (res.sysMenus && res.sysMenus.length > 0) {
  219. tempList.children = this.setTableData(res.sysMenus)
  220. }
  221. return tempList
  222. })
  223. return list
  224. },
  225. onReSet () {
  226. this.$refs.tree.setCheckedNodes([])
  227. this.result = {
  228. roleName: null,
  229. roleDesc: null,
  230. }
  231. this.checkAll = false
  232. },
  233. onCancel () {
  234. this.$router.push({
  235. path: '/specialSetup/adminManager',
  236. query: {
  237. page: this.$route.query.page
  238. }
  239. })
  240. },
  241. seachRole (value, data) {
  242. // console.log(value, data)
  243. if (!value) return true;
  244. return data.label.indexOf(value) !== -1;
  245. },
  246. seachRoles () {
  247. console.log(this.$refs.tree.filter(this.seachRoleValue))
  248. },
  249. onReSetRole () { }
  250. }
  251. }
  252. </script>
  253. <style lang="scss" scoped>
  254. .el-button--primary {
  255. background: #14928a;
  256. border-color: #14928a;
  257. color: #fff;
  258. &:hover,
  259. &:active,
  260. &:focus {
  261. background: #14928a;
  262. border-color: #14928a;
  263. color: #fff;
  264. }
  265. }
  266. .el-row {
  267. margin-top: 40px;
  268. }
  269. .el-col {
  270. display: flex;
  271. align-items: center;
  272. margin-bottom: 20px;
  273. justify-content: flex-end;
  274. margin-right: 50%;
  275. }
  276. .el-input-group {
  277. width: 200px;
  278. margin: 0 20px;
  279. }
  280. /deep/.el-tree-node__content {
  281. height: 40px !important;
  282. }
  283. </style>