|
@@ -5,21 +5,32 @@ import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Paths;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
+import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
|
|
+import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.core.domain.entity.SysMenu;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import com.ruoyi.common.utils.ip.IpUtils;
|
|
|
+import com.ruoyi.system.domain.SysActivationCodeLog;
|
|
|
import com.ruoyi.system.domain.SysTenantMenu;
|
|
|
-import com.ruoyi.system.mapper.SysMenuMapper;
|
|
|
-import com.ruoyi.system.mapper.SysTenantMenuMapper;
|
|
|
-import com.ruoyi.system.mapper.SysUserMapper;
|
|
|
+import com.ruoyi.system.mapper.*;
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import com.ruoyi.system.mapper.SysTenantMapper;
|
|
|
import com.ruoyi.common.core.domain.entity.SysTenant;
|
|
|
import com.ruoyi.system.service.ISysTenantService;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -48,6 +59,12 @@ public class SysTenantServiceImpl implements ISysTenantService
|
|
|
@Autowired
|
|
|
private SysUserMapper sysUserMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysActivationCodeLogMapper sysActivationCodeLogMapper;
|
|
|
+
|
|
|
/**
|
|
|
* 查询租户信息
|
|
|
*
|
|
@@ -184,5 +201,135 @@ public class SysTenantServiceImpl implements ISysTenantService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 生成激活码
|
|
|
+ * @param tenantId
|
|
|
+ * @param tenantExpirationTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public AjaxResult crateTenantCode(String tenantId,String tenantExpirationTime) throws Exception {
|
|
|
+ //加密器
|
|
|
+ SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DES, "sgEsnN6QWq8W7j5H01020304".getBytes());
|
|
|
+ //激活码生成时间
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ String nowStr = DateUtils.toLocalDateTimeStr(now);
|
|
|
+ //激活码有效时间默认为24小时
|
|
|
+ LocalDateTime offset = LocalDateTimeUtil.offset(now, 1, ChronoUnit.DAYS);
|
|
|
+ String offsetStr = DateUtils.toLocalDateTimeStr(offset);
|
|
|
+ //激活码生成时间+租户id+激活码有效期+激活多长时间
|
|
|
+ String dataStr=nowStr+"_"+tenantId+"_"+offsetStr+"_"+tenantExpirationTime;
|
|
|
+ //加密信息
|
|
|
+ String encryptHex = des.encryptHex(dataStr);
|
|
|
+ //生成激活码操作
|
|
|
+ activationCodeLog("生成激活码");
|
|
|
+ return AjaxResult.success(encryptHex);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 激活码激活操作
|
|
|
+ * @param tenantId
|
|
|
+ * @param activationCode
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public AjaxResult activationOperation(String tenantId, String activationCode) throws Exception {
|
|
|
+ String activeCode="active:code:"+activationCode;//魔法值后期抽出来
|
|
|
+ String strCode = stringRedisTemplate.opsForValue().get(activationCode);
|
|
|
+ if(StringUtils.isNotEmpty(strCode)){
|
|
|
+ return AjaxResult.error("当前激活码已经被使用过了不能重复使用");
|
|
|
+ }
|
|
|
+ //激活码生成时间+租户id+激活码有效期+激活多长时间
|
|
|
+ SymmetricCrypto symmetricCrypto = new SymmetricCrypto(SymmetricAlgorithm.DES, "sgEsnN6QWq8W7j5H01020304".getBytes());
|
|
|
+ String decryptStr = symmetricCrypto.decryptStr(activationCode, CharsetUtil.CHARSET_UTF_8);
|
|
|
+ String[] contentString = decryptStr.split("_");
|
|
|
+ //判断激活码是否失效
|
|
|
+ String expirationDateStr=contentString[2];
|
|
|
+ LocalDateTime expirationDate = DateUtils.toLocalDateTime(expirationDateStr,"yyyy-MM-dd HH:mm:ss");
|
|
|
+ long expirationDateSecond = expirationDate.toEpochSecond(ZoneOffset.ofHours(8));
|
|
|
+ long nowSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));
|
|
|
+ if(expirationDateSecond-nowSecond<0){
|
|
|
+ return AjaxResult.error("此激活码已经过期,不能在继续使用");
|
|
|
+ }
|
|
|
+ if(!tenantId.equals(contentString[1])){
|
|
|
+ return AjaxResult.error("当前激活码不能在当前租户使用");
|
|
|
+ }
|
|
|
+ //先查询、这个用户有没有被激活过
|
|
|
+ SysTenant sysTenant = sysTenantMapper.selectSysTenantByTenantId(Long.valueOf(tenantId));
|
|
|
+ SysTenant sysTenantOne=new SysTenant();
|
|
|
+ sysTenantOne.setTenantId(Long.valueOf(tenantId));
|
|
|
+ //判断有没有有被激活过、有时间代表激活过
|
|
|
+ if(StringUtils.isNotEmpty(sysTenant.getTenantExpirationTime())){
|
|
|
+ //解密
|
|
|
+ String LastActiveTime = symmetricCrypto.decryptStr(sysTenant.getTenantExpirationTime(), CharsetUtil.CHARSET_UTF_8);
|
|
|
+ LocalDateTime localDateTime = DateUtils.toLocalDateTime(LastActiveTime, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ //当前日期时间戳
|
|
|
+ long nowTimeSecond =LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));
|
|
|
+ //原来的过期时间
|
|
|
+ long oldTimeSecond = localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
|
|
|
+ //老的过期时间跟当前时间比较 如果小于0证明已经过期好久了
|
|
|
+ if(oldTimeSecond-nowTimeSecond<0){
|
|
|
+ //新续期的时间从当前时间进行续期操作
|
|
|
+ renewalTime(symmetricCrypto, contentString, sysTenantOne);
|
|
|
+ }else {
|
|
|
+ //老的时间快过期了还没过期(旧到期时间+新到期时间(续期操作))
|
|
|
+ LocalDateTime newActiveDateTime = LocalDateTimeUtil.offset(localDateTime, Long.parseLong(contentString[3]), ChronoUnit.DAYS);
|
|
|
+ //加密
|
|
|
+ String newActiveDateTimeStr= symmetricCrypto.encryptHex(DateUtils.toLocalDateTimeStr(newActiveDateTime));
|
|
|
+ //更新到期时间
|
|
|
+ sysTenantOne.setTenantExpirationTime(newActiveDateTimeStr);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //新续期的时间从当前时间进行续期操作
|
|
|
+ renewalTime(symmetricCrypto, contentString, sysTenantOne);
|
|
|
+ }
|
|
|
+ //更新租户的有效时间
|
|
|
+ sysTenantMapper.updateSysTenant(sysTenantOne);
|
|
|
+ //保存验证码操作日志
|
|
|
+ activationCodeLog("使用激活码");
|
|
|
+ //24小时之后就删除了我们保存的验证码信息
|
|
|
+ stringRedisTemplate.opsForValue().set(activeCode,activationCode,24, TimeUnit.HOURS);
|
|
|
+ return AjaxResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从当前时间往后续期租户时间
|
|
|
+ * @param symmetricCrypto
|
|
|
+ * @param contentString
|
|
|
+ * @param sysTenantOne
|
|
|
+ */
|
|
|
+ private void renewalTime(SymmetricCrypto symmetricCrypto, String[] contentString, SysTenant sysTenantOne) {
|
|
|
+ //当前时间
|
|
|
+ LocalDateTime localDateTime = DateUtils.toLocalDateTime(DateUtils.toLocalDateTimeStr(LocalDateTime.now()), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ //设置到期时间
|
|
|
+ LocalDateTime activeDateTime = LocalDateTimeUtil.offset(localDateTime, Long.parseLong(contentString[3]), ChronoUnit.DAYS);
|
|
|
+ //加密到期时间
|
|
|
+ String newActiveDateTimeStr= symmetricCrypto.encryptHex(DateUtils.toLocalDateTimeStr(activeDateTime));
|
|
|
+ sysTenantOne.setTenantExpirationTime(newActiveDateTimeStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存激活码操作日志
|
|
|
+ * @param msg
|
|
|
+ */
|
|
|
+ private void activationCodeLog(String msg) {
|
|
|
+ //保存生成激活码日志
|
|
|
+ SysUser user = SecurityUtils.getLoginUser().getUser();
|
|
|
+ SysActivationCodeLog activationCodeLog=new SysActivationCodeLog();
|
|
|
+ //生成时间
|
|
|
+ activationCodeLog.setGenerationTime(new Date());
|
|
|
+ //ip
|
|
|
+ activationCodeLog.setIpAddress(IpUtils.getIpAddr());
|
|
|
+ //操作人
|
|
|
+ activationCodeLog.setOperator(user.getUserName());
|
|
|
+ //备注
|
|
|
+ activationCodeLog.setNote(msg);
|
|
|
+ //插入操作日志
|
|
|
+ sysActivationCodeLogMapper.insertDataSource(activationCodeLog);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
}
|