Skip to content

Commit 9572d1a

Browse files
committed
feat: Dynamically get the menu from the back end
1 parent 1d70d71 commit 9572d1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1028
-504
lines changed

.vscode/settings.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@
171171
"packages/@vben-core/shared/design-tokens/src/**/*.css"
172172
],
173173

174-
"i18n-ally.localesPaths": [
175-
"packages/locales/src/langs",
176-
"packages/@core/shared/i18n/src/langs"
177-
],
174+
"i18n-ally.localesPaths": ["packages/locales/src/langs"],
178175
"i18n-ally.enabledParsers": ["json", "ts", "js", "yaml"],
179176
"i18n-ally.sourceLanguage": "en",
180177
"i18n-ally.displayLanguage": "zh-CN",

apps/backend-mock/http/menu.http

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@port = 5320
2+
@type = application/json
3+
@token = Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MCwicm9sZXMiOlsiYWRtaW4iXSwidXNlcm5hbWUiOiJ2YmVuIiwiaWF0IjoxNzE5ODkwMTEwLCJleHAiOjE3MTk5NzY1MTB9.eyAFsQ2Jk_mAQGvrEL1jF9O6YmLZ_PSYj5aokL6fCuU
4+
GET http://localhost:{{port}}/api/menu/getAll HTTP/1.1
5+
content-type: {{ type }}
6+
Authorization: {{ token }}

apps/backend-mock/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"typeorm": "^0.3.20"
3737
},
3838
"devDependencies": {
39-
"@nestjs/cli": "^10.3.2",
40-
"@nestjs/schematics": "^10.1.1",
39+
"@nestjs/cli": "^10.4.0",
40+
"@nestjs/schematics": "^10.1.2",
4141
"@types/express": "^4.17.21",
4242
"@types/node": "^20.14.9",
4343
"nodemon": "^3.1.4",

apps/backend-mock/src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Joi from 'joi';
77
import { AuthModule } from './modules/auth/auth.module';
88
import { DatabaseModule } from './modules/database/database.module';
99
import { HealthModule } from './modules/health/health.module';
10+
import { MenuModule } from './modules/menu/menu.module';
1011
import { UsersModule } from './modules/users/users.module';
1112

1213
@Module({
@@ -34,6 +35,7 @@ import { UsersModule } from './modules/users/users.module';
3435
AuthModule,
3536
UsersModule,
3637
DatabaseModule,
38+
MenuModule,
3739
],
3840
})
3941
export class AppModule {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateUserDto {
2+
id: number;
3+
password: string;
4+
realName: string;
5+
roles: string[];
6+
username: string;
7+
}
8+
9+
export { CreateUserDto };
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { sleep } from '@/utils';
2+
import { Controller, Get, HttpCode, HttpStatus, Request } from '@nestjs/common';
3+
4+
@Controller('menu')
5+
export class MenuController {
6+
/**
7+
* 获取用户所有菜单
8+
*/
9+
@Get('getAll')
10+
@HttpCode(HttpStatus.OK)
11+
async getAll(@Request() req: Request) {
12+
// 模拟请求延迟
13+
await sleep(1000);
14+
// 请求用户的id
15+
const userId = req.user.id;
16+
17+
// TODO: 改为表方式获取
18+
const dashboardMenus = [
19+
{
20+
component: 'BasicLayout',
21+
meta: {
22+
order: -1,
23+
title: 'page.dashboard.title',
24+
},
25+
name: 'Dashboard',
26+
path: '/',
27+
redirect: '/analytics',
28+
children: [
29+
{
30+
name: 'Analytics',
31+
path: '/analytics',
32+
component: '/dashboard/analytics/index',
33+
meta: {
34+
affixTab: true,
35+
title: 'page.dashboard.analytics',
36+
},
37+
},
38+
{
39+
name: 'Workspace',
40+
path: '/workspace',
41+
component: '/dashboard/workspace/index',
42+
meta: {
43+
title: 'page.dashboard.workspace',
44+
},
45+
},
46+
],
47+
},
48+
];
49+
const MOCK_MENUS = [
50+
{
51+
menus: [...dashboardMenus],
52+
userId: 0,
53+
},
54+
{
55+
menus: [...dashboardMenus],
56+
userId: 1,
57+
},
58+
];
59+
60+
return MOCK_MENUS.find((item) => item.userId === userId)?.menus ?? [];
61+
}
62+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Module } from '@nestjs/common';
2+
3+
import { MenuController } from './menu.controller';
4+
import { MenuService } from './menu.service';
5+
6+
@Module({
7+
controllers: [MenuController],
8+
providers: [MenuService],
9+
})
10+
export class MenuModule {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
@Injectable()
4+
export class MenuService {}

apps/backend-mock/src/modules/users/users.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { CreateUserDto } from '@/models/dto/user.dto';
12
import type { Repository } from 'typeorm';
23

34
import { UserEntity } from '@/models/entity/user.entity';
@@ -12,7 +13,7 @@ export class UsersService {
1213
private usersRepository: Repository<UserEntity>,
1314
) {}
1415

15-
async create(user: UserEntity): Promise<UserEntity> {
16+
async create(user: CreateUserDto): Promise<UserEntity> {
1617
user.password = await bcrypt.hash(user.password, 10); // 密码哈希
1718
return this.usersRepository.save(user);
1819
}

apps/backend-mock/src/utils/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function sleep(ms: number) {
2+
return new Promise((resolve) => setTimeout(resolve, ms));
3+
}
4+
5+
export { sleep };

0 commit comments

Comments
 (0)