feat:角色添加权限

This commit is contained in:
有来技术
2021-12-23 00:34:57 +08:00
parent 7a6fbd1373
commit 677feb8a4d
3 changed files with 43 additions and 50 deletions

View File

@@ -114,7 +114,7 @@ export function listRolePerms(roleId: number, menuId: number) {
return request({ return request({
url: '/youlai-admin/api/v1/roles/' + roleId + '/permissions', url: '/youlai-admin/api/v1/roles/' + roleId + '/permissions',
method: 'get', method: 'get',
data: {menuId: menuId} params: {menuId: menuId}
}) })
} }

View File

@@ -1,27 +1,20 @@
<template> <template>
<div class="perm-container"> <div class="perm-container">
<el-form size="mini">
<el-form-item>
<el-row>
<el-col :span="12">
<el-button type="success" plain :icon="Switch" @click="toggleExpandAll">展开/折叠</el-button>
</el-col>
<el-col :span="12" style="text-align: right">
<el-button type="primary" :icon="Check" @click="handleSubmit">提交</el-button>
</el-col>
</el-row>
</el-form-item>
</el-form>
<div v-if="permissionOptions.length > 0"> <div v-if="permissionOptions.length > 0">
<el-checkbox <el-row>
:indeterminate="isIndeterminate" <el-col :span="12">
v-model="checkAll" <el-checkbox
@change="handleCheckAllChange" :indeterminate="isIndeterminate"
style="margin-top: 20px" v-model="checkAll"
>全选 @change="handleCheckAllChange"
</el-checkbox> >全选
</el-checkbox>
</el-col>
<el-col :span="12" style="text-align: right">
<el-button type="primary" :icon="Check" size="mini" @click="handleSubmit">提交</el-button>
</el-col>
</el-row>
<el-row> <el-row>
<el-col <el-col
:span="8" :span="8"
@@ -36,7 +29,7 @@
@change="handleCheckedPermChange" @change="handleCheckedPermChange"
size="mini" size="mini"
> >
{{ permission.name }} {{ item.name }}
</el-checkbox> </el-checkbox>
</el-col> </el-col>
</el-row> </el-row>
@@ -49,10 +42,10 @@
<script setup lang="ts"> <script setup lang="ts">
import {listPerms} from "@/api/system/perm"; import {listPerms} from "@/api/system/perm";
import { listRolePerms, saveRolePerms} from "@/api/system/role"; import {listRolePerms, saveRolePerms} from "@/api/system/role";
import {defineProps, onMounted, reactive, toRefs, watch} from "vue"; import {defineProps, onMounted, reactive, toRefs, watch} from "vue";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
import {Check} from '@element-plus/icons'
const props = defineProps({ const props = defineProps({
role: { role: {
@@ -65,15 +58,11 @@ const props = defineProps({
} }
}) })
watch(() => props.role.id as any, (newVal, oldVal) => {
const roleId = props.role.id
if (roleId) {
}
})
watch(() => props.menu.id as any, (newVal, oldVal) => { watch(() => props.menu.id as any, (newVal, oldVal) => {
const menuId = props.menu.id const menuId = props.menu.id
if (menuId) { if (menuId) {
loadData()
} }
}) })
@@ -98,48 +87,50 @@ function handleCheckAllChange(checked: Boolean) {
} }
function handleCheckedPermChange(value: any) { function handleCheckedPermChange(value: any) {
const checkedCount = value.length; const checkedCount = state.permissionOptions.filter(item => item.checked).length;
state.checkAll = checkedCount === state.permissionOptions.length; state.checkAll = checkedCount === state.permissionOptions.length;
state.isIndeterminate = checkedCount > 0 && checkedCount < state.permissionOptions.length; state.isIndeterminate = checkedCount > 0 && checkedCount < state.permissionOptions.length;
} }
function loadData() { function loadData() {
if(! props.menu.id){ if (!props.menu.id) {
resetData()
return false return false
} }
state.loading = true; state.loading = true;
listPerms({menuId: props.menu.id}).then(response => { listPerms({menuId: props.menu.id}).then(response => {
state.permissionOptions = response.data state.permissionOptions = response.data
listRolePerms(props.role.id, props.menu.id).then(res => { listRolePerms(props.role.id, props.menu.id).then(res => {
const checkedPermIds = res.data const checkedPermIds = res.data
state.permissionOptions.map((item:any)=> { state.permissionOptions.map((item: any) => {
if (checkedPermIds.includes(item.id)) { if (checkedPermIds.includes(item.id)) {
item.checked = true; item.checked = true;
} else { } else {
state.checkAll = false state.checkAll = false
} }
}); });
state.loading = false
state.loading=false
}) })
}) })
} }
function handleSubmit(){ function handleSubmit() {
const checkedPermIds = state.permissionOptions const checkedPermIds = state.permissionOptions
.filter((item) => item.checked) .filter((item) => item.checked)
.map((item) => item.id); .map((item) => item.id);
saveRolePerms(
props.role.id,
props.menu.id,
checkedPermIds
).then(() => {
ElMessage.success("提交成功");
});
}
function resetData() {
saveRolePerms( props.menu.id, state.permissionOptions = []
props.role.id, state.isIndeterminate = true
checkedPermIds state.checkAll = false
).then(() => {
ElMessage.success("提交成功");
});
} }
onMounted(() => { onMounted(() => {

View File

@@ -39,9 +39,9 @@
</el-tag> </el-tag>
<el-tag type="warning" style="margin:0 5px 0 0;" v-else size="small"> 请选择角色</el-tag> <el-tag type="warning" style="margin:0 5px 0 0;" v-else size="small"> 请选择角色</el-tag>
<el-tag type="primary" v-if="state.role.id" size="small"> <el-tag type="success" v-if="state.menu.id" size="small">
<svg-icon color="red" icon-class="menu"/> <svg-icon color="red" icon-class="menu"/>
{{ state.role.name }} {{ state.menu.name }}
</el-tag> </el-tag>
<el-tag type="warning" v-else size="small"> 请选择菜单</el-tag> <el-tag type="warning" v-else size="small"> 请选择菜单</el-tag>
</template> </template>
@@ -92,10 +92,12 @@ function handleMenuClick(menuRow: any) {
return false return false
} }
console.log('选择菜单',menuRow)
if (menuRow) { if (menuRow) {
state.menu = { state.menu = {
id: menuRow.id, id: menuRow.id,
name: menuRow.name name: menuRow.label
} }
} else { } else {
state.menu = { state.menu = {