forked from wangziqi/gongxue-base
41 lines
2.1 KiB
TypeScript
41 lines
2.1 KiB
TypeScript
import { Controller, Get, Inject, Injectable, Module, Query, Req, Res } from '@nestjs/common';
|
|
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
import { resolveTenantRoute } from '../features/tenant/routes.js';
|
|
import { DomainRouteService } from './domain-route.service.js';
|
|
import { TenantResolveQueryDto } from './dto.js';
|
|
import { RequestContextFactory } from './request-context.factory.js';
|
|
import { ApiEnvelopeProperties } from './api-doc.decorators.js';
|
|
|
|
const TENANT_HANDLERS = Symbol('TENANT_HANDLERS');
|
|
const tenantHandlers = { resolve: resolveTenantRoute };
|
|
|
|
@Injectable()
|
|
class TenantService extends DomainRouteService {
|
|
constructor(factory: RequestContextFactory, @Inject(TENANT_HANDLERS) injectedHandlers: typeof tenantHandlers) {
|
|
super(factory, injectedHandlers);
|
|
}
|
|
}
|
|
|
|
@ApiTags('租户解析')
|
|
@Controller('/api/tenant')
|
|
class TenantController {
|
|
constructor(private readonly service: TenantService) {}
|
|
|
|
@Get('resolve')
|
|
@ApiOperation({ summary: '解析当前租户', description: '根据访问域名、host 参数或 tenantCode 解析启用中的租户及公开品牌配置。' })
|
|
@ApiEnvelopeProperties({
|
|
tenant: { type: 'object', description: '租户基本信息', properties: { id: { type: 'string', format: 'uuid' }, slug: { type: 'string' }, name: { type: 'string' }, mode: { type: 'string' } } },
|
|
branding: { type: 'object', description: '公开品牌与主题配置', additionalProperties: true },
|
|
features: { type: 'object', description: '学生端功能开关', additionalProperties: true },
|
|
adminFeatures: { type: 'object', description: '管理端功能开关', additionalProperties: true },
|
|
publicConfig: { type: 'object', description: '允许公开给客户端的租户配置', additionalProperties: true },
|
|
})
|
|
resolve(@Query() _query: TenantResolveQueryDto, @Req() req: FastifyRequest, @Res({ passthrough: true }) res: FastifyReply) {
|
|
return this.service.execute('resolve', req, res);
|
|
}
|
|
}
|
|
|
|
@Module({ controllers: [TenantController], providers: [TenantService, { provide: TENANT_HANDLERS, useValue: tenantHandlers }] })
|
|
export class TenantModule {}
|