Commit 28e8e89e by JustSong

Able to manage channels now

parent 9ca6adf4
...@@ -91,7 +91,12 @@ const ( ...@@ -91,7 +91,12 @@ const (
) )
const ( const (
ChannelTypeUnknown = 0 ChannelTypeUnknown = 0
ChannelTypeOpenAI = 1 ChannelTypeOpenAI = 1
ChannelTypeAPI2D = 2 ChannelTypeAPI2D = 2
ChannelTypeAzure = 3
ChannelTypeCloseAI = 4
ChannelTypeOpenAISB = 5
ChannelTypeOpenAIMax = 6
ChannelTypeOhMyGPT = 7
) )
...@@ -82,6 +82,8 @@ func AddChannel(c *gin.Context) { ...@@ -82,6 +82,8 @@ func AddChannel(c *gin.Context) {
}) })
return return
} }
channel.CreatedTime = common.GetTimestamp()
channel.AccessedTime = common.GetTimestamp()
err = channel.Insert() err = channel.Insert()
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
...@@ -136,6 +138,7 @@ func UpdateChannel(c *gin.Context) { ...@@ -136,6 +138,7 @@ func UpdateChannel(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": true, "success": true,
"message": "", "message": "",
"data": channel,
}) })
return return
} }
...@@ -9,7 +9,7 @@ type Channel struct { ...@@ -9,7 +9,7 @@ type Channel struct {
Type int `json:"type" gorm:"default:0"` Type int `json:"type" gorm:"default:0"`
Key string `json:"key"` Key string `json:"key"`
Status int `json:"status" gorm:"default:1"` Status int `json:"status" gorm:"default:1"`
Name string `json:"name" gorm:"unique;index"` Name string `json:"name" gorm:"index"`
Weight int `json:"weight"` Weight int `json:"weight"`
CreatedTime int64 `json:"created_time" gorm:"bigint"` CreatedTime int64 `json:"created_time" gorm:"bigint"`
AccessedTime int64 `json:"accessed_time" gorm:"bigint"` AccessedTime int64 `json:"accessed_time" gorm:"bigint"`
......
...@@ -115,7 +115,7 @@ const TokensTable = () => { ...@@ -115,7 +115,7 @@ const TokensTable = () => {
return; return;
} }
setSearching(true); setSearching(true);
const res = await API.get(`/api/token/search?keyword=${searchKeyword}/`); const res = await API.get(`/api/token/search?keyword=${searchKeyword}`);
const { success, message, data } = res.data; const { success, message, data } = res.data;
if (success) { if (success) {
setTokens(data); setTokens(data);
......
export const CHANNEL_OPTIONS = [
{ key: 1, text: 'OpenAI', value: 1, color: 'green' },
{ key: 2, text: 'API2D', value: 2, color: 'blue' },
{ key: 3, text: 'Azure', value: 3, color: 'olive' },
{ key: 4, text: 'CloseAI', value: 4, color: 'teal' },
{ key: 5, text: 'OpenAI-SB', value: 5, color: 'brown' },
{ key: 6, text: 'OpenAI Max', value: 6, color: 'violet' },
{ key: 7, text: 'OhMyGPT', value: 7, color: 'purple' }
];
export * from './toast.constants'; export * from './toast.constants';
export * from './user.constants'; export * from './user.constants';
export * from './common.constant'; export * from './common.constant';
\ No newline at end of file export * from './channel.constants';
\ No newline at end of file
import React, { useState } from 'react'; import React, { useState } from 'react';
import { Button, Form, Header, Segment } from 'semantic-ui-react'; import { Button, Form, Header, Segment } from 'semantic-ui-react';
import { API, showError, showSuccess } from '../../helpers'; import { API, showError, showSuccess } from '../../helpers';
import { CHANNEL_OPTIONS } from '../../constants';
const AddChannel = () => { const AddChannel = () => {
const originInputs = { const originInputs = {
username: '', name: '',
display_name: '', type: 1,
password: '', key: ''
}; };
const [inputs, setInputs] = useState(originInputs); const [inputs, setInputs] = useState(originInputs);
const { username, display_name, password } = inputs; const { name, type, key } = inputs;
const handleInputChange = (e, { name, value }) => { const handleInputChange = (e, { name, value }) => {
setInputs((inputs) => ({ ...inputs, [name]: value })); setInputs((inputs) => ({ ...inputs, [name]: value }));
}; };
const submit = async () => { const submit = async () => {
if (inputs.username === '' || inputs.password === '') return; if (inputs.name === '' || inputs.key === '') return;
const res = await API.post(`/api/user/`, inputs); const res = await API.post(`/api/channel/`, inputs);
const { success, message } = res.data; const { success, message } = res.data;
if (success) { if (success) {
showSuccess('用户账户创建成功!'); showSuccess('渠道创建成功!');
setInputs(originInputs); setInputs(originInputs);
} else { } else {
showError(message); showError(message);
...@@ -30,38 +31,37 @@ const AddChannel = () => { ...@@ -30,38 +31,37 @@ const AddChannel = () => {
return ( return (
<> <>
<Segment> <Segment>
<Header as="h3">创建新用户账户</Header> <Header as='h3'>创建新的渠道</Header>
<Form autoComplete="off"> <Form autoComplete='off'>
<Form.Field> <Form.Field>
<Form.Input <Form.Select
label="用户名" label='类型'
name="username" name='type'
placeholder={'请输入用户名'} options={CHANNEL_OPTIONS}
value={inputs.type}
onChange={handleInputChange} onChange={handleInputChange}
value={username}
autoComplete="off"
required
/> />
</Form.Field> </Form.Field>
<Form.Field> <Form.Field>
<Form.Input <Form.Input
label="显示名称" label='名称'
name="display_name" name='name'
placeholder={'请输入显示名称'} placeholder={'请输入名称'}
onChange={handleInputChange} onChange={handleInputChange}
value={display_name} value={name}
autoComplete="off" autoComplete='off'
required
/> />
</Form.Field> </Form.Field>
<Form.Field> <Form.Field>
<Form.Input <Form.Input
label="密码" label='密钥'
name="password" name='key'
type={'password'} placeholder={'请输入密钥'}
placeholder={'请输入密码'}
onChange={handleInputChange} onChange={handleInputChange}
value={password} value={key}
autoComplete="off" // type='password'
autoComplete='off'
required required
/> />
</Form.Field> </Form.Field>
......
...@@ -2,32 +2,23 @@ import React, { useEffect, useState } from 'react'; ...@@ -2,32 +2,23 @@ import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Segment } from 'semantic-ui-react'; import { Button, Form, Header, Segment } from 'semantic-ui-react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { API, showError, showSuccess } from '../../helpers'; import { API, showError, showSuccess } from '../../helpers';
import { CHANNEL_OPTIONS } from '../../constants';
const EditChannel = () => { const EditChannel = () => {
const params = useParams(); const params = useParams();
const userId = params.id; const channelId = params.id;
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [inputs, setInputs] = useState({ const [inputs, setInputs] = useState({
username: '', name: '',
display_name: '', key: '',
password: '', type: 1,
github_id: '',
wechat_id: '',
email: '',
}); });
const { username, display_name, password, github_id, wechat_id, email } =
inputs;
const handleInputChange = (e, { name, value }) => { const handleInputChange = (e, { name, value }) => {
setInputs((inputs) => ({ ...inputs, [name]: value })); setInputs((inputs) => ({ ...inputs, [name]: value }));
}; };
const loadUser = async () => { const loadChannel = async () => {
let res = undefined; let res = await API.get(`/api/channel/${channelId}`);
if (userId) {
res = await API.get(`/api/user/${userId}`);
} else {
res = await API.get(`/api/user/self`);
}
const { success, message, data } = res.data; const { success, message, data } = res.data;
if (success) { if (success) {
data.password = ''; data.password = '';
...@@ -38,19 +29,14 @@ const EditChannel = () => { ...@@ -38,19 +29,14 @@ const EditChannel = () => {
setLoading(false); setLoading(false);
}; };
useEffect(() => { useEffect(() => {
loadUser().then(); loadChannel().then();
}, []); }, []);
const submit = async () => { const submit = async () => {
let res = undefined; let res = await API.put(`/api/channel/`, { ...inputs, id: parseInt(channelId) });
if (userId) {
res = await API.put(`/api/user/`, { ...inputs, id: parseInt(userId) });
} else {
res = await API.put(`/api/user/self`, inputs);
}
const { success, message } = res.data; const { success, message } = res.data;
if (success) { if (success) {
showSuccess('用户信息更新成功!'); showSuccess('渠道更新成功!');
} else { } else {
showError(message); showError(message);
} }
...@@ -59,67 +45,36 @@ const EditChannel = () => { ...@@ -59,67 +45,36 @@ const EditChannel = () => {
return ( return (
<> <>
<Segment loading={loading}> <Segment loading={loading}>
<Header as='h3'>更新用户信息</Header> <Header as='h3'>更新渠道信息</Header>
<Form autoComplete='off'> <Form autoComplete='off'>
<Form.Field> <Form.Field>
<Form.Input <Form.Select
label='用户名' label='类型'
name='username' name='type'
placeholder={'请输入新的用户名'} options={CHANNEL_OPTIONS}
value={inputs.type}
onChange={handleInputChange} onChange={handleInputChange}
value={username}
autoComplete='off'
/> />
</Form.Field> </Form.Field>
<Form.Field> <Form.Field>
<Form.Input <Form.Input
label='密码' label='名称'
name='password' name='name'
type={'password'} placeholder={'请输入新的名称'}
placeholder={'请输入新的密码'}
onChange={handleInputChange} onChange={handleInputChange}
value={password} value={inputs.name}
autoComplete='off' autoComplete='off'
/> />
</Form.Field> </Form.Field>
<Form.Field> <Form.Field>
<Form.Input <Form.Input
label='显示名称' label='密钥'
name='display_name' name='key'
placeholder={'请输入新的显示名称'} placeholder={'请输入新的密钥'}
onChange={handleInputChange} onChange={handleInputChange}
value={display_name} value={inputs.key}
autoComplete='off' // type='password'
/>
</Form.Field>
<Form.Field>
<Form.Input
label='已绑定的 GitHub 账户'
name='github_id'
value={github_id}
autoComplete='off'
placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
readOnly
/>
</Form.Field>
<Form.Field>
<Form.Input
label='已绑定的微信账户'
name='wechat_id'
value={wechat_id}
autoComplete='off'
placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
readOnly
/>
</Form.Field>
<Form.Field>
<Form.Input
label='已绑定的邮箱账户'
name='email'
value={email}
autoComplete='off' autoComplete='off'
placeholder='此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改'
readOnly
/> />
</Form.Field> </Form.Field>
<Button onClick={submit}>提交</Button> <Button onClick={submit}>提交</Button>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment