gin-vue-admin 2.0代码重构
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
<template>
|
||||
<div class="authority">
|
||||
<div class="button-box clearflex">
|
||||
<el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
|
||||
</div>
|
||||
<el-table
|
||||
:data="tableData"
|
||||
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
|
||||
border
|
||||
row-key="authorityId"
|
||||
stripe
|
||||
style="width: 100%"
|
||||
>
|
||||
<el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
|
||||
<el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
|
||||
<el-table-column fixed="right" label="操作" min-width="300">
|
||||
<template slot-scope="scope">
|
||||
<el-button @click="opdendrawer(scope.row)" size="small" type="text">设置权限</el-button>
|
||||
<el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
|
||||
<el-button @click="addAuthority(scope.row.authorityId)" size="small" type="text">新增子角色</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
<!-- 新增角色弹窗 -->
|
||||
<el-dialog :visible.sync="dialogFormVisible" title="新增角色">
|
||||
<el-form :model="form" :rules="rules" ref="authorityForm">
|
||||
<el-form-item label="父级角色ID" prop="parentId">
|
||||
<el-input autocomplete="off" disabled v-model="form.parentId"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item label="角色ID" prop="authorityId">
|
||||
<el-input autocomplete="off" v-model="form.authorityId"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item label="角色姓名" prop="authorityName">
|
||||
<el-input autocomplete="off" v-model="form.authorityName"></el-input>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<div class="dialog-footer" slot="footer">
|
||||
<el-button @click="closeDialog">取 消</el-button>
|
||||
<el-button @click="enterDialog" type="primary">确 定</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
|
||||
<el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
|
||||
<el-tabs class="role-box" type="border-card">
|
||||
<el-tab-pane label="角色菜单">
|
||||
<Menus :row="activeRow" />
|
||||
</el-tab-pane>
|
||||
<el-tab-pane label="角色api">
|
||||
<apis :row="activeRow" />
|
||||
</el-tab-pane>
|
||||
<el-tab-pane label="资源权限">
|
||||
<Datas :authority="tableData" :row="activeRow" />
|
||||
</el-tab-pane>
|
||||
</el-tabs>
|
||||
</el-drawer>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
// 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
|
||||
|
||||
import {
|
||||
getAuthorityList,
|
||||
deleteAuthority,
|
||||
createAuthority
|
||||
} from '@/api/authority'
|
||||
|
||||
import Menus from '@/view/superAdmin/authority/components/menus'
|
||||
import Apis from '@/view/superAdmin/authority/components/apis'
|
||||
import Datas from '@/view/superAdmin/authority/components/datas'
|
||||
|
||||
import infoList from '@/components/mixins/infoList'
|
||||
export default {
|
||||
name: 'Authority',
|
||||
mixins: [infoList],
|
||||
data() {
|
||||
return {
|
||||
listApi: getAuthorityList,
|
||||
listKey: 'list',
|
||||
drawer: false,
|
||||
activeRow: {},
|
||||
activeUserId: 0,
|
||||
dialogFormVisible: false,
|
||||
apiDialogFlag: false,
|
||||
form: {
|
||||
authorityId: '',
|
||||
authorityName: '',
|
||||
parentId: '0'
|
||||
},
|
||||
rules: {
|
||||
authorityId: [
|
||||
{ required: true, message: '请输入角色ID', trigger: 'blur' }
|
||||
],
|
||||
authorityName: [
|
||||
{ required: true, message: '请输入角色名', trigger: 'blur' }
|
||||
],
|
||||
parentId: [
|
||||
{ required: true, message: '请选择请求方式', trigger: 'blur' }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
Menus,
|
||||
Apis,
|
||||
Datas
|
||||
},
|
||||
methods: {
|
||||
opdendrawer(row) {
|
||||
this.drawer = true
|
||||
this.activeRow = row
|
||||
},
|
||||
// 删除角色
|
||||
deleteAuth(row) {
|
||||
this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
})
|
||||
.then(async () => {
|
||||
const res = await deleteAuthority({ authorityId: row.authorityId })
|
||||
if (res.success) {
|
||||
this.$message({
|
||||
type: 'success',
|
||||
message: '删除成功!'
|
||||
})
|
||||
this.getTableData()
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
this.$message({
|
||||
type: 'info',
|
||||
message: '已取消删除'
|
||||
})
|
||||
})
|
||||
},
|
||||
// 初始化表单
|
||||
initForm() {
|
||||
for (const key in this.form) {
|
||||
this.form[key] = ''
|
||||
}
|
||||
},
|
||||
// 关闭窗口
|
||||
closeDialog() {
|
||||
this.initForm()
|
||||
this.dialogFormVisible = false
|
||||
this.apiDialogFlag = false
|
||||
},
|
||||
// 确定弹窗
|
||||
|
||||
async enterDialog() {
|
||||
if (this.form.authorityId == '0') {
|
||||
this.$message({
|
||||
type: 'error',
|
||||
message: '角色id不能为0'
|
||||
})
|
||||
return false
|
||||
}
|
||||
this.$refs.authorityForm.validate(async valid => {
|
||||
if (valid) {
|
||||
const res = await createAuthority(this.form)
|
||||
if (res.success) {
|
||||
this.$message({
|
||||
type: 'success',
|
||||
message: '添加成功!'
|
||||
})
|
||||
this.getTableData()
|
||||
this.closeDialog()
|
||||
}
|
||||
this.initForm()
|
||||
this.dialogFormVisible = false
|
||||
}
|
||||
})
|
||||
},
|
||||
// 增加角色
|
||||
addAuthority(parentId) {
|
||||
this.form.parentId = parentId
|
||||
this.dialogFormVisible = true
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.pageSize = 999
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="scss">
|
||||
.authority {
|
||||
.button-box {
|
||||
padding: 10px 20px;
|
||||
.el-button {
|
||||
float: right;
|
||||
}
|
||||
}
|
||||
}
|
||||
.role-box {
|
||||
.el-tabs__content {
|
||||
height: calc(100vh - 150px);
|
||||
overflow: auto;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,99 @@
|
||||
<template>
|
||||
<div>
|
||||
<div class="clearflex">
|
||||
<el-button @click="authApiEnter" class="fl-right" size="small" type="primary">确 定</el-button>
|
||||
</div>
|
||||
<el-tree
|
||||
:data="apiTreeData"
|
||||
:default-checked-keys="apiTreeIds"
|
||||
:props="apiDefaultProps"
|
||||
default-expand-all
|
||||
highlight-current
|
||||
node-key="path"
|
||||
ref="apiTree"
|
||||
show-checkbox
|
||||
></el-tree>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
import { getAllApis } from '@/api/api'
|
||||
import { casbinPUpdate, getPolicyPathByAuthorityId } from '@/api/casbin'
|
||||
export default {
|
||||
name: 'Apis',
|
||||
props: {
|
||||
row: {
|
||||
default: function() {
|
||||
return {}
|
||||
},
|
||||
type: Object
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
apiTreeData: [],
|
||||
apiTreeIds: [],
|
||||
apiDefaultProps: {
|
||||
children: 'children',
|
||||
label: 'description'
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 创建api树方法
|
||||
buildApiTree(apis) {
|
||||
const apiObj = new Object()
|
||||
apis &&
|
||||
apis.map(item => {
|
||||
if (apiObj.hasOwnProperty(item.group)) {
|
||||
apiObj[item.group].push(item)
|
||||
} else {
|
||||
Object.assign(apiObj, { [item.group]: [item] })
|
||||
}
|
||||
})
|
||||
const apiTree = []
|
||||
for (const key in apiObj) {
|
||||
const treeNode = {
|
||||
ID: key,
|
||||
description: key + '组',
|
||||
children: apiObj[key]
|
||||
}
|
||||
apiTree.push(treeNode)
|
||||
}
|
||||
return apiTree
|
||||
},
|
||||
// 关联关系确定
|
||||
async authApiEnter() {
|
||||
const checkArr = this.$refs.apiTree.getCheckedNodes(true)
|
||||
var casbinInfos = []
|
||||
checkArr&&checkArr.map(item=>{
|
||||
var casbinInfo = {
|
||||
path:item.path,
|
||||
method:item.method
|
||||
}
|
||||
casbinInfos.push(casbinInfo)
|
||||
})
|
||||
const res = await casbinPUpdate({
|
||||
authorityId: this.activeUserId,
|
||||
casbinInfos
|
||||
})
|
||||
if (res.success) {
|
||||
this.$message({ type: 'success', message: res.msg })
|
||||
}
|
||||
}
|
||||
},
|
||||
async created() {
|
||||
// 获取api并整理成树结构
|
||||
const res2 = await getAllApis()
|
||||
const apis = res2.data.apis
|
||||
this.apiTreeData = this.buildApiTree(apis)
|
||||
|
||||
const res = await getPolicyPathByAuthorityId({
|
||||
authorityId: this.row.authorityId
|
||||
})
|
||||
this.activeUserId = this.row.authorityId
|
||||
this.apiTreeIds = res.data.paths || []
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="scss">
|
||||
</style>
|
||||
@@ -0,0 +1,96 @@
|
||||
<template>
|
||||
<div>
|
||||
<div class="clearflex" style="margin:18px">
|
||||
<el-button @click="authDataEnter" class="fl-right" size="small" type="primary">确 定</el-button>
|
||||
<el-button @click="all" class="fl-left" size="small" type="primary">全选</el-button>
|
||||
<el-button @click="self" class="fl-left" size="small" type="primary">本部门</el-button>
|
||||
<el-button @click="selfAndChildren" class="fl-left" size="small" type="primary">部门及以下</el-button>
|
||||
</div>
|
||||
<el-checkbox-group v-model="dataAuthorityId" @change="selectAuthority">
|
||||
<el-checkbox v-for="(item,key) in authoritys" :label="item" :key="key">{{item.authorityName}}</el-checkbox>
|
||||
</el-checkbox-group>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
import {setDataAuthority} from '@/api/authority'
|
||||
export default {
|
||||
name: 'Datas',
|
||||
data() {
|
||||
return {
|
||||
authoritys:[],
|
||||
dataAuthorityId:[]
|
||||
}
|
||||
},
|
||||
props: {
|
||||
row: {
|
||||
default: function() {
|
||||
return {}
|
||||
},
|
||||
type: Object
|
||||
},
|
||||
authority: {
|
||||
default: function() {
|
||||
return {}
|
||||
},
|
||||
type: Array
|
||||
}
|
||||
},
|
||||
methods:{
|
||||
all(){
|
||||
this.dataAuthorityId = [...this.authoritys]
|
||||
this.row.dataAuthorityId = this.dataAuthorityId
|
||||
|
||||
},
|
||||
self(){
|
||||
this.dataAuthorityId = this.authoritys.filter(item=>item.authorityId===this.row.authorityId)
|
||||
this.row.dataAuthorityId = this.dataAuthorityId
|
||||
},
|
||||
selfAndChildren(){
|
||||
const arrBox = []
|
||||
this.getChildrenId(this.row,arrBox)
|
||||
this.dataAuthorityId = this.authoritys.filter(item=>arrBox.indexOf(item.authorityId)>-1)
|
||||
this.row.dataAuthorityId = this.dataAuthorityId
|
||||
},
|
||||
getChildrenId(row,arrBox){
|
||||
arrBox.push(row.authorityId)
|
||||
row.children&&row.children.map(item=>{
|
||||
this.getChildrenId(item,arrBox)
|
||||
})
|
||||
},
|
||||
// 提交
|
||||
async authDataEnter(){
|
||||
const res = await setDataAuthority(this.row)
|
||||
if(res.success){
|
||||
this.$message({ type: 'success', message: res.msg })
|
||||
}
|
||||
},
|
||||
// 平铺角色
|
||||
roundAuthority(authoritys){
|
||||
authoritys&&authoritys.map(item=>{
|
||||
const obj = {}
|
||||
obj.authorityId = item.authorityId
|
||||
obj.authorityName = item.authorityName
|
||||
this.authoritys.push(obj)
|
||||
if(item.children.length){
|
||||
this.roundAuthority(item.children)
|
||||
}
|
||||
})
|
||||
},
|
||||
// 选择
|
||||
selectAuthority(){
|
||||
this.row.dataAuthorityId = this.dataAuthorityId
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.authoritys = []
|
||||
this.dataAuthorityId = []
|
||||
this.roundAuthority(this.authority)
|
||||
this.row.dataAuthorityId&&this.row.dataAuthorityId.map(item=>{
|
||||
const obj = this.authoritys&&this.authoritys.filter(au=>au.authorityId === item.authorityId)&&this.authoritys.filter(au=>au.authorityId === item.authorityId)[0]
|
||||
this.dataAuthorityId.push(obj)
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="less">
|
||||
</style>
|
||||
@@ -0,0 +1,76 @@
|
||||
<template>
|
||||
<div>
|
||||
<div class="clearflex">
|
||||
<el-button @click="relation" class="fl-right" size="small" type="primary">确 定</el-button>
|
||||
</div>
|
||||
<el-tree
|
||||
:data="menuTreeData"
|
||||
:default-checked-keys="menuTreeIds"
|
||||
:props="menuDefaultProps"
|
||||
default-expand-all
|
||||
highlight-current
|
||||
node-key="ID"
|
||||
ref="menuTree"
|
||||
show-checkbox
|
||||
></el-tree>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'
|
||||
|
||||
export default {
|
||||
name: 'Menus',
|
||||
props: {
|
||||
row: {
|
||||
default: function() {
|
||||
return {}
|
||||
},
|
||||
type: Object
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
menuTreeData: [],
|
||||
menuTreeIds: [],
|
||||
menuDefaultProps: {
|
||||
children: 'children',
|
||||
label: 'nickName'
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 关联树 确认方法
|
||||
async relation() {
|
||||
const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
|
||||
const res = await addMenuAuthority({
|
||||
menus: checkArr,
|
||||
authorityId: this.row.authorityId
|
||||
})
|
||||
if (res.success) {
|
||||
this.$message({
|
||||
type: 'success',
|
||||
message: '添加成功!'
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
async created() {
|
||||
// 获取所有菜单树
|
||||
const res = await getBaseMenuTree()
|
||||
this.menuTreeData = res.data.menus
|
||||
|
||||
const res1 = await getMenuAuthority({ authorityId: this.row.authorityId })
|
||||
const menus = res1.data.menus
|
||||
const arr = []
|
||||
menus.map(item => {
|
||||
// 防止直接选中父级造成全选
|
||||
if (!menus.some(same => same.parentId === item.menuId)) {
|
||||
arr.push(Number(item.menuId))
|
||||
}
|
||||
})
|
||||
this.menuTreeIds = arr
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="scss">
|
||||
</style>
|
||||
Reference in New Issue
Block a user