import PropTypes from 'prop-types'; import { useState, useEffect } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Divider, Button, TextField, Grid, FormControl, Alert, Stack, Typography } from '@mui/material'; import { API } from 'utils/api'; import { showError, showSuccess } from 'utils/common'; import LoadingButton from '@mui/lab/LoadingButton'; import Label from 'ui-component/Label'; export const CheckUpdates = ({ open, onCancel, onOk, row }) => { const [url, setUrl] = useState('https://raw.githubusercontent.com/MartialBE/one-api/prices/prices.json'); const [loading, setLoading] = useState(false); const [updateLoading, setUpdateLoading] = useState(false); const [newPricing, setNewPricing] = useState([]); const [addModel, setAddModel] = useState([]); const [diffModel, setDiffModel] = useState([]); const handleCheckUpdates = async () => { setLoading(true); try { const res = await API.get(url); // 检测是否是一个列表 if (!Array.isArray(res.data)) { showError('数据格式不正确'); } else { setNewPricing(res.data); } } catch (err) { console.error(err); } setLoading(false); }; const syncPricing = async (overwrite) => { setUpdateLoading(true); if (!newPricing.length) { showError('请先获取数据'); setUpdateLoading(false); return; } if (!overwrite && !addModel.length) { showError('没有新增模型'); setUpdateLoading(false); return; } try { overwrite = overwrite ? 'true' : 'false'; const res = await API.post('/api/prices/sync?overwrite=' + overwrite, newPricing); const { success, message } = res.data; if (success) { showSuccess('操作成功完成!'); onOk(true); } else { showError(message); } } catch (err) { console.error(err); } setUpdateLoading(false); }; useEffect(() => { const newModels = newPricing.filter((np) => !row.some((r) => r.model === np.model)); const changeModel = row.filter((r) => newPricing.some((np) => np.model === r.model && (np.input !== r.input || np.output !== r.output)) ); if (newModels.length > 0) { const newModelsList = newModels.map((model) => model.model); setAddModel(newModelsList); } else { setAddModel(''); } if (changeModel.length > 0) { const changeModelList = changeModel.map((model) => { const newModel = newPricing.find((np) => np.model === model.model); let changes = ''; if (model.input !== newModel.input) { changes += `输入倍率由 ${model.input} 变为 ${newModel.input},`; } if (model.output !== newModel.output) { changes += `输出倍率由 ${model.output} 变为 ${newModel.output}`; } return `${model.model}:${changes}`; }); setDiffModel(changeModelList); } else { setDiffModel(''); } }, [row, newPricing]); return ( 检查更新 setUrl(e.target.value)} /> 获取数据 {newPricing.length > 0 && ( {!addModel.length && !diffModel.length && 无更新} {addModel.length > 0 && ( 新增模型: {addModel.map((model) => ( ))} )} {diffModel.length > 0 && ( 价格变动模型(仅供参考,如果你自己修改了对应模型的价格请忽略): {diffModel.map((model) => ( {model} ))} )} 注意: 你可以选择覆盖或者仅添加新增,如果你选择覆盖,将会删除你自己添加的模型价格,完全使用远程配置,如果你选择仅添加新增,将会只会添加 新增模型的价格 { syncPricing(true); }} loading={updateLoading} > 覆盖数据 { syncPricing(false); }} loading={updateLoading} > 仅添加新增 )} ); }; CheckUpdates.propTypes = { open: PropTypes.bool, row: PropTypes.array, onCancel: PropTypes.func, onOk: PropTypes.func };