captcha.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package captcha
  2. /**
  3. 1、滑动式
  4. 2、区域内拖拽滑动式
  5. 3、旋转式
  6. 4、中文文本、字母/数字混合 点选
  7. 5、浅色 中文文本、字母/数字混合 点选
  8. 6、图形点选
  9. **/
  10. import (
  11. "app.yhyue.com/moapp/jybase/captcha/phoneCache"
  12. "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
  13. "fmt"
  14. "net/http"
  15. "strings"
  16. )
  17. type (
  18. GetCaptcha struct {
  19. W http.ResponseWriter
  20. R *http.Request
  21. Sess *httpsession.Session
  22. Phone string
  23. }
  24. CaptRes struct {
  25. Code int `json:"code"`
  26. CaptchaKey interface{} `json:"captcha_key"`
  27. ImageBase64 interface{} `json:"image_base64"`
  28. TileBase64 interface{} `json:"tile_base64"`
  29. ThumbBase64 interface{} `json:"thumb_base64"`
  30. TileWidth interface{} `json:"tile_width"`
  31. TileHeight interface{} `json:"tile_height"`
  32. TileX interface{} `json:"tile_x"`
  33. TileY interface{} `json:"tile_y"`
  34. ThumbSize int `json:"thumb_size"`
  35. }
  36. )
  37. func InitCaptcha() {
  38. SlideCaptcha()
  39. ClickBasicCaptcha()
  40. ClickShapeCaptcha()
  41. RotateCaptcha()
  42. go phoneCache.RunTimedTask()
  43. }
  44. func (c *GetCaptcha) GetCaptchaData() (cd CaptRes, err error) {
  45. if strings.TrimSpace(c.Phone) == "" || !PhoneCheck(c.Phone) {
  46. err = fmt.Errorf("phone param is empty or incorrect")
  47. return
  48. }
  49. var times int // phoneCache.GetAccessCount(c.Phone)
  50. switch {
  51. case times < MiniTimes:
  52. return c.GetSlideCaptchaData()
  53. case times < MaxTimes:
  54. return c.GetRotateCaptchaData()
  55. default:
  56. return c.GetClickCaptchaData()
  57. }
  58. }