index.vue 10.0 KB

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