antispam.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package realname
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/account/conf"
  6. "go-common/library/cache/memcache"
  7. "github.com/pkg/errors"
  8. )
  9. func alipayAntispamKey(mid int64) string {
  10. return fmt.Sprintf("raa_%d", mid)
  11. }
  12. //AlipayAntispamValue 最低位为pass flag ,高位为计数
  13. //计数:判断用户的申请次数
  14. //flag:是否通过本次防刷验证(极验是否通过)
  15. type AlipayAntispamValue int
  16. // IncreaseCount add antispam count
  17. func (a *AlipayAntispamValue) IncreaseCount() {
  18. *a = AlipayAntispamValue((a.Count()+1)<<1 + a.Flag())
  19. }
  20. // SetPass set is antispam verified (such as when geetest passed)
  21. func (a *AlipayAntispamValue) SetPass(pass bool) {
  22. var flag int
  23. if pass {
  24. flag = 1
  25. }
  26. *a = AlipayAntispamValue(a.Count()<<1 + flag)
  27. }
  28. // Count return antispam hit count
  29. func (a *AlipayAntispamValue) Count() int {
  30. return int(*a) >> 1
  31. }
  32. // Flag return antispam pass flag
  33. func (a *AlipayAntispamValue) Flag() int {
  34. return int(*a) & 0x1
  35. }
  36. // Pass return is antispam passed (such as when geetest passed)
  37. func (a *AlipayAntispamValue) Pass() bool {
  38. return a.Flag() > 0
  39. }
  40. // AlipayAntispam get alipay antispam count by mid
  41. func (d *Dao) AlipayAntispam(c context.Context, mid int64) (value *AlipayAntispamValue, err error) {
  42. var (
  43. key = alipayAntispamKey(mid)
  44. conn = d.mc.Get(c)
  45. item *memcache.Item
  46. )
  47. defer conn.Close()
  48. if item, err = conn.Get(key); err != nil {
  49. if err == memcache.ErrNotFound {
  50. err = nil
  51. return
  52. }
  53. err = errors.Wrapf(err, "conn.Get(%s)", key)
  54. return
  55. }
  56. value = new(AlipayAntispamValue)
  57. if err = conn.Scan(item, &value); err != nil {
  58. err = errors.Wrapf(err, "conn.Scan(%+v)", item)
  59. return
  60. }
  61. return
  62. }
  63. // SetAlipayAntispam set alipay antispam count by mid
  64. func (d *Dao) SetAlipayAntispam(c context.Context, mid int64, value *AlipayAntispamValue) (err error) {
  65. var (
  66. key = alipayAntispamKey(mid)
  67. conn = d.mc.Get(c)
  68. )
  69. defer conn.Close()
  70. if err = conn.Set(&memcache.Item{Key: key, Object: value, Flags: memcache.FlagJSON, Expiration: conf.Conf.Realname.AlipayAntispamTTL}); err != nil {
  71. err = errors.Wrapf(err, "conn.Set(%s,%+v)", key, value)
  72. return
  73. }
  74. return
  75. }
  76. // DeleteAlipayAntispam delete alipay antispam count by mid
  77. func (d *Dao) DeleteAlipayAntispam(c context.Context, mid int64) (err error) {
  78. var (
  79. key = alipayAntispamKey(mid)
  80. conn = d.mc.Get(c)
  81. )
  82. defer conn.Close()
  83. if err = conn.Delete(key); err != nil {
  84. if err == memcache.ErrNotFound {
  85. err = nil
  86. return
  87. }
  88. err = errors.Wrapf(err, "conn.Delete(%s)", key)
  89. return
  90. }
  91. return
  92. }