dao.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package geetest
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/interface/main/videoup/conf"
  10. "go-common/app/interface/main/videoup/model/geetest"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. httpx "go-common/library/net/http/blademaster"
  14. "go-common/library/net/metadata"
  15. )
  16. const (
  17. _validate = "/validate.php"
  18. )
  19. // Dao is account dao.
  20. type Dao struct {
  21. c *conf.Config
  22. // url
  23. validateURI string
  24. // http client
  25. clientX *httpx.Client
  26. }
  27. // New new a dao.
  28. func New(c *conf.Config) (d *Dao) {
  29. d = &Dao{
  30. c: c,
  31. validateURI: c.Host.Geetest + _validate,
  32. clientX: httpx.NewClient(c.HTTPClient.Read),
  33. }
  34. return
  35. }
  36. // Validate recheck the challenge code and get to seccode
  37. func (d *Dao) Validate(c context.Context, challenge, seccode, clientType, captchaID string, mid int64) (res *geetest.ValidateRes, err error) {
  38. params := url.Values{}
  39. params.Set("seccode", seccode)
  40. params.Set("challenge", challenge)
  41. params.Set("captchaid", captchaID)
  42. params.Set("client_type", clientType)
  43. params.Set("ip_address", metadata.String(c, metadata.RemoteIP))
  44. params.Set("json_format", "1")
  45. params.Set("sdk", "golang_3.0.0")
  46. params.Set("user_id", strconv.FormatInt(mid, 10))
  47. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  48. req, err := http.NewRequest("POST", d.validateURI, strings.NewReader(params.Encode()))
  49. if err != nil {
  50. log.Error("http.NewRequest error(%v) | uri(%s) params(%s)", err, d.validateURI, params.Encode())
  51. err = ecode.CreativeGeetestAPIErr
  52. return
  53. }
  54. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  55. if err = d.clientX.Do(c, req, &res); err != nil {
  56. log.Error("d.client.Do error(%v)", err)
  57. err = ecode.CreativeGeetestAPIErr
  58. return
  59. }
  60. return
  61. }