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", "name": "@cool-midway/core",
"version": "8.0.0-beta.2", "version": "8.0.0",
"description": "cool-admin midway core", "description": "cool-admin midway core",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

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

View File

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

View File

@ -69,6 +69,22 @@ export abstract class BaseService {
await this.service.init(); 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) { setEntity(entity: any) {
this.entity = entity; this.entity = entity;

View File

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

View File

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

View File

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