mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-11 00:22:49 +00:00
优化CoolController新增serviceApis
This commit is contained in:
parent
6c39f42a56
commit
afb82751f1
@ -68,7 +68,7 @@ export default {
|
||||
// 是否开启多租户
|
||||
tenant: {
|
||||
// 是否开启多租户
|
||||
enable: false,
|
||||
enable: true,
|
||||
// 需要过滤多租户的url, 支持通配符, 如/admin/**/* 表示admin模块下的所有接口都进行多租户过滤
|
||||
urls: [],
|
||||
},
|
||||
|
||||
@ -16,7 +16,7 @@ export default () => {
|
||||
globalMiddlewares: [
|
||||
BaseTranslateMiddleware,
|
||||
BaseAuthorityMiddleware,
|
||||
BaseLogMiddleware,
|
||||
// BaseLogMiddleware,
|
||||
],
|
||||
// 模块加载顺序,默认为0,值越大越优先加载
|
||||
order: 10,
|
||||
|
||||
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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('登录失效~');
|
||||
|
||||
@ -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');
|
||||
|
||||
23
src/modules/demo/controller/admin/tenant.ts
Normal file
23
src/modules/demo/controller/admin/tenant.ts
Normal 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 {}
|
||||
@ -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 {}
|
||||
|
||||
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
@ -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: [
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user