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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; 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 org.springframework.web.bind.annotation.ResponseBody;
import top.naccl.dwz.annotation.AccessLimit; import top.naccl.dwz.annotation.AccessLimit;
import top.naccl.dwz.entity.GenerateCmd;
import top.naccl.dwz.entity.R; import top.naccl.dwz.entity.R;
import top.naccl.dwz.service.UrlService; import top.naccl.dwz.service.UrlService;
import top.naccl.dwz.util.HashUtils; import top.naccl.dwz.util.HashUtils;
@@ -27,7 +28,7 @@ public class IndexController {
@Value("${server.host}") @Value("${server.host}")
public void setHost(String host) { public void setHost(String host) {
this.host = host; IndexController.host = host;
} }
@GetMapping("/") @GetMapping("/")
@@ -38,7 +39,8 @@ public class IndexController {
@AccessLimit(seconds = 10, maxCount = 1, msg = "10秒内只能生成一次短链接") @AccessLimit(seconds = 10, maxCount = 1, msg = "10秒内只能生成一次短链接")
@PostMapping("/generate") @PostMapping("/generate")
@ResponseBody @ResponseBody
public R generateShortURL(@RequestParam String longURL) { public R generateShortURL(@RequestBody GenerateCmd cmd) {
String longURL = cmd.getLongURL();
if (UrlUtils.checkURL(longURL)) { if (UrlUtils.checkURL(longURL)) {
if (!longURL.startsWith("http")) { if (!longURL.startsWith("http")) {
longURL = "http://" + longURL; 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(() => { $('#generate').click(() => {
let longURL = $('#long').val(); let longURL = $('#long').val();
if (longURL) { if (longURL) {
$.post("/generate", { $.ajax({
longURL url: "/generate",
}, function (res) { type: "POST",
if (res.code === 200) { contentType: "application/json",
$('#short').val(res.data); data: JSON.stringify({
} else { longURL: longURL
alert(res.msg); }),
} dataType: "json",
}, "json").fail(() => { success: function (res) {
alert('异常错误'); if (res.code === 200) {
}); $('#short').val(res.data);
} else {
alert(res.msg);
}
},
error: function () {
alert('异常错误');
}
});
} else { } else {
alert('请输入原始链接'); alert('请输入原始链接');
} }
@@ -54,4 +62,4 @@
}); });
</script> </script>
</body> </body>
</html> </html>