多租户:登录界面,根据 host 域名获取对应的租户编号

This commit is contained in:
YunaiV
2023-11-06 19:47:21 +08:00
parent c162b39e1e
commit feba904fd8
9 changed files with 83 additions and 14 deletions

View File

@ -37,8 +37,17 @@ public class TenantController {
@Operation(summary = "使用租户名,获得租户编号", description = "登录界面,根据用户的租户名,获得租户编号")
@Parameter(name = "name", description = "租户名", required = true, example = "1024")
public CommonResult<Long> getTenantIdByName(@RequestParam("name") String name) {
TenantDO tenantDO = tenantService.getTenantByName(name);
return success(tenantDO != null ? tenantDO.getId() : null);
TenantDO tenant = tenantService.getTenantByName(name);
return success(tenant != null ? tenant.getId() : null);
}
@GetMapping("/get-by-website")
@PermitAll
@Operation(summary = "使用域名,获得租户信息", description = "登录界面,根据用户的域名,获得租户信息")
@Parameter(name = "website", description = "域名", required = true, example = "www.iocoder.cn")
public CommonResult<TenantSimpleRespVO> getTenantByWebsite(@RequestParam("website") String website) {
TenantDO tenant = tenantService.getTenantByWebsite(website);
return success(TenantConvert.INSTANCE.convert03(tenant));
}
@PostMapping("/create")
@ -94,5 +103,4 @@ public class TenantController {
ExcelUtils.write(response, "租户.xls", "数据", TenantExcelVO.class, datas);
}
}

View File

@ -0,0 +1,20 @@
package cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@Schema(description = "管理后台 - 租户精简 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class TenantSimpleRespVO extends TenantBaseVO {
@Schema(description = "租户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
private Long id;
@Schema(description = "租户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道")
private String name;
}

View File

@ -1,10 +1,7 @@
package cn.iocoder.yudao.module.system.convert.tenant;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.*;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserCreateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import org.mapstruct.Mapper;
@ -28,6 +25,8 @@ public interface TenantConvert {
TenantRespVO convert(TenantDO bean);
TenantSimpleRespVO convert03(TenantDO bean);
List<TenantRespVO> convertList(List<TenantDO> list);
PageResult<TenantRespVO> convertPage(PageResult<TenantDO> page);

View File

@ -42,6 +42,10 @@ public interface TenantMapper extends BaseMapperX<TenantDO> {
return selectOne(TenantDO::getName, name);
}
default TenantDO selectByWebsite(String website) {
return selectOne(TenantDO::getWebsite, website);
}
default Long selectCountByPackageId(Long packageId) {
return selectCount(TenantDO::getPackageId, packageId);
}

View File

@ -78,11 +78,19 @@ public interface TenantService {
/**
* 获得名字对应的租户
*
* @param name 户名
* @param name 户名
* @return 租户
*/
TenantDO getTenantByName(String name);
/**
* 获得域名对应的租户
*
* @param website 域名
* @return 租户
*/
TenantDO getTenantByWebsite(String website);
/**
* 获得使用指定套餐的租户数量
*
@ -128,4 +136,5 @@ public interface TenantService {
* @param id 租户编号
*/
void validTenant(Long id);
}

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.service.tenant;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
@ -99,6 +100,8 @@ public class TenantServiceImpl implements TenantService {
public Long createTenant(TenantCreateReqVO createReqVO) {
// 校验租户名称是否重复
validTenantNameDuplicate(createReqVO.getName(), null);
// 校验租户域名是否重复
validTenantWebsiteDuplicate(createReqVO.getWebsite(), null);
// 校验套餐被禁用
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId());
@ -143,6 +146,8 @@ public class TenantServiceImpl implements TenantService {
TenantDO tenant = validateUpdateTenant(updateReqVO.getId());
// 校验租户名称是否重复
validTenantNameDuplicate(updateReqVO.getName(), updateReqVO.getId());
// 校验租户域名是否重复
validTenantWebsiteDuplicate(updateReqVO.getWebsite(), updateReqVO.getId());
// 校验套餐被禁用
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId());
@ -169,6 +174,23 @@ public class TenantServiceImpl implements TenantService {
}
}
private void validTenantWebsiteDuplicate(String website, Long id) {
if (StrUtil.isEmpty(website)) {
return;
}
TenantDO tenant = tenantMapper.selectByWebsite(website);
if (tenant == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同名字的租户
if (id == null) {
throw exception(TENANT_WEBSITE_DUPLICATE, website);
}
if (!tenant.getId().equals(id)) {
throw exception(TENANT_WEBSITE_DUPLICATE, website);
}
}
@Override
@DSTransactional
public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {
@ -234,6 +256,11 @@ public class TenantServiceImpl implements TenantService {
return tenantMapper.selectByName(name);
}
@Override
public TenantDO getTenantByWebsite(String website) {
return tenantMapper.selectByWebsite(website);
}
@Override
public Long getTenantCountByPackageId(Long packageId) {
return tenantMapper.selectCountByPackageId(packageId);