Merge pull request #1768 from dataease/pr@dev@feat_public_link_short

feat: 公共链接使用8位随机字符后缀,兼容老版本数字后缀
This commit is contained in:
fit2cloud-chenyw 2022-02-15 11:38:10 +08:00 committed by GitHub
commit 92b35da452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 160 additions and 10 deletions

View File

@ -11,5 +11,7 @@ public class PanelLinkMapping implements Serializable {
private Long userId;
private String uuid;
private static final long serialVersionUID = 1L;
}

View File

@ -293,6 +293,76 @@ public class PanelLinkMappingExample {
addCriterion("user_id not between", value1, value2, "userId");
return (Criteria) this;
}
public Criteria andUuidIsNull() {
addCriterion("uuid is null");
return (Criteria) this;
}
public Criteria andUuidIsNotNull() {
addCriterion("uuid is not null");
return (Criteria) this;
}
public Criteria andUuidEqualTo(String value) {
addCriterion("uuid =", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidNotEqualTo(String value) {
addCriterion("uuid <>", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidGreaterThan(String value) {
addCriterion("uuid >", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidGreaterThanOrEqualTo(String value) {
addCriterion("uuid >=", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidLessThan(String value) {
addCriterion("uuid <", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidLessThanOrEqualTo(String value) {
addCriterion("uuid <=", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidLike(String value) {
addCriterion("uuid like", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidNotLike(String value) {
addCriterion("uuid not like", value, "uuid");
return (Criteria) this;
}
public Criteria andUuidIn(List<String> values) {
addCriterion("uuid in", values, "uuid");
return (Criteria) this;
}
public Criteria andUuidNotIn(List<String> values) {
addCriterion("uuid not in", values, "uuid");
return (Criteria) this;
}
public Criteria andUuidBetween(String value1, String value2) {
addCriterion("uuid between", value1, value2, "uuid");
return (Criteria) this;
}
public Criteria andUuidNotBetween(String value1, String value2) {
addCriterion("uuid not between", value1, value2, "uuid");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {

View File

@ -5,6 +5,7 @@
<id column="id" jdbcType="BIGINT" property="id" />
<result column="resource_id" jdbcType="VARCHAR" property="resourceId" />
<result column="user_id" jdbcType="BIGINT" property="userId" />
<result column="uuid" jdbcType="VARCHAR" property="uuid" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
@ -65,7 +66,7 @@
</where>
</sql>
<sql id="Base_Column_List">
id, resource_id, user_id
id, resource_id, user_id, uuid
</sql>
<select id="selectByExample" parameterType="io.dataease.base.domain.PanelLinkMappingExample" resultMap="BaseResultMap">
select
@ -98,10 +99,10 @@
</if>
</delete>
<insert id="insert" parameterType="io.dataease.base.domain.PanelLinkMapping">
insert into panel_link_mapping (id, resource_id, user_id
)
values (#{id,jdbcType=BIGINT}, #{resourceId,jdbcType=VARCHAR}, #{userId,jdbcType=BIGINT}
)
insert into panel_link_mapping (id, resource_id, user_id,
uuid)
values (#{id,jdbcType=BIGINT}, #{resourceId,jdbcType=VARCHAR}, #{userId,jdbcType=BIGINT},
#{uuid,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="io.dataease.base.domain.PanelLinkMapping">
insert into panel_link_mapping
@ -115,6 +116,9 @@
<if test="userId != null">
user_id,
</if>
<if test="uuid != null">
uuid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
@ -126,6 +130,9 @@
<if test="userId != null">
#{userId,jdbcType=BIGINT},
</if>
<if test="uuid != null">
#{uuid,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="io.dataease.base.domain.PanelLinkMappingExample" resultType="java.lang.Long">
@ -146,6 +153,9 @@
<if test="record.userId != null">
user_id = #{record.userId,jdbcType=BIGINT},
</if>
<if test="record.uuid != null">
uuid = #{record.uuid,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
@ -155,7 +165,8 @@
update panel_link_mapping
set id = #{record.id,jdbcType=BIGINT},
resource_id = #{record.resourceId,jdbcType=VARCHAR},
user_id = #{record.userId,jdbcType=BIGINT}
user_id = #{record.userId,jdbcType=BIGINT},
uuid = #{record.uuid,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -169,13 +180,17 @@
<if test="userId != null">
user_id = #{userId,jdbcType=BIGINT},
</if>
<if test="uuid != null">
uuid = #{uuid,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="io.dataease.base.domain.PanelLinkMapping">
update panel_link_mapping
set resource_id = #{resourceId,jdbcType=VARCHAR},
user_id = #{userId,jdbcType=BIGINT}
user_id = #{userId,jdbcType=BIGINT},
uuid = #{uuid,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>

View File

@ -7,6 +7,7 @@ import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.UUID;
/**
* 加密解密工具
@ -19,6 +20,13 @@ public class CodingUtil {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z" };
/**
* MD5加密
*
@ -165,4 +173,26 @@ public class CodingUtil {
}
}
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
public static String shortUuid() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}
}

View File

@ -3,6 +3,7 @@ package io.dataease.controller;
import io.dataease.commons.exception.DEException;
import io.dataease.commons.license.DefaultLicenseService;
import io.dataease.commons.license.F2CLicenseResponse;
import io.dataease.commons.utils.CodingUtil;
import io.dataease.commons.utils.LogUtil;
import io.dataease.commons.utils.ServletUtils;
import io.dataease.service.panel.PanelLinkService;
@ -48,8 +49,13 @@ public class IndexController {
}
@GetMapping("/link/{index}")
public void link(@PathVariable(value = "index", required = true) Long index) {
String url = panelLinkService.getUrlByIndex(index);
public void link(@PathVariable(value = "index", required = true) String index) {
String url;
if (CodingUtil.isNumeric(index)) {
url = panelLinkService.getUrlByIndex(Long.parseLong(index));
}else {
url = panelLinkService.getUrlByUuid(index);
}
HttpServletResponse response = ServletUtils.response();
try {
response.sendRedirect(url);
@ -59,6 +65,8 @@ public class IndexController {
}
}
@GetMapping("/tempMobileLink/{id}/{token}")
public void tempMobileLink(@PathVariable("id") String id, @PathVariable("token") String token) {
String url = "/#preview/" + id;

View File

@ -8,7 +8,9 @@ import io.dataease.base.mapper.PanelGroupMapper;
import io.dataease.base.mapper.PanelLinkMapper;
import io.dataease.base.mapper.PanelLinkMappingMapper;
import io.dataease.base.mapper.ext.ExtPanelLinkMapper;
import io.dataease.commons.exception.DEException;
import io.dataease.commons.utils.AuthUtils;
import io.dataease.commons.utils.CodingUtil;
import io.dataease.commons.utils.ServletUtils;
import io.dataease.controller.request.panel.link.EnablePwdRequest;
import io.dataease.controller.request.panel.link.LinkRequest;
@ -114,7 +116,13 @@ public class PanelLinkService {
PanelLinkMapping mapping = new PanelLinkMapping();
mapping.setResourceId(resourceId);
mapping.setUserId(AuthUtils.getUser().getUserId());
mapping.setUuid(CodingUtil.shortUuid());
panelLinkMappingMapper.insert(mapping);
}else {
mappings.stream().filter(mapping -> StringUtils.isBlank(mapping.getUuid())).forEach(item -> {
item.setUuid(CodingUtil.shortUuid());
panelLinkMappingMapper.updateByPrimaryKey(item);
});
}
return convertDto(one);
}
@ -206,7 +214,7 @@ public class PanelLinkService {
example.createCriteria().andResourceIdEqualTo(resourceId).andUserIdEqualTo(AuthUtils.getUser().getUserId());
List<PanelLinkMapping> mappings = panelLinkMappingMapper.selectByExample(example);
PanelLinkMapping mapping = mappings.get(0);
return SHORT_URL_PREFIX + mapping.getId();
return SHORT_URL_PREFIX + mapping.getUuid();
}
public String getUrlByIndex(Long index) {
@ -216,4 +224,18 @@ public class PanelLinkService {
PanelLink one = findOne(resourceId, userId);
return convertDto(one).getUri();
}
public String getUrlByUuid(String uuid) {
PanelLinkMappingExample example = new PanelLinkMappingExample();
example.createCriteria().andUuidEqualTo(uuid);
List<PanelLinkMapping> mappings = panelLinkMappingMapper.selectByExample(example);
if (CollectionUtils.isEmpty(mappings)) {
DEException.throwException("link is not exist");
}
PanelLinkMapping mapping = mappings.get(0);
String resourceId = mapping.getResourceId();
Long userId = mapping.getUserId();
PanelLink one = findOne(resourceId, userId);
return convertDto(one).getUri();
}
}

View File

@ -197,3 +197,6 @@ SELECT
`plugin_sys_menu`.`i_frame` <> 1
)
OR isnull( `plugin_sys_menu`.`i_frame` ));
ALTER TABLE `panel_link_mapping`
ADD COLUMN `uuid` varchar(8) NULL COMMENT 'uuid' AFTER `user_id`;