mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	feat: support batch creation of channels (close #58)
This commit is contained in:
		@@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"one-api/common"
 | 
			
		||||
	"one-api/model"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func GetAllChannels(c *gin.Context) {
 | 
			
		||||
@@ -84,7 +85,17 @@ func AddChannel(c *gin.Context) {
 | 
			
		||||
	}
 | 
			
		||||
	channel.CreatedTime = common.GetTimestamp()
 | 
			
		||||
	channel.AccessedTime = common.GetTimestamp()
 | 
			
		||||
	err = channel.Insert()
 | 
			
		||||
	keys := strings.Split(channel.Key, "\n")
 | 
			
		||||
	channels := make([]model.Channel, 0)
 | 
			
		||||
	for _, key := range keys {
 | 
			
		||||
		if key == "" {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		localChannel := channel
 | 
			
		||||
		localChannel.Key = key
 | 
			
		||||
		channels = append(channels, localChannel)
 | 
			
		||||
	}
 | 
			
		||||
	err = model.BatchInsertChannels(channels)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{
 | 
			
		||||
			"success": false,
 | 
			
		||||
 
 | 
			
		||||
@@ -53,6 +53,12 @@ func GetRandomChannel() (*Channel, error) {
 | 
			
		||||
	return &channel, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BatchInsertChannels(channels []Channel) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	err = DB.Create(&channels).Error
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (channel *Channel) Insert() error {
 | 
			
		||||
	var err error
 | 
			
		||||
	err = DB.Create(channel).Error
 | 
			
		||||
 
 | 
			
		||||
@@ -16,8 +16,10 @@ const EditChannel = () => {
 | 
			
		||||
    base_url: '',
 | 
			
		||||
    other: ''
 | 
			
		||||
  };
 | 
			
		||||
  const [batch, setBatch] = useState(false);
 | 
			
		||||
  const [inputs, setInputs] = useState(originInputs);
 | 
			
		||||
  const handleInputChange = (e, { name, value }) => {
 | 
			
		||||
    console.log(name, value);
 | 
			
		||||
    setInputs((inputs) => ({ ...inputs, [name]: value }));
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
@@ -130,17 +132,38 @@ const EditChannel = () => {
 | 
			
		||||
              autoComplete='new-password'
 | 
			
		||||
            />
 | 
			
		||||
          </Form.Field>
 | 
			
		||||
          <Form.Field>
 | 
			
		||||
            <Form.Input
 | 
			
		||||
              label='密钥'
 | 
			
		||||
              name='key'
 | 
			
		||||
              placeholder={'请输入密钥'}
 | 
			
		||||
              onChange={handleInputChange}
 | 
			
		||||
              value={inputs.key}
 | 
			
		||||
              // type='password'
 | 
			
		||||
              autoComplete='new-password'
 | 
			
		||||
            />
 | 
			
		||||
          </Form.Field>
 | 
			
		||||
          {
 | 
			
		||||
            batch ? <Form.Field>
 | 
			
		||||
              <Form.TextArea
 | 
			
		||||
                label='密钥'
 | 
			
		||||
                name='key'
 | 
			
		||||
                placeholder={'请输入密钥,一行一个'}
 | 
			
		||||
                onChange={handleInputChange}
 | 
			
		||||
                value={inputs.key}
 | 
			
		||||
                style={{ minHeight: 150, fontFamily: 'JetBrains Mono, Consolas' }}
 | 
			
		||||
                autoComplete='new-password'
 | 
			
		||||
              />
 | 
			
		||||
            </Form.Field> : <Form.Field>
 | 
			
		||||
              <Form.Input
 | 
			
		||||
                label='密钥'
 | 
			
		||||
                name='key'
 | 
			
		||||
                placeholder={'请输入密钥'}
 | 
			
		||||
                onChange={handleInputChange}
 | 
			
		||||
                value={inputs.key}
 | 
			
		||||
                autoComplete='new-password'
 | 
			
		||||
                />
 | 
			
		||||
            </Form.Field>
 | 
			
		||||
          }
 | 
			
		||||
          {
 | 
			
		||||
            !isEdit && (
 | 
			
		||||
              <Form.Checkbox
 | 
			
		||||
                checked={batch}
 | 
			
		||||
                label='批量创建'
 | 
			
		||||
                name='batch'
 | 
			
		||||
                onChange={() => setBatch(!batch)}
 | 
			
		||||
              />
 | 
			
		||||
            )
 | 
			
		||||
          }
 | 
			
		||||
          <Button onClick={submit}>提交</Button>
 | 
			
		||||
        </Form>
 | 
			
		||||
      </Segment>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user