67969a041d
补齐页面缺译,统一动态文案的整句插值方式。
632 lines
21 KiB
React
632 lines
21 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 { useEffectEvent } from '../../hooks/common/useEffectEvent';
|
||
import React, { useContext, useEffect, useRef, useState } from 'react';
|
||
import { Button, Col, Form, Row, Modal, Space, Card } from '@/components/ui';
|
||
import { API, showError, showSuccess, timestamp2string } from '../../helpers';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { StatusContext } from '../../context/Status';
|
||
import { Text } from '@/components/ui';
|
||
import './other-setting.css';
|
||
|
||
const LEGAL_USER_AGREEMENT_KEY = 'legal.user_agreement';
|
||
const LEGAL_PRIVACY_POLICY_KEY = 'legal.privacy_policy';
|
||
|
||
const OtherSetting = () => {
|
||
const { t } = useTranslation();
|
||
let [inputs, setInputs] = useState({
|
||
Notice: '',
|
||
[LEGAL_USER_AGREEMENT_KEY]: '',
|
||
[LEGAL_PRIVACY_POLICY_KEY]: '',
|
||
SystemName: '',
|
||
Logo: '',
|
||
AuthBackgroundTheme: 'ocean',
|
||
Footer: '',
|
||
About: '',
|
||
HomePageContent: '',
|
||
ConsoleWelcomeTitle: '',
|
||
OfficialWebsite: '',
|
||
});
|
||
let [, setLoading] = useState(false);
|
||
const [showUpdateModal, setShowUpdateModal] = useState(false);
|
||
const [statusState] = useContext(StatusContext);
|
||
const [updateData] = useState({
|
||
tag_name: '',
|
||
content: '',
|
||
});
|
||
|
||
const updateOption = async (key, value) => {
|
||
setLoading(true);
|
||
const res = await API.put('/api/option/', {
|
||
key,
|
||
value,
|
||
});
|
||
const { success, message } = res.data;
|
||
if (success) {
|
||
setInputs((inputs) => ({ ...inputs, [key]: value }));
|
||
} else {
|
||
showError(message);
|
||
}
|
||
setLoading(false);
|
||
return success;
|
||
};
|
||
|
||
const [loadingInput, setLoadingInput] = useState({
|
||
Notice: false,
|
||
[LEGAL_USER_AGREEMENT_KEY]: false,
|
||
[LEGAL_PRIVACY_POLICY_KEY]: false,
|
||
SystemName: false,
|
||
Logo: false,
|
||
AuthBackgroundTheme: false,
|
||
HomePageContent: false,
|
||
About: false,
|
||
Footer: false,
|
||
CheckUpdate: false,
|
||
ConsoleWelcome: false,
|
||
OfficialWebsite: false,
|
||
});
|
||
const handleInputChange = async (value, e) => {
|
||
const name = e.target.id;
|
||
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
||
};
|
||
|
||
// 通用设置
|
||
const formAPISettingGeneral = useRef();
|
||
// 通用设置 - Notice
|
||
const submitNotice = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: true }));
|
||
const ok = await updateOption('Notice', inputs.Notice);
|
||
if (ok) showSuccess(t('公告已更新'));
|
||
} catch (error) {
|
||
console.error(t('公告更新失败'), error);
|
||
showError(t('公告更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: false }));
|
||
}
|
||
};
|
||
// 通用设置 - UserAgreement
|
||
const submitUserAgreement = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
[LEGAL_USER_AGREEMENT_KEY]: true,
|
||
}));
|
||
const ok = await updateOption(
|
||
LEGAL_USER_AGREEMENT_KEY,
|
||
inputs[LEGAL_USER_AGREEMENT_KEY],
|
||
);
|
||
if (ok) showSuccess(t('服务协议已更新'));
|
||
} catch (error) {
|
||
console.error(t('服务协议更新失败'), error);
|
||
showError(t('服务协议更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
[LEGAL_USER_AGREEMENT_KEY]: false,
|
||
}));
|
||
}
|
||
};
|
||
// 通用设置 - PrivacyPolicy
|
||
const submitPrivacyPolicy = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
[LEGAL_PRIVACY_POLICY_KEY]: true,
|
||
}));
|
||
const ok = await updateOption(
|
||
LEGAL_PRIVACY_POLICY_KEY,
|
||
inputs[LEGAL_PRIVACY_POLICY_KEY],
|
||
);
|
||
if (ok) showSuccess(t('隐私政策已更新'));
|
||
} catch (error) {
|
||
console.error(t('隐私政策更新失败'), error);
|
||
showError(t('隐私政策更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
[LEGAL_PRIVACY_POLICY_KEY]: false,
|
||
}));
|
||
}
|
||
};
|
||
// 个性化设置
|
||
const formAPIPersonalization = useRef();
|
||
// 个性化设置 - SystemName
|
||
const submitSystemName = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
SystemName: true,
|
||
}));
|
||
const ok = await updateOption('SystemName', inputs.SystemName);
|
||
if (ok) showSuccess(t('系统名称已更新'));
|
||
} catch (error) {
|
||
console.error(t('系统名称更新失败'), error);
|
||
showError(t('系统名称更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
SystemName: false,
|
||
}));
|
||
}
|
||
};
|
||
|
||
// 个性化设置 - Logo
|
||
const submitLogo = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: true }));
|
||
const ok = await updateOption('Logo', inputs.Logo);
|
||
if (ok) showSuccess(t('Logo 已更新'));
|
||
} catch (error) {
|
||
console.error('Logo 更新失败', error);
|
||
showError(t('Logo 更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: false }));
|
||
}
|
||
};
|
||
// 个性化设置 - 首页内容
|
||
const submitOption = async (key) => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
HomePageContent: true,
|
||
}));
|
||
const ok = await updateOption(key, inputs[key]);
|
||
if (ok) showSuccess(t('首页内容已更新'));
|
||
} catch (error) {
|
||
console.error('首页内容更新失败', error);
|
||
showError(t('首页内容更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
HomePageContent: false,
|
||
}));
|
||
}
|
||
};
|
||
// 个性化设置 - 登录页背景动画
|
||
const submitAuthBackgroundTheme = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
AuthBackgroundTheme: true,
|
||
}));
|
||
const ok = await updateOption(
|
||
'AuthBackgroundTheme',
|
||
inputs.AuthBackgroundTheme,
|
||
);
|
||
if (ok) {
|
||
showSuccess(t('登录页背景动画已更新'));
|
||
}
|
||
} catch (error) {
|
||
console.error('登录页背景动画更新失败', error);
|
||
showError(t('登录页背景动画更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
AuthBackgroundTheme: false,
|
||
}));
|
||
}
|
||
};
|
||
// 个性化设置 - 关于
|
||
const submitAbout = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, About: true }));
|
||
const ok = await updateOption('About', inputs.About);
|
||
if (ok) showSuccess(t('关于内容已更新'));
|
||
} catch (error) {
|
||
console.error('关于内容更新失败', error);
|
||
showError(t('关于内容更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, About: false }));
|
||
}
|
||
};
|
||
// 个性化设置 - 控制台欢迎语标题
|
||
const submitConsoleWelcome = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
ConsoleWelcome: true,
|
||
}));
|
||
const ok = await updateOption(
|
||
'ConsoleWelcomeTitle',
|
||
inputs.ConsoleWelcomeTitle,
|
||
);
|
||
if (ok) showSuccess(t('控制台欢迎语已更新'));
|
||
} catch (error) {
|
||
console.error('控制台欢迎语更新失败', error);
|
||
showError(t('控制台欢迎语更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
ConsoleWelcome: false,
|
||
}));
|
||
}
|
||
};
|
||
|
||
// 个性化设置 - 官网链接(控制台欢迎语 / 关于页 / 检查更新弹窗共用)
|
||
const submitOfficialWebsite = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
OfficialWebsite: true,
|
||
}));
|
||
const ok = await updateOption('OfficialWebsite', inputs.OfficialWebsite);
|
||
if (ok) showSuccess(t('官网链接已更新'));
|
||
} catch (error) {
|
||
console.error('官网链接更新失败', error);
|
||
showError(t('官网链接更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
OfficialWebsite: false,
|
||
}));
|
||
}
|
||
};
|
||
|
||
// 个性化设置 - 页脚
|
||
const submitFooter = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: true }));
|
||
const ok = await updateOption('Footer', inputs.Footer);
|
||
if (ok) showSuccess(t('页脚内容已更新'));
|
||
} catch (error) {
|
||
console.error('页脚内容更新失败', error);
|
||
showError(t('页脚内容更新失败'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: false }));
|
||
}
|
||
};
|
||
|
||
const checkUpdate = async () => {
|
||
try {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
CheckUpdate: true,
|
||
}));
|
||
|
||
// 版本检查已禁用 - Weelinking 是独立项目
|
||
showSuccess(t('当前已是最新版本'));
|
||
return;
|
||
|
||
// 以下代码已禁用
|
||
// const res = await fetch(
|
||
// 'https://api.github.com/repos/Calcium-Ion/new-api/releases/latest',
|
||
// {
|
||
// headers: {
|
||
// Accept: 'application/json',
|
||
// 'Content-Type': 'application/json',
|
||
// 'User-Agent': 'weelinking-update-checker',
|
||
// },
|
||
// },
|
||
// ).then((response) => response.json());
|
||
|
||
// const { tag_name, body } = res;
|
||
// if (tag_name === statusState?.status?.version) {
|
||
// showSuccess(`已是最新版本:${tag_name}`);
|
||
// } else {
|
||
// setUpdateData({
|
||
// tag_name: tag_name,
|
||
// content: marked.parse(body),
|
||
// });
|
||
// setShowUpdateModal(true);
|
||
// }
|
||
} catch (error) {
|
||
console.error('Failed to check for updates:', error);
|
||
showError(t('检查更新失败,请稍后再试'));
|
||
} finally {
|
||
setLoadingInput((loadingInput) => ({
|
||
...loadingInput,
|
||
CheckUpdate: false,
|
||
}));
|
||
}
|
||
};
|
||
const getOptions = async () => {
|
||
const res = await API.get('/api/option/');
|
||
const { success, message, data } = res.data;
|
||
if (success) {
|
||
let newInputs = {};
|
||
data.forEach((item) => {
|
||
if (item.key in inputs) {
|
||
newInputs[item.key] = item.value;
|
||
}
|
||
});
|
||
setInputs(newInputs);
|
||
formAPISettingGeneral.current.setValues(newInputs);
|
||
formAPIPersonalization.current.setValues(newInputs);
|
||
} else {
|
||
showError(message);
|
||
}
|
||
};
|
||
|
||
const onMount = useEffectEvent(() => {
|
||
getOptions();
|
||
});
|
||
useEffect(() => onMount(), [onMount]);
|
||
|
||
// 打开官网(可配置,未配置则按钮不显示)
|
||
const officialWebsite = (statusState?.status?.official_website || '').trim();
|
||
const openOfficialWebsite = () => {
|
||
if (officialWebsite) window.open(officialWebsite, '_blank');
|
||
};
|
||
|
||
const getStartTimeString = () => {
|
||
const timestamp = statusState?.status?.start_time;
|
||
return statusState.status ? timestamp2string(timestamp) : '';
|
||
};
|
||
|
||
return (
|
||
<Row>
|
||
<Col span={24} className='settings-other-sections'>
|
||
{/* 版本信息 */}
|
||
<Form>
|
||
<Card>
|
||
<Form.Section text={t('系统信息')} className='settings-other-info'>
|
||
<Row>
|
||
<Col span={24}>
|
||
<Space wrap>
|
||
<Text>
|
||
{t('当前版本')}:
|
||
{statusState?.status?.version || t('未知')}
|
||
</Text>
|
||
<Button
|
||
type='primary'
|
||
onClick={checkUpdate}
|
||
loading={loadingInput['CheckUpdate']}
|
||
>
|
||
{t('检查更新')}
|
||
</Button>
|
||
</Space>
|
||
</Col>
|
||
</Row>
|
||
<Row>
|
||
<Col span={24}>
|
||
<Text>
|
||
{t('启动时间')}:{getStartTimeString()}
|
||
</Text>
|
||
</Col>
|
||
</Row>
|
||
</Form.Section>
|
||
</Card>
|
||
</Form>
|
||
{/* 通用设置 */}
|
||
<Form
|
||
values={inputs}
|
||
getFormApi={(formAPI) => (formAPISettingGeneral.current = formAPI)}
|
||
>
|
||
<Card>
|
||
<Form.Section
|
||
text={t('通用设置')}
|
||
className='settings-other-fields'
|
||
>
|
||
<Form.TextArea
|
||
label={t('公告')}
|
||
placeholder={t(
|
||
'在此输入新的公告内容,支持 Markdown & HTML 代码',
|
||
)}
|
||
field={'Notice'}
|
||
onChange={handleInputChange}
|
||
style={{ fontFamily: 'JetBrains Mono, Consolas' }}
|
||
autosize={{ minRows: 6, maxRows: 12 }}
|
||
/>
|
||
<Button onClick={submitNotice} loading={loadingInput['Notice']}>
|
||
{t('设置公告')}
|
||
</Button>
|
||
<Form.TextArea
|
||
label={t('服务协议')}
|
||
placeholder={t(
|
||
'在此输入服务协议内容,支持 Markdown & HTML 代码',
|
||
)}
|
||
field={LEGAL_USER_AGREEMENT_KEY}
|
||
onChange={handleInputChange}
|
||
style={{ fontFamily: 'JetBrains Mono, Consolas' }}
|
||
autosize={{ minRows: 6, maxRows: 12 }}
|
||
helpText={t(
|
||
'填写服务协议内容后,用户注册时将被要求勾选已阅读服务协议',
|
||
)}
|
||
/>
|
||
<Button
|
||
onClick={submitUserAgreement}
|
||
loading={loadingInput[LEGAL_USER_AGREEMENT_KEY]}
|
||
>
|
||
{t('设置服务协议')}
|
||
</Button>
|
||
<Form.TextArea
|
||
label={t('隐私政策')}
|
||
placeholder={t(
|
||
'在此输入隐私政策内容,支持 Markdown & HTML 代码',
|
||
)}
|
||
field={LEGAL_PRIVACY_POLICY_KEY}
|
||
onChange={handleInputChange}
|
||
style={{ fontFamily: 'JetBrains Mono, Consolas' }}
|
||
autosize={{ minRows: 6, maxRows: 12 }}
|
||
helpText={t(
|
||
'填写隐私政策内容后,用户注册时将被要求勾选已阅读隐私政策',
|
||
)}
|
||
/>
|
||
<Button
|
||
onClick={submitPrivacyPolicy}
|
||
loading={loadingInput[LEGAL_PRIVACY_POLICY_KEY]}
|
||
>
|
||
{t('设置隐私政策')}
|
||
</Button>
|
||
</Form.Section>
|
||
</Card>
|
||
</Form>
|
||
{/* 个性化设置 */}
|
||
<Form
|
||
values={inputs}
|
||
getFormApi={(formAPI) => (formAPIPersonalization.current = formAPI)}
|
||
>
|
||
<Card>
|
||
<Form.Section
|
||
text={t('个性化设置')}
|
||
className='settings-other-fields'
|
||
>
|
||
<Form.Input
|
||
label={t('系统名称')}
|
||
placeholder={t('在此输入系统名称')}
|
||
field={'SystemName'}
|
||
onChange={handleInputChange}
|
||
/>
|
||
<Button
|
||
onClick={submitSystemName}
|
||
loading={loadingInput['SystemName']}
|
||
>
|
||
{t('设置系统名称')}
|
||
</Button>
|
||
<Form.Input
|
||
label={t('Logo 图片地址')}
|
||
placeholder={t('在此输入 Logo 图片地址')}
|
||
field={'Logo'}
|
||
onChange={handleInputChange}
|
||
/>
|
||
<Button onClick={submitLogo} loading={loadingInput['Logo']}>
|
||
{t('设置 Logo')}
|
||
</Button>
|
||
<Form.Select
|
||
label={t('登录页背景动画')}
|
||
field={'AuthBackgroundTheme'}
|
||
style={{ width: 280 }}
|
||
optionList={[
|
||
{ value: 'ocean', label: t('深海幽光(默认)') },
|
||
{ value: 'token-factory', label: t('Token 工厂 · 算力机房') },
|
||
{
|
||
value: 'token-factory-cinematic',
|
||
label: t('Token 工厂 · 电影级算力机房'),
|
||
},
|
||
{ value: 'none', label: t('无动画') },
|
||
]}
|
||
onChange={(value) =>
|
||
setInputs((inputs) => ({
|
||
...inputs,
|
||
AuthBackgroundTheme: value,
|
||
}))
|
||
}
|
||
helpText={t(
|
||
'登录、注册、企业登录、企业注册四个页面共用的背景动画',
|
||
)}
|
||
/>
|
||
<Button
|
||
onClick={submitAuthBackgroundTheme}
|
||
loading={loadingInput['AuthBackgroundTheme']}
|
||
>
|
||
{t('设置背景动画')}
|
||
</Button>
|
||
<Form.TextArea
|
||
label={t('首页内容')}
|
||
placeholder={t(
|
||
'在此输入首页内容,支持 Markdown & HTML 代码,设置后首页的状态信息将不再显示。如果输入的是一个链接,则会使用该链接作为 iframe 的 src 属性,这允许你设置任意网页作为首页',
|
||
)}
|
||
field={'HomePageContent'}
|
||
onChange={handleInputChange}
|
||
style={{ fontFamily: 'JetBrains Mono, Consolas' }}
|
||
autosize={{ minRows: 6, maxRows: 12 }}
|
||
/>
|
||
<Button
|
||
onClick={() => submitOption('HomePageContent')}
|
||
loading={loadingInput['HomePageContent']}
|
||
>
|
||
{t('设置首页内容')}
|
||
</Button>
|
||
<Form.TextArea
|
||
label={t('关于')}
|
||
placeholder={t(
|
||
'在此输入新的关于内容,支持 Markdown & HTML 代码。如果输入的是一个链接,则会使用该链接作为 iframe 的 src 属性,这允许你设置任意网页作为关于页面',
|
||
)}
|
||
field={'About'}
|
||
onChange={handleInputChange}
|
||
style={{ fontFamily: 'JetBrains Mono, Consolas' }}
|
||
autosize={{ minRows: 6, maxRows: 12 }}
|
||
/>
|
||
<Button onClick={submitAbout} loading={loadingInput['About']}>
|
||
{t('设置关于')}
|
||
</Button>
|
||
<Form.Input
|
||
label={t('官网链接')}
|
||
placeholder={t('例如 https://www.example.com')}
|
||
field={'OfficialWebsite'}
|
||
onChange={handleInputChange}
|
||
helpText={t(
|
||
'用于控制台欢迎语、关于页默认内容与检查更新弹窗,留空则不显示',
|
||
)}
|
||
/>
|
||
<Button
|
||
onClick={submitOfficialWebsite}
|
||
loading={loadingInput['OfficialWebsite']}
|
||
>
|
||
{t('设置官网链接')}
|
||
</Button>
|
||
<Form.Input
|
||
label={t('控制台欢迎语标题')}
|
||
placeholder={t('浏览器控制台(F12)打印的欢迎语标题')}
|
||
field={'ConsoleWelcomeTitle'}
|
||
onChange={handleInputChange}
|
||
helpText={t('标题与官网链接均留空则控制台不打印欢迎语')}
|
||
/>
|
||
<Button
|
||
onClick={submitConsoleWelcome}
|
||
loading={loadingInput['ConsoleWelcome']}
|
||
>
|
||
{t('设置控制台欢迎语')}
|
||
</Button>
|
||
<Form.Input
|
||
label={t('页脚')}
|
||
placeholder={t(
|
||
'在此输入新的页脚,留空则使用默认页脚,支持 HTML 代码',
|
||
)}
|
||
field={'Footer'}
|
||
onChange={handleInputChange}
|
||
/>
|
||
<Button onClick={submitFooter} loading={loadingInput['Footer']}>
|
||
{t('设置页脚')}
|
||
</Button>
|
||
</Form.Section>
|
||
</Card>
|
||
</Form>
|
||
</Col>
|
||
<Modal
|
||
title={t('新版本:{{tag_name}}', { tag_name: updateData.tag_name })}
|
||
visible={showUpdateModal}
|
||
onCancel={() => setShowUpdateModal(false)}
|
||
footer={
|
||
officialWebsite
|
||
? [
|
||
<Button
|
||
key='details'
|
||
type='primary'
|
||
onClick={() => {
|
||
setShowUpdateModal(false);
|
||
openOfficialWebsite();
|
||
}}
|
||
>
|
||
{t('详情')}
|
||
</Button>,
|
||
]
|
||
: null
|
||
}
|
||
>
|
||
<div dangerouslySetInnerHTML={{ __html: updateData.content }}></div>
|
||
</Modal>
|
||
</Row>
|
||
);
|
||
};
|
||
|
||
export default OtherSetting;
|