captcha.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/app/service/main/account-recovery/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "go-common/library/net/metadata"
  9. )
  10. // GetToken get open token.
  11. func (d *Dao) GetToken(c context.Context, bid string) (res *model.TokenResq, err error) {
  12. params := url.Values{}
  13. params.Add("bid", bid)
  14. if err = d.httpClient.Get(c, d.c.CaptchaConf.TokenURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  15. log.Error("GetToken HTTP request err(%v)", err)
  16. return
  17. }
  18. if res.Code != 0 {
  19. log.Error("GetToken service return err(%v)", res.Code)
  20. err = ecode.Int(int(res.Code))
  21. return
  22. }
  23. return
  24. }
  25. // Verify verify code.
  26. func (d *Dao) Verify(c context.Context, code, token string) (ok bool, err error) {
  27. params := url.Values{}
  28. params.Add("token", token)
  29. params.Add("code", code)
  30. res := new(struct {
  31. Code int `json:"code"`
  32. })
  33. if err = d.httpClient.Post(c, d.c.CaptchaConf.VerifyURL, metadata.String(c, metadata.RemoteIP), params, res); err != nil {
  34. log.Error("Verify HTTP request err(%v)", err)
  35. return
  36. }
  37. if res.Code != 0 {
  38. log.Error("Verify service return err(%v)", res.Code)
  39. err = ecode.Int(res.Code)
  40. return
  41. }
  42. return true, nil
  43. }