init Project

This commit is contained in:
2025-04-09 12:10:46 +08:00
parent 505d08443c
commit 75a1447d66
207 changed files with 26387 additions and 13 deletions

View File

@@ -0,0 +1,422 @@
<template>
<div class="authority">
<warning-bar title="注:右上角头像下拉可切换角色" />
<div class="gva-table-box">
<div class="gva-btn-list">
<el-button type="primary" icon="plus" @click="addAuthority(0)"
>新增角色</el-button
>
</div>
<el-table
:data="tableData"
:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
row-key="authorityId"
style="width: 100%"
>
<el-table-column label="角色ID" min-width="180" prop="authorityId" />
<el-table-column
align="left"
label="角色名称"
min-width="180"
prop="authorityName"
/>
<el-table-column align="left" label="操作" width="460">
<template #default="scope">
<el-button
icon="setting"
type="primary"
link
@click="openDrawer(scope.row)"
>设置权限</el-button
>
<el-button
icon="plus"
type="primary"
link
@click="addAuthority(scope.row.authorityId)"
>新增子角色</el-button
>
<el-button
icon="copy-document"
type="primary"
link
@click="copyAuthorityFunc(scope.row)"
>拷贝</el-button
>
<el-button
icon="edit"
type="primary"
link
@click="editAuthority(scope.row)"
>编辑</el-button
>
<el-button
icon="delete"
type="primary"
link
@click="deleteAuth(scope.row)"
>删除</el-button
>
</template>
</el-table-column>
</el-table>
</div>
<!-- 新增角色弹窗 -->
<el-drawer v-model="authorityFormVisible" :size="appStore.drawerSize" :show-close="false">
<template #header>
<div class="flex justify-between items-center">
<span class="text-lg">{{ authorityTitleForm }}</span>
<div>
<el-button @click="closeAuthorityForm"> </el-button>
<el-button type="primary" @click="submitAuthorityForm"
> </el-button
>
</div>
</div>
</template>
<el-form
ref="authorityForm"
:model="form"
:rules="rules"
label-width="80px"
>
<el-form-item label="父级角色" prop="parentId">
<el-cascader
v-model="form.parentId"
style="width: 100%"
:disabled="dialogType === 'add'"
:options="AuthorityOption"
:props="{
checkStrictly: true,
label: 'authorityName',
value: 'authorityId',
disabled: 'disabled',
emitPath: false
}"
:show-all-levels="false"
filterable
/>
</el-form-item>
<el-form-item label="角色ID" prop="authorityId">
<el-input
v-model="form.authorityId"
:disabled="dialogType === 'edit'"
autocomplete="off"
maxlength="15"
/>
</el-form-item>
<el-form-item label="角色姓名" prop="authorityName">
<el-input v-model="form.authorityName" autocomplete="off" />
</el-form-item>
</el-form>
</el-drawer>
<el-drawer
v-if="drawer"
v-model="drawer"
:size="appStore.drawerSize"
title="角色配置"
>
<el-tabs :before-leave="autoEnter" type="border-card">
<el-tab-pane label="角色菜单">
<Menus ref="menus" :row="activeRow" @changeRow="changeRow" />
</el-tab-pane>
<el-tab-pane label="角色api">
<Apis ref="apis" :row="activeRow" @changeRow="changeRow" />
</el-tab-pane>
<el-tab-pane label="资源权限">
<Datas
ref="datas"
:authority="tableData"
:row="activeRow"
@changeRow="changeRow"
/>
</el-tab-pane>
</el-tabs>
</el-drawer>
</div>
</template>
<script setup>
import {
getAuthorityList,
deleteAuthority,
createAuthority,
updateAuthority,
copyAuthority
} from '@/api/authority'
import Menus from '@/view/superAdmin/authority/components/menus.vue'
import Apis from '@/view/superAdmin/authority/components/apis.vue'
import Datas from '@/view/superAdmin/authority/components/datas.vue'
import WarningBar from '@/components/warningBar/warningBar.vue'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useAppStore } from "@/pinia"
defineOptions({
name: 'Authority'
})
const mustUint = (rule, value, callback) => {
if (!/^[0-9]*[1-9][0-9]*$/.test(value)) {
return callback(new Error('请输入正整数'))
}
return callback()
}
const AuthorityOption = ref([
{
authorityId: 0,
authorityName: '根角色/严格模式下为当前角色'
}
])
const drawer = ref(false)
const dialogType = ref('add')
const activeRow = ref({})
const appStore = useAppStore()
const authorityTitleForm = ref('新增角色')
const authorityFormVisible = ref(false)
const apiDialogFlag = ref(false)
const copyForm = ref({})
const form = ref({
authorityId: 0,
authorityName: '',
parentId: 0
})
const rules = ref({
authorityId: [
{ required: true, message: '请输入角色ID', trigger: 'blur' },
{ validator: mustUint, trigger: 'blur', message: '必须为正整数' }
],
authorityName: [
{ required: true, message: '请输入角色名', trigger: 'blur' }
],
parentId: [{ required: true, message: '请选择父角色', trigger: 'blur' }]
})
const tableData = ref([])
// 查询
const getTableData = async () => {
const table = await getAuthorityList()
if (table.code === 0) {
tableData.value = table.data
}
}
getTableData()
const changeRow = (key, value) => {
activeRow.value[key] = value
}
const menus = ref(null)
const apis = ref(null)
const datas = ref(null)
const autoEnter = (activeName, oldActiveName) => {
const paneArr = [menus, apis, datas]
if (oldActiveName) {
if (paneArr[oldActiveName].value.needConfirm) {
paneArr[oldActiveName].value.enterAndNext()
paneArr[oldActiveName].value.needConfirm = false
}
}
}
// 拷贝角色
const copyAuthorityFunc = (row) => {
setOptions()
authorityTitleForm.value = '拷贝角色'
dialogType.value = 'copy'
for (const k in form.value) {
form.value[k] = row[k]
}
copyForm.value = row
authorityFormVisible.value = true
}
const openDrawer = (row) => {
drawer.value = true
activeRow.value = row
}
// 删除角色
const deleteAuth = (row) => {
ElMessageBox.confirm('此操作将永久删除该角色, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(async () => {
const res = await deleteAuthority({ authorityId: row.authorityId })
if (res.code === 0) {
ElMessage({
type: 'success',
message: '删除成功!'
})
getTableData()
}
})
.catch(() => {
ElMessage({
type: 'info',
message: '已取消删除'
})
})
}
// 初始化表单
const authorityForm = ref(null)
const initForm = () => {
if (authorityForm.value) {
authorityForm.value.resetFields()
}
form.value = {
authorityId: 0,
authorityName: '',
parentId: 0
}
}
// 关闭窗口
const closeAuthorityForm = () => {
initForm()
authorityFormVisible.value = false
apiDialogFlag.value = false
}
// 确定弹窗
const submitAuthorityForm = () => {
authorityForm.value.validate(async (valid) => {
if (valid) {
form.value.authorityId = Number(form.value.authorityId)
switch (dialogType.value) {
case 'add':
{
const res = await createAuthority(form.value)
if (res.code === 0) {
ElMessage({
type: 'success',
message: '添加成功!'
})
getTableData()
closeAuthorityForm()
}
}
break
case 'edit':
{
const res = await updateAuthority(form.value)
if (res.code === 0) {
ElMessage({
type: 'success',
message: '添加成功!'
})
getTableData()
closeAuthorityForm()
}
}
break
case 'copy': {
const data = {
authority: {
authorityId: 0,
authorityName: '',
datauthorityId: [],
parentId: 0
},
oldAuthorityId: 0
}
data.authority.authorityId = form.value.authorityId
data.authority.authorityName = form.value.authorityName
data.authority.parentId = form.value.parentId
data.authority.dataAuthorityId = copyForm.value.dataAuthorityId
data.oldAuthorityId = copyForm.value.authorityId
const res = await copyAuthority(data)
if (res.code === 0) {
ElMessage({
type: 'success',
message: '复制成功!'
})
getTableData()
}
}
}
initForm()
authorityFormVisible.value = false
}
})
}
const setOptions = () => {
AuthorityOption.value = [
{
authorityId: 0,
authorityName: '根角色(严格模式下为当前用户角色)'
}
]
setAuthorityOptions(tableData.value, AuthorityOption.value, false)
}
const setAuthorityOptions = (AuthorityData, optionsData, disabled) => {
AuthorityData &&
AuthorityData.forEach((item) => {
if (item.children && item.children.length) {
const option = {
authorityId: item.authorityId,
authorityName: item.authorityName,
disabled: disabled || item.authorityId === form.value.authorityId,
children: []
}
setAuthorityOptions(
item.children,
option.children,
disabled || item.authorityId === form.value.authorityId
)
optionsData.push(option)
} else {
const option = {
authorityId: item.authorityId,
authorityName: item.authorityName,
disabled: disabled || item.authorityId === form.value.authorityId
}
optionsData.push(option)
}
})
}
// 增加角色
const addAuthority = (parentId) => {
initForm()
authorityTitleForm.value = '新增角色'
dialogType.value = 'add'
form.value.parentId = parentId
setOptions()
authorityFormVisible.value = true
}
// 编辑角色
const editAuthority = (row) => {
setOptions()
authorityTitleForm.value = '编辑角色'
dialogType.value = 'edit'
for (const key in form.value) {
form.value[key] = row[key]
}
setOptions()
authorityForm.value && authorityForm.value.clearValidate()
authorityFormVisible.value = true
}
</script>
<style lang="scss">
.authority {
.el-input-number {
margin-left: 15px;
span {
display: none;
}
}
}
.tree-content {
margin-top: 10px;
height: calc(100vh - 158px);
overflow: auto;
}
</style>

