From 4746f8c4ebd98cb2a23adaee5510168adbab0a32 Mon Sep 17 00:00:00 2001
From: ybc <2483488988@qq.com>
Date: Fri, 23 Jan 2026 15:09:29 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=9F=AD=E9=93=BE=E6=8E=A5?=
=?UTF-8?q?=E8=BD=AC=E6=8D=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../useShorturlchange.js} | 32 +++++++++++++++++--
src/views/accounts/ShorturlConversion.jsx | 15 +++++++--
2 files changed, 42 insertions(+), 5 deletions(-)
rename src/{components/Shorturlchange.jsx => hooks/useShorturlchange.js} (76%)
diff --git a/src/components/Shorturlchange.jsx b/src/hooks/useShorturlchange.js
similarity index 76%
rename from src/components/Shorturlchange.jsx
rename to src/hooks/useShorturlchange.js
index b2ac13d..2d01cc3 100644
--- a/src/components/Shorturlchange.jsx
+++ b/src/hooks/useShorturlchange.js
@@ -15,7 +15,7 @@ const useShortUrlChange = () => {
const fetchNowConversationsitems = async (base64Url, apiUrl) => {
try {
const formData = new FormData();
- formData.append('url', base64Url);
+ formData.append(' url', base64Url);
formData.append('type', 'info');
const response = await fetch(`${apiUrl}/apps/short_link/index/create`, {
method: 'POST',
@@ -52,9 +52,35 @@ const useShortUrlChange = () => {
return { base64Url: '', extracted2: '' };
}
};
+ const normalizeUrl = (longUrl) => {
+ const trimmed = longUrl.trim();
+ // 排除纯数字、纯字母、纯文字(中文、日文等)以及它们的组合
+ if (/^[\p{L}\p{N}]+$/u.test(trimmed)) {
+ return '';
+ }
+ if (!trimmed) return '';
+ // 1. 已是 http/https 开头,直接验证合法性
+ if (/^https?:\/\//i.test(trimmed)) {
+ try {
+ const urlObj = new URL(trimmed);
+ return urlObj.href;
+ } catch (e) {
+ return '';
+ }
+ }
+ // 2. www 开头/纯域名/IP+端口,补 https:// 后验证
+ const fullUrl = `https://${trimmed}`;
+ try {
+ const urlObj = new URL(fullUrl);
+ return urlObj.href;
+ } catch (e) {
+ return '';
+ }
+ };
const convertUrl = useCallback(async (longUrl) => {
- if (!longUrl.trim()) {
+ const normalizedUrl = normalizeUrl(longUrl);
+ if (!normalizedUrl) {
setGlobalNotify([{
key: Date.now().toString(),
title: '错误',
@@ -64,7 +90,7 @@ const useShortUrlChange = () => {
return null;
}
- const { base64Url, extracted2 } = urlBase64(longUrl);
+ const { base64Url, extracted2 } = urlBase64(normalizedUrl);
if (base64Url) {
const apiUrl = apiPrefix[extracted2] || apiPrefix["chinahighlights.com"];
diff --git a/src/views/accounts/ShorturlConversion.jsx b/src/views/accounts/ShorturlConversion.jsx
index 7798fa9..d2aa6fd 100644
--- a/src/views/accounts/ShorturlConversion.jsx
+++ b/src/views/accounts/ShorturlConversion.jsx
@@ -1,6 +1,6 @@
import { Input, Button, Space, Typography } from 'antd'
import { LinkOutlined } from '@ant-design/icons'
-import useShortUrlChange from '@/components/Shorturlchange'
+import useShortUrlChange from '@/hooks/useShorturlchange'
import { useState } from 'react'
const { Title, Text, Paragraph } = Typography
@@ -25,6 +25,7 @@ function ShorturlConversion() {
placeholder="输入需要转换的长链接"
value={longUrl}
onChange={(e) => setLongUrl(e.target.value)}
+ onPressEnter={handleConvert}
prefix={}
size="large"
/>
@@ -47,8 +48,18 @@ function ShorturlConversion() {
} : false}
style={{ fontSize: '17px' }}
>
- {shortUrl}
+
+ {shortUrl}
+
+
+ 说明:使用短链接前,可点击测试短链接是否可用
+
)}
);