captcha.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package http
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "go-common/app/interface/main/answer/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func validate(c *bm.Context) {
  12. var (
  13. mid, _ = c.Get("mid")
  14. params = c.Request.Form
  15. challenge = params.Get("geetest_challenge")
  16. validate = params.Get("geetest_validate")
  17. seccode = params.Get("geetest_seccode")
  18. success = params.Get("geetest_success")
  19. captchaType = params.Get("captcha_type") // typ == "gt"
  20. cookie = c.Request.Header.Get("cookie")
  21. mobile = strings.Contains(c.Request.UserAgent(), model.MobileUserAgentFlag)
  22. ct string
  23. sid string
  24. )
  25. if mobile {
  26. ct = model.PlatH5
  27. } else {
  28. ct = model.PlatPC
  29. }
  30. successi, err := strconv.Atoi(success)
  31. if err != nil {
  32. successi = 1
  33. }
  34. sidCookie, err := c.Request.Cookie("sid")
  35. if err != nil {
  36. log.Warn("cookie do not contains sid error(%v)", err)
  37. } else {
  38. sid = sidCookie.Value
  39. }
  40. comargs := map[string]string{
  41. "ua": c.Request.Header.Get("User-Agent"),
  42. "buvid": c.Request.Header.Get("Buvid"),
  43. "refer": c.Request.Header.Get("Referer"),
  44. "url": c.Request.Header.Get("URL"),
  45. "sid": sid,
  46. }
  47. if captchaType == model.BiliCaptcha {
  48. validate = params.Get("token")
  49. seccode = params.Get("code")
  50. if validate == "" || seccode == "" {
  51. log.Error("validate(%+v) ", params)
  52. c.JSON(nil, ecode.RequestErr)
  53. }
  54. }
  55. req, err := svc.Validate(c, challenge, validate, seccode, ct, successi, mid.(int64), cookie, captchaType, comargs)
  56. if err != nil {
  57. log.Error("svc.Validate(%d,%d,%s,%s,%s,%s,%s,%s) error(%+v)", mid.(int64), successi, challenge, validate, seccode, ct,
  58. cookie, comargs, err)
  59. c.JSON(nil, err)
  60. return
  61. }
  62. cool, err := svc.Cool(c, req.HistoryID, mid.(int64))
  63. if err != nil {
  64. c.JSON(nil, err)
  65. return
  66. }
  67. cool.Hid = req.HistoryID
  68. cool.URL = fmt.Sprintf(model.ProPassed, req.HistoryID)
  69. c.JSON(cool, nil)
  70. }
  71. // captcha get captcha
  72. func captcha(c *bm.Context) {
  73. var (
  74. err error
  75. mid, _ = c.Get("mid")
  76. mobile = strings.Contains(c.Request.UserAgent(), model.MobileUserAgentFlag)
  77. ct string
  78. )
  79. if mobile {
  80. ct = model.PlatH5
  81. } else {
  82. ct = model.PlatPC
  83. }
  84. req, err := svc.Captcha(c, mid.(int64), ct, 1)
  85. if err != nil {
  86. log.Error("svc.QueCaptcha(%d,%s) error(%+v)", mid.(int64), ct, err)
  87. c.JSON(nil, err)
  88. return
  89. }
  90. c.JSON(req, nil)
  91. }