继续改进短链接转换2

main
ybc 3 months ago
parent cb09c9d819
commit cf98a38559

@ -1,20 +1,9 @@
import { useCallback } from 'react';
import { message } from 'antd';
import useConversationStore from '@/stores/ConversationStore';
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 setGlobalNotify = useConversationStore((state) => state.setGlobalNotify);
const apiPrefix = {
"japanhighlights.com": "https://www.japanhighlights.com/index.php",
"chinahighlights.com": "https://www.chinahighlights.com/guide-use.php",
@ -53,7 +42,12 @@ const useShortUrlChange = () => {
return { base64Url, extracted2 };
} catch (error) {
error('转换失败请检查输入的URL是否正确');
setGlobalNotify([{
key: Date.now().toString(),
title: '错误',
content: '转换失败请检查输入的URL是否正确',
type: 'error'
}]);
console.error('URL转换错误:', error);
return { base64Url: '', extracted2: '' };
}
@ -61,7 +55,12 @@ const useShortUrlChange = () => {
const convertUrl = useCallback(async (longUrl) => {
if (!longUrl.trim()) {
error('不是有效的长链接');
setGlobalNotify([{
key: Date.now().toString(),
title: '错误',
content: '不是有效的长链接',
type: 'error'
}]);
return null;
}
@ -74,19 +73,34 @@ const useShortUrlChange = () => {
const data = await fetchNowConversationsitems(base64Url, apiUrl);
if (data) {
const resultShortUrl = extracted1 + data.isl_link;
success('转换成功!');
setGlobalNotify([{
key: Date.now().toString(),
title: '成功',
content: '转换成功!',
type: 'success'
}]);
return resultShortUrl;
} else {
error('转换失败请检查输入的URL是否正确');
setGlobalNotify([{
key: Date.now().toString(),
title: '错误',
content: '转换失败请检查输入的URL是否正确',
type: 'error'
}]);
return null;
}
} else {
error('URL格式不正确请输入完整的URL');
setGlobalNotify([{
key: Date.now().toString(),
title: '错误',
content: 'URL格式不正确请输入完整的URL',
type: 'error'
}]);
return null;
}
}, [messageApi]);
}, [setGlobalNotify]);
return { convertUrl, contextHolder };
return { convertUrl };
};
export default useShortUrlChange;

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

Loading…
Cancel
Save