distributeapisecretlogic.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Code scaffolded by goctl. Safe to edit.
  2. // goctl 1.10.2
  3. package application
  4. import (
  5. "context"
  6. "errors"
  7. "udeServer/internal/apierr"
  8. "udeServer/internal/log"
  9. "udeServer/internal/model"
  10. "udeServer/internal/svc"
  11. "udeServer/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type DistributeApiSecretLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. // 下发应用凭证密钥
  20. func NewDistributeApiSecretLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DistributeApiSecretLogic {
  21. return &DistributeApiSecretLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *DistributeApiSecretLogic) DistributeApiSecret(req *types.ApplicationCredentialsDistributeReq) (*types.ApplicationCredentialsDistributeResp, error) {
  28. if err := checkId(req.Id); err != nil {
  29. return nil, err
  30. }
  31. cred, err := l.svcCtx.ApplicationCredModel.FindOne(l.ctx, req.Id)
  32. if errors.Is(err, model.ErrNotFound) {
  33. return nil, apierr.NotFound("application credential", req.Id)
  34. }
  35. if err != nil {
  36. log.RecordError(l.ctx, err)
  37. return nil, apierr.New(500, "查询应用凭证失败")
  38. }
  39. // 仅此接口返回密钥原文
  40. return &types.ApplicationCredentialsDistributeResp{ApiSecret: cred.ApiSecret}, nil
  41. }