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.
67 lines
1.7 KiB
67 lines
1.7 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 AiSystemKeyUpdateLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewAiSystemKeyUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AiSystemKeyUpdateLogic {
|
|
return &AiSystemKeyUpdateLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *AiSystemKeyUpdateLogic) AiSystemKeyUpdate(req *types.AIApiKeyUpdateRequest) (resp *types.AIApiKeyInfo, err error) {
|
|
existing, err := model.AIApiKeyFindOne(l.ctx, l.svcCtx.DB, req.Id)
|
|
if err != nil {
|
|
if err == model.ErrNotFound {
|
|
return nil, fmt.Errorf("系统密钥不存在")
|
|
}
|
|
return nil, fmt.Errorf("查询系统密钥失败: %v", err)
|
|
}
|
|
|
|
if existing.UserId != 0 {
|
|
return nil, fmt.Errorf("该密钥不是系统密钥")
|
|
}
|
|
|
|
if req.KeyValue != "" {
|
|
existing.KeyValue = req.KeyValue
|
|
}
|
|
existing.IsActive = req.IsActive
|
|
existing.Remark = req.Remark
|
|
|
|
if err = model.AIApiKeyUpdate(l.ctx, l.svcCtx.DB, existing); err != nil {
|
|
return nil, fmt.Errorf("更新系统密钥失败: %v", err)
|
|
}
|
|
|
|
providerName := ""
|
|
provider, provErr := model.AIProviderFindOne(l.ctx, l.svcCtx.DB, existing.ProviderId)
|
|
if provErr == nil {
|
|
providerName = provider.DisplayName
|
|
}
|
|
|
|
return &types.AIApiKeyInfo{
|
|
Id: existing.Id,
|
|
ProviderId: existing.ProviderId,
|
|
ProviderName: providerName,
|
|
UserId: 0,
|
|
KeyPreview: maskKey(existing.KeyValue),
|
|
IsActive: existing.IsActive,
|
|
Remark: existing.Remark,
|
|
CreatedAt: existing.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}, nil
|
|
}
|
|
|