改进短链接转换

main
ybc 3 months ago
parent 0c43d84631
commit e947c78a73

@ -1,6 +1,8 @@
import { message } from 'antd'; import { useCallback } from 'react';
import { notification } from 'antd';
const Shorturlchange = async (longUrl) => { const useShortUrlChange = () => {
const apiPrefix = { const apiPrefix = {
"japanhighlights.com": "https://www.japanhighlights.com/index.php", "japanhighlights.com": "https://www.japanhighlights.com/index.php",
"chinahighlights.com": "https://www.chinahighlights.com/guide-use.php", "chinahighlights.com": "https://www.chinahighlights.com/guide-use.php",
@ -29,50 +31,65 @@ const Shorturlchange = async (longUrl) => {
} }
}; };
const urlConversion = async (longUrl) => { const urlBase64 = (longUrl) => {
try {
const extracted1 = longUrl.match(/^https?:\/\/[^\/]*/)?.[0] || '';
const extracted2 = longUrl.match(/https:\/\/www\.([^\/]+)/)?.[1] || '';
const encoder = new TextEncoder();
const utf8Bytes = encoder.encode(longUrl);
const base64Url = btoa(String.fromCharCode(...utf8Bytes));
return { base64Url, extracted1, extracted2 };
} catch (error) {
notification.error({
message: '错误',
description: '转换失败请检查输入的URL是否正确',
});
console.error('URL转换错误:', error);
return { base64Url: '', extracted1: '', extracted2: '' };
}
};
const convertUrl = useCallback(async (longUrl) => {
if (!longUrl.trim()) { if (!longUrl.trim()) {
message.error('不是有效的长链接'); notification.error({
message: '错误',
description: '不是有效的长链接',
});
return null; return null;
} }
const { base64Url, extracted1, extracted2 } = urlBase64(longUrl); const { base64Url, extracted1, extracted2 } = urlBase64(longUrl);
if (base64Url && extracted1 && extracted2) { if (base64Url && extracted1) {
const apiUrl = apiPrefix[extracted2]; const apiUrl = apiPrefix[extracted2] || apiPrefix["chinahighlights.com"];
const data = await fetchNowConversationsitems(base64Url, apiUrl); const data = await fetchNowConversationsitems(base64Url, apiUrl);
if (data) { if (data) {
const resultShortUrl = extracted1 + data.isl_link; const resultShortUrl = extracted1 + data.isl_link;
message.success('转换成功!'); notification.success({
message: '成功',
description: '转换成功!',
});
return resultShortUrl; return resultShortUrl;
} else { } else {
message.error('转换失败请检查输入的URL是否正确'); notification.error({
message: '错误',
description: '转换失败请检查输入的URL是否正确',
});
return null; return null;
} }
} else { } else {
message.error('URL格式不正确请输入完整的URL'); notification.error({
message: '错误',
description: 'URL格式不正确请输入完整的URL',
});
return null; return null;
} }
}; }, []);
const urlBase64 = (longUrl) => {
try {
const extracted1 = longUrl.match(/^.*?com/)?.[0] || '';
const extracted2 = longUrl.match(/https:\/\/www\.([^\/]+)/)?.[1] || '';
const encoder = new TextEncoder();
const utf8Bytes = encoder.encode(longUrl);
const base64Url = btoa(String.fromCharCode(...utf8Bytes));
return { base64Url, extracted1, extracted2 };
} catch (error) {
message.error('转换失败请检查输入的URL是否正确');
console.error('URL转换错误:', error);
return { base64Url: '', extracted1: '', extracted2: '' };
}
}
return await urlConversion(longUrl); return { convertUrl };
} };
export default Shorturlchange; export default useShortUrlChange;

@ -1,40 +1,17 @@
import { Input, Button, Space, message, Typography } from 'antd' import { Input, Button, Space, Typography } from 'antd'
import { CopyOutlined, LinkOutlined } from '@ant-design/icons' import { LinkOutlined } from '@ant-design/icons'
import Shorturlchange from '@/components/Shorturlchange' import useShortUrlChange from '@/components/Shorturlchange'
import { useState } from 'react' import { useState } from 'react'
const { Title, Text } = Typography const { Title, Text, Paragraph } = Typography
function ShorturlConversion() { function ShorturlConversion() {
const [longUrl, setLongUrl] = useState('') const [longUrl, setLongUrl] = useState('')
const [shortUrl, setShortUrl] = useState('') const [shortUrl, setShortUrl] = useState('')
const { convertUrl } = useShortUrlChange()
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
//
textarea.style.position = 'fixed';
textarea.style.opacity = 0;
document.body.appendChild(textarea);
//
textarea.select();
try {
const successful = document.execCommand('copy');
if (successful) {
message.success('已复制');
} else {
throw new Error('复制失败');
}
} catch (err) {
message.error('复制失败,请手动复制');
} finally {
//
document.body.removeChild(textarea);
}
}
const handleConvert = async () => { const handleConvert = async () => {
const result = await Shorturlchange(longUrl); const result = await convertUrl(longUrl);
if (result) { if (result) {
setShortUrl(result); setShortUrl(result);
} }
@ -43,9 +20,9 @@ function ShorturlConversion() {
return ( return (
<Space direction='vertical' size='large' className='w-full'> <Space direction='vertical' size='large' className='w-full'>
<div> <div>
<Text strong>长链接</Text> <Text strong style={{ fontSize: '18px' }}>长链接</Text>
<Input <Input
placeholder="输入长链接" placeholder="输入需要转换的长链接"
value={longUrl} value={longUrl}
onChange={(e) => setLongUrl(e.target.value)} onChange={(e) => setLongUrl(e.target.value)}
prefix={<LinkOutlined />} prefix={<LinkOutlined />}
@ -54,30 +31,25 @@ function ShorturlConversion() {
</div> </div>
<Button <Button
type='primary' type='primary'
size='large' size='medium'
onClick={handleConvert} onClick={handleConvert}
icon={<LinkOutlined />} icon={<LinkOutlined />}
> >
转换 转换
</Button> </Button>
<div> {shortUrl && (
<Text strong>短链接</Text> <div>
<Input <Text strong style={{ fontSize: '18px' }}>转换后的短链接</Text>
placeholder="转换后的短链接" <Paragraph
value={shortUrl} copyable={shortUrl ? {
readOnly text: shortUrl,
suffix={ tooltips: ['点击复制', '已复制!'],
shortUrl && ( } : false}
<Button style={{ fontSize: '17px' }}
type="text" >
icon={<CopyOutlined />} {shortUrl}
onClick={() => copyToClipboard(shortUrl)} </Paragraph>
/> </div>)}
)
}
size="large"
/>
</div>
</Space> </Space>
); );
} }

Loading…
Cancel
Save