adminOperation.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.$store.dispatch('delVisitedViews', this.$route)
  136. this.$router.push({ path: '/specialSetup/adminManager', query: { page: this.page } })
  137. } else {
  138. this.$message.error(res.msg)
  139. }
  140. },
  141. async lookSilder () {
  142. let silderList = await getSilder({ hid: 0 })
  143. let tempData = []
  144. if (silderList.code == 200) {
  145. this.silderList = silderList.data
  146. tempData = this.setTableData(silderList.data)
  147. this.data = tempData
  148. console.log(this.data)
  149. }
  150. // console.log(this.pageType)
  151. if (this.pageType == 'update') {
  152. let roleInfo = await getRoleInfo({ id: this.id })
  153. if (roleInfo.code == 200) {
  154. let roleData = roleInfo.data
  155. // 是否是全部选中
  156. this.checkAll = roleData.menuIds.length >= this.slideCount
  157. // 是否是半选
  158. this.isIndeterminate = roleData.menuIds.length > 0 && roleData.menuIds.length < this.slideCount
  159. let tSplice = this.getParent(roleData.menuIds, tempData)
  160. roleData.menuIds = tSplice
  161. this.result = roleData
  162. }
  163. } else {
  164. this.onReSet()
  165. }
  166. },
  167. onTreeCheck () {
  168. let checkTree = this.$refs.tree.getCheckedKeys()
  169. this.checkAll = checkTree.length >= this.slideCount
  170. this.isIndeterminate = checkTree.length > 0 && checkTree.length < this.slideCount
  171. },
  172. onCheckAll (val) {
  173. if (val) {
  174. // 先去掉半选
  175. this.isIndeterminate = false
  176. this.$refs.tree.setCheckedNodes(this.data)
  177. } else {
  178. this.$refs.tree.setCheckedNodes([])
  179. }
  180. },
  181. //递归获取到所有的为子级的ID
  182. getParent (checkIds, data) {
  183. let removeIds = JSON.parse(JSON.stringify(checkIds))
  184. this.getAllChildIds(data)
  185. let tempAllChildIds = this.allChildIds
  186. for (let i = checkIds.length; i > 0; i--) {
  187. if (!tempAllChildIds.includes(checkIds[i - 1])) {
  188. removeIds.splice(i - 1, 1)
  189. }
  190. }
  191. return removeIds
  192. },
  193. getAllChildIds (data) {
  194. // 获取所有最子集编号
  195. let child = this.allChildIds
  196. let tempList = []
  197. data.forEach((item, index) => {
  198. let temp = []
  199. if (item.children && item.children.length > 0) {
  200. temp = this.getAllChildIds(item.children)
  201. } else {
  202. child.push(item.id)
  203. }
  204. })
  205. },
  206. setTableData (result) {
  207. let list = []
  208. list = result.map(res => {
  209. let tempList = {}
  210. tempList = {
  211. id: res.id,
  212. name: res.name,
  213. label: res.name,
  214. type: res.type,
  215. path: res.path,
  216. permission: res.permission,
  217. icon: res.icon,
  218. parentId: res.parentId
  219. }
  220. this.slideCount++
  221. if (res.sysMenus && res.sysMenus.length > 0) {
  222. tempList.children = this.setTableData(res.sysMenus)
  223. }
  224. return tempList
  225. })
  226. return list
  227. },
  228. onReSet () {
  229. this.$refs.tree.setCheckedNodes([])
  230. this.result = {
  231. roleName: null,
  232. roleDesc: null,
  233. }
  234. this.checkAll = false
  235. },
  236. onCancel () {
  237. this.$store.dispatch('delVisitedViews', this.$route)
  238. this.$router.push({
  239. path: '/specialSetup/adminManager',
  240. query: {
  241. page: this.$route.query.page
  242. }
  243. })
  244. },
  245. seachRoles () {
  246. this.$refs.tree.filter(this.seachRoleValue);
  247. },
  248. filterNode(value, data) {
  249. console.log(data)
  250. if (!value) return true;
  251. return data.label.indexOf(value) !== -1;
  252. },
  253. onReSetRole () {
  254. this.seachRoleValue = ''
  255. this.data = this.setTableData(this.silderList)
  256. }
  257. }
  258. }
  259. </script>
  260. <style lang="scss" scoped>
  261. .el-button--primary {
  262. background: #14928a;
  263. border-color: #14928a;
  264. color: #fff;
  265. &:hover,
  266. &:active,
  267. &:focus {
  268. background: #14928a;
  269. border-color: #14928a;
  270. color: #fff;
  271. }
  272. }
  273. .el-row {
  274. margin-top: 40px;
  275. }
  276. .el-col {
  277. display: flex;
  278. align-items: center;
  279. margin-bottom: 20px;
  280. justify-content: flex-end;
  281. margin-right: 50%;
  282. }
  283. .el-input-group {
  284. width: 200px;
  285. margin: 0 20px;
  286. }
  287. /deep/.el-tree-node__content {
  288. height: 40px !important;
  289. }
  290. </style>