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} + + + 说明:使用短链接前,可点击测试短链接是否可用 + )} );