check.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package captcha
  2. import (
  3. "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10. type CheckCaptcha struct {
  11. W http.ResponseWriter
  12. R *http.Request
  13. Sess *httpsession.Session
  14. Phone string
  15. Point string
  16. Key string
  17. Mold int
  18. }
  19. // CheckCaptchaData .
  20. func (c *CheckCaptcha) CheckCaptchaData() (err error) {
  21. if strings.TrimSpace(c.Phone) == "" || !PhoneCheck(c.Phone) {
  22. err = fmt.Errorf("phone param is empty or incorrect")
  23. return
  24. }
  25. if c.Point == "" || c.Key == "" {
  26. err = fmt.Errorf("point or key param is empty")
  27. return
  28. }
  29. var (
  30. cacheDataByte *[]byte
  31. resCacheKey = fmt.Sprintf(ResCacheKey, c.Key, c.Phone)
  32. moldCacheKey = fmt.Sprintf(MoldCacheKey, c.Key, c.Phone)
  33. )
  34. cacheDataByte, err = redis.GetBytes(RedisCode, resCacheKey)
  35. if cacheDataByte == nil || len(*cacheDataByte) == 0 {
  36. log.Println("resCacheKey:", resCacheKey)
  37. err = fmt.Errorf("get cache value false key")
  38. return
  39. }
  40. if c.Mold == 0 {
  41. c.Mold = redis.GetInt(RedisCode, moldCacheKey)
  42. }
  43. if c.Mold == 0 {
  44. err = fmt.Errorf("check captcha time out")
  45. return
  46. }
  47. //每次缓存数据只用一次
  48. redis.Del(RedisCode, resCacheKey)
  49. redis.Del(RedisCode, moldCacheKey)
  50. switch c.Mold {
  51. case 1, 2:
  52. return c.CheckSlideCaptchaData(*cacheDataByte)
  53. case 3, 4:
  54. return c.CheckClickCaptchaData(*cacheDataByte)
  55. case 5:
  56. return c.CheckRotateCaptchaData(*cacheDataByte)
  57. }
  58. return
  59. }