ShortLinkServiceImpl.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package jnpf.message.service.impl;
  2. import jnpf.base.service.SuperServiceImpl;
  3. import jnpf.message.entity.ShortLinkEntity;
  4. import jnpf.message.mapper.ShortLInkMapper;
  5. import jnpf.message.service.ShortLinkService;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import jnpf.permission.service.AuthorizeService;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  11. import jnpf.util.*;
  12. import java.security.MessageDigest;
  13. import java.util.*;
  14. /**
  15. * 消息模板(新)
  16. * 版本: V3.2.0
  17. * 版权: 引迈信息技术有限公司(https://www.jnpfsoft.com)
  18. * 作者: JNPF开发平台组
  19. * 日期: 2022-08-18
  20. */
  21. @Service
  22. public class ShortLinkServiceImpl extends SuperServiceImpl<ShortLInkMapper, ShortLinkEntity> implements ShortLinkService {
  23. @Override
  24. public ShortLinkEntity getInfoByLink(String link){
  25. QueryWrapper<ShortLinkEntity> queryWrapper = new QueryWrapper<>();
  26. queryWrapper.lambda().eq(ShortLinkEntity::getShortLink,link);
  27. return this.getOne(queryWrapper);
  28. }
  29. @Override
  30. public String shortLink (String link){
  31. String shortUrl = null;
  32. if(StringUtil.isNotBlank(link)) {
  33. String[] aResult = shortUrl(link);//将产生4组6位字符串
  34. // 打印出结果
  35. Random random = new Random();
  36. int j = random.nextInt(4);//产成4以内随机数
  37. shortUrl = aResult[j];
  38. if(StringUtil.isNotBlank(shortUrl)) {
  39. QueryWrapper<ShortLinkEntity> queryWrapper = new QueryWrapper<>();
  40. if (StringUtil.isNotEmpty(shortUrl)) {
  41. queryWrapper.lambda().eq(ShortLinkEntity::getShortLink, shortUrl);
  42. }
  43. if (this.list(queryWrapper) != null && this.list(queryWrapper).size() > 0) {
  44. shortUrl = shortUrl + j;
  45. }
  46. }
  47. }
  48. return shortUrl;
  49. }
  50. public static String[] shortUrl(String url) {
  51. // 可以自定义生成 MD5 加密字符传前的混合 KEY
  52. String key = "test";
  53. // 要使用生成 URL 的字符
  54. String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h",
  55. "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
  56. "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
  57. "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
  58. "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
  59. "U", "V", "W", "X", "Y", "Z"
  60. };
  61. // 对传入网址进行 MD5 加密
  62. String hex = md5ByHex(key + url);
  63. String[] resUrl = new String[4];
  64. for (int i = 0; i < 4; i++) {
  65. // 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算
  66. String sTempSubString = hex.substring(i * 8, i * 8 + 8);
  67. // 这里需要使用 long 型来转换,因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 , 如果不用long ,则会越界
  68. long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
  69. String outChars = "";
  70. for (int j = 0; j < 6; j++) {
  71. // 把得到的值与 0x0000003D 进行位与运算,取得字符数组 chars 索引
  72. long index = 0x0000003D & lHexLong;
  73. // 把取得的字符相加
  74. outChars += chars[(int) index];
  75. // 每次循环按位右移 5 位
  76. lHexLong = lHexLong >> 5;
  77. }
  78. // 把字符串存入对应索引的输出数组
  79. resUrl[i] = outChars;
  80. }
  81. return resUrl;
  82. }
  83. /**
  84. * MD5加密(32位大写)
  85. * @param src
  86. * @return
  87. */
  88. public static String md5ByHex(String src) {
  89. try {
  90. MessageDigest md = MessageDigest.getInstance("MD5");
  91. byte[] b = src.getBytes();
  92. md.reset();
  93. md.update(b);
  94. byte[] hash = md.digest();
  95. String hs = "";
  96. String stmp = "";
  97. for (int i = 0; i < hash.length; i++) {
  98. stmp = Integer.toHexString(hash[i] & 0xFF);
  99. if (stmp.length() == 1)
  100. hs = hs + "0" + stmp;
  101. else {
  102. hs = hs + stmp;
  103. }
  104. }
  105. return hs.toUpperCase();
  106. } catch (Exception e) {
  107. return "";
  108. }
  109. }
  110. }