添加角色配置

This commit is contained in:
joylink_zhaoerwei 2024-10-22 10:32:37 +08:00
parent fb18c04248
commit 2a7cf2e186
2 changed files with 61 additions and 115 deletions

View File

@ -77,6 +77,19 @@ export async function getRoleInfo(id: number): Promise<RoleInfo> {
return response.data; return response.data;
} }
/**
*
* @param id id
* @param data
*/
export function roleConfig(id: number, data: string) {
return api.post(`${AuthBase}/config/${id}`, data, {
headers: {
'Content-Type': 'text/plain',
},
});
}
interface LinkRole { interface LinkRole {
id: number; id: number;
roleList: number[]; roleList: number[];

View File

@ -15,35 +15,21 @@
binary-state-sort binary-state-sort
@request="onRequest" @request="onRequest"
> >
<!-- <template v-slot:top-right> <template
<q-input v-if="userStore.defaultRole == 'ADMIN'"
dense v-slot:body-cell-operations="props"
debounce="1000" >
v-model="filter.name"
label="名称"
></q-input>
<q-btn flat round color="primary" icon="search" />
<q-btn color="primary" label="新建" @click="editFormShow = true" />
</template>
<template v-slot:body-cell-operations="props">
<q-td :props="props"> <q-td :props="props">
<div class="q-gutter-sm row justify-center"> <div class="q-gutter-sm row justify-center">
<q-btn <q-btn
:disable="operateDisabled"
color="primary" color="primary"
label="编辑" label="配置"
@click="edieRoleData(props.row)" @click="edieRoleData(props.row)"
:disable="props.row.id == 1"
/>
<q-btn
color="red"
:disable="operateDisabled || [1, 2].includes(props.row.id)"
label="删除"
@click="deleteRoleData(props.row)"
/> />
</div> </div>
</q-td> </q-td>
</template> --> </template>
</q-table> </q-table>
<q-dialog <q-dialog
@ -52,37 +38,31 @@
transition-show="scale" transition-show="scale"
transition-hide="scale" transition-hide="scale"
> >
<q-card style="width: 1400px; max-width: 80vw"> </q-dialog>
<q-dialog
v-model="editFormShow"
persistent
transition-show="scale"
transition-hide="scale"
>
<q-card style="width: 300px">
<q-card-section>
<q-form <q-form
ref="myForm" ref="myForm"
@submit="edieRolePath" @submit="roleConfigFn"
@reset="onReset" @reset="onReset"
class="q-gutter-md" class="q-gutter-md"
> >
<q-card-section> <div class="text-h6">配置角色信息</div>
<div class="text-h5">{{ roleInfo.id ? '编辑' : '新建' }}</div> <q-input outlined disable label="角色" v-model="roleInfo.name" />
<div class="q-pa-md"> <q-input outlined label="配置" v-model="roleInfo.config" />
<q-input
outlined
dense
label="角色名称"
v-model="roleInfo.name"
lazy-rules
:rules="[(val) => val.length > 0 || '请输入角色名称!']"
style="width: 300px"
/>
</div>
<AuthPathManage
:sizeHeight="600"
:selects="roleInfo.editPaths || []"
@selectsed="pathSelectsed"
/>
<q-card-actions align="right"> <q-card-actions align="right">
<q-btn color="primary" label="保存" type="submit" /> <q-btn color="primary" label="保存" type="submit" />
<q-btn label="取消" type="reset" v-close-popup /> <q-btn label="取消" type="reset" v-close-popup />
</q-card-actions> </q-card-actions>
</q-card-section>
</q-form> </q-form>
</q-card-section>
</q-card> </q-card>
</q-dialog> </q-dialog>
</div> </div>
@ -91,18 +71,10 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'; import { ref, reactive, onMounted, computed } from 'vue';
import { useQuasar, type QTableColumn, QForm } from 'quasar'; import { useQuasar, type QTableColumn, QForm } from 'quasar';
import { import { pageQueryRole, roleConfig, RoleInfo } from 'src/api/AuthApi';
PathItem,
RoleInfo,
createRole,
createRoleParams,
deleteRole,
pageQueryRole,
saveRoleData,
} from 'src/api/AuthApi';
import { ApiError } from 'src/boot/axios'; import { ApiError } from 'src/boot/axios';
import { successNotify } from 'src/utils/CommonNotify'; import { successNotify } from 'src/utils/CommonNotify';
import AuthPathManage from './AuthPathManage.vue'; import { useUserStore } from 'src/stores/user-store';
const $q = useQuasar(); const $q = useQuasar();
const props = withDefaults( const props = withDefaults(
@ -114,8 +86,17 @@ const props = withDefaults(
const tableHeight = computed(() => { const tableHeight = computed(() => {
return props.sizeHeight - 32; return props.sizeHeight - 32;
}); });
const userStore = useUserStore();
onMounted(() => { onMounted(() => {
if (userStore.defaultRole == 'ADMIN') {
columnDefs.push({
name: 'operations',
label: '操作',
field: 'operations',
align: 'center',
});
}
tableRef.value.requestServerInteraction(); tableRef.value.requestServerInteraction();
}); });
@ -130,8 +111,6 @@ const columnDefs: QTableColumn[] = [
}, },
]; ];
const operateDisabled = ref(false);
const tableRef = ref(); const tableRef = ref();
const rows = reactive([]); const rows = reactive([]);
const filter = reactive({ const filter = reactive({
@ -176,53 +155,36 @@ async function onRequest(props: any) {
} }
} }
const editFormShow = ref(false); //
interface RoleItemInfo extends Omit<RoleInfo, 'id'> { interface RoleItemInfo extends Omit<RoleInfo, 'id'> {
id: string; id: string;
editPaths: { id: number }[]; editPaths: { id: number }[];
config: string;
} }
const roleInfo = reactive<RoleItemInfo>({ const roleInfo = reactive<RoleItemInfo>({
id: '', id: '',
editPaths: [], editPaths: [],
name: '', name: '',
resList: [], resList: [],
config: '',
}); });
//
function edieRoleData(row: RoleInfo) { function edieRoleData(row: RoleInfo) {
roleInfo.id = row.id + ''; roleInfo.id = row.id + '';
roleInfo.name = row.name; roleInfo.name = row.name;
const list = row.resList || [];
roleInfo.resList = list;
roleInfo.editPaths = list.map((item) => {
return { id: item };
});
editFormShow.value = true; editFormShow.value = true;
} }
const operateDisabled = ref(false);
const myForm = ref<QForm | null>(null); const myForm = ref<QForm | null>(null);
const editFormShow = ref(false);
// function roleConfigFn() {
async function edieRolePath() {
myForm.value?.validate().then(async (res) => { myForm.value?.validate().then(async (res) => {
if (res) { if (res) {
operateDisabled.value = true; operateDisabled.value = true;
try { try {
const params: createRoleParams = { await roleConfig(+roleInfo.id, roleInfo.config);
name: roleInfo.name,
resList: roleInfo.resList,
};
if (roleInfo.id) {
const cloneParams = Object.assign(params, { id: +roleInfo.id });
await saveRoleData(cloneParams);
} else {
await createRole(params);
}
onReset();
tableRef.value.requestServerInteraction(); //
editFormShow.value = false; editFormShow.value = false;
successNotify('保存成功!'); successNotify('修改成功');
} catch (err) { } catch (err) {
const error = err as ApiError; const error = err as ApiError;
$q.notify({ $q.notify({
@ -236,39 +198,10 @@ async function edieRolePath() {
}); });
} }
function deleteRoleData(row: RoleInfo) {
operateDisabled.value = true;
$q.dialog({
title: '确认',
message: `确认删除角色 "${row.name}" 吗?`,
cancel: true,
})
.onOk(async () => {
try {
await deleteRole(row.id);
tableRef.value.requestServerInteraction(); //
} catch (err) {
const error = err as ApiError;
$q.notify({
type: 'negative',
message: error.title,
});
}
})
.onDismiss(() => {
operateDisabled.value = false;
});
}
function onReset() { function onReset() {
roleInfo.id = ''; roleInfo.id = '';
roleInfo.name = ''; roleInfo.name = '';
roleInfo.editPaths = []; roleInfo.config = '';
roleInfo.resList = [];
myForm.value?.resetValidation(); myForm.value?.resetValidation();
} }
function pathSelectsed(val: PathItem[]) {
roleInfo.resList = val.map((item) => item.id);
}
</script> </script>