|
|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
import { message } from 'antd';
|
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
import { notification } from 'antd';
|
|
|
|
|
|
|
|
|
|
const useShortUrlChange = () => {
|
|
|
|
|
|
|
|
|
|
const Shorturlchange = async (longUrl) => {
|
|
|
|
|
const apiPrefix = {
|
|
|
|
|
"japanhighlights.com": "https://www.japanhighlights.com/index.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()) {
|
|
|
|
|
message.error('不是有效的长链接');
|
|
|
|
|
notification.error({
|
|
|
|
|
message: '错误',
|
|
|
|
|
description: '不是有效的长链接',
|
|
|
|
|
});
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { base64Url, extracted1, extracted2 } = urlBase64(longUrl);
|
|
|
|
|
|
|
|
|
|
if (base64Url && extracted1 && extracted2) {
|
|
|
|
|
const apiUrl = apiPrefix[extracted2];
|
|
|
|
|
if (base64Url && extracted1) {
|
|
|
|
|
const apiUrl = apiPrefix[extracted2] || apiPrefix["chinahighlights.com"];
|
|
|
|
|
|
|
|
|
|
const data = await fetchNowConversationsitems(base64Url, apiUrl);
|
|
|
|
|
if (data) {
|
|
|
|
|
const resultShortUrl = extracted1 + data.isl_link;
|
|
|
|
|
message.success('转换成功!');
|
|
|
|
|
notification.success({
|
|
|
|
|
message: '成功',
|
|
|
|
|
description: '转换成功!',
|
|
|
|
|
});
|
|
|
|
|
return resultShortUrl;
|
|
|
|
|
} else {
|
|
|
|
|
message.error('转换失败,请检查输入的URL是否正确');
|
|
|
|
|
notification.error({
|
|
|
|
|
message: '错误',
|
|
|
|
|
description: '转换失败,请检查输入的URL是否正确',
|
|
|
|
|
});
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
message.error('URL格式不正确,请输入完整的URL');
|
|
|
|
|
notification.error({
|
|
|
|
|
message: '错误',
|
|
|
|
|
description: 'URL格式不正确,请输入完整的URL',
|
|
|
|
|
});
|
|
|
|
|
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;
|