2025-04-02 21:59:19 +08:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2025-04-02 16:14:30 +08:00
|
|
|
import { Input, Button, message } from 'antd';
|
|
|
|
|
import styles from './ShareCodeValidator.module.css';
|
|
|
|
|
import {env} from '../../../env'
|
2025-04-02 21:59:19 +08:00
|
|
|
import { api } from '@nice/client';
|
2025-04-02 16:14:30 +08:00
|
|
|
interface ShareCodeValidatorProps {
|
|
|
|
|
onValidSuccess: (fileId: string, fileName: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ShareCodeValidator: React.FC<ShareCodeValidatorProps> = ({
|
|
|
|
|
onValidSuccess,
|
|
|
|
|
}) => {
|
|
|
|
|
const [code, setCode] = useState('');
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2025-04-02 21:59:19 +08:00
|
|
|
const { data: result, isLoading } = api.shareCode.getFileByShareCode.useQuery(
|
|
|
|
|
{ code: code.trim() },
|
|
|
|
|
{
|
|
|
|
|
enabled: !!code.trim()
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-04-02 16:14:30 +08:00
|
|
|
|
2025-04-02 21:59:19 +08:00
|
|
|
const validateCode = useCallback(() => {
|
2025-04-03 13:27:58 +08:00
|
|
|
|
2025-04-02 16:14:30 +08:00
|
|
|
if (!code.trim()) {
|
2025-04-03 13:27:58 +08:00
|
|
|
message.warning('请输入正确的分享码');
|
2025-04-02 16:14:30 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
2025-04-02 21:59:19 +08:00
|
|
|
console.log('验证分享码返回数据:', result);
|
2025-04-03 13:27:58 +08:00
|
|
|
onValidSuccess(result.url, result.fileName);
|
|
|
|
|
message.success(`验证成功,文件名:${result.fileName},请等待下载...`);
|
2025-04-02 16:14:30 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('验证分享码失败:', error);
|
|
|
|
|
message.error('分享码无效或已过期');
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
2025-04-03 13:27:58 +08:00
|
|
|
},[result,code, onValidSuccess])
|
2025-04-02 16:14:30 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={styles.container}>
|
|
|
|
|
<Input
|
|
|
|
|
className={styles.input}
|
|
|
|
|
value={code}
|
|
|
|
|
onChange={(e) => setCode(e.target.value.toUpperCase())}
|
|
|
|
|
placeholder="请输入分享码"
|
|
|
|
|
maxLength={8}
|
|
|
|
|
onPressEnter={validateCode}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="primary"
|
|
|
|
|
onClick={validateCode}
|
|
|
|
|
loading={loading}
|
|
|
|
|
disabled={!code.trim()}
|
|
|
|
|
>
|
|
|
|
|
验证并下载
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|