/*
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
*/
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 (
{/* 版本信息 */}
{/* 通用设置 */}
{/* 个性化设置 */}
setShowUpdateModal(false)}
footer={
officialWebsite
? [
,
]
: null
}
>
);
};
export default OtherSetting;