This commit is contained in:
COOL 2025-02-19 10:41:14 +08:00
parent 5638c19434
commit b612d7eaf8
7 changed files with 73 additions and 30 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/core",
"version": "8.0.0-beta.2",
"version": "8.0.0",
"description": "cool-admin midway core",
"main": "dist/index.js",
"typings": "index.d.ts",

View File

@ -8,7 +8,6 @@ import {
} from '@midwayjs/core';
import { GlobalConfig } from '../constant/global';
import { ControllerOption, CurdOption } from '../decorator/controller';
import { BaseService } from '../service/base';
import { IMidwayApplication } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
@ -22,8 +21,7 @@ export abstract class BaseController {
@Inject('ctx')
baseCtx: Context;
@Inject()
service: BaseService;
service: any;
@App()
baseApp: IMidwayApplication;
@ -38,6 +36,7 @@ export abstract class BaseController {
@Init()
async init() {
const option: ControllerOption = getClassMetadata(CONTROLLER_KEY, this);
this.service = await this.baseCtx.requestContext.getAsync('baseService');
const curdOption: CurdOption = option.curdOption;
this.curdOption = curdOption;
if (!this.curdOption) {
@ -53,6 +52,17 @@ export abstract class BaseController {
await this.createDynamicMethods(curdOption);
}
/**
* ID
* @param type
* @returns
*/
protected getUserId(type: 'admin' | 'app' = 'admin') {
return type === 'admin'
? this.baseCtx.admin?.userId
: this.baseCtx.user?.id;
}
/**
*
* @param curdOption

View File

@ -8,6 +8,7 @@ import {
MiddlewareParamArray,
WEB_ROUTER_KEY,
attachClassMetadata,
getClassMetadata,
} from '@midwayjs/core';
import * as fs from 'fs';
import * as _ from 'lodash';
@ -74,7 +75,7 @@ export interface QueryOp {
// 需要模糊查询的字段
keyWordLikeFields?: string[];
// 查询条件
where?: Function;
where?: Function | any[][];
// 查询字段
select?: string[];
// 字段模糊查询
@ -209,11 +210,18 @@ function saveMetadata(prefix, routerOptions, target, curdOption, module) {
);
// 追加CRUD路由
if (!_.isEmpty(curdOption?.api)) {
// 获取已存在的路由
const existingRoutes = getClassMetadata(WEB_ROUTER_KEY, target) || [];
const existingPaths = existingRoutes.map(route => route.path);
curdOption?.api.forEach(path => {
const routePath = `/${path}`;
// 检查路由是否已存在
if (!existingPaths.includes(routePath)) {
attachClassMetadata(
WEB_ROUTER_KEY,
{
path: `/${path}`,
path: routePath,
requestMethod: path == 'info' ? 'get' : 'post',
method: path,
summary: apiDesc[path] || path,
@ -221,15 +229,23 @@ function saveMetadata(prefix, routerOptions, target, curdOption, module) {
},
target
);
}
});
}
if (!_.isEmpty(curdOption?.serviceApis)) {
// 获取已存在的路由
const existingRoutes = getClassMetadata(WEB_ROUTER_KEY, target) || [];
const existingPaths = existingRoutes.map(route => route.path);
curdOption.serviceApis.forEach(api => {
const methodName = typeof api === 'string' ? api : api.method;
const routePath = `/${methodName}`;
// 检查路由是否已存在
if (!existingPaths.includes(routePath)) {
attachClassMetadata(
WEB_ROUTER_KEY,
{
path: `/${methodName}`,
path: routePath,
requestMethod: 'post',
method: methodName,
summary: typeof api === 'string' ? api : api.summary,
@ -237,6 +253,7 @@ function saveMetadata(prefix, routerOptions, target, curdOption, module) {
},
target
);
}
});
}
Scope(ScopeEnum.Request)(target);

View File

@ -69,6 +69,22 @@ export abstract class BaseService {
await this.service.init();
}
/**
* ID
* @param type
* @returns
*/
/**
* ID
* @param type
* @returns
*/
protected getUserId(type: 'admin' | 'app' = 'admin') {
return type === 'admin'
? this.baseCtx.admin?.userId
: this.baseCtx.user?.id;
}
// 设置模型
setEntity(entity: any) {
this.entity = entity;

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/rpc",
"version": "8.0.0-beta.2",
"version": "8.0.0",
"description": "cool-admin midway rpc",
"main": "dist/index.js",
"typings": "index.d.ts",

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/task",
"version": "8.0.0-beta.2",
"version": "8.0.0",
"description": "cool-admin midway task",
"main": "dist/index.js",
"typings": "index.d.ts",

View File

@ -1,7 +1,7 @@
{
"name": "@cool-midway/typeorm",
"private": false,
"version": "8.0.0-beta.1",
"version": "8.0.0",
"description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.",
"license": "MIT",
"readmeFilename": "README.md",