mirror of
https://github.com/nitezs/sub2clash.git
synced 2025-04-03 04:03:44 +08:00
add:生成短链支持自定义id
This commit is contained in:
parent
b57e4cf49f
commit
cc0b73d7a4
@ -32,10 +32,27 @@ func GenerateLinkHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
hash, err := generateUniqueHash()
|
||||
if err != nil {
|
||||
respondWithError(c, http.StatusInternalServerError, "生成短链接失败")
|
||||
return
|
||||
var hash string
|
||||
var err error
|
||||
|
||||
if params.CustomID != "" {
|
||||
// 检查自定义ID是否已存在
|
||||
exists, err := database.CheckShortLinkHashExists(params.CustomID)
|
||||
if err != nil {
|
||||
respondWithError(c, http.StatusInternalServerError, "数据库错误")
|
||||
return
|
||||
}
|
||||
if exists {
|
||||
respondWithError(c, http.StatusBadRequest, "自定义ID已存在")
|
||||
return
|
||||
}
|
||||
hash = params.CustomID
|
||||
} else {
|
||||
hash, err = generateUniqueHash()
|
||||
if err != nil {
|
||||
respondWithError(c, http.StatusInternalServerError, "生成短链接失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
shortLink := model.ShortLink{
|
||||
@ -74,11 +91,27 @@ func UpdateLinkHandler(c *gin.Context) {
|
||||
respondWithError(c, http.StatusBadRequest, "参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 先获取原有的短链接
|
||||
existingLink, err := database.FindShortLinkByHash(params.Hash)
|
||||
if err != nil {
|
||||
respondWithError(c, http.StatusNotFound, "未找到短链接")
|
||||
return
|
||||
}
|
||||
|
||||
// 验证密码
|
||||
if existingLink.Password != params.Password {
|
||||
respondWithError(c, http.StatusUnauthorized, "密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 更新URL,但保持原密码不变
|
||||
shortLink := model.ShortLink{
|
||||
Hash: params.Hash,
|
||||
Url: params.Url,
|
||||
Password: params.Password,
|
||||
Password: existingLink.Password, // 保持原密码不变
|
||||
}
|
||||
|
||||
if err := database.SaveShortLink(&shortLink); err != nil {
|
||||
respondWithError(c, http.StatusInternalServerError, "数据库错误")
|
||||
return
|
||||
|
@ -152,19 +152,22 @@
|
||||
复制链接
|
||||
</button>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<input class="form-control" id="apiShortLink" type="text" placeholder="链接" />
|
||||
<input class="form-control" id="password" type="text" placeholder="密码" />
|
||||
<div class="input-group mb-2">
|
||||
<input class="form-control" id="customId" type="text" placeholder="自定义ID(可选)" />
|
||||
<input class="form-control" id="password" type="text" placeholder="密码(可选)" />
|
||||
<button class="btn btn-primary" onclick="generateShortLink()" type="button">
|
||||
生成短链
|
||||
</button>
|
||||
<button class="btn btn-primary" onclick="updateShortLink()" type="button">
|
||||
更新短链
|
||||
</button>
|
||||
<button class="btn btn-primary" onclick="copyToClipboard('apiShortLink',this)" type="button">
|
||||
复制短链
|
||||
</button>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<input class="form-control bg-light" id="apiShortLink" type="text" placeholder="短链接" readonly style="cursor: not-allowed;" />
|
||||
<button class="btn btn-primary" onclick="updateShortLink()" type="button">
|
||||
更新短链
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- footer-->
|
||||
|
@ -190,6 +190,7 @@ async function parseInputURL() {
|
||||
url = new URL(window.location.href + response.data);
|
||||
document.querySelector("#apiShortLink").value = inputURL;
|
||||
document.querySelector("#password").value = password;
|
||||
document.querySelector("#customId").value = hash;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
alert("获取短链失败,请检查密码!");
|
||||
@ -433,16 +434,27 @@ function generateURL() {
|
||||
function generateShortLink() {
|
||||
const apiShortLink = document.getElementById("apiShortLink");
|
||||
const password = document.getElementById("password");
|
||||
const customId = document.getElementById("customId");
|
||||
let uri = generateURI();
|
||||
if (uri === "") {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果用户没有输入自定义ID和密码,自动生成
|
||||
if (!customId.value && !password.value) {
|
||||
const randomId = Math.random().toString(36).substring(2, 8);
|
||||
const randomPassword = Math.random().toString(36).substring(2, 8);
|
||||
customId.value = randomId;
|
||||
password.value = randomPassword;
|
||||
}
|
||||
|
||||
axios
|
||||
.post(
|
||||
"./short",
|
||||
{
|
||||
url: uri,
|
||||
password: password.value.trim(),
|
||||
customId: customId.value.trim()
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
@ -455,7 +467,11 @@ function generateShortLink() {
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
alert("生成短链失败,请重试!");
|
||||
if (error.response && error.response.data) {
|
||||
alert(error.response.data);
|
||||
} else {
|
||||
alert("生成短链失败,请重试!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -468,7 +484,7 @@ function updateShortLink() {
|
||||
hash = u.pathname.substring(u.pathname.lastIndexOf("/s/") + 3);
|
||||
}
|
||||
if (password.value.trim() === "") {
|
||||
alert("请输入密码!");
|
||||
alert("请输入原密码进行验证!");
|
||||
return;
|
||||
}
|
||||
let uri = generateURI();
|
||||
@ -494,7 +510,13 @@ function updateShortLink() {
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
alert(error.response.data);
|
||||
if (error.response && error.response.status === 401) {
|
||||
alert("密码错误,请输入正确的原密码!");
|
||||
} else if (error.response && error.response.data) {
|
||||
alert(error.response.data);
|
||||
} else {
|
||||
alert("更新短链失败,请重试!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package validator
|
||||
type ShortLinkGenValidator struct {
|
||||
Url string `form:"url" binding:"required"`
|
||||
Password string `form:"password"`
|
||||
CustomID string `form:"customId"`
|
||||
}
|
||||
|
||||
type GetUrlValidator struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user