refactor: permission (#357)

* refactor: permission

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* fix: 修复`mix`混合模式导航在生产环境左侧菜单一定机率不显示的问题

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update

* chore: update
This commit is contained in:
RealityBoy
2022-10-25 12:17:13 +08:00
committed by GitHub
parent cedc84d31a
commit 6ef4cf9fb6
47 changed files with 605 additions and 386 deletions

View File

@@ -1,7 +1,12 @@
// 根据角色动态生成路由
// 模拟后端动态生成路由
import { MockMethod } from "vite-plugin-mock";
// http://mockjs.com/examples.html#Object
/**
* roles页面级别权限这里模拟二种 "admin"、"common"
* admin管理员角色
* common普通角色
*/
const systemRouter = {
path: "/system",
meta: {
@@ -15,7 +20,8 @@ const systemRouter = {
name: "User",
meta: {
icon: "flUser",
title: "menus.hsUser"
title: "menus.hsUser",
roles: ["admin"]
}
},
{
@@ -23,7 +29,8 @@ const systemRouter = {
name: "Role",
meta: {
icon: "role",
title: "menus.hsRole"
title: "menus.hsRole",
roles: ["admin"]
}
},
{
@@ -31,7 +38,8 @@ const systemRouter = {
name: "Dept",
meta: {
icon: "dept",
title: "menus.hsDept"
title: "menus.hsDept",
roles: ["admin"]
}
},
{
@@ -41,7 +49,8 @@ const systemRouter = {
meta: {
icon: "dict",
title: "menus.hsDict",
keepAlive: true
keepAlive: true,
roles: ["admin"]
}
}
]
@@ -52,13 +61,14 @@ const permissionRouter = {
meta: {
title: "menus.permission",
icon: "lollipop",
rank: 7
rank: 10
},
children: [
{
path: "/permission/page/index",
name: "PermissionPage",
meta: {
roles: ["admin", "common"],
title: "menus.permissionPage"
}
},
@@ -67,7 +77,8 @@ const permissionRouter = {
name: "PermissionButton",
meta: {
title: "menus.permissionButton",
authority: []
roles: ["admin", "common"],
auths: ["btn_add", "btn_edit", "btn_delete"]
}
}
]
@@ -78,7 +89,7 @@ const frameRouter = {
meta: {
icon: "monitor",
title: "menus.hsExternalPage",
rank: 10
rank: 7
},
children: [
{
@@ -86,14 +97,16 @@ const frameRouter = {
name: "FramePure",
meta: {
title: "menus.hsPureDocument",
frameSrc: "http://yiming_chang.gitee.io/pure-admin-doc"
frameSrc: "http://yiming_chang.gitee.io/pure-admin-doc",
roles: ["admin", "common"]
}
},
{
path: "/external",
name: "http://yiming_chang.gitee.io/pure-admin-doc",
meta: {
title: "menus.externalLink"
title: "menus.externalLink",
roles: ["admin", "common"]
}
},
{
@@ -101,7 +114,8 @@ const frameRouter = {
name: "FrameEp",
meta: {
title: "menus.hsEpDocument",
frameSrc: "https://element-plus.org/zh-CN/"
frameSrc: "https://element-plus.org/zh-CN/",
roles: ["admin", "common"]
}
}
]
@@ -119,7 +133,8 @@ const tabsRouter = {
path: "/tabs/index",
name: "Tabs",
meta: {
title: "menus.hstabs"
title: "menus.hstabs",
roles: ["admin", "common"]
}
},
{
@@ -127,7 +142,8 @@ const tabsRouter = {
name: "TabQueryDetail",
meta: {
// 不在menu菜单中显示
showLink: false
showLink: false,
roles: ["admin", "common"]
}
},
{
@@ -135,39 +151,22 @@ const tabsRouter = {
component: "params-detail",
name: "TabParamsDetail",
meta: {
showLink: false
showLink: false,
roles: ["admin", "common"]
}
}
]
};
// 添加不同按钮权限到/permission/button页面中
function setDifAuthority(authority, routes) {
routes.children[1].meta.authority = [authority];
return routes;
}
export default [
{
url: "/getAsyncRoutes",
method: "get",
response: ({ query }) => {
if (query.name === "admin") {
return {
code: 0,
info: [
tabsRouter,
frameRouter,
systemRouter,
setDifAuthority("v-admin", permissionRouter)
]
};
} else {
return {
code: 0,
info: [tabsRouter, setDifAuthority("v-test", permissionRouter)]
};
}
response: () => {
return {
success: true,
data: [systemRouter, permissionRouter, frameRouter, tabsRouter]
};
}
}
] as MockMethod[];

View File

@@ -6,7 +6,7 @@ export default [
method: "post",
response: () => {
return {
code: 0,
success: true,
data: {
list: [
{

36
mock/login.ts Normal file
View File

@@ -0,0 +1,36 @@
// 根据角色动态生成路由
import { MockMethod } from "vite-plugin-mock";
export default [
{
url: "/login",
method: "post",
response: ({ body }) => {
if (body.username === "admin") {
return {
success: true,
data: {
username: "admin",
// 一个用户可能有多个角色
roles: ["admin"],
accessToken: "eyJhbGciOiJIUzUxMiJ9.admin",
refreshToken: "eyJhbGciOiJIUzUxMiJ9.adminRefresh",
expires: "2023/10/30 00:00:00"
}
};
} else {
return {
success: true,
data: {
username: "common",
// 一个用户可能有多个角色
roles: ["common"],
accessToken: "eyJhbGciOiJIUzUxMiJ9.common",
refreshToken: "eyJhbGciOiJIUzUxMiJ9.commonRefresh",
expires: "2023/10/30 00:00:00"
}
};
}
}
}
] as MockMethod[];

View File

@@ -29,8 +29,8 @@ export default [
method: "get",
response: () => {
return {
code: 0,
info: mapList()
success: true,
data: mapList()
};
}
}

27
mock/refreshToken.ts Normal file
View File

@@ -0,0 +1,27 @@
import { MockMethod } from "vite-plugin-mock";
// 模拟刷新token接口
export default [
{
url: "/refreshToken",
method: "post",
response: ({ body }) => {
if (body.refreshToken) {
return {
success: true,
data: {
accessToken: "eyJhbGciOiJIUzUxMiJ9.admin",
refreshToken: "eyJhbGciOiJIUzUxMiJ9.adminRefresh",
// `expires`选择这种日期格式是为了方便调试,后端直接设置时间戳或许更方便(每次都应该递增)。如果后端返回的是时间戳格式,前端开发请来到这个目录`src/utils/auth.ts`,把第`38`行的代码换成expires = data.expires即可。
expires: "2023/10/30 23:59:59"
}
};
} else {
return {
success: false,
data: {}
};
}
}
}
] as MockMethod[];

View File

@@ -6,7 +6,7 @@ export default [
method: "post",
response: () => {
return {
code: 0,
success: true,
data: {
list: [
{
@@ -71,7 +71,7 @@ export default [
method: "post",
response: () => {
return {
code: 0,
success: true,
data: [
{
name: "杭州总公司",
@@ -212,7 +212,7 @@ export default [
method: "post",
response: () => {
return {
code: 0,
success: true,
data: {
list: [
{