update_token_logic.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package token
  2. import (
  3. "context"
  4. "github.com/zeromicro/go-zero/core/errorx"
  5. "time"
  6. "wechat-api/ent"
  7. "wechat-api/ent/token"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type UpdateTokenLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewUpdateTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateTokenLogic {
  20. return &UpdateTokenLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *UpdateTokenLogic) UpdateToken(req *types.TokenInfo) (*types.BaseMsgResp, error) {
  27. //isAdmin := l.ctx.Value("isAdmin").(bool)
  28. //if !isAdmin {
  29. // l.Error("没有权限,请联系管理员", nil)
  30. // return nil, errorx.NewDefaultError("没有权限,请联系管理员")
  31. //}
  32. var (
  33. expireAt *time.Time
  34. err error
  35. )
  36. if req.ExpireAtStr != nil {
  37. expireAtdata, err := time.Parse("2006-01-02 15:04:05", *req.ExpireAtStr)
  38. if err != nil {
  39. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  40. }
  41. expireAt = &expireAtdata
  42. }
  43. l.Logger.Errorf("------------------------item--------------------------- %+v", expireAt)
  44. if req.Token != nil {
  45. item, err := l.svcCtx.DB.Token.Query().Where(token.IDNEQ(*req.Id)).Where(token.TokenEQ(*req.Token)).First(l.ctx)
  46. l.Logger.Errorf("------------------------err--------------------------- %+v", err)
  47. l.Logger.Errorf("------------------------item--------------------------- %+v", item)
  48. if err != nil && !ent.IsNotFound(err) {
  49. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  50. }
  51. if item != nil && item.ID > 0 {
  52. return nil, errorx.NewCodeInvalidArgumentError("Token已经存在")
  53. }
  54. }
  55. l.Logger.Errorf("------------------------item2--------------------------- %+v")
  56. err = l.svcCtx.DB.Token.UpdateOneID(*req.Id).
  57. SetNotNilExpireAt(expireAt).
  58. SetNotNilToken(req.Token).
  59. SetNotNilMAC(req.Mac).
  60. SetNotNilOrganizationID(req.OrganizationId).
  61. SetNotNilAgentID(req.AgentId).
  62. SetNotNilCustomAgentBase(req.CustomAgentBase).
  63. SetNotNilCustomAgentKey(req.CustomAgentKey).
  64. SetNotNilOpenaiBase(req.OpenaiBase).
  65. SetNotNilOpenaiKey(req.OpenaiKey).
  66. Exec(l.ctx)
  67. if err != nil {
  68. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  69. }
  70. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  71. }