service_create.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package authorized
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "io"
  6. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  7. "github.com/xinliangnote/go-gin-api/internal/repository/mysql/authorized"
  8. )
  9. type CreateAuthorizedData struct {
  10. BusinessKey string `json:"business_key"` // 调用方key
  11. BusinessDeveloper string `json:"business_developer"` // 调用方对接人
  12. Remark string `json:"remark"` // 备注
  13. }
  14. func (s *service) Create(ctx core.Context, authorizedData *CreateAuthorizedData) (id int32, err error) {
  15. buf := make([]byte, 10)
  16. io.ReadFull(rand.Reader, buf)
  17. secret := hex.EncodeToString(buf)
  18. model := authorized.NewModel()
  19. model.BusinessKey = authorizedData.BusinessKey
  20. model.BusinessSecret = secret
  21. model.BusinessDeveloper = authorizedData.BusinessDeveloper
  22. model.Remark = authorizedData.Remark
  23. model.CreatedUser = ctx.SessionUserInfo().UserName
  24. model.IsUsed = 1
  25. model.IsDeleted = -1
  26. id, err = model.Create(s.db.GetDbW().WithContext(ctx.RequestContext()))
  27. if err != nil {
  28. return 0, err
  29. }
  30. return
  31. }