update 优化生成短链接租户域名获取

This commit is contained in:
AprilWind 2024-04-12 14:16:28 +08:00
parent 1bc60fbc4b
commit bf6102cf64
3 changed files with 16 additions and 4 deletions

View File

@ -195,7 +195,8 @@ sms:
sdk-app-id: 您的sdkAppId
shortlink:
enabled: false
enabled: true
api: /dev-api
address: http://localhost/dev-api/resource/short/link?shortUrl=
errorAddress: http://localhost/error

View File

@ -17,6 +17,11 @@ public class ShortLinkProperties {
*/
private Boolean enabled = false;
/**
* 访问前缀 开启时启用可不填写能访问到后端即可
*/
private String api;
/**
* 短链接主机名或域名
*/

View File

@ -14,6 +14,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 短链接 控制层
*/
@ -44,13 +47,16 @@ public class SysShortLinkController {
* @return 完整的短链接
*/
@PostMapping("/shorturl")
public R<String> generateShortUrl(HttpServletRequest request, @NotBlank(message = "参数不能为空") String longUrl) {
public R<String> generateShortUrl(HttpServletRequest request, @NotBlank(message = "参数不能为空") String longUrl) throws MalformedURLException {
String shorturl;
if (Boolean.FALSE.equals(shortLinkProperties.getEnabled())) {
shorturl = ShortLinkUtils.generateShortUrl(shortLinkProperties.getAddress(), longUrl, ValidityType.THREE_DAYS);
} else {
StringBuffer requestURL = request.getRequestURL();
String address = requestURL.substring(0, requestURL.indexOf("shorturl")) + "link?shortUrl=";
String requestUrl = request.getRequestURL().toString();
String host = new URL(requestUrl).getHost();
String address = requestUrl
.replace(host, host + shortLinkProperties.getApi())
.replace("shorturl", "link?shortUrl=");
shorturl = ShortLinkUtils.generateShortUrl(address, longUrl, ValidityType.THREE_DAYS);
}
return R.ok(shorturl);