From a98f5af183f12f17395f372fd083e899ed8c8dfa Mon Sep 17 00:00:00 2001 From: chenos Date: Sun, 24 Jan 2021 09:23:32 +0800 Subject: [PATCH] feat: support users views as submenu --- .../plugin-pages/src/actions/getRoutes.ts | 34 ++++++++++++ .../plugin-pages/src/actions/roles.pages.ts | 53 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/packages/plugin-pages/src/actions/getRoutes.ts b/packages/plugin-pages/src/actions/getRoutes.ts index 48544c0246..b850b2490c 100644 --- a/packages/plugin-pages/src/actions/getRoutes.ts +++ b/packages/plugin-pages/src/actions/getRoutes.ts @@ -39,6 +39,7 @@ function pages2routes(pages: Array) { export default async function getRoutes(ctx, next) { const database: Database = ctx.database; const Page = database.getModel('pages'); + const View = database.getModel('views'); const Collection = database.getModel('collections'); const RoutePermission = database.getModel('routes_permissions'); const roles = await ctx.ac.getRoles(); @@ -134,6 +135,39 @@ export default async function getRoutes(ctx, next) { } } } + } else if (page.get('path') === '/users/users') { + const userViews = await View.findAll(View.parseApiJson(ctx.state.developerMode ? { + filter: { + collection_name: 'users', + showInDataMenu: true, + }, + sort: ['sort'], + }: { + filter: { + collection_name: 'users', + developerMode: {'$isFalsy': true}, + showInDataMenu: true, + }, + sort: ['sort'], + })); + if (userViews.length > 1) { + for (const view of userViews) { + if (!isRoot && !routesPermissionsMap.has(`views:${view.id}`)) { + continue; + } + items.push({ + id: `view-${view.get('id')}`, + type: 'collection', + collection: 'users', + title: view.title, + viewName: view.name, + path: `${page.get('path')}/views/${view.name}`, + parent_id: page.id, + showInMenu: true, + sort: view.get('sort'), + }); + } + } } } const data = flatToTree(items, { diff --git a/packages/plugin-pages/src/actions/roles.pages.ts b/packages/plugin-pages/src/actions/roles.pages.ts index 84c3486bdb..48a0616f21 100644 --- a/packages/plugin-pages/src/actions/roles.pages.ts +++ b/packages/plugin-pages/src/actions/roles.pages.ts @@ -6,6 +6,7 @@ import { Op } from 'sequelize'; async function getRoutes(ctx) { const database: Database = ctx.db; const Page = database.getModel('pages'); + const View = database.getModel('views'); const Collection = database.getModel('collections'); let pages = await Page.findAll(Page.parseApiJson(ctx.state.developerMode ? { filter: { @@ -61,6 +62,29 @@ async function getRoutes(ctx) { } } } + } else if (page.get('path') === '/users/users') { + const userViews = await View.findAll(View.parseApiJson(ctx.state.developerMode ? { + filter: { + showInDataMenu: true, + collection_name: 'users', + }, + sort: ['sort'], + }: { + filter: { + developerMode: {'$isFalsy': true}, + showInDataMenu: true, + collection_name: 'users', + }, + sort: ['sort'], + })); + if (userViews.length > 1) { + for (const view of userViews) { + items.push({ + routable_id: view.id, + routable_type: 'views', + }); + } + } } } return items; @@ -70,6 +94,7 @@ export async function list(ctx: actions.Context, next: actions.Next) { const database: Database = ctx.db; const { associatedKey, associated } = ctx.action.params; const Page = database.getModel('pages'); + const View = database.getModel('views'); const Collection = database.getModel('collections'); // TODO(optimize): isRoot 的判断需要在内部完成,尽量不要交给调用者 const isRoot = ctx.ac.constructor.isRoot(associated); @@ -150,6 +175,34 @@ export async function list(ctx: actions.Context, next: actions.Next) { } } } + } else if (page.get('path') === '/users/users') { + const userViews = await View.findAll(View.parseApiJson(ctx.state.developerMode ? { + filter: { + showInDataMenu: true, + collection_name: 'users', + }, + sort: ['sort'], + }: { + filter: { + collection_name: 'users', + developerMode: {'$isFalsy': true}, + showInDataMenu: true, + }, + sort: ['sort'], + })); + if (userViews.length > 1) { + for (const view of userViews) { + items.push({ + associatedKey, + id: view.id, + tableName: 'views', + title: view.title, + key: `view-${view.id}`, + parent_id: `page-${page.id}`, + accessible: isRoot || routesPermissionsMap.has(`views:${view.id}`), // TODO 对接权限 + }); + } + } } } const data = flatToTree(items, {