139 lines
4.2 KiB
React
139 lines
4.2 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
import { Button, Image, Typography } from '@/components/ui';
|
|
import { Upload } from 'lucide-react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { showError } from '../../helpers';
|
|
import { validateImageFile } from './model';
|
|
import { uploadWebsiteQrCode } from './settingsApi';
|
|
|
|
export default function QrCodeUpload({
|
|
value,
|
|
onChange,
|
|
busy,
|
|
onBusyChange,
|
|
resetVersion,
|
|
invalid,
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const inputRef = useRef(null);
|
|
const requestVersion = useRef(0);
|
|
const [uploading, setUploading] = useState(false);
|
|
|
|
useEffect(() => {
|
|
requestVersion.current += 1;
|
|
setUploading(false);
|
|
onBusyChange(false);
|
|
if (inputRef.current) inputRef.current.value = '';
|
|
return () => {
|
|
requestVersion.current += 1;
|
|
onBusyChange(false);
|
|
};
|
|
}, [resetVersion, onBusyChange]);
|
|
|
|
const upload = async (event) => {
|
|
const file = event.target.files?.[0];
|
|
event.target.value = '';
|
|
if (!file) return;
|
|
const error = validateImageFile(file);
|
|
if (error) {
|
|
showError(t(error));
|
|
return;
|
|
}
|
|
const version = ++requestVersion.current;
|
|
setUploading(true);
|
|
onBusyChange(true);
|
|
try {
|
|
// Verify the content is decodable, rather than trusting the filename or MIME alone.
|
|
try {
|
|
const bitmap = await createImageBitmap(file);
|
|
bitmap.close();
|
|
} catch {
|
|
throw new Error(
|
|
'无法读取二维码图片,请选择有效的 JPG、PNG 或 WebP 图片',
|
|
);
|
|
}
|
|
if (version !== requestVersion.current) return;
|
|
const url = await uploadWebsiteQrCode(file);
|
|
if (version === requestVersion.current) onChange(url);
|
|
} catch (err) {
|
|
if (version === requestVersion.current)
|
|
showError(t(err.message || '二维码上传失败,请选择有效图片后重试'));
|
|
} finally {
|
|
if (version === requestVersion.current) {
|
|
setUploading(false);
|
|
onBusyChange(false);
|
|
}
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div
|
|
className='flex flex-wrap items-center gap-4 rounded-lg border border-dashed p-4'
|
|
style={{
|
|
borderColor: invalid
|
|
? 'var(--ui-color-danger)'
|
|
: 'var(--ui-color-border)',
|
|
background: 'var(--ui-color-fill-0)',
|
|
}}
|
|
>
|
|
{value ? (
|
|
<div className='relative inline-flex'>
|
|
<Image
|
|
src={value}
|
|
width={100}
|
|
height={100}
|
|
style={{ objectFit: 'contain' }}
|
|
alt={t('企业微信二维码')}
|
|
/>
|
|
</div>
|
|
) : (
|
|
<Upload size={28} className='m-4 text-[var(--ui-color-text-2)]' />
|
|
)}
|
|
<div className='flex flex-col gap-2'>
|
|
<input
|
|
ref={inputRef}
|
|
type='file'
|
|
accept='image/jpeg,image/png,image/webp'
|
|
onChange={upload}
|
|
hidden
|
|
/>
|
|
<Button
|
|
icon={<Upload size={14} />}
|
|
loading={uploading}
|
|
disabled={busy || uploading}
|
|
onClick={() => inputRef.current?.click()}
|
|
>
|
|
{t(value ? '重新上传' : '上传二维码')}
|
|
</Button>
|
|
<Typography.Text type='tertiary' size='small'>
|
|
{t('支持 JPG、PNG、WebP,单张不超过 2 MB')}
|
|
</Typography.Text>
|
|
{value && (
|
|
<Typography.Text type='tertiary' size='small'>
|
|
{t('点击图片可放大预览')}
|
|
</Typography.Text>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|