feat: 创建租户并关联腾讯云域名
This commit is contained in:
@@ -125,6 +125,13 @@
|
|||||||
<version>2.4.6</version>
|
<version>2.4.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencentcloudapi</groupId>
|
||||||
|
<artifactId>tencentcloud-sdk-java-dnspod</artifactId>
|
||||||
|
<version>3.1.322</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package com.starry.admin.modules.platform.service.impl;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
@@ -22,6 +24,12 @@ import com.starry.admin.utils.SecurityUtils;
|
|||||||
import com.starry.common.constant.CacheConstants;
|
import com.starry.common.constant.CacheConstants;
|
||||||
import com.starry.common.redis.RedisCache;
|
import com.starry.common.redis.RedisCache;
|
||||||
import com.starry.common.result.R;
|
import com.starry.common.result.R;
|
||||||
|
import com.tencentcloudapi.common.Credential;
|
||||||
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||||
|
import com.tencentcloudapi.dnspod.v20210323.DnspodClient;
|
||||||
|
import com.tencentcloudapi.dnspod.v20210323.models.CreateRecordRequest;
|
||||||
|
import com.tencentcloudapi.dnspod.v20210323.models.CreateRecordResponse;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@@ -37,6 +45,7 @@ import java.util.stream.Collectors;
|
|||||||
* @author admin
|
* @author admin
|
||||||
* @since 2023-03-03
|
* @since 2023-03-03
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenantEntity> implements ISysTenantService {
|
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenantEntity> implements ISysTenantService {
|
||||||
@Resource
|
@Resource
|
||||||
@@ -163,16 +172,18 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
public R addTenant(SysTenantEntity sysTenantEntity) {
|
public R addTenant(SysTenantEntity sysTenantEntity) {
|
||||||
|
// 判断账号是否存在
|
||||||
|
if (sysUserMapper.checkUserNameUnique(sysTenantEntity.getUserName()) > 0) {
|
||||||
|
return R.error("管理员账号已存在,请重新设置!");
|
||||||
|
}
|
||||||
if (StrUtil.isBlankIfStr(sysTenantEntity.getTenantId())) {
|
if (StrUtil.isBlankIfStr(sysTenantEntity.getTenantId())) {
|
||||||
sysTenantEntity.setTenantId(IdUtil.fastSimpleUUID());
|
sysTenantEntity.setTenantId(IdUtil.fastSimpleUUID());
|
||||||
}
|
}
|
||||||
if (StrUtil.isBlankIfStr(sysTenantEntity.getTenantCode())) {
|
if (StrUtil.isBlankIfStr(sysTenantEntity.getTenantCode())) {
|
||||||
sysTenantEntity.setTenantCode(IdUtil.fastSimpleUUID());
|
sysTenantEntity.setTenantCode(IdUtil.fastSimpleUUID());
|
||||||
}
|
}
|
||||||
// 判断账号是否存在
|
// 生成随机key,用来生成域名,关键!
|
||||||
if (sysUserMapper.checkUserNameUnique(sysTenantEntity.getUserName()) > 0) {
|
sysTenantEntity.setTenantKey(RandomUtil.randomString(8));
|
||||||
return R.error("管理员账号已存在,请重新设置!");
|
|
||||||
}
|
|
||||||
// 创建租户
|
// 创建租户
|
||||||
sysTenantMapper.insert(sysTenantEntity);
|
sysTenantMapper.insert(sysTenantEntity);
|
||||||
// 创建默认部门--部门默认名称以租户名称
|
// 创建默认部门--部门默认名称以租户名称
|
||||||
@@ -181,9 +192,12 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|||||||
Long roleId = createRole(sysTenantEntity);
|
Long roleId = createRole(sysTenantEntity);
|
||||||
// 创建默认账号
|
// 创建默认账号
|
||||||
createUser(sysTenantEntity, deptId, roleId);
|
createUser(sysTenantEntity, deptId, roleId);
|
||||||
|
// 创建域名
|
||||||
|
this.createDomainAndDns(sysTenantEntity.getTenantKey());
|
||||||
return R.ok("租户创建成功!");
|
return R.ok("租户创建成功!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Long createDept(SysTenantEntity sysTenantEntity) {
|
private Long createDept(SysTenantEntity sysTenantEntity) {
|
||||||
// 创建部门
|
// 创建部门
|
||||||
SysDeptEntity dept = new SysDeptEntity();
|
SysDeptEntity dept = new SysDeptEntity();
|
||||||
@@ -293,4 +307,22 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
|||||||
queryWrapper.eq(SysTenantEntity::getTenantId, packageId);
|
queryWrapper.eq(SysTenantEntity::getTenantId, packageId);
|
||||||
return this.baseMapper.selectList(queryWrapper);
|
return this.baseMapper.selectList(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createDomainAndDns(String tenentKey) {
|
||||||
|
Credential cred = new Credential("AKIDESceXUSmHvcFnFbqv3Tjnjn02FRZWKwW", "cFyys2FG7sV1Mm6KONVqNuHXjrvoaVSU");
|
||||||
|
DnspodClient dnspodClient = new DnspodClient(cred, "");
|
||||||
|
CreateRecordRequest req = new CreateRecordRequest();
|
||||||
|
req.setDomain("hucs.top");
|
||||||
|
req.setRecordType("A");
|
||||||
|
req.setRecordLine("默认");
|
||||||
|
req.setValue("122.51.20.105");
|
||||||
|
req.setSubDomain(tenentKey + ".july");
|
||||||
|
CreateRecordResponse createRecordResponse;
|
||||||
|
try {
|
||||||
|
createRecordResponse = dnspodClient.CreateRecord(req);
|
||||||
|
} catch (TencentCloudSDKException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
log.info("腾讯云域名映射结果:{}", JSONObject.toJSONString(createRecordResponse));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user