mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 10:13:28 +08:00
add 新增短链接模块
This commit is contained in:
parent
b4c3cf13a4
commit
6acc6587c1
@ -23,6 +23,7 @@
|
|||||||
<module>ruoyi-common-oss</module>
|
<module>ruoyi-common-oss</module>
|
||||||
<module>ruoyi-common-ratelimiter</module>
|
<module>ruoyi-common-ratelimiter</module>
|
||||||
<module>ruoyi-common-redis</module>
|
<module>ruoyi-common-redis</module>
|
||||||
|
<module>ruoyi-common-shortlink</module>
|
||||||
<module>ruoyi-common-satoken</module>
|
<module>ruoyi-common-satoken</module>
|
||||||
<module>ruoyi-common-security</module>
|
<module>ruoyi-common-security</module>
|
||||||
<module>ruoyi-common-sms</module>
|
<module>ruoyi-common-sms</module>
|
||||||
|
|||||||
@ -96,6 +96,13 @@
|
|||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 短链接 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara</groupId>
|
||||||
|
<artifactId>ruoyi-common-shortlink</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- satoken -->
|
<!-- satoken -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.dromara</groupId>
|
<groupId>org.dromara</groupId>
|
||||||
|
|||||||
37
ruoyi-common/ruoyi-common-shortlink/pom.xml
Normal file
37
ruoyi-common/ruoyi-common-shortlink/pom.xml
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<groupId>org.dromara</groupId>
|
||||||
|
<artifactId>ruoyi-common</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>ruoyi-common-shortlink</artifactId>
|
||||||
|
|
||||||
|
<description>
|
||||||
|
ruoyi-common-shortlink 短链接模块
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- RuoYi Common Core-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara</groupId>
|
||||||
|
<artifactId>ruoyi-common-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara</groupId>
|
||||||
|
<artifactId>ruoyi-common-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara</groupId>
|
||||||
|
<artifactId>ruoyi-common-mybatis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
package org.dromara.common.shortlink.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 有效期
|
||||||
|
*
|
||||||
|
* @author AprilWind
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum ValidityType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 三分钟
|
||||||
|
*/
|
||||||
|
THREE_MINUTES(3 * 60L),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一个小时
|
||||||
|
*/
|
||||||
|
ONE_HOUR(60 * 60L),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表示一天(24小时)
|
||||||
|
*/
|
||||||
|
ONE_DAY(24 * 60 * 60L),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表示三天
|
||||||
|
*/
|
||||||
|
THREE_DAYS(3 * 24 * 60 * 60L),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表示七天(一周)
|
||||||
|
*/
|
||||||
|
SEVEN_DAYS(7 * 24 * 60 * 60L),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表示三十天(一个月)
|
||||||
|
*/
|
||||||
|
THIRTY_DAYS(30 * 24 * 60 * 60L);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 有效期(单位:秒)
|
||||||
|
*/
|
||||||
|
private final Long validityType;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package org.dromara.common.shortlink.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短链接 配置属性
|
||||||
|
*
|
||||||
|
* @author AprilWind
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "shortlink")
|
||||||
|
public class ShortLinkProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短链接主机名或域名
|
||||||
|
*/
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误页面的主机名或域名
|
||||||
|
*/
|
||||||
|
private String erroHost;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,88 @@
|
|||||||
|
package org.dromara.common.shortlink.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Validator;
|
||||||
|
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
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.properties.ShortLinkProperties;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短链接生成工具类
|
||||||
|
*
|
||||||
|
* @author AprilWind
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public class ShortLinkUtils {
|
||||||
|
|
||||||
|
private static final IdentifierGenerator IDENTIFIER_GENERATOR = SpringUtils.getBean(IdentifierGenerator.class);
|
||||||
|
private static final ShortLinkProperties shortLinkProperties = SpringUtils.getBean(ShortLinkProperties.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成带有短链接主机名或域名的完整短链接
|
||||||
|
*
|
||||||
|
* @param longUrl 完整的长链接
|
||||||
|
* @param validityType 有效期类型枚举
|
||||||
|
* @return 带有短链接主机名或域名的完整短链接
|
||||||
|
*/
|
||||||
|
public static String generateShortUrlHost(String longUrl, ValidityType validityType) {
|
||||||
|
String url = generateShortUrl(longUrl, validityType);
|
||||||
|
return shortLinkProperties.getHost() + 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定短链接并将长链接存储到缓存中
|
||||||
|
*
|
||||||
|
* @param shortUrl 指定的短链接标识符
|
||||||
|
* @param longUrl 完整的长链接
|
||||||
|
* @param validityType 有效期类型枚举
|
||||||
|
* @return 生成的指定短链接标识符
|
||||||
|
*/
|
||||||
|
public static String generateShortUrl(String shortUrl, String longUrl, ValidityType validityType) {
|
||||||
|
Validator.validateUrl(longUrl, "请输入有效的url");
|
||||||
|
RedisUtils.setCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl, longUrl, Duration.ofSeconds(validityType.getValidityType()));
|
||||||
|
return shortUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据短链接获取对应的完整长链接,如果找不到长链接则返回错误链接
|
||||||
|
*
|
||||||
|
* @param shortUrl 短链接标识符
|
||||||
|
* @return 对应的完整长链接,如果未找到则返回错误链接
|
||||||
|
*/
|
||||||
|
public static String getLongLink(String shortUrl) {
|
||||||
|
return getLongLink(shortUrl, shortLinkProperties.getErroHost());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据短链接获取对应的完整长链接,如果找不到长链接则返回指定的错误链接
|
||||||
|
*
|
||||||
|
* @param shortUrl 短链接标识符
|
||||||
|
* @param errUrl 错误链接
|
||||||
|
* @return 对应的完整长链接,如果未找到则返回错误链接
|
||||||
|
*/
|
||||||
|
public static String getLongLink(String shortUrl, String errUrl) {
|
||||||
|
String longLink = RedisUtils.getCacheObject(GlobalConstants.SHORT_LINK_KEY + shortUrl);
|
||||||
|
return StringUtils.isNotEmpty(longLink) ? longLink : errUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
org.dromara.common.shortlink.properties.ShortLinkProperties
|
||||||
Loading…
Reference in New Issue
Block a user