硅谷(12)菜单管理
菜单管理模块
11.1 模块初始界面
11.1.1 API&&type
API:
import request from '@/utils/request'
import type { PermisstionResponseData, MenuParams } from './type'
//枚举地址
enum API {
//获取全部菜单与按钮的标识数据
ALLPERMISSTION_URL = '/admin/acl/permission',
}
//获取菜单数据
export const reqAllPermisstion = () => {
return request.get<any, PermisstionResponseData>(API.ALLPERMISSTION_URL)
}
TYPE:
注意:type这里使用了嵌套
//数据类型定义
export interface ResponseData {
code: number
message: string
ok: boolean
}
//菜单数据与按钮数据的ts类型
export interface Permisstion {
id?: number
createTime: string
updateTime: string
pid: number
name: string
code: null
toCode: null
type: number
status: null
level: number
children?: PermisstionList
select: boolean
}
export type PermisstionList = Permisstion[]
//菜单接口返回的数据类型
export interface PermisstionResponseData extends ResponseData {
data: PermisstionList
}
11.1.2 组件获取初始数据
//存储菜单的数据
let PermisstionArr = ref<PermisstionList>([])
//组件挂载完毕
onMounted(() => {
getHasPermisstion()
})
//获取菜单数据的方法
const getHasPermisstion = async () => {
let result: PermisstionResponseData = await reqAllPermisstion()
if (result.code == 200) {
PermisstionArr.value = result.data
}
}
11.1.3 模板展示数据
<div>
<el-table
:data="PermisstionArr"
style="width: 100%; margin-bottom: 20px"
row-key="id"
border
>
<el-table-column label="名称" prop="name"></el-table-column>
<el-table-column label="权限值" prop="code"></el-table-column>
<el-table-column label="修改时间" prop="updateTime"></el-table-column>
<el-table-column label="操作">
<!-- row:即为已有的菜单对象|按钮的对象的数据 -->
<template #="{ row, $index }">
<el-button
type="primary"
size="small"
:disabled="row.level == 4 ? true : false"
>
{{ row.level == 3 ? '添加功能' : '添加菜单' }}
</el-button>
<el-button
type="primary"
size="small"
:disabled="row.level == 1 ? true : false"
>
编辑
</el-button>
<el-button
type="primary"
size="small"
:disabled="row.level == 1 ? true : false"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
</div>
11.2 更新与添加菜单功能
11.2.1 API&&TYPE
API:
//给某一级菜单新增一个子菜单
ADDMENU_URL = '/admin/acl/permission/save',
//更新某一个已有的菜单
UPDATE_URL = '/admin/acl/permission/update',
//添加与更新菜单的方法
export const reqAddOrUpdateMenu = (data: MenuParams) => {
if (data.id) {
return request.put<any, any>(API.UPDATE_URL, data)
} else {
return request.post<any, any>(API.ADDMENU_URL, data)
}
}
11.2.2 对话框静态
<!-- 对话框组件:添加或者更新已有的菜单的数据结构 -->
<el-dialog
v-model="dialogVisible"
>
<!-- 表单组件:收集新增与已有的菜单的数据 -->
<el-form>
<el-form-item label="名称">
<el-input
placeholder="请你输入菜单名称"
></el-input>
</el-form-item>
<el-form-item label="权限">
<el-input
placeholder="请你输入权限数值"
></el-input>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="save">确定</el-button>
</span>
</template>
</el-dialog>
11.2.3 收集数据
需要的参数一共是4个,其中code、name由v-model绑定的对话框收集。其余俩个通过点击按钮传递的参数收集。
//携带的参数
let menuData = reactive<MenuParams>({
code: '',
level: 0,
name: '',
pid: 0,
})
//添加菜单按钮的回调
const addPermisstion = (row: Permisstion) => {
//清空数据
Object.assign(menuData, {
id: 0,
code: '',
level: 0,
name: '',
pid: 0,
})
//对话框显示出来
dialogVisible.value = true
//收集新增的菜单的level数值
menuData.level = row.level + 1
//给谁新增子菜单
menuData.pid = row.id as number
}
//编辑已有的菜单
const updatePermisstion = (row: Permisstion) => {
dialogVisible.value = true
//点击修改按钮:收集已有的菜单的数据进行更新
Object.assign(menuData, row)
}
11.2.4 发送请求
//确定按钮的回调
const save = async () => {
//发请求:新增子菜单|更新某一个已有的菜单的数据
let result: any = await reqAddOrUpdateMenu(menuData)
if (result.code == 200) {
//对话框隐藏
dialogVisible.value = false
//提示信息
ElMessage({
type: 'success',
message: menuData.id ? '更新成功' : '添加成功',
})
//再次获取全部最新的菜单的数据
getHasPermisstion()
}
}
11.3 删除模块
11.3.1 API
//删除已有的菜单
DELETEMENU_URL = '/admin/acl/permission/remove/',
//删除某一个已有的菜单
export const reqRemoveMenu = (id: number) => {
return request.delete<any, any>(API.DELETEMENU_URL + id)
}
11.3.2 删除点击函数
<el-popconfirm
:title="`你确定要删除${row.name}?`"
width="260px"
@confirm="removeMenu(row.id)"
>
<template #reference>
<el-button
type="primary"
size="small"
:disabled="row.level == 1 ? true : false"
>
删除
</el-button>
</template>
</el-popconfirm>
11.3.3 删除的回调
//删除按钮回调
const removeMenu = async (id: number) => {
let result = await reqRemoveMenu(id)
if (result.code == 200) {
ElMessage({ type: 'success', message: '删除成功' })
getHasPermisstion()
}
}