ShortLinkController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package jnpf.message.controller;
  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.Parameter;
  4. import io.swagger.v3.oas.annotations.Parameters;
  5. import io.swagger.v3.oas.annotations.tags.Tag;
  6. import jakarta.servlet.http.HttpServletResponse;
  7. import jnpf.base.ActionResult;
  8. import jnpf.base.controller.SuperController;
  9. import jnpf.config.ConfigValueUtil;
  10. import jnpf.constant.MsgCode;
  11. import jnpf.consts.DeviceType;
  12. import jnpf.database.util.TenantDataSourceUtil;
  13. import jnpf.exception.LoginException;
  14. import jnpf.message.entity.ShortLinkEntity;
  15. import jnpf.message.service.ShortLinkService;
  16. import jnpf.util.*;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.GetMapping;
  20. import org.springframework.web.bind.annotation.PathVariable;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import java.io.IOException;
  24. @Slf4j
  25. @RestController
  26. @Tag(name = "短链接跳转", description = "message")
  27. @RequestMapping("/api/message/ShortLink")
  28. public class ShortLinkController extends SuperController<ShortLinkService, ShortLinkEntity> {
  29. @Autowired
  30. private ShortLinkService shortLinkService;
  31. @Autowired
  32. private ConfigValueUtil configValueUtil;
  33. @Autowired
  34. protected AuthUtil authUtil;
  35. /**
  36. * 消息发送配置弹窗列表
  37. *
  38. * @return
  39. */
  40. @NoDataSourceBind
  41. @Operation(summary = "根据短链接获取实际链接地址")
  42. @Parameters({
  43. @Parameter(name = "shortLink", description = "短链接", required = true),
  44. @Parameter(name = "tenant", description = "租户")
  45. })
  46. @GetMapping(value = {"/{shortLink}/{tenant}", "/{shortLink}"})
  47. public ActionResult getShortUrl(@PathVariable("shortLink") String shortLink, @PathVariable(value = "tenant", required = false) String tenant, HttpServletResponse response) throws LoginException, IOException {
  48. if (configValueUtil.isMultiTenancy()) {
  49. if (StringUtil.isNotEmpty(tenant)) {
  50. //切换成租户库
  51. TenantDataSourceUtil.switchTenant(tenant);
  52. } else {
  53. return ActionResult.fail(MsgCode.LOG115.get());
  54. }
  55. }
  56. String link = new String();
  57. ShortLinkEntity entity = shortLinkService.getInfoByLink(shortLink);
  58. String frontDomain = configValueUtil.getFrontDomain();
  59. String appDomain = configValueUtil.getAppDomain();
  60. String realPcLink = entity.getRealPcLink();
  61. String realAppLink = entity.getRealAppLink();
  62. if (!realPcLink.contains("http")) {
  63. realPcLink = frontDomain + realPcLink;
  64. }
  65. if (!realAppLink.contains("http")) {
  66. realAppLink = appDomain + realAppLink;
  67. }
  68. DeviceType type = UserProvider.getDeviceForAgent();
  69. if (entity != null) {
  70. // String encode = "";
  71. String token = authUtil.loginTempUser(entity.getUserId(), tenant);
  72. if (StringUtil.isEmpty(token)) {
  73. return ActionResult.fail(MsgCode.AD104.get());
  74. }
  75. if (entity.getIsUsed() == 1) {
  76. if (entity.getClickNum() < entity.getUnableNum() && entity.getUnableTime().after(DateUtil.getNowDate())) {
  77. if (DeviceType.PC.equals(type)) {
  78. link = realPcLink + "&token=" + token;
  79. entity.setClickNum(entity.getClickNum() + 1);
  80. shortLinkService.updateById(entity);
  81. } else {
  82. link = realAppLink + "&token=" + token;
  83. entity.setClickNum(entity.getClickNum() + 1);
  84. shortLinkService.updateById(entity);
  85. }
  86. } else {
  87. return ActionResult.fail(MsgCode.FA039.get());
  88. }
  89. } else {
  90. if (entity.getUnableTime().after(DateUtil.getNowDate())) {
  91. if (DeviceType.PC.equals(type)) {
  92. link = realPcLink + "&token=" + token;
  93. entity.setClickNum(entity.getClickNum() + 1);
  94. shortLinkService.updateById(entity);
  95. } else {
  96. link = realAppLink + "&token=" + token;
  97. entity.setClickNum(entity.getClickNum() + 1);
  98. shortLinkService.updateById(entity);
  99. }
  100. } else {
  101. return ActionResult.fail(MsgCode.FA039.get());
  102. }
  103. }
  104. } else {
  105. return ActionResult.fail(MsgCode.FA039.get());
  106. }
  107. response.sendRedirect(link);
  108. return ActionResult.success("");
  109. }
  110. }