123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <!--
- - Copyright (c) 2018-2025, lengleng All rights reserved.
- -
- - Redistribution and use in source and binary forms, with or without
- - modification, are permitted provided that the following conditions are met:
- -
- - Redistributions of source code must retain the above copyright notice,
- - this list of conditions and the following disclaimer.
- - Redistributions in binary form must reproduce the above copyright
- - notice, this list of conditions and the following disclaimer in the
- - documentation and/or other materials provided with the distribution.
- - Neither the name of the pig4cloud.com developer nor the names of its
- - contributors may be used to endorse or promote products derived from
- - this software without specific prior written permission.
- - Author: lengleng (wangiegie@gmail.com)
- -->
- <template>
- <div class="app-container calendar-list-container">
- <basic-container>
- <avue-crud
- ref="crud"
- :option="tableOption"
- :data="list"
- :page="page"
- v-model="form"
- :table-loading="listLoading"
- :before-open="handleOpenBefore"
- @on-load="getList"
- @search-change="handleFilter"
- @refresh-change="handleRefreshChange"
- @row-update="update"
- @row-save="create">
- <template slot="menuLeft">
- <el-button
- v-if="roleManager_btn_add"
- class="filter-item"
- size="small"
- type="primary"
- icon="el-icon-edit"
- @click="handleCreate">添加
- </el-button>
- </template>
- <template slot="dsScopeForm" slot-scope="scope">
- <div v-if="form.dsType == 1">
- <el-tree
- ref="scopeTree"
- :data="dsScopeData"
- :check-strictly="true"
- :props="defaultProps"
- :default-checked-keys="checkedDsScope"
- class="filter-tree"
- node-key="id"
- highlight-current
- show-checkbox/>
- </div>
- </template>
- <template
- slot="menu"
- slot-scope="scope">
- <el-button
- v-if="roleManager_btn_edit"
- size="mini"
- type="text"
- icon="el-icon-edit"
- @click="handleUpdate(scope.row,scope.index)">编辑
- </el-button>
- <el-button
- v-if="roleManager_btn_del"
- size="mini"
- type="text"
- icon="el-icon-delete"
- @click="handleDelete(scope.row,scope.index)">删除
- </el-button>
- <el-button
- v-if="roleManager_btn_perm"
- size="mini"
- type="text"
- icon="el-icon-plus"
- @click="handlePermission(scope.row,scope.index)">权限
- </el-button>
- </template>
- </avue-crud>
- </basic-container>
- <el-dialog
- :visible.sync="dialogPermissionVisible"
- title="分配权限">
- <el-tree
- ref="menuTree"
- :data="treeData"
- :default-checked-keys="checkedKeys"
- :check-strictly="false"
- :props="defaultProps"
- :filter-node-method="filterNode"
- class="filter-tree"
- node-key="id"
- highlight-current
- show-checkbox
- default-expand-all/>
- <div
- slot="footer"
- class="dialog-footer">
- <el-button
- type="primary"
- @click="updatePermession(roleId)">更 新
- </el-button>
- </div>
- </el-dialog>
- </div>
- </template>
- <script>
- import { addObj, delObj, fetchList, fetchRoleTree, permissionUpd, putObj } from '@/api/admin/role'
- import { tableOption } from '@/const/crud/admin/role'
- import { fetchTree } from '@/api/admin/dept'
- import { fetchMenuTree } from '@/api/admin/menu'
- import { mapGetters } from 'vuex'
- export default {
- name: 'TableRole',
- data() {
- return {
- tableOption: tableOption,
- dsScopeData: [],
- treeData: [],
- checkedKeys: [],
- checkedDsScope: [],
- defaultProps: {
- label: 'name',
- value: 'id'
- },
- page: {
- total: 0, // 总页数
- currentPage: 1, // 当前页数
- pageSize: 20 // 每页显示多少条
- },
- menuIds: '',
- list: [],
- listLoading: true,
- form: {},
- roleId: undefined,
- roleCode: undefined,
- rolesOptions: undefined,
- dialogPermissionVisible: false,
- roleManager_btn_add: false,
- roleManager_btn_edit: false,
- roleManager_btn_del: false,
- roleManager_btn_perm: false
- }
- },
- created() {
- this.roleManager_btn_add = this.permissions['sys_role_add']
- this.roleManager_btn_edit = this.permissions['sys_role_edit']
- this.roleManager_btn_del = this.permissions['sys_role_del']
- this.roleManager_btn_perm = this.permissions['sys_role_perm']
- },
- computed: {
- ...mapGetters(['elements', 'permissions'])
- },
- methods: {
- getList(page, params) {
- this.listLoading = true
- fetchList(Object.assign({
- current: page.currentPage,
- size: page.pageSize
- }, params)).then(response => {
- this.list = response.data.data.records
- this.page.total = response.data.data.total
- this.listLoading = false
- }).catch(() => {
- this.listLoading = false
- })
- },
- handleRefreshChange() {
- this.getList(this.page)
- },
- handleFilter(param) {
- this.page.page = 1
- this.getList(this.page, param)
- },
- handleCreate() {
- this.$refs.crud.rowAdd()
- },
- handleOpenBefore(show) {
- fetchTree().then(response => {
- this.dsScopeData = response.data.data
- if (this.form.dsScope) {
- this.checkedDsScope = (this.form.dsScope).split(',')
- } else {
- this.checkedDsScope = []
- }
- })
- show()
- },
- handleUpdate(row, index) {
- this.$refs.crud.rowEdit(row, index)
- },
- handlePermission(row) {
- fetchRoleTree(row.roleId)
- .then(response => {
- this.checkedKeys = response.data.data
- return fetchMenuTree()
- })
- .then(response => {
- this.treeData = response.data.data
- // 解析出所有的太监节点
- this.checkedKeys = this.resolveAllEunuchNodeId(this.treeData, this.checkedKeys, [])
- this.dialogPermissionVisible = true
- this.roleId = row.roleId
- this.roleCode = row.roleCode
- })
- },
- /**
- * 解析出所有的太监节点id
- * @param json 待解析的json串
- * @param idArr 原始节点数组
- * @param temp 临时存放节点id的数组
- * @return 太监节点id数组
- */
- resolveAllEunuchNodeId(json, idArr, temp) {
- for (let i = 0; i < json.length; i++) {
- const item = json[i]
- // 存在子节点,递归遍历;不存在子节点,将json的id添加到临时数组中
- if (item.children && item.children.length !== 0) {
- this.resolveAllEunuchNodeId(item.children, idArr, temp)
- } else {
- temp.push(idArr.filter(id => id === item.id))
- }
- }
- return temp
- },
- filterNode(value, data) {
- if (!value) return true
- return data.label.indexOf(value) !== -1
- },
- getNodeData(data, done) {
- done()
- },
- handleDelete(row, index) {
- var _this = this
- this.$confirm('是否确认删除名称为"' + row.roleName + '"' + '"的数据项?', '警告', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning'
- }).then(function() {
- return delObj(row.roleId)
- }).then(() => {
- this.getList(this.page)
- this.list.splice(index, 1)
- _this.$message({
- showClose: true,
- message: '删除成功',
- type: 'success'
- })
- }).catch(function() {
- })
- },
- create(row, done, loading) {
- if (this.form.dsType === 1) {
- this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
- }
- addObj(this.form).then(() => {
- this.getList(this.page)
- done()
- this.$notify({
- title: '成功',
- message: '创建成功',
- type: 'success',
- duration: 2000
- })
- }).catch(() => {
- loading()
- })
- },
- update(row, index, done, loading) {
- if (this.form.dsType === 1) {
- this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
- }
- putObj(this.form).then(() => {
- this.getList(this.page)
- done()
- this.$notify({
- title: '成功',
- message: '修改成功',
- type: 'success',
- duration: 2000
- })
- }).catch(() => {
- loading()
- })
- },
- updatePermession(roleId) {
- this.menuIds = ''
- this.menuIds = this.$refs.menuTree.getCheckedKeys().join(',').concat(',').concat(this.$refs.menuTree.getHalfCheckedKeys().join(','))
- permissionUpd(roleId, this.menuIds).then(() => {
- this.dialogPermissionVisible = false
- fetchMenuTree()
- .then(response => {
- this.form = response.data.data
- return fetchRoleTree(roleId)
- })
- .then(response => {
- this.checkedKeys = response.data
- this.$notify({
- title: '成功',
- message: '修改成功',
- type: 'success',
- duration: 2000
- })
- })
- })
- }
- }
- }
- </script>
|