refactor(menu): 菜单项增加类型判别并统一入口判权
- 菜单项增加 kind 字段,导航按判别字段分发而非嗅探地址前缀 - 菜单配置的权限标识接入 PERM 常量 - 新增 isMenuAllowed 统一首页与工作台的入口判权 - 首页删除与 WorkMenuItem 重复的局部类型
This commit is contained in:
1 parent
9c42f79e99
commit
12f9b0057a
5 files changed
+47
-38
No files matched your search
@@ -1,14 +1,15 @@
|
||||
import { ensureAuthenticated } from "@/utils/auth";
|
||||
|
||||
// 导航目标:url 为空表示功能未开放
|
||||
interface NavItem {
|
||||
// 导航目标:kind 区分站内路由与站外链接,url 为空表示功能未开放
|
||||
interface NavTarget {
|
||||
kind: "route" | "link";
|
||||
url?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务菜单导航
|
||||
*
|
||||
* 站外地址在 H5 新开标签、其余端进内置 webview,站内地址走 navigateTo。
|
||||
* 站外链接在 H5 新开标签、其余端进内置 webview,站内路由走 navigateTo。
|
||||
*/
|
||||
export function useNavigation() {
|
||||
const toast = useToast();
|
||||
@@ -18,30 +19,30 @@ export function useNavigation() {
|
||||
*
|
||||
* 未登录先跳登录页并带上来路;地址缺失或页面不存在时给出提示。
|
||||
*
|
||||
* @param item - 菜单项,url 支持站内路径与 http(s) 地址
|
||||
* @param target - 菜单项
|
||||
*/
|
||||
const navigate = (item: NavItem) => {
|
||||
if (!item.url) {
|
||||
const navigate = (target: NavTarget) => {
|
||||
if (!target.url) {
|
||||
toast.info("功能开发中");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ensureAuthenticated()) return;
|
||||
|
||||
if (item.url.startsWith("http://") || item.url.startsWith("https://")) {
|
||||
if (target.kind === "link") {
|
||||
// #ifdef H5
|
||||
window.open(item.url, "_blank");
|
||||
window.open(target.url, "_blank");
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
uni.navigateTo({
|
||||
url: `/pages/webview/index?url=${encodeURIComponent(item.url)}`,
|
||||
url: `/pages/webview/index?url=${encodeURIComponent(target.url)}`,
|
||||
});
|
||||
// #endif
|
||||
return;
|
||||
}
|
||||
|
||||
uni.navigateTo({
|
||||
url: item.url,
|
||||
url: target.url,
|
||||
fail: () => {
|
||||
toast.info("页面不存在");
|
||||
},
|
||||
|
||||
+24
-8
@@ -1,6 +1,10 @@
|
||||
/** 工作台菜单配置 */
|
||||
|
||||
import { PERM } from "@/constants/permissions";
|
||||
|
||||
export interface WorkMenuItem {
|
||||
/** 入口类型:route 站内路由、link 站外链接 */
|
||||
kind: "route" | "link";
|
||||
icon: string;
|
||||
title: string;
|
||||
url: string;
|
||||
@@ -17,52 +21,60 @@ export const workMenuGroups: WorkMenuGroup[] = [
|
||||
title: "系统管理",
|
||||
children: [
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/user.svg",
|
||||
title: "用户管理",
|
||||
url: "/subPages/work/user/index",
|
||||
perm: "sys:user:list",
|
||||
perm: PERM.user.list,
|
||||
},
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/role.svg",
|
||||
title: "角色管理",
|
||||
url: "/subPages/work/role/index",
|
||||
perm: "sys:role:list",
|
||||
perm: PERM.role.list,
|
||||
},
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/company.svg",
|
||||
title: "部门管理",
|
||||
url: "/subPages/work/dept/index",
|
||||
perm: "sys:dept:list",
|
||||
perm: PERM.dept.list,
|
||||
},
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/settings.svg",
|
||||
title: "系统配置",
|
||||
url: "/subPages/work/config/index",
|
||||
perm: "sys:config:list",
|
||||
perm: PERM.config.list,
|
||||
},
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/file.svg",
|
||||
title: "字典管理",
|
||||
url: "/subPages/work/dict/index",
|
||||
perm: "sys:dict:list",
|
||||
perm: PERM.dict.list,
|
||||
},
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/menu.svg",
|
||||
title: "菜单管理",
|
||||
url: "/subPages/work/menu/index",
|
||||
perm: "sys:menu:list",
|
||||
perm: PERM.menu.list,
|
||||
},
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/bell.svg",
|
||||
title: "通知公告",
|
||||
url: "/subPages/work/notice/index",
|
||||
perm: "sys:notice:list",
|
||||
perm: PERM.notice.list,
|
||||
},
|
||||
{
|
||||
kind: "route",
|
||||
icon: "/static/icons/log.svg",
|
||||
title: "系统日志",
|
||||
url: "/subPages/work/log/index",
|
||||
perm: "sys:log:list",
|
||||
perm: PERM.log.list,
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -70,24 +82,28 @@ export const workMenuGroups: WorkMenuGroup[] = [
|
||||
title: "项目链接",
|
||||
children: [
|
||||
{
|
||||
kind: "link",
|
||||
icon: "/static/icons/company.svg",
|
||||
title: "官网",
|
||||
url: "https://www.youlai.tech/",
|
||||
perm: "",
|
||||
},
|
||||
{
|
||||
kind: "link",
|
||||
icon: "/static/icons/file.svg",
|
||||
title: "文档",
|
||||
url: "https://youlai.blog.csdn.net/article/details/143222890",
|
||||
perm: "",
|
||||
},
|
||||
{
|
||||
kind: "link",
|
||||
icon: "/static/icons/api.svg",
|
||||
title: "接口",
|
||||
url: "https://youlai.apifox.cn/",
|
||||
perm: "",
|
||||
},
|
||||
{
|
||||
kind: "link",
|
||||
icon: "/static/icons/repo.svg",
|
||||
title: "仓库",
|
||||
url: "https://atomgit.com/youlai/youlai-app",
|
||||
|
||||
@@ -98,8 +98,8 @@ import dayjs from "dayjs";
|
||||
import { onReady, onShow } from "@dcloudio/uni-app";
|
||||
import { useThemeStore, useUserStore } from "@/store";
|
||||
import { useNavigation } from "@/composables/useNavigation";
|
||||
import { workMenuGroups } from "@/config/work-menu";
|
||||
import { hasPermission } from "@/utils/permission";
|
||||
import { workMenuGroups, type WorkMenuItem } from "@/config/work-menu";
|
||||
import { isMenuAllowed } from "@/utils/permission";
|
||||
import LogAPI, { type VisitOverview, type VisitTrend } from "@/api/log";
|
||||
import NoticeAPI, { type NoticeItem } from "@/api/notice";
|
||||
|
||||
@@ -109,13 +109,6 @@ definePage({
|
||||
layout: "tabbar",
|
||||
});
|
||||
|
||||
interface NavItem {
|
||||
icon: string;
|
||||
title: string;
|
||||
url: string;
|
||||
perm: string;
|
||||
}
|
||||
|
||||
const userStore = useUserStore();
|
||||
const themeStore = useThemeStore();
|
||||
const { navigate } = useNavigation();
|
||||
@@ -148,11 +141,11 @@ const QUICK_NAV_LIMIT = 4;
|
||||
|
||||
// 快捷入口:未登录时展示前几个作为引导,点击后统一跳登录页;
|
||||
// 已登录按权限过滤,不足 4 个就显示多少
|
||||
const quickNavList = computed<NavItem[]>(() => {
|
||||
const result: NavItem[] = [];
|
||||
const quickNavList = computed<WorkMenuItem[]>(() => {
|
||||
const result: WorkMenuItem[] = [];
|
||||
for (const group of workMenuGroups) {
|
||||
for (const item of group.children) {
|
||||
if (!isAuthenticated.value || hasPermission(item.perm)) result.push(item);
|
||||
if (!isAuthenticated.value || isMenuAllowed(item.perm)) result.push(item);
|
||||
if (result.length >= QUICK_NAV_LIMIT) return result;
|
||||
}
|
||||
}
|
||||
@@ -223,7 +216,7 @@ async function loadVisitTrendData() {
|
||||
}
|
||||
|
||||
// 快捷导航点击:未登录先跳登录页,已登录按权限跳转
|
||||
function handleNavItemClick(item: NavItem) {
|
||||
function handleNavItemClick(item: WorkMenuItem) {
|
||||
if (!isAuthenticated.value) {
|
||||
navigateToLogin();
|
||||
return;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
import { computed } from "vue";
|
||||
import { useNavigation } from "@/composables/useNavigation";
|
||||
import { workMenuGroups } from "@/config/work-menu";
|
||||
import { hasPermission } from "@/utils/permission";
|
||||
import { isMenuAllowed } from "@/utils/permission";
|
||||
|
||||
definePage({
|
||||
name: "work",
|
||||
@@ -40,12 +40,6 @@ definePage({
|
||||
|
||||
const { navigate } = useNavigation();
|
||||
|
||||
// 无权限码的菜单视为公开入口,其余按当前账号的权限码判断
|
||||
const isMenuAllowed = (perm: string) => {
|
||||
if (!perm) return true;
|
||||
return hasPermission(perm);
|
||||
};
|
||||
|
||||
// 根据权限过滤后的菜单分组,未授权分组整体不展示
|
||||
const visibleGroups = computed(() => {
|
||||
return workMenuGroups
|
||||
|
||||
@@ -17,3 +17,8 @@ export function hasPermission(perm: string | string[]): boolean {
|
||||
|
||||
return keys.some((key) => perms.includes(key));
|
||||
}
|
||||
|
||||
/** 菜单入口判权:无权限码的入口视为公开,其余按当前账号权限判断 */
|
||||
export function isMenuAllowed(perm: string): boolean {
|
||||
return !perm || hasPermission(perm);
|
||||
}
|
||||
Reference in new issue
Block a user