Просмотр исходного кода

feat: able to manage group now

JustSong 2 лет назад
Родитель
Сommit
2c53424db8
5 измененных файлов с 51 добавлено и 13 удалено
  1. 3 3
      controller/user.go
  2. 2 2
      model/ability.go
  3. 1 0
      model/channel.go
  4. 31 5
      web/src/pages/Channel/EditChannel.js
  5. 14 3
      web/src/pages/User/EditUser.js

+ 3 - 3
controller/user.go

@@ -228,7 +228,7 @@ func GetUser(c *gin.Context) {
 		return
 		return
 	}
 	}
 	myRole := c.GetInt("role")
 	myRole := c.GetInt("role")
-	if myRole <= user.Role {
+	if myRole <= user.Role && myRole != common.RoleRootUser {
 		c.JSON(http.StatusOK, gin.H{
 		c.JSON(http.StatusOK, gin.H{
 			"success": false,
 			"success": false,
 			"message": "无权获取同级或更高等级用户的信息",
 			"message": "无权获取同级或更高等级用户的信息",
@@ -326,14 +326,14 @@ func UpdateUser(c *gin.Context) {
 		return
 		return
 	}
 	}
 	myRole := c.GetInt("role")
 	myRole := c.GetInt("role")
-	if myRole <= originUser.Role {
+	if myRole <= originUser.Role && myRole != common.RoleRootUser {
 		c.JSON(http.StatusOK, gin.H{
 		c.JSON(http.StatusOK, gin.H{
 			"success": false,
 			"success": false,
 			"message": "无权更新同权限等级或更高权限等级的用户信息",
 			"message": "无权更新同权限等级或更高权限等级的用户信息",
 		})
 		})
 		return
 		return
 	}
 	}
-	if myRole <= updatedUser.Role {
+	if myRole <= updatedUser.Role && myRole != common.RoleRootUser {
 		c.JSON(http.StatusOK, gin.H{
 		c.JSON(http.StatusOK, gin.H{
 			"success": false,
 			"success": false,
 			"message": "无权将其他用户权限等级提升到大于等于自己的权限等级",
 			"message": "无权将其他用户权限等级提升到大于等于自己的权限等级",

+ 2 - 2
model/ability.go

@@ -9,7 +9,7 @@ type Ability struct {
 	Group     string `json:"group" gorm:"type:varchar(32);primaryKey;autoIncrement:false"`
 	Group     string `json:"group" gorm:"type:varchar(32);primaryKey;autoIncrement:false"`
 	Model     string `json:"model" gorm:"primaryKey;autoIncrement:false"`
 	Model     string `json:"model" gorm:"primaryKey;autoIncrement:false"`
 	ChannelId int    `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
 	ChannelId int    `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
-	Enabled   bool   `json:"enabled" gorm:"default:1"`
+	Enabled   bool   `json:"enabled"`
 }
 }
 
 
 func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
 func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
@@ -68,5 +68,5 @@ func (channel *Channel) UpdateAbilities() error {
 }
 }
 
 
 func UpdateAbilityStatus(channelId int, status bool) error {
 func UpdateAbilityStatus(channelId int, status bool) error {
-	return DB.Model(&Ability{}).Where("channel_id = ?", channelId).Update("enabled", status).Error
+	return DB.Model(&Ability{}).Where("channel_id = ?", channelId).Select("enabled").Update("enabled", status).Error
 }
 }

+ 1 - 0
model/channel.go

@@ -91,6 +91,7 @@ func (channel *Channel) Update() error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
+	DB.Model(channel).First(channel, "id = ?", channel.Id)
 	err = channel.UpdateAbilities()
 	err = channel.UpdateAbilities()
 	return err
 	return err
 }
 }

+ 31 - 5
web/src/pages/Channel/EditChannel.js

@@ -1,7 +1,7 @@
 import React, { useEffect, useState } from 'react';
 import React, { useEffect, useState } from 'react';
 import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
 import { Button, Form, Header, Message, 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, showInfo, showSuccess } from '../../helpers';
 import { CHANNEL_OPTIONS } from '../../constants';
 import { CHANNEL_OPTIONS } from '../../constants';
 
 
 const EditChannel = () => {
 const EditChannel = () => {
@@ -15,13 +15,15 @@ const EditChannel = () => {
     key: '',
     key: '',
     base_url: '',
     base_url: '',
     other: '',
     other: '',
+    group: 'default',
     models: [],
     models: [],
   };
   };
   const [batch, setBatch] = useState(false);
   const [batch, setBatch] = useState(false);
   const [inputs, setInputs] = useState(originInputs);
   const [inputs, setInputs] = useState(originInputs);
   const [modelOptions, setModelOptions] = useState([]);
   const [modelOptions, setModelOptions] = useState([]);
+  const [basicModels, setBasicModels] = useState([]);
+  const [fullModels, setFullModels] = useState([]);
   const handleInputChange = (e, { name, value }) => {
   const handleInputChange = (e, { name, value }) => {
-    console.log(name, value);
     setInputs((inputs) => ({ ...inputs, [name]: value }));
     setInputs((inputs) => ({ ...inputs, [name]: value }));
   };
   };
 
 
@@ -49,8 +51,10 @@ const EditChannel = () => {
         text: model.id,
         text: model.id,
         value: model.id,
         value: model.id,
       })));
       })));
+      setFullModels(res.data.data.map((model) => model.id));
+      setBasicModels(res.data.data.filter((model) => !model.id.startsWith("gpt-4")).map((model) => model.id));
     } catch (error) {
     } catch (error) {
-      console.error('Error fetching models:', error);
+      showError(error.message);
     }
     }
   };
   };
 
 
