index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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
  21. ref="crud"
  22. :option="tableOption"
  23. :data="list"
  24. :page="page"
  25. v-model="form"
  26. :table-loading="listLoading"
  27. :before-open="handleOpenBefore"
  28. @on-load="getList"
  29. @search-change="handleFilter"
  30. @refresh-change="handleRefreshChange"
  31. @row-update="update"
  32. @row-save="create">
  33. <template slot="menuLeft">
  34. <el-button
  35. v-if="roleManager_btn_add"
  36. class="filter-item"
  37. type="primary"
  38. icon="el-icon-edit"
  39. @click="handleCreate">添加
  40. </el-button>
  41. </template>
  42. <template slot="dsScopeForm" slot-scope="scope">
  43. <div v-if="form.dsType == 1">
  44. <el-tree
  45. ref="scopeTree"
  46. :data="dsScopeData"
  47. :check-strictly="true"
  48. :props="defaultProps"
  49. :default-checked-keys="checkedDsScope"
  50. class="filter-tree"
  51. node-key="id"
  52. highlight-current
  53. show-checkbox/>
  54. </div>
  55. </template>
  56. <template
  57. slot="menu"
  58. slot-scope="scope">
  59. <el-button
  60. v-if="roleManager_btn_edit"
  61. type="text"
  62. size="mini"
  63. icon="el-icon-edit"
  64. @click="handleUpdate(scope.row,scope.index)">编辑
  65. </el-button>
  66. <el-button
  67. v-if="roleManager_btn_del"
  68. type="text"
  69. size="mini"
  70. icon="el-icon-delete"
  71. @click="handleDelete(scope.row,scope.index)">删除
  72. </el-button>
  73. <el-button
  74. v-if="roleManager_btn_perm"
  75. type="text"
  76. size="mini"
  77. icon="el-icon-plus"
  78. @click="handlePermission(scope.row,scope.index)">权限
  79. </el-button>
  80. </template>
  81. </avue-crud>
  82. </basic-container>
  83. <el-dialog
  84. :visible.sync="dialogPermissionVisible"
  85. :close-on-click-modal="false"
  86. title="分配权限">
  87. <div class="dialog-main-tree">
  88. <el-tree
  89. ref="menuTree"
  90. :data="treeData"
  91. :default-checked-keys="checkedKeys"
  92. :check-strictly="false"
  93. :props="defaultProps"
  94. :filter-node-method="filterNode"
  95. class="filter-tree"
  96. node-key="id"
  97. highlight-current
  98. show-checkbox
  99. default-expand-all/>
  100. </div>
  101. <div slot="footer"
  102. class="dialog-footer">
  103. <el-button
  104. type="primary"
  105. size="small"
  106. @click="updatePermession(roleId)">更 新
  107. </el-button>
  108. <el-button
  109. type="default"
  110. size="small"
  111. @click="cancal()">取消</el-button>
  112. </div>
  113. </el-dialog>
  114. </div>
  115. </template>
  116. <script>
  117. import { addObj, delObj, fetchList, fetchRoleTree, permissionUpd, putObj } from '@/api/admin/role'
  118. import { tableOption } from '@/const/crud/admin/role'
  119. import { fetchTree } from '@/api/admin/dept'
  120. import { fetchMenuTree } from '@/api/admin/menu'
  121. import { mapGetters } from 'vuex'
  122. export default {
  123. name: 'TableRole',
  124. data() {
  125. return {
  126. searchForm: {},
  127. tableOption: tableOption,
  128. dsScopeData: [],
  129. treeData: [],
  130. checkedKeys: [],
  131. checkedDsScope: [],
  132. defaultProps: {
  133. label: 'name',
  134. value: 'id'
  135. },
  136. page: {
  137. total: 0, // 总页数
  138. currentPage: 1, // 当前页数
  139. pageSize: 20 // 每页显示多少条
  140. },
  141. menuIds: '',
  142. list: [],
  143. listLoading: true,
  144. form: {},
  145. roleId: undefined,
  146. roleCode: undefined,
  147. rolesOptions: undefined,
  148. dialogPermissionVisible: false,
  149. roleManager_btn_add: false,
  150. roleManager_btn_edit: false,
  151. roleManager_btn_del: false,
  152. roleManager_btn_perm: false
  153. }
  154. },
  155. created() {
  156. this.roleManager_btn_add = this.permissions['sys_role_add']
  157. this.roleManager_btn_edit = this.permissions['sys_role_edit']
  158. this.roleManager_btn_del = this.permissions['sys_role_del']
  159. this.roleManager_btn_perm = this.permissions['sys_role_perm']
  160. },
  161. computed: {
  162. ...mapGetters(['elements', 'permissions'])
  163. },
  164. methods: {
  165. getList(page, params) {
  166. this.listLoading = true
  167. fetchList(Object.assign({
  168. current: page.currentPage,
  169. size: page.pageSize
  170. }, params, this.searchForm)).then(response => {
  171. this.list = response.data.data.records
  172. this.page.total = response.data.data.total
  173. this.listLoading = false
  174. }).catch(() => {
  175. this.listLoading = false
  176. })
  177. },
  178. handleRefreshChange() {
  179. this.getList(this.page)
  180. },
  181. handleFilter(form) {
  182. this.searchForm = form
  183. this.getList(this.page, form)
  184. },
  185. handleCreate() {
  186. this.$refs.crud.rowAdd()
  187. },
  188. handleOpenBefore(show) {
  189. fetchTree().then(response => {
  190. this.dsScopeData = response.data.data
  191. if (this.form.dsScope) {
  192. this.checkedDsScope = (this.form.dsScope).split(',')
  193. } else {
  194. this.checkedDsScope = []
  195. }
  196. })
  197. show()
  198. },
  199. handleUpdate(row, index) {
  200. this.$refs.crud.rowEdit(row, index)
  201. },
  202. cancal () {
  203. this.dialogPermissionVisible = false;
  204. },
  205. handlePermission(row) {
  206. fetchRoleTree(row.roleId)
  207. .then(response => {
  208. this.checkedKeys = response.data.data
  209. return fetchMenuTree()
  210. })
  211. .then(response => {
  212. this.treeData = response.data.data
  213. // 解析出所有的太监节点
  214. this.checkedKeys = this.resolveAllEunuchNodeId(this.treeData, this.checkedKeys, [])
  215. this.dialogPermissionVisible = true
  216. this.roleId = row.roleId
  217. this.roleCode = row.roleCode
  218. })
  219. },
  220. /**
  221. * 解析出所有的太监节点id
  222. * @param json 待解析的json串
  223. * @param idArr 原始节点数组
  224. * @param temp 临时存放节点id的数组
  225. * @return 太监节点id数组
  226. */
  227. resolveAllEunuchNodeId(json, idArr, temp) {
  228. for (let i = 0; i < json.length; i++) {
  229. const item = json[i]
  230. // 存在子节点,递归遍历;不存在子节点,将json的id添加到临时数组中
  231. if (item.children && item.children.length !== 0) {
  232. this.resolveAllEunuchNodeId(item.children, idArr, temp)
  233. } else {
  234. temp.push(idArr.filter(id => id === item.id))
  235. }
  236. }
  237. return temp
  238. },
  239. filterNode(value, data) {
  240. if (!value) return true
  241. return data.label.indexOf(value) !== -1
  242. },
  243. getNodeData(data, done) {
  244. done()
  245. },
  246. handleDelete(row, index) {
  247. var _this = this
  248. this.$confirm('是否确认删除名称为"' + row.roleName + '"' + '"的数据项?', '警告', {
  249. confirmButtonText: '确定',
  250. cancelButtonText: '取消',
  251. type: 'warning'
  252. }).then(function() {
  253. return delObj(row.roleId)
  254. }).then(() => {
  255. this.getList(this.page)
  256. this.$notify.success('删除成功')
  257. })
  258. },
  259. create(row, done, loading) {
  260. if (this.form.dsType === 1) {
  261. this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
  262. }
  263. addObj(this.form).then(() => {
  264. this.getList(this.page)
  265. done()
  266. this.$notify.success('创建成功')
  267. }).catch(() => {
  268. loading()
  269. })
  270. },
  271. update(row, index, done, loading) {
  272. if (this.form.dsType === 1) {
  273. this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
  274. }
  275. putObj(this.form).then(() => {
  276. this.getList(this.page)
  277. done()
  278. this.$notify.success('修改成功')
  279. }).catch(() => {
  280. loading()
  281. })
  282. },
  283. updatePermession(roleId) {
  284. this.menuIds = ''
  285. this.menuIds = this.$refs.menuTree.getCheckedKeys().join(',').concat(',').concat(this.$refs.menuTree.getHalfCheckedKeys().join(','))
  286. permissionUpd(roleId, this.menuIds).then(() => {
  287. this.dialogPermissionVisible = false
  288. this.$notify.success('修改成功')
  289. })
  290. }
  291. }
  292. }
  293. </script>
  294. <style lang="scss" scoped>
  295. .el-dialog__wrapper {
  296. .el-dialog {
  297. width: 61% !important;
  298. .dialog-main-tree {
  299. max-height: 400px;
  300. overflow-y: auto;
  301. }
  302. }
  303. .el-form-item__label {
  304. width: 20% !important;
  305. padding-right: 20px;
  306. }
  307. .el-form-item__content {
  308. margin-left: 20% !important;
  309. }
  310. }
  311. </style>