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