341 lines
9.0 KiB
Vue
Raw Normal View History

2025-07-03 20:19:09 +08:00
<template>
2025-07-09 17:43:57 +08:00
<div class="main__container table">
<header class="table-header">
2025-07-12 14:19:52 +08:00
<div class="table-header__search">
2025-07-12 15:48:30 +08:00
<n-input placeholder="请输入用户名称" v-model:value="queryInfo.userName" size="small" style="width: 240px;"
clearable />
2025-07-12 14:19:52 +08:00
<n-select placeholder="请选择角色" v-model:value="queryInfo.roleName" :options="options" size="small"
2025-07-12 15:48:30 +08:00
style="width: 240px;" clearable />
2025-07-12 14:19:52 +08:00
</div>
<ls-list-button @search="init" @reset="resetList"></ls-list-button>
2025-07-09 17:43:57 +08:00
</header>
<main class="table-main" ref="tableMainRef">
<n-data-table v-if="height" :columns="columns" :data="data" :max-height="`${height}px`"
2025-07-12 14:19:52 +08:00
:min-height="`${height}px`">
2025-07-12 15:48:30 +08:00
<template #empty>
<LsEmpty type="no-data" title="暂无数据" description="Not Found 404"></LsEmpty>
</template>
2025-07-12 14:19:52 +08:00
</n-data-table>
2025-07-09 17:43:57 +08:00
<div class="table-main__pagination">
2025-07-11 23:22:08 +08:00
<n-pagination v-model:page="pagination.page" v-model:page-size="pagination.pageSize" :item-count="data.length"
show-size-picker :page-sizes="[10, 20, 30, 40]">
2025-07-12 14:19:52 +08:00
<template #prefix="{ itemCount }">
2025-07-11 23:22:08 +08:00
{{ itemCount }}
</template>
</n-pagination>
2025-07-09 17:43:57 +08:00
</div>
</main>
</div>
2025-07-12 14:19:52 +08:00
<!-- 弹窗 -->
<n-modal v-model:show="addRoleDialog">
<n-card style="width: 450px" title="授权角色" :bordered="false" size="huge" role="dialog" aria-modal="true">
<n-input-group>
2025-07-12 15:48:30 +08:00
<n-input v-model:value="roleName" readonly :style="{ width: '70%' }" placeholder="请从角色库选择角色" />
<n-button type="warning" strong secondary :style="{ width: '30%' }" @click="openRoleDialog">
2025-07-12 14:19:52 +08:00
角色库
<template #icon>
<n-icon>
<Server />
</n-icon>
</template>
</n-button>
</n-input-group>
<template #footer>
<div class="flex-content right">
2025-07-12 15:48:30 +08:00
<n-button type="primary" size="small" @click="handleEnableRole"> </n-button>
2025-07-12 14:19:52 +08:00
<n-button @click="addRoleDialog = false" size="small"> </n-button>
</div>
</template>
</n-card>
</n-modal>
2025-07-12 15:48:30 +08:00
<!-- 角色库 -->
<LsRoleBase ref="roleRef" @choose="chooseRole"></LsRoleBase>
2025-07-03 20:19:09 +08:00
</template>
<script setup lang='ts'>
2025-07-12 14:19:52 +08:00
import { Server } from '@vicons/ionicons5';
2025-07-12 15:48:30 +08:00
import { getUserList, deleteUser, enableRole } from '@/api/roleApi';
2025-07-12 14:19:52 +08:00
import { DataTableColumns, NButton, NTag, useDialog, useMessage } from 'naive-ui';
import LsEmpty from '@/components/Ls-UI/LsEmpty.vue';
2025-07-12 15:48:30 +08:00
import LsRoleBase from '@/components/Ls-UI/LsRoleBase.vue';
2025-07-09 17:43:57 +08:00
2025-07-10 22:27:33 +08:00
const message = useMessage();
2025-07-09 17:43:57 +08:00
const tableMainRef = ref<HTMLElement | null>(null);
2025-07-12 15:48:30 +08:00
const roleRef = ref<InstanceType<typeof LsRoleBase> | null>(null);
2025-07-12 14:19:52 +08:00
const queryInfo = reactive({
roleName: void 0,
userName: ''
});
2025-07-11 23:22:08 +08:00
const total = ref(0);
2025-07-10 20:05:35 +08:00
const pagination = reactive<PageController>({
2025-07-09 17:43:57 +08:00
pageSize: 10,
page: 1,
});
2025-07-12 14:19:52 +08:00
const options = ref([]);
2025-07-12 15:48:30 +08:00
2025-07-11 23:22:08 +08:00
const data = ref([]);
const height = ref(null);
2025-07-12 14:19:52 +08:00
const dialog = useDialog();
const addRoleDialog = ref(false);
const operationUuid = ref('');
2025-07-12 15:48:30 +08:00
const roleName = ref(''); //授权角色
2025-07-09 17:43:57 +08:00
2025-07-10 22:27:33 +08:00
// 类似render - 函数式组件渲染
function createColumns({
2025-07-12 14:19:52 +08:00
handleDelete,
handleAuthorize,
handleMenuAssign,
handleAssignJob
2025-07-10 22:27:33 +08:00
}: {
2025-07-12 14:19:52 +08:00
handleDelete: (rowData: UserRaw) => void,
handleAuthorize: (rowData: UserRaw) => void,
handleMenuAssign: (rowData: UserRaw) => void,
handleAssignJob: (rowData: UserRaw) => void,
2025-07-11 23:22:08 +08:00
}): DataTableColumns<UserRaw> {
2025-07-10 22:27:33 +08:00
return [
{
title: '用户名',
2025-07-12 14:19:52 +08:00
key: 'userName',
align: 'center'
2025-07-10 22:27:33 +08:00
},
{
title: '性别',
2025-07-11 23:22:08 +08:00
key: 'sex',
2025-07-12 14:19:52 +08:00
align: 'center',
2025-07-11 23:22:08 +08:00
render(row: UserRaw) {
2025-07-12 14:19:52 +08:00
return h(NTag, {
type: row.sex === '男' ? 'primary' : row.sex === '女' ? 'error' : 'default',
bordered: false,
class: 'user-table__gender-tag'
}, {
2025-07-11 23:22:08 +08:00
default: () => `${row.sex || '未知'}`
})
}
2025-07-10 22:27:33 +08:00
},
{
2025-07-11 23:22:08 +08:00
title: '职位',
key: 'jobCode',
2025-07-12 14:19:52 +08:00
align: 'center',
2025-07-11 23:22:08 +08:00
render(row: UserRaw) {
2025-07-12 14:19:52 +08:00
return h('span', { class: 'user-table__job' }, {
2025-07-11 23:22:08 +08:00
default: () => `${row.jobCode || '无职位'}`
})
}
2025-07-10 22:27:33 +08:00
},
{
2025-07-11 23:22:08 +08:00
title: '菜单权限',
key: 'menuName',
2025-07-12 14:19:52 +08:00
align: 'center',
2025-07-11 23:22:08 +08:00
render(row: UserRaw) {
2025-07-12 14:19:52 +08:00
return h('span', { class: 'user-table__menu' }, {
2025-07-11 23:22:08 +08:00
default: () => `${row.menuName || '无权限'}`
})
}
},
2025-07-12 14:19:52 +08:00
{
title: '电话',
key: 'phoneNumber',
align: 'center',
render(row: UserRaw) {
return h('span', { class: 'user-table__phone' }, {
default: () => `${row.phoneNumber || '无号码'}`
})
}
},
2025-07-11 23:22:08 +08:00
{
title: '操作',
2025-07-10 22:27:33 +08:00
key: 'actions',
2025-07-12 14:19:52 +08:00
align: 'center',
2025-07-11 23:22:08 +08:00
render(row: UserRaw) {
2025-07-12 14:19:52 +08:00
return h('div', { class: 'user-table__actions' }, [
h(NButton, {
size: 'small',
type: 'primary',
strong: true,
secondary: true,
class: 'user-table__action-btn',
onClick: () => handleAuthorize(row)
}, { default: () => '授权用户' }),
h(NButton, {
2025-07-10 22:27:33 +08:00
size: 'small',
2025-07-12 14:19:52 +08:00
type: 'error',
strong: true,
secondary: true,
class: 'user-table__action-btn',
onClick: () => handleDelete(row)
}, { default: () => '删除用户' }),
h(NButton, {
size: 'small',
type: 'info',
strong: true,
secondary: true,
class: 'user-table__action-btn',
onClick: () => handleMenuAssign(row)
}, { default: () => '菜单分配' }),
h(NButton, {
size: 'small',
type: 'warning',
strong: true,
secondary: true,
class: 'user-table__action-btn',
onClick: () => handleAssignJob(row)
}, { default: () => '赋予职位' })
])
2025-07-10 22:27:33 +08:00
}
}
]
}
// 生成表格列
const columns = createColumns({
2025-07-12 14:19:52 +08:00
handleAuthorize(rowData) {
addRoleDialog.value = true;
operationUuid.value = rowData.id;
},
handleDelete(rowData) {
dialog.warning({
title: '警告',
content: `你确定删除用户${rowData.userName}`,
positiveText: '确定',
negativeText: '不确定',
draggable: true,
onPositiveClick: () => {
deleteUser(rowData.id).then(() => {
message.success('删除成功!');
init();
}).catch((error) => {
message.error(error.message);
})
},
onNegativeClick: () => {
message.info('取消')
}
})
},
handleMenuAssign(rowData) {
message.error(`我点击了${rowData.userName}`);
},
handleAssignJob(rowData) {
2025-07-11 23:22:08 +08:00
message.error(`我点击了${rowData.userName}`);
2025-07-09 17:43:57 +08:00
}
2025-07-10 22:27:33 +08:00
})
2025-07-10 20:05:35 +08:00
const init = async () => {
try {
2025-07-11 23:22:08 +08:00
const { users, totalCount } = await getUserList({
2025-07-11 21:15:05 +08:00
...pagination,
2025-07-12 14:19:52 +08:00
...queryInfo
2025-07-11 21:15:05 +08:00
});
2025-07-11 23:22:08 +08:00
total.value = totalCount;
data.value = users;
2025-07-10 20:05:35 +08:00
} catch (error) {
2025-07-11 21:15:05 +08:00
message.error(error.message);
2025-07-10 20:05:35 +08:00
}
2025-07-12 14:19:52 +08:00
};
const resetList = () => {
pagination.page = 1;
pagination.pageSize = 10;
queryInfo.roleName = void 0;
queryInfo.userName = '';
init();
2025-07-10 20:05:35 +08:00
}
2025-07-09 17:43:57 +08:00
onMounted(() => {
if (tableMainRef.value) {
height.value = tableMainRef.value.clientHeight - 64 - 50; //减去两次内边距 - 分页的大小
2025-07-10 20:05:35 +08:00
};
2025-07-12 15:48:30 +08:00
initRoleList();
2025-07-10 20:05:35 +08:00
init();
2025-07-09 17:43:57 +08:00
});
2025-07-12 15:48:30 +08:00
const openRoleDialog = () => {
roleRef.value?.openDialog();
}
const handleEnableRole = async () => {
try {
const query = {
userId: operationUuid.value,
roleName: roleName.value
}
await enableRole(query);
init();
message.success('授权成功!');
} catch (error) {
message.error(error.message);
}
}
// 获取角色
const initRoleList = async () => {
try {
const result = await getRoleList();
options.value = result.map(item => {
return {
label:item.normalizedName,
value:item.name
}
});
} catch (error) {
message.error(error instanceof Error ? error.message : error);
}
};
// 赋予角色
const chooseRole = (row) => {
console.log(row, 'row');
roleName.value = row.name;
}
2025-07-03 20:19:09 +08:00
</script>
<style scoped lang='scss'>
2025-07-09 17:43:57 +08:00
.table-header {
background: #fff;
border-radius: 4px;
height: 100%;
display: flex;
align-items: center;
2025-07-12 14:19:52 +08:00
justify-content: space-between;
2025-07-09 17:43:57 +08:00
padding: 0 $normolGap;
2025-07-12 14:19:52 +08:00
&__search {
display: flex;
align-items: center;
gap: $miniGap;
}
2025-07-09 17:43:57 +08:00
}
.table-main {
background: #fff;
padding: $normolGap;
border-radius: 4px;
height: calc(100% - 2 * $normolGap);
&__pagination {
height: 50px;
display: flex;
align-items: center;
justify-content: flex-end;
}
}
2025-07-12 14:19:52 +08:00
.user-table {
// 文本样式
:deep() &__job,
:deep() &__menu,
:deep() &__phone {
font-size: 14px;
color: var(--n-text-color);
}
// 操作按钮容器
:deep() &__actions {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
gap: 6px;
width: 100%;
max-width: 200px;
margin: 0 auto;
}
// 操作按钮
:deep() &__action-btn.n-button {
width: 100%;
white-space: nowrap;
}
}
2025-07-03 20:19:09 +08:00
</style>