366 lines
12 KiB
React
366 lines
12 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
|
||
*/
|
||
|
||
// 系统设置 →「WeyAi」→「内容存储」(M21):WeyAi 展示内容(作品视频、封面、模板包)
|
||
// 专用的 COS 桶。和系统 COS 桶分开,权限、费用、生命周期都单独管;没配置时
|
||
// 内容功能整体关闭,不会退回系统桶。只对超级管理员显示,后端按 root-only 校验。
|
||
|
||
import React, { useCallback, useEffect, useState } from 'react';
|
||
import {
|
||
Banner,
|
||
Button,
|
||
Card,
|
||
Col,
|
||
Form,
|
||
Input,
|
||
Row,
|
||
Spin,
|
||
Switch,
|
||
Typography,
|
||
} from '@/components/ui';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { API, showError, showSuccess } from '../../../helpers';
|
||
|
||
const { Text } = Typography;
|
||
|
||
const MASKED_PLACEHOLDER = '***ALREADY_SET***';
|
||
const BUCKET_KEY = 'WeyAiContentCosBucket';
|
||
const REGION_KEY = 'WeyAiContentCosRegion';
|
||
const SECRET_ID_KEY = 'WeyAiContentCosSecretId';
|
||
const SECRET_KEY_KEY = 'WeyAiContentCosSecretKey';
|
||
const USE_SYSTEM_KEY = 'WeyAiContentCosUseSystemKey';
|
||
|
||
const KEY_LABELS = {
|
||
[BUCKET_KEY]: '存储桶名称',
|
||
[REGION_KEY]: '地域',
|
||
[SECRET_ID_KEY]: 'SecretId',
|
||
[SECRET_KEY_KEY]: 'SecretKey',
|
||
[USE_SYSTEM_KEY]: '沿用系统 COS 的密钥',
|
||
};
|
||
|
||
const CHECK_LABELS = {
|
||
config: '配置完整',
|
||
bucket: '能访问存储桶',
|
||
write: '能写入并删除对象',
|
||
cors: '已有浏览器直传所需的跨域规则',
|
||
};
|
||
|
||
const EMPTY_VALUES = {
|
||
[BUCKET_KEY]: '',
|
||
[REGION_KEY]: '',
|
||
[SECRET_ID_KEY]: '',
|
||
[USE_SYSTEM_KEY]: false,
|
||
};
|
||
|
||
const SettingsWeyAiContentStorage = () => {
|
||
const { t } = useTranslation();
|
||
const [loading, setLoading] = useState(true);
|
||
const [saving, setSaving] = useState(false);
|
||
const [testing, setTesting] = useState(false);
|
||
const [writingCors, setWritingCors] = useState(false);
|
||
const [values, setValues] = useState(EMPTY_VALUES);
|
||
const [original, setOriginal] = useState(EMPTY_VALUES);
|
||
const [secretKeySet, setSecretKeySet] = useState(false);
|
||
const [secretKeyInput, setSecretKeyInput] = useState('');
|
||
const [checks, setChecks] = useState(null);
|
||
|
||
const load = useCallback(async () => {
|
||
setLoading(true);
|
||
try {
|
||
const res = await API.get('/api/option/');
|
||
if (!res?.data?.success) {
|
||
showError(res?.data?.message || t('加载失败,请重试'));
|
||
return;
|
||
}
|
||
const map = {};
|
||
(res.data.data || []).forEach((item) => {
|
||
map[item.key] = item.value;
|
||
});
|
||
const next = {
|
||
[BUCKET_KEY]: map[BUCKET_KEY] || '',
|
||
[REGION_KEY]: map[REGION_KEY] || '',
|
||
[SECRET_ID_KEY]: map[SECRET_ID_KEY] || '',
|
||
[USE_SYSTEM_KEY]: map[USE_SYSTEM_KEY] === 'true',
|
||
};
|
||
setValues(next);
|
||
setOriginal(next);
|
||
setSecretKeySet(Boolean(map[SECRET_KEY_KEY]));
|
||
setSecretKeyInput('');
|
||
} catch (_e) {
|
||
showError(t('加载失败,请重试'));
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
}, [t]);
|
||
|
||
useEffect(() => {
|
||
load();
|
||
}, [load]);
|
||
|
||
const setField = (key) => (value) =>
|
||
setValues((prev) => ({ ...prev, [key]: value }));
|
||
|
||
const save = async () => {
|
||
const updates = [];
|
||
Object.keys(values).forEach((key) => {
|
||
if (values[key] !== original[key]) {
|
||
const value =
|
||
typeof values[key] === 'boolean'
|
||
? String(values[key])
|
||
: values[key].trim();
|
||
updates.push({ key, value });
|
||
}
|
||
});
|
||
// 密钥只在填写了新值时提交,留空不覆盖已保存的值
|
||
const secret = secretKeyInput.trim();
|
||
if (secret && secret !== MASKED_PLACEHOLDER) {
|
||
updates.push({ key: SECRET_KEY_KEY, value: secret });
|
||
}
|
||
if (updates.length === 0) {
|
||
showSuccess(t('没有需要保存的修改'));
|
||
return;
|
||
}
|
||
setSaving(true);
|
||
try {
|
||
const results = await Promise.all(
|
||
updates.map((item) =>
|
||
API.put('/api/option/', item, { skipErrorHandler: true }).then(
|
||
(res) => ({ key: item.key, res }),
|
||
(err) => ({ key: item.key, err }),
|
||
),
|
||
),
|
||
);
|
||
const failed = results.filter(({ res }) => !res?.data?.success);
|
||
if (failed.length > 0) {
|
||
failed.forEach(({ key, res, err }) => {
|
||
const message =
|
||
res?.data?.message ||
|
||
err?.response?.data?.message ||
|
||
t('保存失败,请重试');
|
||
showError(
|
||
t('{{label}}:{{message}}', {
|
||
label: t(KEY_LABELS[key] || key),
|
||
message,
|
||
}),
|
||
);
|
||
});
|
||
// 已成功的项记为新的原值,改回旧值时才能再次提交
|
||
const saved = results
|
||
.filter(({ res }) => res?.data?.success)
|
||
.map(({ key }) => key);
|
||
setOriginal((prev) => {
|
||
const next = { ...prev };
|
||
saved.forEach((key) => {
|
||
if (key in values) next[key] = values[key];
|
||
});
|
||
return next;
|
||
});
|
||
if (saved.includes(SECRET_KEY_KEY)) {
|
||
setSecretKeySet(true);
|
||
setSecretKeyInput('');
|
||
}
|
||
return;
|
||
}
|
||
showSuccess(t('保存成功'));
|
||
setChecks(null);
|
||
await load();
|
||
} finally {
|
||
setSaving(false);
|
||
}
|
||
};
|
||
|
||
const dirty =
|
||
secretKeyInput.trim() !== '' ||
|
||
Object.keys(values).some((key) => values[key] !== original[key]);
|
||
|
||
const runTest = async () => {
|
||
setTesting(true);
|
||
try {
|
||
const res = await API.post(
|
||
'/api/option/weyai/content_storage/test',
|
||
{},
|
||
{ skipErrorHandler: true },
|
||
);
|
||
if (!res?.data?.success) {
|
||
showError(res?.data?.message || t('测试失败,请重试'));
|
||
return;
|
||
}
|
||
setChecks(res.data.data?.checks || []);
|
||
} catch (err) {
|
||
showError(err?.response?.data?.message || t('测试失败,请重试'));
|
||
} finally {
|
||
setTesting(false);
|
||
}
|
||
};
|
||
|
||
const writeCors = async () => {
|
||
setWritingCors(true);
|
||
try {
|
||
const res = await API.post(
|
||
'/api/option/weyai/content_storage/cors',
|
||
{},
|
||
{ skipErrorHandler: true },
|
||
);
|
||
if (!res?.data?.success) {
|
||
showError(res?.data?.message || t('写入失败,请重试'));
|
||
return;
|
||
}
|
||
showSuccess(t('跨域规则已写入'));
|
||
await runTest();
|
||
} catch (err) {
|
||
showError(err?.response?.data?.message || t('写入失败,请重试'));
|
||
} finally {
|
||
setWritingCors(false);
|
||
}
|
||
};
|
||
|
||
const corsMissing = checks?.some(
|
||
(check) => check.name === 'cors' && !check.ok,
|
||
);
|
||
|
||
return (
|
||
<Card>
|
||
<Form.Section
|
||
component='form'
|
||
onSubmit={(event) => event.preventDefault()}
|
||
autoComplete='off'
|
||
text={t('内容存储')}
|
||
extraText={t(
|
||
'WeyAi 展示内容(作品视频、封面、模板包)专用的腾讯云 COS 存储桶,和系统 COS 桶分开;没有配置时内容功能不可用,不会改用系统桶',
|
||
)}
|
||
>
|
||
<Spin spinning={loading}>
|
||
<Banner
|
||
type='info'
|
||
closeIcon={null}
|
||
className='mb-4'
|
||
description={t(
|
||
'请在腾讯云单独建一个私有读写的存储桶,推荐再建一个只授权给这个桶的子账号密钥。客户端拿到的是有时效的签名地址,不需要公有读',
|
||
)}
|
||
/>
|
||
<Row gutter={16}>
|
||
<Col xs={24} md={12} className='mb-3'>
|
||
<Text className='block mb-1'>{t('存储桶名称')}</Text>
|
||
<Input
|
||
value={values[BUCKET_KEY]}
|
||
onChange={setField(BUCKET_KEY)}
|
||
placeholder='weyai-content-1250000000'
|
||
aria-label={t('存储桶名称')}
|
||
/>
|
||
<Text type='tertiary' size='small'>
|
||
{t('带 APPID 后缀的完整桶名')}
|
||
</Text>
|
||
</Col>
|
||
<Col xs={24} md={12} className='mb-3'>
|
||
<Text className='block mb-1'>{t('地域')}</Text>
|
||
<Input
|
||
value={values[REGION_KEY]}
|
||
onChange={setField(REGION_KEY)}
|
||
placeholder='ap-guangzhou'
|
||
aria-label={t('地域')}
|
||
/>
|
||
</Col>
|
||
</Row>
|
||
<div className='mb-3 flex items-center gap-2'>
|
||
<Switch
|
||
checked={values[USE_SYSTEM_KEY]}
|
||
onChange={setField(USE_SYSTEM_KEY)}
|
||
aria-label={t('沿用系统 COS 的密钥')}
|
||
/>
|
||
<Text>{t('沿用系统 COS 的密钥')}</Text>
|
||
</div>
|
||
<Text type='tertiary' size='small' className='block mb-3'>
|
||
{values[USE_SYSTEM_KEY]
|
||
? t(
|
||
'使用「运营设置」里腾讯云 COS 的 SecretId 和 SecretKey,只需填写桶名和地域;那组密钥要有这个桶的读写权限',
|
||
)
|
||
: t(
|
||
'填写一组有这个桶读写权限的密钥;推荐使用只授权给这个桶的子账号密钥,泄露时影响最小',
|
||
)}
|
||
</Text>
|
||
{!values[USE_SYSTEM_KEY] && (
|
||
<Row gutter={16}>
|
||
<Col xs={24} md={12} className='mb-3'>
|
||
<Text className='block mb-1'>SecretId</Text>
|
||
<Input
|
||
value={values[SECRET_ID_KEY]}
|
||
onChange={setField(SECRET_ID_KEY)}
|
||
aria-label='SecretId'
|
||
/>
|
||
</Col>
|
||
<Col xs={24} md={12} className='mb-3'>
|
||
<Text className='block mb-1'>SecretKey</Text>
|
||
<Input
|
||
mode='password'
|
||
autoComplete='new-password'
|
||
value={secretKeyInput}
|
||
onChange={setSecretKeyInput}
|
||
aria-label='SecretKey'
|
||
placeholder={
|
||
secretKeySet
|
||
? t('已设置,留空则不修改')
|
||
: t('敏感信息不会发送到前端显示')
|
||
}
|
||
/>
|
||
</Col>
|
||
</Row>
|
||
)}
|
||
<div className='flex flex-wrap gap-2 mt-2'>
|
||
<Button type='primary' loading={saving} onClick={save}>
|
||
{t('保存')}
|
||
</Button>
|
||
<Button loading={testing} disabled={dirty} onClick={runTest}>
|
||
{t('测试连接')}
|
||
</Button>
|
||
{corsMissing && (
|
||
<Button loading={writingCors} onClick={writeCors}>
|
||
{t('写入跨域规则')}
|
||
</Button>
|
||
)}
|
||
</div>
|
||
{dirty && (
|
||
<Text type='tertiary' size='small' className='block mt-2'>
|
||
{t('测试的是已保存的配置,请先保存再测试')}
|
||
</Text>
|
||
)}
|
||
{checks && (
|
||
<div className='mt-4'>
|
||
{checks.map((check) => (
|
||
<div key={check.name} className='mb-1'>
|
||
<Text type={check.ok ? 'success' : 'danger'}>
|
||
{check.ok ? '✓ ' : '✗ '}
|
||
{t(CHECK_LABELS[check.name] || check.name)}
|
||
</Text>
|
||
{!check.ok && check.message && (
|
||
<Text type='tertiary' size='small' className='ml-2'>
|
||
{check.message}
|
||
</Text>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</Spin>
|
||
</Form.Section>
|
||
</Card>
|
||
);
|
||
};
|
||
|
||
export default SettingsWeyAiContentStorage;
|