@@ -62,7 +66,10 @@ const EditChannel = () => {
   }, []);
   }, []);
 
 
   const submit = async () => {
   const submit = async () => {
-    if (!isEdit && (inputs.name === '' || inputs.key === '')) return;
+    if (!isEdit && (inputs.name === '' || inputs.key === '')) {
+      showInfo('请填写渠道名称和渠道密钥!');
+      return;
+    }
     let localInputs = inputs;
     let localInputs = inputs;
     if (localInputs.base_url.endsWith('/')) {
     if (localInputs.base_url.endsWith('/')) {
       localInputs.base_url = localInputs.base_url.slice(0, localInputs.base_url.length - 1);
       localInputs.base_url = localInputs.base_url.slice(0, localInputs.base_url.length - 1);
@@ -159,9 +166,20 @@ const EditChannel = () => {
               autoComplete='new-password'
               autoComplete='new-password'
             />
             />
           </Form.Field>
           </Form.Field>
+          <Form.Field>
+            <Form.Input
+              label='分组'
+              name='group'
+              placeholder={'请输入分组'}
+              onChange={handleInputChange}
+              value={inputs.group}
+              autoComplete='new-password'
+            />
+          </Form.Field>
           <Form.Field>
           <Form.Field>
             <Form.Dropdown
             <Form.Dropdown
-              label='支持的模型'
+              label='模型'
+              placeholder={'请选择该通道所支持的模型'}
               name='models'
               name='models'
               fluid
               fluid
               multiple
               multiple
@@ -172,6 +190,14 @@ const EditChannel = () => {
               options={modelOptions}
               options={modelOptions}
             />
             />
           </Form.Field>
           </Form.Field>
+          <div style={{ lineHeight: '40px', marginBottom: '12px'}}>
+            <Button type={'button'} onClick={() => {
+              handleInputChange(null, { name: 'models', value: basicModels });
+            }}>填入基础模型</Button>
+            <Button type={'button'} onClick={() => {
+              handleInputChange(null, { name: 'models', value: fullModels });
+            }}>填入所有模型</Button>
+          </div>
           {
           {
             batch ? <Form.Field>
             batch ? <Form.Field>
               <Form.TextArea
               <Form.TextArea

+ 14 - 3
web/src/pages/User/EditUser.js

@@ -15,8 +15,9 @@ const EditUser = () => {
     wechat_id: '',
     wechat_id: '',
     email: '',
     email: '',
     quota: 0,
     quota: 0,
+    group: 'default'
   });
   });
-  const { username, display_name, password, github_id, wechat_id, email, quota } =
+  const { username, display_name, password, github_id, wechat_id, email, quota, group } =
     inputs;
     inputs;
   const handleInputChange = (e, { name, value }) => {
   const handleInputChange = (e, { name, value }) => {
     setInputs((inputs) => ({ ...inputs, [name]: value }));
     setInputs((inputs) => ({ ...inputs, [name]: value }));
@@ -98,7 +99,17 @@ const EditUser = () => {
             />
             />
           </Form.Field>
           </Form.Field>
           {
           {
-            userId && (
+            userId && <>
+              <Form.Field>
+                <Form.Input
+                  label='分组'
+                  name='group'
+                  placeholder={'请输入用户分组'}
+                  onChange={handleInputChange}
+                  value={group}
+                  autoComplete='new-password'
+                />
+              </Form.Field>
               <Form.Field>
               <Form.Field>
                 <Form.Input
                 <Form.Input
                   label='剩余额度'
                   label='剩余额度'
@@ -110,7 +121,7 @@ const EditUser = () => {
                   autoComplete='new-password'
                   autoComplete='new-password'
                 />
                 />
               </Form.Field>
               </Form.Field>
-            )
+            </>
           }
           }
           <Form.Field>
           <Form.Field>
             <Form.Input
             <Form.Input