resource.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package vip
  2. import (
  3. "context"
  4. "go-common/app/interface/main/account/model"
  5. vipmod "go-common/app/service/main/vip/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "go-common/library/net/metadata"
  9. "github.com/pkg/errors"
  10. )
  11. // CodeOpen .
  12. func (s *Service) CodeOpen(c context.Context, mid int64, code, token, verify string) (codeInfo *model.ResourceCode, err error) {
  13. var (
  14. codeResp *model.ResourceCodeResq
  15. )
  16. if codeResp, err = s.vipDao.CodeOpen(c, mid, code, token, verify); err != nil {
  17. err = errors.WithStack(err)
  18. return
  19. }
  20. codeInfo = codeResp.Data
  21. return
  22. }
  23. //CodeOpeneds sel code opened
  24. func (s *Service) CodeOpeneds(c context.Context, arg *model.CodeInfoReq, ip string) (resp []*vipmod.CodeInfoResp, err error) {
  25. if err = s.checkIP(arg.Appkey, ip); err != nil {
  26. err = errors.WithStack(err)
  27. return
  28. }
  29. if resp, err = s.vipDao.CodeOpeneds(c, arg, ip); err != nil {
  30. err = errors.WithStack(err)
  31. }
  32. return
  33. }
  34. func (s *Service) checkIP(appkey, ip string) (err error) {
  35. var (
  36. strings []string
  37. ok bool
  38. )
  39. if strings, ok = s.c.Vipproperty.CodeOpenwhiteIPMap[appkey]; !ok {
  40. log.Error("checkIP s.c.Vipproperty.CodeOpenwhiteIPMap empty(%s)", appkey)
  41. err = ecode.VipWhiteIPListErr
  42. return
  43. }
  44. for _, v := range strings {
  45. if v == ip {
  46. return
  47. }
  48. }
  49. log.Error("checkIP fail(%s, %s)", appkey, ip)
  50. err = ecode.VipWhiteIPListErr
  51. return
  52. }
  53. // CodeVerify .
  54. func (s *Service) CodeVerify(c context.Context) (token *model.Token, err error) {
  55. if token, err = s.vipDao.CodeVerify(c); err != nil {
  56. err = errors.WithStack(err)
  57. }
  58. return
  59. }
  60. //UseBatch use resource batch
  61. func (s *Service) UseBatch(c context.Context, arg *vipmod.ArgUseBatch) (err error) {
  62. ip := metadata.String(c, metadata.RemoteIP)
  63. if err = s.checkIP(arg.Appkey, ip); err != nil {
  64. err = errors.WithStack(err)
  65. return
  66. }
  67. if err = s.vipRPC.ResourceBatchOpenVip(c, arg); err != nil {
  68. err = errors.WithStack(err)
  69. return
  70. }
  71. return
  72. }