View File

@@ -0,0 +1,174 @@
<template>
<div>
<div class="sticky top-0.5 z-10 flex space-x-2">
<el-input
v-model="filterTextName"
class="flex-1"
placeholder="筛选名字"
/>
<el-input
v-model="filterTextPath"
class="flex-1"
placeholder="筛选路径"
/>
<el-button class="float-right" type="primary" @click="authApiEnter"
> </el-button
>
</div>
<div class="tree-content">
<el-scrollbar>
<el-tree
ref="apiTree"
:data="apiTreeData"
:default-checked-keys="apiTreeIds"
:props="apiDefaultProps"
default-expand-all
highlight-current
node-key="onlyId"
show-checkbox
:filter-node-method="filterNode"
@check="nodeChange"
>
<template #default="{ _, data }">
<div class="flex items-center justify-between w-full pr-1">
<span>{{ data.description }} </span>
<el-tooltip :content="data.path">
<span
class="max-w-[240px] break-all overflow-ellipsis overflow-hidden"
>{{ data.path }}</span
>
</el-tooltip>
</div>
</template>
</el-tree>
</el-scrollbar>
</div>
</div>
</template>
<script setup>
import { getAllApis } from '@/api/api'
import { UpdateCasbin, getPolicyPathByAuthorityId } from '@/api/casbin'
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
defineOptions({
name: 'Apis'
})
const props = defineProps({
row: {
default: function () {
return {}
},
type: Object
}
})
const apiDefaultProps = ref({
children: 'children',
label: 'description'
})
const filterTextName = ref('')
const filterTextPath = ref('')
const apiTreeData = ref([])
const apiTreeIds = ref([])
const activeUserId = ref('')
const init = async () => {
const res2 = await getAllApis()
const apis = res2.data.apis
apiTreeData.value = buildApiTree(apis)
const res = await getPolicyPathByAuthorityId({
authorityId: props.row.authorityId
})
activeUserId.value = props.row.authorityId
apiTreeIds.value = []
res.data.paths &&
res.data.paths.forEach((item) => {
apiTreeIds.value.push('p:' + item.path + 'm:' + item.method)
})
}
init()
const needConfirm = ref(false)
const nodeChange = () => {
needConfirm.value = true
}
// 暴露给外层使用的切换拦截统一方法
const enterAndNext = () => {
authApiEnter()
}
// 创建api树方法
const buildApiTree = (apis) => {
const apiObj = {}
apis &&
apis.forEach((item) => {
item.onlyId = 'p:' + item.path + 'm:' + item.method
if (Object.prototype.hasOwnProperty.call(apiObj, item.apiGroup)) {
apiObj[item.apiGroup].push(item)
} else {
Object.assign(apiObj, { [item.apiGroup]: [item] })
}
})
const apiTree = []
for (const key in apiObj) {
const treeNode = {
ID: key,
description: key + '组',
children: apiObj[key]
}
apiTree.push(treeNode)
}
return apiTree
}
// 关联关系确定
const apiTree = ref(null)
const authApiEnter = async () => {
const checkArr = apiTree.value.getCheckedNodes(true)
var casbinInfos = []
checkArr &&
checkArr.forEach((item) => {
var casbinInfo = {
path: item.path,
method: item.method
}
casbinInfos.push(casbinInfo)
})
const res = await UpdateCasbin({
authorityId: activeUserId.value,
casbinInfos
})
if (res.code === 0) {
ElMessage({ type: 'success', message: 'api设置成功' })
}
}
defineExpose({
needConfirm,
enterAndNext
})
const filterNode = (value, data) => {
if (!filterTextName.value && !filterTextPath.value) return true
let matchesName, matchesPath
if (!filterTextName.value) {
matchesName = true
} else {
matchesName =
data.description && data.description.includes(filterTextName.value)
}
if (!filterTextPath.value) {
matchesPath = true
} else {
matchesPath = data.path && data.path.includes(filterTextPath.value)
}
return matchesName && matchesPath
}
watch([filterTextName, filterTextPath], () => {
apiTree.value.filter('')
})
</script>

