/* 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 . 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 ( event.preventDefault()} autoComplete='off' text={t('内容存储')} extraText={t( 'WeyAi 展示内容(作品视频、封面、模板包)专用的腾讯云 COS 存储桶,和系统 COS 桶分开;没有配置时内容功能不可用,不会改用系统桶', )} > {t('存储桶名称')} {t('带 APPID 后缀的完整桶名')} {t('地域')}
{t('沿用系统 COS 的密钥')}
{values[USE_SYSTEM_KEY] ? t( '使用「运营设置」里腾讯云 COS 的 SecretId 和 SecretKey,只需填写桶名和地域;那组密钥要有这个桶的读写权限', ) : t( '填写一组有这个桶读写权限的密钥;推荐使用只授权给这个桶的子账号密钥,泄露时影响最小', )} {!values[USE_SYSTEM_KEY] && ( SecretId SecretKey )}
{corsMissing && ( )}
{dirty && ( {t('测试的是已保存的配置,请先保存再测试')} )} {checks && (
{checks.map((check) => (
{check.ok ? '✓ ' : '✗ '} {t(CHECK_LABELS[check.name] || check.name)} {!check.ok && check.message && ( {check.message} )}
))}
)}
); }; export default SettingsWeyAiContentStorage;