index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <!--
  2. - Copyright (c) 2018-2025, lengleng All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions are met:
  6. -
  7. - Redistributions of source code must retain the above copyright notice,
  8. - this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. - notice, this list of conditions and the following disclaimer in the
  11. - documentation and/or other materials provided with the distribution.
  12. - Neither the name of the pig4cloud.com developer nor the names of its
  13. - contributors may be used to endorse or promote products derived from
  14. - this software without specific prior written permission.
  15. - Author: lengleng (wangiegie@gmail.com)
  16. -->
  17. <template>
  18. <div class="app-container calendar-list-container">
  19. <basic-container>
  20. <avue-crud :option="tableOption"
  21. :data="list"
  22. ref="crud"
  23. :page="page"
  24. v-model="form"
  25. :table-loading="listLoading"
  26. :before-open="handleOpenBefore"
  27. @on-load="getList"
  28. @search-change="handleFilter"
  29. @refresh-change="handleRefreshChange"
  30. @row-update="update"
  31. @row-save="create">
  32. <template slot="menuLeft">
  33. <el-button v-if="roleManager_btn_add"
  34. class="filter-item"
  35. @click="handleCreate"
  36. size="small"
  37. type="primary"
  38. icon="el-icon-edit">添加</el-button>
  39. </template>
  40. <template slot="menu"
  41. slot-scope="scope">
  42. <el-button size="mini"
  43. type="text"
  44. icon="el-icon-edit"
  45. v-if="roleManager_btn_edit"
  46. @click="handleUpdate(scope.row,scope.index)">编辑
  47. </el-button>
  48. <el-button size="mini"
  49. type="text"
  50. icon="el-icon-delete"
  51. v-if="roleManager_btn_del"
  52. @click="handleDelete(scope.row,scope.index)">删除
  53. </el-button>
  54. <el-button size="mini"
  55. type="text"
  56. icon="el-icon-plus"
  57. plain
  58. @click="handlePermission(scope.row,scope.index)"
  59. v-if="roleManager_btn_perm">权限
  60. </el-button>
  61. </template>
  62. </avue-crud>
  63. </basic-container>
  64. <el-dialog title="分配权限"
  65. :visible.sync="dialogPermissionVisible">
  66. <el-tree class="filter-tree"
  67. :data="treeData"
  68. :default-checked-keys="checkedKeys"
  69. :check-strictly="false"
  70. node-key="id"
  71. highlight-current
  72. :props="defaultProps"
  73. show-checkbox
  74. ref="menuTree"
  75. :filter-node-method="filterNode"
  76. default-expand-all>
  77. </el-tree>
  78. <div slot="footer"
  79. class="dialog-footer">
  80. <el-button type="primary"
  81. @click="updatePermession(roleId, roleCode)">更 新</el-button>
  82. </div>
  83. </el-dialog>
  84. </div>
  85. </template>
  86. <script>
  87. import {
  88. fetchList,
  89. getObj,
  90. addObj,
  91. putObj,
  92. delObj,
  93. permissionUpd,
  94. fetchRoleTree
  95. } from '@/api/admin/role'
  96. import { fetchTree } from '@/api/admin/menu'
  97. import { mapGetters } from 'vuex'
  98. import { tableOption } from '@/const/crud/admin/role'
  99. export default {
  100. name: 'table_role',
  101. data () {
  102. return {
  103. tableOption: tableOption,
  104. treeData: [],
  105. checkedKeys: [],
  106. defaultProps: {
  107. label: "name",
  108. value: 'id'
  109. },
  110. page: {
  111. total: 0, // 总页数
  112. currentPage: 1, // 当前页数
  113. pageSize: 20 // 每页显示多少条
  114. },
  115. menuIds: '',
  116. list: [],
  117. listLoading: true,
  118. form: {},
  119. roleId: undefined,
  120. roleCode: undefined,
  121. rolesOptions: undefined,
  122. dialogPermissionVisible: false,
  123. roleManager_btn_add: false,
  124. roleManager_btn_edit: false,
  125. roleManager_btn_del: false,
  126. roleManager_btn_perm: false
  127. }
  128. },
  129. created () {
  130. this.roleManager_btn_add = this.permissions['sys_role_add']
  131. this.roleManager_btn_edit = this.permissions['sys_role_edit']
  132. this.roleManager_btn_del = this.permissions['sys_role_del']
  133. this.roleManager_btn_perm = this.permissions['sys_role_perm']
  134. },
  135. computed: {
  136. ...mapGetters(['elements', 'permissions'])
  137. },
  138. methods: {
  139. getList (page, params) {
  140. this.listLoading = true
  141. fetchList(Object.assign({
  142. current: page.currentPage,
  143. size: page.pageSize
  144. }, params)).then(response => {
  145. this.list = response.data.data.records
  146. this.page.total = response.data.data.total
  147. this.listLoading = false
  148. })
  149. },
  150. handleRefreshChange () {
  151. this.getList(this.page)
  152. },
  153. handleFilter (param) {
  154. this.page.page = 1;
  155. this.getList(this.page, param);
  156. },
  157. handleCreate () {
  158. this.$refs.crud.rowAdd();
  159. },
  160. handleOpenBefore (show, type) {
  161. show();
  162. },
  163. handleUpdate (row, index) {
  164. this.$refs.crud.rowEdit(row, index);
  165. },
  166. handlePermission (row) {
  167. fetchRoleTree(row.roleId)
  168. .then(response => {
  169. this.checkedKeys = response.data
  170. return fetchTree()
  171. })
  172. .then(response => {
  173. this.treeData = response.data.data
  174. // 解析出所有的太监节点
  175. this.checkedKeys = this.resolveAllEunuchNodeId(this.treeData, this.checkedKeys, [])
  176. this.dialogStatus = 'permission'
  177. this.dialogPermissionVisible = true
  178. this.roleId = row.roleId
  179. this.roleCode = row.roleCode
  180. })
  181. },
  182. /**
  183. * 解析出所有的太监节点id
  184. * @param json 待解析的json串
  185. * @param idArr 原始节点数组
  186. * @param temp 临时存放节点id的数组
  187. * @return 太监节点id数组
  188. */
  189. resolveAllEunuchNodeId (json, idArr, temp) {
  190. for (let i = 0; i < json.length; i++) {
  191. const item = json[i]
  192. // 存在子节点,递归遍历;不存在子节点,将json的id添加到临时数组中
  193. if (item.children && item.children.length !== 0) {
  194. this.resolveAllEunuchNodeId(item.children, idArr, temp)
  195. } else {
  196. temp.push(idArr.filter(id => id === item.id))
  197. }
  198. }
  199. return temp
  200. },
  201. filterNode (value, data) {
  202. if (!value) return true
  203. return data.label.indexOf(value) !== -1
  204. },
  205. getNodeData (data, done) {
  206. done();
  207. },
  208. handleDelete (row, index) {
  209. delObj(row.roleId).then(response => {
  210. this.list.splice(index, 1);
  211. this.$notify({
  212. title: '成功',
  213. message: '删除成功',
  214. type: 'success',
  215. duration: 2000
  216. })
  217. })
  218. },
  219. create (row, done, loading) {
  220. addObj(this.form).then(() => {
  221. this.getList(this.page)
  222. done();
  223. this.$notify({
  224. title: '成功',
  225. message: '创建成功',
  226. type: 'success',
  227. duration: 2000
  228. })
  229. }).catch(() => {
  230. loading();
  231. });
  232. },
  233. update (row, index, done, loading) {
  234. putObj(this.form).then(() => {
  235. this.getList(this.page)
  236. done();
  237. this.$notify({
  238. title: '成功',
  239. message: '修改成功',
  240. type: 'success',
  241. duration: 2000
  242. })
  243. }).catch(() => {
  244. loading();
  245. });
  246. },
  247. updatePermession (roleId, roleCode) {
  248. this.menuIds = ''
  249. this.menuIds = this.$refs.menuTree.getCheckedKeys().join(',').concat(',').concat(this.$refs.menuTree.getHalfCheckedKeys().join(','))
  250. permissionUpd(roleId, this.menuIds).then(() => {
  251. this.dialogPermissionVisible = false
  252. fetchTree()
  253. .then(response => {
  254. this.form = response.data.data
  255. return fetchRoleTree(roleId)
  256. })
  257. .then(response => {
  258. this.checkedKeys = response.data
  259. this.$notify({
  260. title: '成功',
  261. message: '修改成功',
  262. type: 'success',
  263. duration: 2000
  264. })
  265. })
  266. })
  267. }
  268. }
  269. }
  270. </script>