View File

@@ -0,0 +1,145 @@
<template>
<div>
<warning-bar
title="此功能仅用于创建角色和角色的many2many关系表具体使用还须自己结合表实现业务详情参考示例代码客户示例。此功能不建议使用建议使用插件市场【组织管理功能点击前往】来管理资源权限。"
href="https://plugin.gin-vue-admin.com/#/layout/newPluginInfo?id=36"
/>
<div class="sticky top-0.5 z-10 my-4">
<el-button class="float-left" type="primary" @click="all">全选</el-button>
<el-button class="float-left" type="primary" @click="self"
>本角色</el-button
>
<el-button class="float-left" type="primary" @click="selfAndChildren"
>本角色及子角色</el-button
>
<el-button class="float-right" type="primary" @click="authDataEnter"
> </el-button
>
</div>
<div class="clear-both pt-4">
<el-checkbox-group v-model="dataAuthorityId" @change="selectAuthority">
<el-checkbox
v-for="(item, key) in authoritys"
:key="key"
:label="item"
>{{ item.authorityName }}</el-checkbox
>
</el-checkbox-group>
</div>
</div>
</template>
<script setup>
import { setDataAuthority } from '@/api/authority'
import WarningBar from '@/components/warningBar/warningBar.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
defineOptions({
name: 'Datas'
})
const props = defineProps({
row: {
default: function () {
return {}
},
type: Object
},
authority: {
default: function () {
return []
},
type: Array
}
})
const authoritys = ref([])
const needConfirm = ref(false)
// 平铺角色
const roundAuthority = (authoritysData) => {
authoritysData &&
authoritysData.forEach((item) => {
const obj = {}
obj.authorityId = item.authorityId
obj.authorityName = item.authorityName
authoritys.value.push(obj)
if (item.children && item.children.length) {
roundAuthority(item.children)
}
})
}
const dataAuthorityId = ref([])
const init = () => {
roundAuthority(props.authority)
props.row.dataAuthorityId &&
props.row.dataAuthorityId.forEach((item) => {
const obj =
authoritys.value &&
authoritys.value.filter(
(au) => au.authorityId === item.authorityId
) &&
authoritys.value.filter(
(au) => au.authorityId === item.authorityId
)[0]
dataAuthorityId.value.push(obj)
})
}
init()
// 暴露给外层使用的切换拦截统一方法
const enterAndNext = () => {
authDataEnter()
}
const emit = defineEmits(['changeRow'])
const all = () => {
dataAuthorityId.value = [...authoritys.value]
emit('changeRow', 'dataAuthorityId', dataAuthorityId.value)
needConfirm.value = true
}
const self = () => {
dataAuthorityId.value = authoritys.value.filter(
(item) => item.authorityId === props.row.authorityId
)
emit('changeRow', 'dataAuthorityId', dataAuthorityId.value)
needConfirm.value = true
}
const selfAndChildren = () => {
const arrBox = []
getChildrenId(props.row, arrBox)
dataAuthorityId.value = authoritys.value.filter(
(item) => arrBox.indexOf(item.authorityId) > -1
)
emit('changeRow', 'dataAuthorityId', dataAuthorityId.value)
needConfirm.value = true
}
const getChildrenId = (row, arrBox) => {
arrBox.push(row.authorityId)
row.children &&
row.children.forEach((item) => {
getChildrenId(item, arrBox)
})
}
// 提交
const authDataEnter = async () => {
const res = await setDataAuthority(props.row)
if (res.code === 0) {
ElMessage({ type: 'success', message: '资源设置成功' })
}
}
// 选择
const selectAuthority = () => {
dataAuthorityId.value = dataAuthorityId.value.filter((item) => item)
emit('changeRow', 'dataAuthorityId', dataAuthorityId.value)
needConfirm.value = true
}
defineExpose({
enterAndNext,
needConfirm
})
</script>

