2023-04-23 04:43:10 +00:00
|
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-05-13 03:36:36 +00:00
|
|
|
|
import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
|
2023-04-23 04:43:10 +00:00
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
import { API, showError, showSuccess } from '../../helpers';
|
2023-04-23 07:42:23 +00:00
|
|
|
|
import { CHANNEL_OPTIONS } from '../../constants';
|
2023-04-23 04:43:10 +00:00
|
|
|
|
|
|
|
|
|
const EditChannel = () => {
|
|
|
|
|
const params = useParams();
|
2023-04-23 07:42:23 +00:00
|
|
|
|
const channelId = params.id;
|
2023-05-13 03:36:36 +00:00
|
|
|
|
const isEdit = channelId !== undefined;
|
|
|
|
|
const [loading, setLoading] = useState(isEdit);
|
|
|
|
|
const originInputs = {
|
2023-04-23 07:42:23 +00:00
|
|
|
|
name: '',
|
|
|
|
|
type: 1,
|
2023-05-13 03:36:36 +00:00
|
|
|
|
key: '',
|
|
|
|
|
base_url: ''
|
|
|
|
|
};
|
|
|
|
|
const [inputs, setInputs] = useState(originInputs);
|
2023-04-23 04:43:10 +00:00
|
|
|
|
const handleInputChange = (e, { name, value }) => {
|
|
|
|
|
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-23 07:42:23 +00:00
|
|
|
|
const loadChannel = async () => {
|
|
|
|
|
let res = await API.get(`/api/channel/${channelId}`);
|
2023-04-23 04:43:10 +00:00
|
|
|
|
const { success, message, data } = res.data;
|
|
|
|
|
if (success) {
|
|
|
|
|
data.password = '';
|
|
|
|
|
setInputs(data);
|
|
|
|
|
} else {
|
|
|
|
|
showError(message);
|
|
|
|
|
}
|
|
|
|
|
setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
useEffect(() => {
|
2023-05-13 03:36:36 +00:00
|
|
|
|
if (isEdit) {
|
|
|
|
|
loadChannel().then();
|
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const submit = async () => {
|
2023-05-13 03:36:36 +00:00
|
|
|
|
if (!isEdit && (inputs.name === '' || inputs.key === '')) return;
|
|
|
|
|
let localInputs = inputs;
|
|
|
|
|
if (localInputs.base_url.endsWith('/')) {
|
|
|
|
|
localInputs.base_url = localInputs.base_url.slice(0, localInputs.base_url.length - 1);
|
|
|
|
|
}
|
|
|
|
|
let res;
|
|
|
|
|
if (isEdit) {
|
|
|
|
|
res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
|
|
|
|
|
} else {
|
|
|
|
|
res = await API.post(`/api/channel/`, localInputs);
|
2023-04-23 12:35:49 +00:00
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
const { success, message } = res.data;
|
|
|
|
|
if (success) {
|
2023-05-13 03:36:36 +00:00
|
|
|
|
if (isEdit) {
|
|
|
|
|
showSuccess('渠道更新成功!');
|
|
|
|
|
} else {
|
|
|
|
|
showSuccess('渠道创建成功!');
|
|
|
|
|
setInputs(originInputs);
|
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
} else {
|
|
|
|
|
showError(message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Segment loading={loading}>
|
2023-05-13 03:36:36 +00:00
|
|
|
|
<Header as='h3'>{isEdit ? '更新渠道信息' : '创建新的渠道'}</Header>
|
2023-05-12 03:44:38 +00:00
|
|
|
|
<Form autoComplete='new-password'>
|
2023-04-23 04:43:10 +00:00
|
|
|
|
<Form.Field>
|
2023-04-23 07:42:23 +00:00
|
|
|
|
<Form.Select
|
|
|
|
|
label='类型'
|
|
|
|
|
name='type'
|
|
|
|
|
options={CHANNEL_OPTIONS}
|
|
|
|
|
value={inputs.type}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
2023-05-13 03:36:36 +00:00
|
|
|
|
{
|
|
|
|
|
inputs.type === 3 && (
|
|
|
|
|
<>
|
|
|
|
|
<Message>
|
|
|
|
|
注意,创建资源时,部署名称必须和 OpenAI 官方的模型名称保持一致,因为 One API 会把请求体中的 model 参数替换为你的部署名称。
|
|
|
|
|
</Message>
|
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
|
|
|
|
label='AZURE_OPENAI_ENDPOINT'
|
|
|
|
|
name='base_url'
|
|
|
|
|
placeholder={'请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com'}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
value={inputs.base_url}
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-04-23 12:35:49 +00:00
|
|
|
|
{
|
|
|
|
|
inputs.type === 8 && (
|
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
|
|
|
|
label='Base URL'
|
|
|
|
|
name='base_url'
|
2023-05-13 03:36:36 +00:00
|
|
|
|
placeholder={'请输入自定义渠道的 Base URL,例如:https://openai.justsong.cn'}
|
2023-04-23 12:35:49 +00:00
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
value={inputs.base_url}
|
2023-05-12 03:44:38 +00:00
|
|
|
|
autoComplete='new-password'
|
2023-04-23 12:35:49 +00:00
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
2023-04-23 07:42:23 +00:00
|
|
|
|
label='名称'
|
|
|
|
|
name='name'
|
2023-05-13 03:36:36 +00:00
|
|
|
|
placeholder={'请输入名称'}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
onChange={handleInputChange}
|
2023-04-23 07:42:23 +00:00
|
|
|
|
value={inputs.name}
|
2023-05-12 03:44:38 +00:00
|
|
|
|
autoComplete='new-password'
|
2023-04-23 04:43:10 +00:00
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
|
|
|
|
<Form.Field>
|
|
|
|
|
<Form.Input
|
2023-04-23 07:42:23 +00:00
|
|
|
|
label='密钥'
|
|
|
|
|
name='key'
|
2023-05-13 03:36:36 +00:00
|
|
|
|
placeholder={'请输入密钥'}
|
2023-04-23 04:43:10 +00:00
|
|
|
|
onChange={handleInputChange}
|
2023-04-23 07:42:23 +00:00
|
|
|
|
value={inputs.key}
|
|
|
|
|
// type='password'
|
2023-05-12 03:44:38 +00:00
|
|
|
|
autoComplete='new-password'
|
2023-04-23 04:43:10 +00:00
|
|
|
|
/>
|
|
|
|
|
</Form.Field>
|
|
|
|
|
<Button onClick={submit}>提交</Button>
|
|
|
|
|
</Form>
|
|
|
|
|
</Segment>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default EditChannel;
|