captcha.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package http
  2. import (
  3. "net/http"
  4. bm "go-common/library/net/http/blademaster"
  5. )
  6. // get user get a image.
  7. func get(c *bm.Context) {
  8. var (
  9. err error
  10. v = new(struct {
  11. Token string `form:"token" validate:"required"`
  12. Bid string `form:"bid" validate:"required"`
  13. })
  14. img []byte
  15. )
  16. if err = c.Bind(v); err != nil {
  17. return
  18. }
  19. if img, err = svr.CaptchaImg(c, v.Token, v.Bid); err != nil {
  20. c.JSON(nil, err)
  21. return
  22. }
  23. code := http.StatusOK
  24. c.Render(code, Image{
  25. Body: img,
  26. })
  27. }
  28. // token third business get token.
  29. func token(c *bm.Context) {
  30. var (
  31. err error
  32. v = new(struct {
  33. Bid string `form:"bid" validate:"required"`
  34. })
  35. token, url string
  36. )
  37. if err = c.Bind(v); err != nil {
  38. return
  39. }
  40. if url, token, err = svr.Token(c, v.Bid); err != nil {
  41. c.JSON(nil, err)
  42. return
  43. }
  44. data := make(map[string]interface{}, 1)
  45. data["data"] = map[string]string{
  46. "token": token,
  47. "url": url,
  48. }
  49. c.JSONMap(data, nil)
  50. }
  51. // verify third business verify.
  52. func verify(c *bm.Context) {
  53. var (
  54. err error
  55. v = new(struct {
  56. Token string `form:"token" validate:"required"`
  57. Code string `form:"code" validate:"required"`
  58. })
  59. )
  60. if err = c.Bind(v); err != nil {
  61. return
  62. }
  63. err = svr.VerifyCaptcha(c, v.Token, v.Code)
  64. c.JSON(nil, err)
  65. }