mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 02:08:15 +08:00
update 优化短连接重定向
This commit is contained in:
parent
6acc6587c1
commit
ddc78eb548
@ -22,45 +22,57 @@ import java.time.Duration;
|
||||
public class ShortLinkUtils {
|
||||
|
||||
private static final IdentifierGenerator IDENTIFIER_GENERATOR = SpringUtils.getBean(IdentifierGenerator.class);
|
||||
private static final ShortLinkProperties shortLinkProperties = SpringUtils.getBean(ShortLinkProperties.class);
|
||||
private static final ShortLinkProperties SHORT_LINK_PROPERTIES = SpringUtils.getBean(ShortLinkProperties.class);
|
||||
|
||||
/**
|
||||
* 生成带有短链接主机名或域名的完整短链接
|
||||
* 根据默认主机名(或域名)生成完整短链接
|
||||
*
|
||||
* @param longUrl 完整的长链接
|
||||
* @param validityType 有效期类型枚举
|
||||
* @return 带有短链接主机名或域名的完整短链接
|
||||
* @return 带有默认主机名(或域名)的完整短链接
|
||||
*/
|
||||
public static String generateShortUrlHost(String longUrl, ValidityType validityType) {
|
||||
String url = generateShortUrl(longUrl, validityType);
|
||||
return shortLinkProperties.getHost() + url;
|
||||
public static String generateShortUrl(String longUrl, ValidityType validityType) {
|
||||
return generateShortUrl(SHORT_LINK_PROPERTIES.getHost(), longUrl, validityType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成短链接并将长链接存储到缓存中
|
||||
* 根据指定主机名(或域名)生成完整短链接
|
||||
*
|
||||
* @param host 主机名(或域名)
|
||||
* @param longUrl 完整的长链接
|
||||
* @param validityType 有效期类型枚举
|
||||
* @return 带有指定主机名(或域名)的完整短链接
|
||||
*/
|
||||
public static String generateShortUrl(String host, String longUrl, ValidityType validityType) {
|
||||
String url = generateShortLinkIdentifier(longUrl, validityType);
|
||||
return host + url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成短链接标识符并将长链接存储到缓存中
|
||||
*
|
||||
* @param longUrl 完整的长链接
|
||||
* @param validityType 有效期类型枚举
|
||||
* @return 生成的短链接标识符
|
||||
*/
|
||||
public static String generateShortUrl(String longUrl, ValidityType validityType) {
|
||||
// 使用标识符生成器生成短链接
|
||||
String shortUrl = IDENTIFIER_GENERATOR.nextId(null).toString();
|
||||
return generateShortUrl(shortUrl, longUrl, validityType);
|
||||
public static String generateShortLinkIdentifier(String longUrl, ValidityType validityType) {
|
||||
// 使用标识符生成器生成短链接标识符
|
||||
String shortLinkIdentifier = IDENTIFIER_GENERATOR.nextId(null).toString();
|
||||
return generateShortLinkIdentifier(shortLinkIdentifier, longUrl, validityType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定短链接并将长链接存储到缓存中
|
||||
* 生成指定短链接标识符并将长链接存储到缓存中
|
||||
*
|
||||
* @param shortUrl 指定的短链接标识符
|
||||
* @param longUrl 完整的长链接
|
||||
* @param validityType 有效期类型枚举
|
||||
* @param shortLinkIdentifier 指定的短链接标识符
|
||||
* @param longUrl 完整的长链接
|
||||
* @param validityType 有效期类型枚举
|
||||
* @return 生成的指定短链接标识符
|
||||
*/
|
||||
public static String generateShortUrl(String shortUrl, String longUrl, ValidityType validityType) {
|
||||
public static String generateShortLinkIdentifier(String shortLinkIdentifier, String longUrl, ValidityType validityType) {
|
||||
Validator.validateUrl(longUrl, "请输入有效的url");
|
||||
RedisUtils.setCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl, longUrl, Duration.ofSeconds(validityType.getValidityType()));
|
||||
return shortUrl;
|
||||
RedisUtils.setCacheObject(GlobalConstants.SHORT_LINK_KEY + shortLinkIdentifier, longUrl, Duration.ofSeconds(validityType.getValidityType()));
|
||||
return shortLinkIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,7 +82,7 @@ public class ShortLinkUtils {
|
||||
* @return 对应的完整长链接,如果未找到则返回错误链接
|
||||
*/
|
||||
public static String getLongLink(String shortUrl) {
|
||||
return getLongLink(shortUrl, shortLinkProperties.getErroHost());
|
||||
return getLongLink(shortUrl, SHORT_LINK_PROPERTIES.getErroHost());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -43,6 +43,12 @@
|
||||
<artifactId>ruoyi-common-oss</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 短链接功能模 -->
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-shortlink</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-log</artifactId>
|
||||
|
||||
@ -1,12 +1,9 @@
|
||||
package org.dromara.system.controller.system;
|
||||
|
||||
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.dromara.common.core.constant.GlobalConstants;
|
||||
import org.dromara.common.core.utils.SpringUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.redis.utils.RedisUtils;
|
||||
import org.dromara.common.shortlink.enums.ValidityType;
|
||||
import org.dromara.common.shortlink.utils.ShortLinkUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@ -14,8 +11,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* 短链接 控制层
|
||||
*/
|
||||
@ -24,7 +19,6 @@ import java.time.Duration;
|
||||
@RestController
|
||||
@RequestMapping("/resource/short")
|
||||
public class SysShortLinkController {
|
||||
private final IdentifierGenerator identifierGenerator;
|
||||
|
||||
/**
|
||||
* 根据短链接获取长链接并重定向
|
||||
@ -34,14 +28,8 @@ public class SysShortLinkController {
|
||||
*/
|
||||
@GetMapping("/link")
|
||||
public RedirectView getLongLink(@NotBlank(message = "参数不能为空") String shortUrl) {
|
||||
// 从 Redis 获取长链接
|
||||
String longLink = RedisUtils.getCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl);
|
||||
if (StringUtils.isNotEmpty(longLink)) {
|
||||
// 如果长链接存在,则重定向到长链接
|
||||
return new RedirectView(longLink);
|
||||
}
|
||||
// 如果长链接不存在,则重定向到错误页面
|
||||
return new RedirectView("/error");
|
||||
String longLink = ShortLinkUtils.getLongLink(shortUrl, "/error");
|
||||
return new RedirectView(longLink);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,14 +40,7 @@ public class SysShortLinkController {
|
||||
*/
|
||||
@PostMapping("/shorturl")
|
||||
public String generateShortUrl(@NotBlank(message = "参数不能为空") String longUrl) {
|
||||
// 使用标识符生成器生成短链接
|
||||
String shortUrl = identifierGenerator.nextId(null).toString();
|
||||
// 将短链接与长链接的映射关系存储到 Redis 中,并设置过期时间为 3 分钟
|
||||
RedisUtils.setCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl, longUrl, Duration.ofMinutes(3));
|
||||
// 获取短链接的域名部分
|
||||
String host = SpringUtils.getProperty("short.host");
|
||||
// 拼接完整的短链接并返回
|
||||
return host + shortUrl;
|
||||
return ShortLinkUtils.generateShortUrl(longUrl, ValidityType.THREE_DAYS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user