captcha_api.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/app/service/main/coupon/model"
  6. "go-common/library/ecode"
  7. "github.com/pkg/errors"
  8. )
  9. //CaptchaToken get captcha token.
  10. func (d *Dao) CaptchaToken(c context.Context, bid string, ip string) (res *model.Token, err error) {
  11. args := url.Values{}
  12. args.Add("bid", bid)
  13. resq := new(struct {
  14. Code int `json:"code"`
  15. Data *model.Token `json:"data"`
  16. })
  17. if err = d.client.Get(c, d.c.Property.CaptchaTokenURL, ip, args, resq); err != nil {
  18. err = errors.Wrapf(err, "dao captcha token do")
  19. return
  20. }
  21. if resq.Code != ecode.OK.Code() {
  22. err = ecode.Int(resq.Code)
  23. err = errors.Wrapf(err, "dao captcha token code")
  24. return
  25. }
  26. res = resq.Data
  27. return
  28. }
  29. //CaptchaVerify get captcha verify.
  30. func (d *Dao) CaptchaVerify(c context.Context, code string, token string, ip string) (err error) {
  31. args := url.Values{}
  32. args.Add("token", token)
  33. args.Add("code", code)
  34. resq := new(struct {
  35. Code int `json:"code"`
  36. })
  37. if err = d.client.Post(c, d.c.Property.CaptchaVerifyURL, ip, args, resq); err != nil {
  38. err = errors.Wrapf(err, "dao captcha verify do")
  39. return
  40. }
  41. if resq.Code != ecode.OK.Code() {
  42. err = ecode.CouponCodeVerifyFaildErr
  43. }
  44. return
  45. }