This commit is contained in:
Naccl
2023-07-16 00:45:05 +08:00
parent 237b379a08
commit 755fa6ae52
3 changed files with 43 additions and 15 deletions
@@ -6,9 +6,10 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import top.naccl.dwz.annotation.AccessLimit;
import top.naccl.dwz.entity.GenerateCmd;
import top.naccl.dwz.entity.R;
import top.naccl.dwz.service.UrlService;
import top.naccl.dwz.util.HashUtils;
@@ -27,7 +28,7 @@ public class IndexController {
@Value("${server.host}")
public void setHost(String host) {
this.host = host;
IndexController.host = host;
}
@GetMapping("/")
@@ -38,7 +39,8 @@ public class IndexController {
@AccessLimit(seconds = 10, maxCount = 1, msg = "10秒内只能生成一次短链接")
@PostMapping("/generate")
@ResponseBody
public R generateShortURL(@RequestParam String longURL) {
public R generateShortURL(@RequestBody GenerateCmd cmd) {
String longURL = cmd.getLongURL();
if (UrlUtils.checkURL(longURL)) {
if (!longURL.startsWith("http")) {
longURL = "http://" + longURL;
@@ -0,0 +1,18 @@
package top.naccl.dwz.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Naccl
* @date 2023-07-16
*/
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GenerateCmd {
private String longURL;
}
+20 -12
View File
@@ -29,17 +29,25 @@
$('#generate').click(() => {
let longURL = $('#long').val();
if (longURL) {
$.post("/generate", {
longURL
}, function (res) {
if (res.code === 200) {
$('#short').val(res.data);
} else {
alert(res.msg);
}
}, "json").fail(() => {
alert('异常错误');
});
$.ajax({
url: "/generate",
type: "POST",
contentType: "application/json",
data: JSON.stringify({
longURL: longURL
}),
dataType: "json",
success: function (res) {
if (res.code === 200) {
$('#short').val(res.data);
} else {
alert(res.msg);
}
},
error: function () {
alert('异常错误');
}
});
} else {
alert('请输入原始链接');
}
@@ -54,4 +62,4 @@
});
</script>
</body>
</html>
</html>