You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

63 lines
1.6 KiB

package ai
import (
"context"
"fmt"
"github.com/youruser/base/internal/svc"
"github.com/youruser/base/internal/types"
"github.com/youruser/base/model"
"github.com/zeromicro/go-zero/core/logx"
)
type AiSystemKeyCreateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAiSystemKeyCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AiSystemKeyCreateLogic {
return &AiSystemKeyCreateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AiSystemKeyCreateLogic) AiSystemKeyCreate(req *types.AIApiKeyCreateRequest) (resp *types.AIApiKeyInfo, err error) {
apiKey := &model.AIApiKey{
ProviderId: req.ProviderId,
UserId: 0, // system key
KeyValue: req.KeyValue,
IsActive: true,
Remark: req.Remark,
}
id, err := model.AIApiKeyInsert(l.ctx, l.svcCtx.DB, apiKey)
if err != nil {
return nil, fmt.Errorf("创建系统密钥失败: %v", err)
}
created, err := model.AIApiKeyFindOne(l.ctx, l.svcCtx.DB, id)
if err != nil {
return nil, fmt.Errorf("查询系统密钥失败: %v", err)
}
providerName := ""
provider, provErr := model.AIProviderFindOne(l.ctx, l.svcCtx.DB, created.ProviderId)
if provErr == nil {
providerName = provider.DisplayName
}
return &types.AIApiKeyInfo{
Id: created.Id,
ProviderId: created.ProviderId,
ProviderName: providerName,
UserId: 0,
KeyPreview: maskKey(created.KeyValue),
IsActive: created.IsActive,
Remark: created.Remark,
CreatedAt: created.CreatedAt.Format("2006-01-02 15:04:05"),
}, nil
}