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.
 
 
 
 
 
 

73 lines
1.7 KiB

// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
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 AiApiKeyCreateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 创建API Key
func NewAiApiKeyCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AiApiKeyCreateLogic {
return &AiApiKeyCreateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AiApiKeyCreateLogic) AiApiKeyCreate(req *types.AIApiKeyCreateRequest) (resp *types.AIApiKeyInfo, err error) {
userId, _ := l.ctx.Value("userId").(int64)
apiKey := &model.AIApiKey{
ProviderId: req.ProviderId,
UserId: userId,
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("创建API Key失败: %v", err)
}
// Retrieve the inserted record to get timestamps
created, err := model.AIApiKeyFindOne(l.ctx, l.svcCtx.DB, id)
if err != nil {
return nil, fmt.Errorf("查询API Key失败: %v", err)
}
// Look up provider name
providerName := ""
provider, provErr := model.AIProviderFindOne(l.ctx, l.svcCtx.DB, created.ProviderId)
if provErr == nil {
providerName = provider.DisplayName
}
resp = &types.AIApiKeyInfo{
Id: created.Id,
ProviderId: created.ProviderId,
ProviderName: providerName,
UserId: created.UserId,
KeyPreview: maskKey(created.KeyValue),
IsActive: created.IsActive,
Remark: created.Remark,
CreatedAt: created.CreatedAt.Format("2006-01-02 15:04:05"),
}
return resp, nil
}