继续改进短链接转换

main
ybc 3 months ago
parent 02ef6b4d86
commit a7509400f6

@ -1,8 +1,20 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import { notification } from 'antd'; import { message } from 'antd';
const useShortUrlChange = () => { const useShortUrlChange = () => {
const [messageApi, contextHolder] = message.useMessage();
const success = (content) => {
messageApi.open({
type: 'success',
content: content,
});
};
const error = (content) => {
messageApi.open({
type: 'error',
content: content,
});
};
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",
@ -33,63 +45,48 @@ const useShortUrlChange = () => {
const urlBase64 = (longUrl) => { const urlBase64 = (longUrl) => {
try { try {
const extracted1 = longUrl.match(/^https?:\/\/[^\/]*/)?.[0] || '';
const extracted2 = longUrl.match(/https:\/\/www\.([^\/]+)/)?.[1] || ''; const extracted2 = longUrl.match(/https:\/\/www\.([^\/]+)/)?.[1] || '';
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const utf8Bytes = encoder.encode(longUrl); const utf8Bytes = encoder.encode(longUrl);
const base64Url = btoa(String.fromCharCode(...utf8Bytes)); const base64Url = btoa(String.fromCharCode(...utf8Bytes));
return { base64Url, extracted1, extracted2 }; return { base64Url, extracted2 };
} catch (error) { } catch (error) {
notification.error({ error('转换失败请检查输入的URL是否正确');
message: '错误',
description: '转换失败请检查输入的URL是否正确',
});
console.error('URL转换错误:', error); console.error('URL转换错误:', error);
return { base64Url: '', extracted1: '', extracted2: '' }; return { base64Url: '', extracted2: '' };
} }
}; };
const convertUrl = useCallback(async (longUrl) => { const convertUrl = useCallback(async (longUrl) => {
if (!longUrl.trim()) { if (!longUrl.trim()) {
notification.error({ error('不是有效的长链接');
message: '错误',
description: '不是有效的长链接',
});
return null; return null;
} }
const { base64Url, extracted1, extracted2 } = urlBase64(longUrl); const { base64Url, extracted2 } = urlBase64(longUrl);
if (base64Url && extracted1) { if (base64Url) {
const apiUrl = apiPrefix[extracted2] || apiPrefix["chinahighlights.com"]; const apiUrl = apiPrefix[extracted2] || apiPrefix["chinahighlights.com"];
const extracted1 = apiUrl.match(/^https?:\/\/[^\/]*\.com/)?.[0] || '';
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;
notification.success({ success('转换成功!');
message: '成功',
description: '转换成功!',
});
return resultShortUrl; return resultShortUrl;
} else { } else {
notification.error({ error('转换失败请检查输入的URL是否正确');
message: '错误',
description: '转换失败请检查输入的URL是否正确',
});
return null; return null;
} }
} else { } else {
notification.error({ error('URL格式不正确请输入完整的URL');
message: '错误',
description: 'URL格式不正确请输入完整的URL',
});
return null; return null;
} }
}, []); }, [messageApi]);
return { convertUrl }; return { convertUrl, contextHolder };
}; };
export default useShortUrlChange; export default useShortUrlChange;

@ -8,7 +8,7 @@ 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() const { convertUrl, contextHolder } = useShortUrlChange()
const handleConvert = async () => { const handleConvert = async () => {
const result = await convertUrl(longUrl); const result = await convertUrl(longUrl);
@ -18,6 +18,8 @@ function ShorturlConversion() {
}; };
return ( return (
<>
{contextHolder}
<Space direction='vertical' size='large' className='w-full'> <Space direction='vertical' size='large' className='w-full'>
<div> <div>
<Text strong style={{ fontSize: '18px' }}>长链接</Text> <Text strong style={{ fontSize: '18px' }}>长链接</Text>
@ -51,6 +53,7 @@ function ShorturlConversion() {
</Paragraph> </Paragraph>
</div>)} </div>)}
</Space> </Space>
</>
); );
} }

Loading…
Cancel
Save