Răsfoiți Sursa

feat: now one channel can belong to multiple groups (close #153)

JustSong 2 ani în urmă
părinte
comite
7f9577a386
2 a modificat fișierele cu 19 adăugiri și 10 ștergeri
  1. 9 6
      model/ability.go
  2. 10 4
      web/src/pages/Channel/EditChannel.js

+ 9 - 6
model/ability.go

@@ -30,15 +30,18 @@ func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
 
 func (channel *Channel) AddAbilities() error {
 	models_ := strings.Split(channel.Models, ",")
+	groups_ := strings.Split(channel.Group, ",")
 	abilities := make([]Ability, 0, len(models_))
 	for _, model := range models_ {
-		ability := Ability{
-			Group:     channel.Group,
-			Model:     model,
-			ChannelId: channel.Id,
-			Enabled:   channel.Status == common.ChannelStatusEnabled,
+		for _, group := range groups_ {
+			ability := Ability{
+				Group:     group,
+				Model:     model,
+				ChannelId: channel.Id,
+				Enabled:   channel.Status == common.ChannelStatusEnabled,
+			}
+			abilities = append(abilities, ability)
 		}
-		abilities = append(abilities, ability)
 	}
 	return DB.Create(&abilities).Error
 }

+ 10 - 4
web/src/pages/Channel/EditChannel.js

@@ -15,8 +15,8 @@ const EditChannel = () => {
     key: '',
     base_url: '',
     other: '',
-    group: 'default',
     models: [],
+    groups: ['default']
   };
   const [batch, setBatch] = useState(false);
   const [inputs, setInputs] = useState(originInputs);
@@ -37,6 +37,11 @@ const EditChannel = () => {
       } else {
         data.models = data.models.split(",")
       }
+      if (data.group === "") {
+        data.groups = []
+      } else {
+        data.groups = data.group.split(",")
+      }
       setInputs(data);
     } else {
       showError(message);
@@ -94,6 +99,7 @@ const EditChannel = () => {
     }
     let res;
     localInputs.models = localInputs.models.join(",")
+    localInputs.group = localInputs.groups.join(",")
     if (isEdit) {
       res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
     } else {
@@ -185,14 +191,14 @@ const EditChannel = () => {
             <Form.Dropdown
               label='分组'
               placeholder={'请选择分组'}
-              name='group'
+              name='groups'
               fluid
-              search
+              multiple
               selection
               allowAdditions
               additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
               onChange={handleInputChange}
-              value={inputs.group}
+              value={inputs.groups}
               autoComplete='new-password'
               options={groupOptions}
             />