View File

@@ -0,0 +1,233 @@
<template>
<div>
<div class="sticky top-0.5 z-10">
<el-input v-model="filterText" class="w-3/5" placeholder="筛选" />
<el-button class="float-right" type="primary" @click="relation"
> </el-button
>
</div>
<div class="tree-content clear-both">
<el-scrollbar>
<el-tree
ref="menuTree"
:data="menuTreeData"
:default-checked-keys="menuTreeIds"
:props="menuDefaultProps"
default-expand-all
highlight-current
node-key="ID"
show-checkbox
:filter-node-method="filterNode"
@check="nodeChange"
>
<template #default="{ node, data }">
<span class="custom-tree-node">
<span>{{ node.label }}</span>
<span v-if="node.checked">
<el-button
type="primary"
link
:style="{
color:
row.defaultRouter === data.name ? '#E6A23C' : '#85ce61'
}"
@click="() => setDefault(data)"
>
{{ row.defaultRouter === data.name ? '首页' : '设为首页' }}
</el-button>
</span>
<span v-if="data.menuBtn.length">
<el-button type="primary" link @click="() => OpenBtn(data)">
分配按钮
</el-button>
</span>
</span>
</template>
</el-tree>
</el-scrollbar>
</div>
<el-dialog v-model="btnVisible" title="分配按钮" destroy-on-close>
<el-table
ref="btnTableRef"
:data="btnData"
row-key="ID"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="55" />
<el-table-column label="按钮名称" prop="name" />
<el-table-column label="按钮备注" prop="desc" />
</el-table>
<template #footer>
<div class="dialog-footer">
<el-button @click="closeDialog"> </el-button>
<el-button type="primary" @click="enterDialog"> </el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup>
import {
getBaseMenuTree,
getMenuAuthority,
addMenuAuthority
} from '@/api/menu'
import { updateAuthority } from '@/api/authority'
import { getAuthorityBtnApi, setAuthorityBtnApi } from '@/api/authorityBtn'
import { nextTick, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
defineOptions({
name: 'Menus'
})
const props = defineProps({
row: {
default: function () {
return {}
},
type: Object
}
})
const emit = defineEmits(['changeRow'])
const filterText = ref('')
const menuTreeData = ref([])
const menuTreeIds = ref([])
const needConfirm = ref(false)
const menuDefaultProps = ref({
children: 'children',
label: function (data) {
return data.meta.title
},
disabled: function (data) {
return props.row.defaultRouter === data.name
}
})
const init = async () => {
// 获取所有菜单树
const res = await getBaseMenuTree()
menuTreeData.value = res.data.menus
const res1 = await getMenuAuthority({ authorityId: props.row.authorityId })
const menus = res1.data.menus
const arr = []
menus.forEach((item) => {
// 防止直接选中父级造成全选
if (!menus.some((same) => same.parentId === item.menuId)) {
arr.push(Number(item.menuId))
}
})
menuTreeIds.value = arr
}
init()
const setDefault = async (data) => {
const res = await updateAuthority({
authorityId: props.row.authorityId,
AuthorityName: props.row.authorityName,
parentId: props.row.parentId,
defaultRouter: data.name
})
if (res.code === 0) {
ElMessage({ type: 'success', message: '设置成功' })
emit('changeRow', 'defaultRouter', res.data.authority.defaultRouter)
}
}
const nodeChange = () => {
needConfirm.value = true
}
// 暴露给外层使用的切换拦截统一方法
const enterAndNext = () => {
relation()
}
// 关联树 确认方法
const menuTree = ref(null)
const relation = async () => {
const checkArr = menuTree.value.getCheckedNodes(false, true)
const res = await addMenuAuthority({
menus: checkArr,
authorityId: props.row.authorityId
})
if (res.code === 0) {
ElMessage({
type: 'success',
message: '菜单设置成功!'
})
}
}
defineExpose({ enterAndNext, needConfirm })
const btnVisible = ref(false)
const btnData = ref([])
const multipleSelection = ref([])
const btnTableRef = ref()
let menuID = ''
const OpenBtn = async (data) => {
menuID = data.ID
const res = await getAuthorityBtnApi({
menuID: menuID,
authorityId: props.row.authorityId
})
if (res.code === 0) {
openDialog(data)
await nextTick()
if (res.data.selected) {
res.data.selected.forEach((id) => {
btnData.value.some((item) => {
if (item.ID === id) {
btnTableRef.value.toggleRowSelection(item, true)
}
})
})
}
}
}
const handleSelectionChange = (val) => {
multipleSelection.value = val
}
const openDialog = (data) => {
btnVisible.value = true
btnData.value = data.menuBtn
}
const closeDialog = () => {
btnVisible.value = false
}
const enterDialog = async () => {
const selected = multipleSelection.value.map((item) => item.ID)
const res = await setAuthorityBtnApi({
menuID,
selected,
authorityId: props.row.authorityId
})
if (res.code === 0) {
ElMessage({ type: 'success', message: '设置成功' })
btnVisible.value = false
}
}
const filterNode = (value, data) => {
if (!value) return true
// console.log(data.mate.title)
return data.meta.title.indexOf(value) !== -1
}
watch(filterText, (val) => {
menuTree.value.filter(val)
})
</script>
<style lang="scss" scoped>
.custom-tree-node {
span + span {
@apply ml-3;
}
}
</style>