优化CoolController新增serviceApis

This commit is contained in:
COOL 2025-01-22 18:07:44 +08:00
parent 6c39f42a56
commit afb82751f1
9 changed files with 69 additions and 12 deletions

View File

@ -68,7 +68,7 @@ export default {
// 是否开启多租户
tenant: {
// 是否开启多租户
enable: false,
enable: true,
// 需要过滤多租户的url, 支持通配符, 如/admin/**/* 表示admin模块下的所有接口都进行多租户过滤
urls: [],
},

View File

@ -16,7 +16,7 @@ export default () => {
globalMiddlewares: [
BaseTranslateMiddleware,
BaseAuthorityMiddleware,
BaseLogMiddleware,
// BaseLogMiddleware,
],
// 模块加载顺序默认为0值越大越优先加载
order: 10,

View File

@ -116,7 +116,9 @@ export class TenantSubscriber implements EntitySubscriberInterface<any> {
if (!this.tenant.enable) return;
const tenantId = this.getTenantId();
if (tenantId) {
queryBuilder.where('tenantId = :tenantId', { tenantId });
queryBuilder.where(`${queryBuilder.alias}.tenantId = :tenantId`, {
tenantId,
});
}
}

View File

@ -55,6 +55,7 @@ export class BaseAuthorityMiddleware
if (_.startsWith(url, adminUrl)) {
try {
ctx.admin = jwt.verify(token, this.jwtConfig.jwt.secret);
ctx.admin.tenantId = 1;
if (ctx.admin.isRefresh) {
ctx.status = 401;
throw new CoolCommException('登录失效~');

View File

@ -78,6 +78,9 @@ export class BaseTranslateService {
*
*/
async loadTranslations() {
if (!(this.config?.enable && this.app.getEnv() == 'local')) {
return;
}
if (!this.basePath) {
this.basePath = path.join(this.app.getBaseDir(), '..', 'src', 'locales');
}
@ -195,7 +198,7 @@ export class BaseTranslateService {
*
*/
async check() {
if (this.config?.enable) {
if (this.config?.enable && this.app.getEnv() == 'local') {
this.basePath = path.join(this.app.getBaseDir(), '..', 'src', 'locales');
const menuLockExists = this.checkLockFile('menu');

View File

@ -0,0 +1,23 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoGoodsEntity } from '../../entity/goods';
import { DemoTenantService } from '../../service/tenant';
/**
*
*/
@CoolController({
serviceApis: [
'use',
{
method: 'noUse',
summary: '不使用多租户',
},
{
method: 'noTenant',
summary: '局部不使用多租户',
},
],
entity: DemoGoodsEntity,
service: DemoTenantService,
})
export class AdminDemoTenantController extends BaseController {}

View File

@ -1,7 +1,13 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoGoodsEntity } from '../../entity/goods';
import { DemoTenantService } from '../../service/tenant';
/**
*
*/
@CoolController({})
export class DemoTenantController extends BaseController {}
@CoolController({
api: [],
entity: DemoGoodsEntity,
service: DemoTenantService,
})
export class OpenDemoTenantController extends BaseController {}

View File

@ -1,8 +1,10 @@
import { Provide } from '@midwayjs/core';
import { Inject, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { DemoGoodsEntity } from '../entity/goods';
import { UserInfoEntity } from '../../user/entity/info';
import { noTenant } from '../../base/db/tenant';
/**
*
@ -12,18 +14,38 @@ export class DemoTenantService extends BaseService {
@InjectEntityModel(DemoGoodsEntity)
demoGoodsEntity: Repository<DemoGoodsEntity>;
@Inject()
ctx;
/**
* 使
*/
async use() {}
async use() {
await this.demoGoodsEntity.createQueryBuilder().getMany();
await this.demoGoodsEntity.find();
}
/**
* 使(使)
*/
async noUse() {}
async noUse() {
// 过滤多租户
await this.demoGoodsEntity.createQueryBuilder().getMany();
// 被noTenant包裹不会过滤多租户
await noTenant(this.ctx, async () => {
return await this.demoGoodsEntity.createQueryBuilder().getMany();
});
// 过滤多租户
await this.demoGoodsEntity.find();
}
/**
*
*/
async invalid() {}
async invalid() {
// 自定义sql不进行多租户过滤
await this.nativeQuery('select * from demo_goods');
// 自定义分页sql进行多租户过滤
await this.sqlRenderPage('select * from demo_goods');
}
}

View File

@ -9,9 +9,9 @@ import { DemoGoodsEntity } from '../../../demo/entity/goods';
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: UserInfoEntity,
pageQueryOp: {
fieldEq: ['status', 'gender', 'loginType'],
fieldEq: ['a.status', 'a.gender', 'a.loginType'],
fieldLike: ['b.title'],
keyWordLikeFields: ['nickName', 'phone'],
keyWordLikeFields: ['a.nickName', 'a.phone'],
select: ['a.*', 'b.title as goodsName'],
join: [
{