create-young-proj 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -21
- package/README.md +13 -2
- package/dist/index.mjs +18 -18
- package/index.mjs +3 -3
- package/package.json +10 -12
- package/template-admin-server/.editorconfig +11 -0
- package/template-admin-server/.nvmrc +1 -0
- package/template-admin-server/.vscode/extensions.json +6 -0
- package/template-admin-server/.vscode/settings.json +4 -0
- package/template-admin-server/README.md +73 -0
- package/template-admin-server/_gitignore +15 -0
- package/template-admin-server/boot.mjs +11 -0
- package/template-admin-server/package.json +60 -0
- package/template-admin-server/rome.json +22 -0
- package/template-admin-server/src/config/config.default.ts +56 -0
- package/template-admin-server/src/configuration.ts +47 -0
- package/template-admin-server/src/controller/admin.controller.ts +397 -0
- package/template-admin-server/src/controller/api.controller.ts +98 -0
- package/template-admin-server/src/controller/base.controller.ts +70 -0
- package/template-admin-server/src/controller/dto/api.ts +47 -0
- package/template-admin-server/src/controller/dto/index.ts +36 -0
- package/template-admin-server/src/controller/dto/menu.ts +41 -0
- package/template-admin-server/src/controller/dto/role.ts +41 -0
- package/template-admin-server/src/controller/dto/user.ts +52 -0
- package/template-admin-server/src/controller/menu.controller.ts +138 -0
- package/template-admin-server/src/controller/role.controller.ts +116 -0
- package/template-admin-server/src/controller/user.controller.ts +108 -0
- package/template-admin-server/src/entities/Api.ts +29 -0
- package/template-admin-server/src/entities/BaseCreate.ts +30 -0
- package/template-admin-server/src/entities/Menu.ts +39 -0
- package/template-admin-server/src/entities/Role.ts +36 -0
- package/template-admin-server/src/entities/User.ts +35 -0
- package/template-admin-server/src/entities/index.ts +10 -0
- package/template-admin-server/src/filter/default.filter.ts +22 -0
- package/template-admin-server/src/filter/notfound.filter.ts +23 -0
- package/template-admin-server/src/middleware/helper.middleware.ts +28 -0
- package/template-admin-server/src/middleware/index.ts +9 -0
- package/template-admin-server/src/middleware/jwt.middleware.ts +32 -0
- package/template-admin-server/src/middleware/report.middleware.ts +26 -0
- package/template-admin-server/src/service/api.service.ts +174 -0
- package/template-admin-server/src/service/basic.ts +118 -0
- package/template-admin-server/src/service/index.ts +10 -0
- package/template-admin-server/src/service/menu.service.ts +139 -0
- package/template-admin-server/src/service/role.service.ts +286 -0
- package/template-admin-server/src/service/user.service.ts +124 -0
- package/template-admin-server/src/strategy/jwt.strategy.ts +26 -0
- package/template-admin-server/src/types/index.ts +42 -0
- package/template-admin-server/src/types/types.d.ts +31 -0
- package/template-admin-server/tsconfig.json +24 -0
- package/template-vue-admin/.vscode/extensions.json +10 -0
- package/template-vue-admin/.vscode/list-add.code-snippets +108 -0
- package/template-vue-admin/.vscode/list-export.code-snippets +72 -0
- package/template-vue-admin/.vscode/list.code-snippets +61 -0
- package/template-vue-admin/.vscode/settings.json +7 -0
- package/template-vue-admin/Dockerfile +42 -0
- package/template-vue-admin/README.md +75 -0
- package/template-vue-admin/_env +8 -0
- package/template-vue-admin/_gitignore +30 -0
- package/template-vue-admin/boot.mjs +16 -0
- package/template-vue-admin/config/.devrc +2 -0
- package/template-vue-admin/config/.onlinerc +2 -0
- package/template-vue-admin/config/.testrc +2 -0
- package/template-vue-admin/index.html +21 -0
- package/template-vue-admin/nitro.config.ts +19 -0
- package/template-vue-admin/package.json +50 -0
- package/template-vue-admin/plugins/env.ts +26 -0
- package/template-vue-admin/public/vite.svg +1 -0
- package/template-vue-admin/rome.json +26 -0
- package/template-vue-admin/routes/api/[...all].ts +49 -0
- package/template-vue-admin/routes/get/env.ts +18 -0
- package/template-vue-admin/src/App.vue +14 -0
- package/template-vue-admin/src/apis/delete.ts +36 -0
- package/template-vue-admin/src/apis/get.ts +84 -0
- package/template-vue-admin/src/apis/index.ts +10 -0
- package/template-vue-admin/src/apis/patch.ts +79 -0
- package/template-vue-admin/src/apis/post.ts +77 -0
- package/template-vue-admin/src/assets/img/login_background.jpg +0 -0
- package/template-vue-admin/src/auto-components.d.ts +36 -0
- package/template-vue-admin/src/auto-imports.d.ts +282 -0
- package/template-vue-admin/src/layouts/blank.vue +9 -0
- package/template-vue-admin/src/layouts/default/components/Link.vue +23 -0
- package/template-vue-admin/src/layouts/default/components/Logo.vue +20 -0
- package/template-vue-admin/src/layouts/default/components/Menu.vue +54 -0
- package/template-vue-admin/src/layouts/default/components/NavSearch.vue +52 -0
- package/template-vue-admin/src/layouts/default/components/ScrollPane.vue +79 -0
- package/template-vue-admin/src/layouts/default/components/TagsView.vue +137 -0
- package/template-vue-admin/src/layouts/default/components/TopMenu.vue +21 -0
- package/template-vue-admin/src/layouts/default/components/UserCenter.vue +50 -0
- package/template-vue-admin/src/layouts/default/index.vue +95 -0
- package/template-vue-admin/src/main.ts +44 -0
- package/template-vue-admin/src/modules/1-router.ts +66 -0
- package/template-vue-admin/src/modules/2-pinia.ts +10 -0
- package/template-vue-admin/src/modules/3-net.ts +75 -0
- package/template-vue-admin/src/modules/4-auth.ts +122 -0
- package/template-vue-admin/src/stores/index.ts +9 -0
- package/template-vue-admin/src/stores/local/index.ts +23 -0
- package/template-vue-admin/src/stores/session/index.ts +63 -0
- package/template-vue-admin/src/stores/tags.ts +109 -0
- package/template-vue-admin/src/typings/global.d.ts +70 -0
- package/template-vue-admin/src/typings/index.ts +50 -0
- package/template-vue-admin/src/views/403.vue +32 -0
- package/template-vue-admin/src/views/[...all_404].vue +556 -0
- package/template-vue-admin/src/views/base/login.vue +193 -0
- package/template-vue-admin/src/views/dashboard/[name].vue +23 -0
- package/template-vue-admin/src/views/index.vue +19 -0
- package/template-vue-admin/src/views/system/api.vue +161 -0
- package/template-vue-admin/src/views/system/hooks/useRole.ts +286 -0
- package/template-vue-admin/src/views/system/menuList.vue +195 -0
- package/template-vue-admin/src/views/system/role.vue +132 -0
- package/template-vue-admin/src/views/system/user.vue +193 -0
- package/template-vue-admin/src/vite-env.d.ts +52 -0
- package/template-vue-admin/tsconfig.json +21 -0
- package/template-vue-admin/tsconfig.node.json +9 -0
- package/template-vue-admin/unocss.config.ts +47 -0
- package/template-vue-admin/vite.config.ts +77 -0
- package/template-vue-thin/package.json +14 -13
- package/template-vue-thin/vite.config.ts +1 -6
@@ -0,0 +1,36 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-26 14:11:44
|
4
|
+
* @LastEditTime: 2022-12-28 16:36:23
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Column, Entity, OneToMany, ManyToMany, JoinTable } from 'typeorm';
|
8
|
+
import { Api } from './Api';
|
9
|
+
import { BaseCreate } from './BaseCreate';
|
10
|
+
import { Menu } from './Menu';
|
11
|
+
import { User } from './User';
|
12
|
+
|
13
|
+
@Entity({
|
14
|
+
engine: 'InnoDB',
|
15
|
+
})
|
16
|
+
export class Role extends BaseCreate {
|
17
|
+
@Column({ length: '20' })
|
18
|
+
name!: string;
|
19
|
+
|
20
|
+
@Column({ length: '20', unique: true })
|
21
|
+
keyword!: string;
|
22
|
+
|
23
|
+
@Column({ default: '' })
|
24
|
+
desc?: string;
|
25
|
+
|
26
|
+
@OneToMany(() => User, user => user.role)
|
27
|
+
users?: User[];
|
28
|
+
|
29
|
+
@ManyToMany(() => Api, api => api.roles)
|
30
|
+
@JoinTable()
|
31
|
+
apis?: Api[];
|
32
|
+
|
33
|
+
@ManyToMany(() => Menu, menu => menu.roles)
|
34
|
+
@JoinTable()
|
35
|
+
menus?: Menu[];
|
36
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-26 14:11:58
|
4
|
+
* @LastEditTime: 2022-12-28 15:51:23
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Column, Entity, ManyToOne } from 'typeorm';
|
8
|
+
import { BaseCreate } from './BaseCreate';
|
9
|
+
import { Role } from './Role';
|
10
|
+
|
11
|
+
@Entity({
|
12
|
+
engine: 'InnoDB',
|
13
|
+
})
|
14
|
+
export class User extends BaseCreate {
|
15
|
+
@Column({ length: '20', unique: true })
|
16
|
+
username!: string;
|
17
|
+
|
18
|
+
@Column({ length: '11' })
|
19
|
+
mobile!: string;
|
20
|
+
|
21
|
+
@Column({ length: '20', default: '' })
|
22
|
+
nickname?: string;
|
23
|
+
|
24
|
+
@Column({ default: '' })
|
25
|
+
avatar?: string;
|
26
|
+
|
27
|
+
@Column({ length: '50', default: '' })
|
28
|
+
introduction?: string;
|
29
|
+
|
30
|
+
@Column({ select: false })
|
31
|
+
password!: string;
|
32
|
+
|
33
|
+
@ManyToOne(() => Role, role => role.users, { cascade: true })
|
34
|
+
role!: Role;
|
35
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-27 11:18:26
|
4
|
+
* @LastEditTime: 2022-12-27 11:18:27
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Catch } from '@midwayjs/decorator';
|
8
|
+
import { Context } from '@midwayjs/koa';
|
9
|
+
import { Code } from '../types';
|
10
|
+
|
11
|
+
@Catch()
|
12
|
+
export class DefaultErrorFilter {
|
13
|
+
async catch(err: Error, ctx: Context) {
|
14
|
+
// 所有的未分类错误会到这里
|
15
|
+
ctx.status = 500;
|
16
|
+
return {
|
17
|
+
code: Code.fail,
|
18
|
+
msg: err.message,
|
19
|
+
data: null,
|
20
|
+
} as ResponseMsg;
|
21
|
+
}
|
22
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-27 11:06:11
|
4
|
+
* @LastEditTime: 2022-12-27 11:14:48
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Catch } from '@midwayjs/decorator';
|
8
|
+
import { httpError, MidwayHttpError } from '@midwayjs/core';
|
9
|
+
import { Context } from '@midwayjs/koa';
|
10
|
+
import { Code } from '../types';
|
11
|
+
|
12
|
+
@Catch(httpError.NotFoundError)
|
13
|
+
export class NotFoundFilter {
|
14
|
+
async catch(err: MidwayHttpError, ctx: Context) {
|
15
|
+
// 404 错误会到这里
|
16
|
+
ctx.status = 404;
|
17
|
+
return {
|
18
|
+
code: Code.fail,
|
19
|
+
msg: '404, ' + ctx.path,
|
20
|
+
data: null,
|
21
|
+
} as ResponseMsg;
|
22
|
+
}
|
23
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-27 15:10:18
|
4
|
+
* @LastEditTime: 2022-12-27 16:12:42
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { IMiddleware } from '@midwayjs/core';
|
8
|
+
import { Middleware } from '@midwayjs/decorator';
|
9
|
+
import { NextFunction, Context } from '@midwayjs/koa';
|
10
|
+
import { fail, success } from '../types';
|
11
|
+
|
12
|
+
@Middleware()
|
13
|
+
export class HelperMiddleware implements IMiddleware<Context, NextFunction> {
|
14
|
+
resolve() {
|
15
|
+
return async (ctx: Context, next: NextFunction) => {
|
16
|
+
ctx.helper = {
|
17
|
+
success,
|
18
|
+
fail,
|
19
|
+
};
|
20
|
+
|
21
|
+
return next();
|
22
|
+
};
|
23
|
+
}
|
24
|
+
|
25
|
+
static getName(): string {
|
26
|
+
return 'helper';
|
27
|
+
}
|
28
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-27 16:05:38
|
4
|
+
* @LastEditTime: 2023-01-09 10:16:46
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Middleware } from '@midwayjs/decorator';
|
8
|
+
import { PassportMiddleware, AuthenticateOptions } from '@midwayjs/passport';
|
9
|
+
import { JwtStrategy } from '../strategy/jwt.strategy';
|
10
|
+
import { Context } from '@midwayjs/koa';
|
11
|
+
@Middleware()
|
12
|
+
export class JwtPassportMiddleware extends PassportMiddleware(JwtStrategy) {
|
13
|
+
getAuthenticateOptions(): Promise<AuthenticateOptions> | AuthenticateOptions {
|
14
|
+
return {};
|
15
|
+
}
|
16
|
+
|
17
|
+
/**
|
18
|
+
* 免验 jwt 的白名单
|
19
|
+
*/
|
20
|
+
ignore(ctx: Context) {
|
21
|
+
ctx.logger.info(ctx.path);
|
22
|
+
return [
|
23
|
+
'/api/base/login',
|
24
|
+
// 初始化后台数据表,仅供开发使用
|
25
|
+
// '/api/admin/init',
|
26
|
+
].includes(ctx.path);
|
27
|
+
}
|
28
|
+
|
29
|
+
static getName(): string {
|
30
|
+
return 'jwt';
|
31
|
+
}
|
32
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { IMiddleware } from '@midwayjs/core';
|
2
|
+
import { Middleware } from '@midwayjs/decorator';
|
3
|
+
import { NextFunction, Context } from '@midwayjs/koa';
|
4
|
+
|
5
|
+
@Middleware()
|
6
|
+
export class ReportMiddleware implements IMiddleware<Context, NextFunction> {
|
7
|
+
resolve() {
|
8
|
+
return async (ctx: Context, next: NextFunction) => {
|
9
|
+
// 控制器前执行的逻辑
|
10
|
+
const startTime = Date.now();
|
11
|
+
// 执行下一个 Web 中间件,最后执行到控制器
|
12
|
+
// 这里可以拿到下一个中间件或者控制器的返回值
|
13
|
+
const result = await next();
|
14
|
+
// 控制器之后执行的逻辑
|
15
|
+
ctx.logger.info(
|
16
|
+
`Report in "src/middleware/report.middleware.ts", rt = ${Date.now() - startTime}ms`,
|
17
|
+
);
|
18
|
+
// 返回给上一个中间件的结果
|
19
|
+
return result;
|
20
|
+
};
|
21
|
+
}
|
22
|
+
|
23
|
+
static getName(): string {
|
24
|
+
return 'report';
|
25
|
+
}
|
26
|
+
}
|
@@ -0,0 +1,174 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-27 14:31:57
|
4
|
+
* @LastEditTime: 2022-12-27 15:19:51
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Provide } from '@midwayjs/decorator';
|
8
|
+
import { InjectEntityModel } from '@midwayjs/typeorm';
|
9
|
+
import type { Repository } from 'typeorm';
|
10
|
+
import { Api, Role } from '../entities';
|
11
|
+
import { BasicService } from './basic';
|
12
|
+
|
13
|
+
export type CreateApiItem = Partial<Api> & { rids?: number[] };
|
14
|
+
|
15
|
+
@Provide()
|
16
|
+
export class ApiService extends BasicService<Api, CreateApiItem> {
|
17
|
+
@InjectEntityModel(Api)
|
18
|
+
repo: Repository<Api>;
|
19
|
+
|
20
|
+
async create(item: CreateApiItem) {
|
21
|
+
const api = new Api();
|
22
|
+
for (const [key, value] of Object.entries(item)) {
|
23
|
+
if (key === 'rids' && Array.isArray(value)) {
|
24
|
+
const roles: Role[] = [];
|
25
|
+
for (const id of value) {
|
26
|
+
const role = await this.dataSource.getRepository(Role).findOne({
|
27
|
+
where: {
|
28
|
+
id: id as number,
|
29
|
+
},
|
30
|
+
});
|
31
|
+
if (role) {
|
32
|
+
roles.push(role);
|
33
|
+
} else {
|
34
|
+
throw new Error(`roleId: ${value} doesn't exsist`);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
api.roles = roles;
|
38
|
+
} else {
|
39
|
+
api[key] = value;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
await this.repo.save(api);
|
43
|
+
}
|
44
|
+
|
45
|
+
async getAllTree() {
|
46
|
+
const apis = await this.repo.find({
|
47
|
+
where: {
|
48
|
+
status: 1,
|
49
|
+
},
|
50
|
+
order: {
|
51
|
+
id: 'ASC',
|
52
|
+
},
|
53
|
+
});
|
54
|
+
const res: Record<string, Api[]> = {};
|
55
|
+
for (const api of apis) {
|
56
|
+
api.id = Number(api.id);
|
57
|
+
const group = api.category;
|
58
|
+
if (res[group]) {
|
59
|
+
res[group].push(api);
|
60
|
+
} else {
|
61
|
+
res[group] = [api];
|
62
|
+
}
|
63
|
+
}
|
64
|
+
return Object.entries(res).map(([key, value]) => {
|
65
|
+
return {
|
66
|
+
category: key,
|
67
|
+
desc: key,
|
68
|
+
title: `${key}分组`,
|
69
|
+
id: value[0].id,
|
70
|
+
children: value,
|
71
|
+
};
|
72
|
+
});
|
73
|
+
}
|
74
|
+
|
75
|
+
async findAll(item: CreateApiItem & Paginition) {
|
76
|
+
const query = this.repo.createQueryBuilder();
|
77
|
+
query.select('*');
|
78
|
+
|
79
|
+
const { noPagination, pageNum, pageSize, path } = item;
|
80
|
+
|
81
|
+
if (path) {
|
82
|
+
query.andWhere(`path like '%${path}%'`);
|
83
|
+
}
|
84
|
+
|
85
|
+
const page = Number(pageNum) || 1;
|
86
|
+
const size = Number(pageSize) || 10;
|
87
|
+
|
88
|
+
if (!item.noPagination) {
|
89
|
+
const skip = (page - 1) * size;
|
90
|
+
|
91
|
+
query.skip(skip);
|
92
|
+
query.take(size);
|
93
|
+
}
|
94
|
+
|
95
|
+
const total = await query.getCount();
|
96
|
+
const list = await query.getRawMany();
|
97
|
+
|
98
|
+
for (const api of list) {
|
99
|
+
api.id = Number(api.id);
|
100
|
+
}
|
101
|
+
|
102
|
+
return {
|
103
|
+
pageNum: page,
|
104
|
+
pageSize: size,
|
105
|
+
total,
|
106
|
+
noPagination,
|
107
|
+
list,
|
108
|
+
};
|
109
|
+
}
|
110
|
+
|
111
|
+
async getById(id: number) {
|
112
|
+
const api = await this.repo.findOne({
|
113
|
+
where: { id },
|
114
|
+
relations: ['roles'],
|
115
|
+
});
|
116
|
+
if (!api) {
|
117
|
+
throw new Error(`apiId: ${id} 不存在`);
|
118
|
+
}
|
119
|
+
return api;
|
120
|
+
}
|
121
|
+
|
122
|
+
async deleteBatch(ids: number[]) {
|
123
|
+
const queryRunner = this.dataSource.createQueryRunner();
|
124
|
+
let res: any = true;
|
125
|
+
|
126
|
+
await queryRunner.connect();
|
127
|
+
await queryRunner.startTransaction();
|
128
|
+
try {
|
129
|
+
for (const id of ids) {
|
130
|
+
const api = await this.getById(id);
|
131
|
+
api.roles = [];
|
132
|
+
await this.repo.save(api);
|
133
|
+
await this.delete(id);
|
134
|
+
}
|
135
|
+
await queryRunner.commitTransaction();
|
136
|
+
} catch (error) {
|
137
|
+
await queryRunner.rollbackTransaction();
|
138
|
+
res = error;
|
139
|
+
} finally {
|
140
|
+
await queryRunner.release();
|
141
|
+
}
|
142
|
+
|
143
|
+
if (res !== true) {
|
144
|
+
throw res;
|
145
|
+
}
|
146
|
+
}
|
147
|
+
|
148
|
+
async update(id: number, item: CreateApiItem) {
|
149
|
+
const m = await this.getById(id);
|
150
|
+
for (const [key, value] of Object.entries(item)) {
|
151
|
+
if (value !== undefined && value !== null) {
|
152
|
+
if (key === 'rids' && Array.isArray(value)) {
|
153
|
+
const roles: Role[] = [];
|
154
|
+
for (const id of value) {
|
155
|
+
const role = await this.dataSource.getRepository(Role).findOne({
|
156
|
+
where: {
|
157
|
+
id: id as number,
|
158
|
+
},
|
159
|
+
});
|
160
|
+
if (role) {
|
161
|
+
roles.push(role);
|
162
|
+
} else {
|
163
|
+
throw new Error(`roleId: ${value} doesn't exsist`);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
m.roles = [...new Set([...(m?.roles ?? []), ...roles])];
|
167
|
+
} else {
|
168
|
+
m[key] = value;
|
169
|
+
}
|
170
|
+
}
|
171
|
+
}
|
172
|
+
await this.repo.save(m);
|
173
|
+
}
|
174
|
+
}
|
@@ -0,0 +1,118 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-27 14:06:30
|
4
|
+
* @LastEditTime: 2022-12-27 14:21:40
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Logger } from '@midwayjs/decorator';
|
8
|
+
import type { ILogger } from '@midwayjs/logger';
|
9
|
+
import type { BaseCreate } from '../entities/BaseCreate';
|
10
|
+
import type { Repository, DataSource } from 'typeorm';
|
11
|
+
import { InjectDataSource } from '@midwayjs/typeorm';
|
12
|
+
|
13
|
+
export class BasicService<U extends BaseCreate, T extends Partial<U> = Partial<U>> {
|
14
|
+
@Logger()
|
15
|
+
logger: ILogger;
|
16
|
+
|
17
|
+
@InjectDataSource()
|
18
|
+
dataSource: DataSource;
|
19
|
+
|
20
|
+
repo: Repository<U>;
|
21
|
+
|
22
|
+
async create(item: T) {
|
23
|
+
this.logger.error('create method must be implement ---', item);
|
24
|
+
}
|
25
|
+
|
26
|
+
async createBatch(items: T[]) {
|
27
|
+
let res: any = true;
|
28
|
+
|
29
|
+
const queryRunner = this.dataSource.createQueryRunner();
|
30
|
+
await queryRunner.connect();
|
31
|
+
await queryRunner.startTransaction();
|
32
|
+
try {
|
33
|
+
for (const item of items) {
|
34
|
+
await this.create(item);
|
35
|
+
}
|
36
|
+
await queryRunner.commitTransaction();
|
37
|
+
} catch (error) {
|
38
|
+
await queryRunner.rollbackTransaction();
|
39
|
+
res = error;
|
40
|
+
} finally {
|
41
|
+
await queryRunner.release();
|
42
|
+
}
|
43
|
+
|
44
|
+
if (res !== true) {
|
45
|
+
throw res;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
async findOne(item: Partial<U>) {
|
50
|
+
const query = this.repo.createQueryBuilder();
|
51
|
+
query.select('*');
|
52
|
+
for (const [key, value] of Object.entries(item)) {
|
53
|
+
if (value) {
|
54
|
+
query.andWhere(`${key} = :value`, { value });
|
55
|
+
}
|
56
|
+
}
|
57
|
+
return query.getRawOne() as Promise<U | null>;
|
58
|
+
}
|
59
|
+
|
60
|
+
async findAll(item: Partial<U> & Paginition) {
|
61
|
+
const query = this.repo.createQueryBuilder();
|
62
|
+
query.select('*');
|
63
|
+
for (const [key, value] of Object.entries(item)) {
|
64
|
+
if (value) {
|
65
|
+
query.andWhere(`${key} = :value`, { value });
|
66
|
+
}
|
67
|
+
}
|
68
|
+
const pageNum = Number(item.pageNum) || 1;
|
69
|
+
const pageSize = Number(item.pageSize) || 10;
|
70
|
+
if (!item.noPagination) {
|
71
|
+
const page = pageNum;
|
72
|
+
const size = pageSize;
|
73
|
+
|
74
|
+
const skip = (page - 1) * size;
|
75
|
+
|
76
|
+
query.skip(skip);
|
77
|
+
query.take(size);
|
78
|
+
}
|
79
|
+
|
80
|
+
const total = await query.getCount();
|
81
|
+
const list = await query.getRawMany();
|
82
|
+
|
83
|
+
return {
|
84
|
+
pageNum,
|
85
|
+
pageSize,
|
86
|
+
total,
|
87
|
+
list,
|
88
|
+
noPagination: item.noPagination,
|
89
|
+
};
|
90
|
+
}
|
91
|
+
|
92
|
+
async delete(id: number) {
|
93
|
+
await this.repo.delete(id);
|
94
|
+
}
|
95
|
+
|
96
|
+
async deleteBatch(ids: number[]) {
|
97
|
+
const queryRunner = this.dataSource.createQueryRunner();
|
98
|
+
let res: any = true;
|
99
|
+
|
100
|
+
await queryRunner.connect();
|
101
|
+
await queryRunner.startTransaction();
|
102
|
+
try {
|
103
|
+
for (const id of ids) {
|
104
|
+
await this.delete(id);
|
105
|
+
}
|
106
|
+
await queryRunner.commitTransaction();
|
107
|
+
} catch (error) {
|
108
|
+
await queryRunner.rollbackTransaction();
|
109
|
+
res = error;
|
110
|
+
} finally {
|
111
|
+
await queryRunner.release();
|
112
|
+
}
|
113
|
+
|
114
|
+
if (res !== true) {
|
115
|
+
throw res;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
@@ -0,0 +1,139 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: zhangyang
|
3
|
+
* @Date: 2022-12-27 14:29:19
|
4
|
+
* @LastEditTime: 2022-12-27 16:49:39
|
5
|
+
* @Description:
|
6
|
+
*/
|
7
|
+
import { Provide } from '@midwayjs/decorator';
|
8
|
+
import { InjectEntityModel } from '@midwayjs/typeorm';
|
9
|
+
import type { Repository } from 'typeorm';
|
10
|
+
import { Menu } from '../entities';
|
11
|
+
import { BasicService } from './basic';
|
12
|
+
|
13
|
+
export type CreateMenuItem = Partial<Menu> & { pid?: number };
|
14
|
+
|
15
|
+
@Provide()
|
16
|
+
export class MenuService extends BasicService<Menu, CreateMenuItem> {
|
17
|
+
@InjectEntityModel(Menu)
|
18
|
+
repo: Repository<Menu>;
|
19
|
+
|
20
|
+
async create(item: CreateMenuItem) {
|
21
|
+
const m = new Menu();
|
22
|
+
for (const [key, value] of Object.entries(item)) {
|
23
|
+
if (key === 'pid') {
|
24
|
+
if (value === 0) {
|
25
|
+
continue;
|
26
|
+
}
|
27
|
+
const p = await this.repo.findOne({ where: { id: value as number } });
|
28
|
+
if (p) {
|
29
|
+
m.parentId = p;
|
30
|
+
} else {
|
31
|
+
throw new Error(`parentId: ${value} doesn't exsist`);
|
32
|
+
}
|
33
|
+
} else {
|
34
|
+
m[key] = value;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
await this.repo.save(m);
|
38
|
+
}
|
39
|
+
|
40
|
+
async update(id: number, item: CreateMenuItem) {
|
41
|
+
const m = await this.getById(id);
|
42
|
+
if (m) {
|
43
|
+
for (const [key, value] of Object.entries(item)) {
|
44
|
+
if (key === 'pid') {
|
45
|
+
if (value === undefined) {
|
46
|
+
continue;
|
47
|
+
}
|
48
|
+
if (value === 0) {
|
49
|
+
m.parentId = null;
|
50
|
+
continue;
|
51
|
+
}
|
52
|
+
const p = await this.repo.findOne({ where: { id: value as number } });
|
53
|
+
if (p) {
|
54
|
+
m.parentId = p;
|
55
|
+
} else {
|
56
|
+
throw new Error(`parentId: ${value} doesn't exsist`);
|
57
|
+
}
|
58
|
+
} else {
|
59
|
+
m[key] = value;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
await this.repo.save(m);
|
63
|
+
} else {
|
64
|
+
throw new Error(`menuId: ${id}, 菜单不存在`);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
async deleteBatch(ids: number[]) {
|
69
|
+
const queryRunner = this.dataSource.createQueryRunner();
|
70
|
+
let res: any = true;
|
71
|
+
|
72
|
+
await queryRunner.connect();
|
73
|
+
await queryRunner.startTransaction();
|
74
|
+
try {
|
75
|
+
for (const id of ids) {
|
76
|
+
const menu = await this.getById(id);
|
77
|
+
|
78
|
+
const childrenIds = menu.children?.map((i) => Number(i.id)) ?? [];
|
79
|
+
|
80
|
+
// 删除角色与菜单之间的关联
|
81
|
+
menu.roles = [];
|
82
|
+
menu.parentId = null;
|
83
|
+
// 断开 children 的关联
|
84
|
+
menu.children = [];
|
85
|
+
await this.repo.save(menu);
|
86
|
+
|
87
|
+
// 递归删除子节点
|
88
|
+
await this.deleteBatch(childrenIds);
|
89
|
+
|
90
|
+
await this.delete(id);
|
91
|
+
}
|
92
|
+
await queryRunner.commitTransaction();
|
93
|
+
} catch (error) {
|
94
|
+
await queryRunner.rollbackTransaction();
|
95
|
+
res = error;
|
96
|
+
} finally {
|
97
|
+
await queryRunner.release();
|
98
|
+
}
|
99
|
+
|
100
|
+
if (res !== true) {
|
101
|
+
throw res;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
async getById(id: number) {
|
106
|
+
const m = await this.repo.findOne({
|
107
|
+
where: { id },
|
108
|
+
relations: ['roles', 'children', 'parentId'],
|
109
|
+
});
|
110
|
+
if (!m) {
|
111
|
+
throw new Error(`menuId: ${id} 不存在`);
|
112
|
+
}
|
113
|
+
return m;
|
114
|
+
}
|
115
|
+
|
116
|
+
async getAllTree() {
|
117
|
+
// 获取树形结构的数据
|
118
|
+
const menus = await this.dataSource.getTreeRepository(Menu).findTrees({
|
119
|
+
relations: ['children', 'parentId'],
|
120
|
+
});
|
121
|
+
|
122
|
+
// 设置 parentId
|
123
|
+
const setParentId = (item: Menu) => {
|
124
|
+
item.id = Number(item.id);
|
125
|
+
// @ts-expect-error
|
126
|
+
item.parentId = Number(item?.parentId?.id ?? 0);
|
127
|
+
if (item.children?.length) {
|
128
|
+
for (const i of item.children) {
|
129
|
+
setParentId(i);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
};
|
133
|
+
for (const menu of menus) {
|
134
|
+
setParentId(menu);
|
135
|
+
}
|
136
|
+
|
137
|
+
return menus;
|
138
|
+
}
|
139
|
+
}
|