import React, { useEffect, useRef, useState } from 'react'; import { Banner, Button, Col, Form, Row } from '@douyinfe/semi-ui'; import { API, showError, showSuccess } from '../helpers'; import { marked } from 'marked'; const OtherSetting = () => { let [inputs, setInputs] = useState({ Notice: '', SystemName: '', Logo: '', Footer: '', About: '', HomePageContent: '', }); let [loading, setLoading] = useState(false); const [showUpdateModal, setShowUpdateModal] = useState(false); const [updateData, setUpdateData] = 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); }; const [loadingInput, setLoadingInput] = useState({ Notice: false, SystemName: false, Logo: false, HomePageContent: false, About: false, Footer: 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 })); await updateOption('Notice', inputs.Notice); showSuccess('公告已更新'); } catch (error) { console.error('公告更新失败', error); showError('公告更新失败'); } finally { setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: false })); } }; // 个性化设置 const formAPIPersonalization = useRef(); // 个性化设置 - SystemName const submitSystemName = async () => { try { setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: true, })); await updateOption('SystemName', inputs.SystemName); showSuccess('系统名称已更新'); } catch (error) { console.error('系统名称更新失败', error); showError('系统名称更新失败'); } finally { setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: false, })); } }; // 个性化设置 - Logo const submitLogo = async () => { try { setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: true })); await updateOption('Logo', inputs.Logo); showSuccess('Logo 已更新'); } catch (error) { console.error('Logo 更新失败', error); showError('Logo 更新失败'); } finally { setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: false })); } }; // 个性化设置 - 首页内容 const submitOption = async (key) => { try { setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: true, })); await updateOption(key, inputs[key]); showSuccess('首页内容已更新'); } catch (error) { console.error('首页内容更新失败', error); showError('首页内容更新失败'); } finally { setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: false, })); } }; // 个性化设置 - 关于 const submitAbout = async () => { try { setLoadingInput((loadingInput) => ({ ...loadingInput, About: true })); await updateOption('About', inputs.About); showSuccess('关于内容已更新'); } catch (error) { console.error('关于内容更新失败', error); showError('关于内容更新失败'); } finally { setLoadingInput((loadingInput) => ({ ...loadingInput, About: false })); } }; // 个性化设置 - 页脚 const submitFooter = async () => { try { setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: true })); await updateOption('Footer', inputs.Footer); showSuccess('页脚内容已更新'); } catch (error) { console.error('页脚内容更新失败', error); showError('页脚内容更新失败'); } finally { setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: false })); } }; const openGitHubRelease = () => { window.location = 'https://github.com/songquanpeng/one-api/releases/latest'; }; const checkUpdate = async () => { const res = await API.get( 'https://api.github.com/repos/songquanpeng/one-api/releases/latest', ); const { tag_name, body } = res.data; if (tag_name === process.env.REACT_APP_VERSION) { showSuccess(`已是最新版本:${tag_name}`); } else { setUpdateData({ tag_name: tag_name, content: marked.parse(body), }); setShowUpdateModal(true); } }; 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); } }; useEffect(() => { getOptions(); }, []); return ( {/* 通用设置 */}
(formAPISettingGeneral.current = formAPI)} style={{ marginBottom: 15 }} >
{/* 个性化设置 */}
(formAPIPersonalization.current = formAPI)} style={{ marginBottom: 15 }} > {/* */}
{/* setShowUpdateModal(false)}*/} {/* onOpen={() => setShowUpdateModal(true)}*/} {/* open={showUpdateModal}*/} {/*>*/} {/* 新版本:{updateData.tag_name}*/} {/* */} {/* */} {/*
*/} {/*
*/} {/*
*/} {/* */} {/* */} {/* {*/} {/* setShowUpdateModal(false);*/} {/* openGitHubRelease();*/} {/* }}*/} {/* />*/} {/* */} {/**/}
); }; export default OtherSetting;