diff --git a/core/package.json b/core/package.json index 5f7144c..4453ac0 100644 --- a/core/package.json +++ b/core/package.json @@ -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", diff --git a/core/src/controller/base.ts b/core/src/controller/base.ts index 9f7500a..7ef15cc 100644 --- a/core/src/controller/base.ts +++ b/core/src/controller/base.ts @@ -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 配置 diff --git a/core/src/decorator/controller.ts b/core/src/decorator/controller.ts index ab6e1b5..2b9a400 100644 --- a/core/src/decorator/controller.ts +++ b/core/src/decorator/controller.ts @@ -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,34 +210,50 @@ 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 => { - attachClassMetadata( - WEB_ROUTER_KEY, - { - path: `/${path}`, - requestMethod: path == 'info' ? 'get' : 'post', - method: path, - summary: apiDesc[path] || path, - description: '', - }, - target - ); + const routePath = `/${path}`; + // 检查路由是否已存在 + if (!existingPaths.includes(routePath)) { + attachClassMetadata( + WEB_ROUTER_KEY, + { + path: routePath, + requestMethod: path == 'info' ? 'get' : 'post', + method: path, + summary: apiDesc[path] || path, + description: '', + }, + 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; - attachClassMetadata( - WEB_ROUTER_KEY, - { - path: `/${methodName}`, - requestMethod: 'post', - method: methodName, - summary: typeof api === 'string' ? api : api.summary, - description: '', - }, - target - ); + const routePath = `/${methodName}`; + // 检查路由是否已存在 + if (!existingPaths.includes(routePath)) { + attachClassMetadata( + WEB_ROUTER_KEY, + { + path: routePath, + requestMethod: 'post', + method: methodName, + summary: typeof api === 'string' ? api : api.summary, + description: '', + }, + target + ); + } }); } Scope(ScopeEnum.Request)(target); diff --git a/core/src/service/base.ts b/core/src/service/base.ts index b608cff..db0d03c 100644 --- a/core/src/service/base.ts +++ b/core/src/service/base.ts @@ -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; diff --git a/rpc/package.json b/rpc/package.json index ed47ccb..c14d052 100644 --- a/rpc/package.json +++ b/rpc/package.json @@ -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", diff --git a/task/package.json b/task/package.json index 6abf032..8676c78 100644 --- a/task/package.json +++ b/task/package.json @@ -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", diff --git a/typeorm/package.json b/typeorm/package.json index 93697d8..57460db 100644 --- a/typeorm/package.json +++ b/typeorm/package.json @@ -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",