resetapplicationlogic.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 ResetApplicationLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. // 重置应用凭证密钥
  20. func NewResetApplicationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetApplicationLogic {
  21. return &ResetApplicationLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *ResetApplicationLogic) ResetApplication(req *types.ApplicationCredentialsResetReq) (*types.ApplicationCredentialsResetResp, 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. // 只换密钥,apiKey/状态等保持不变
  40. cred.ApiSecret = GenerateApiSecret()
  41. cred.UpdatedBy = operatorName
  42. if err := l.svcCtx.ApplicationCredModel.Update(l.ctx, cred); err != nil {
  43. log.RecordError(l.ctx, err)
  44. return nil, apierr.New(500, "重置应用凭证密钥失败")
  45. }
  46. return &types.ApplicationCredentialsResetResp{Iden: cred.CredIden}, nil
  47. }