captcha_util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package utils
  2. import (
  3. "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
  5. "github.com/dchest/captcha"
  6. "errors"
  7. "bytes"
  8. "time"
  9. "fmt"
  10. )
  11. func init() {
  12. imageCaptchaConfig.length = captcha.DefaultLen
  13. imageCaptchaConfig.collect_num = captcha.CollectNum
  14. imageCaptchaConfig.expiration = captcha.Expiration
  15. sms_url := config.AppConfig.GetKey("sms_url")
  16. sms_tmpl := config.AppConfig.GetKey("sms_tmpl")
  17. smsConfig.send_format = fmt.Sprintf("%s/send?mobile=%%s&name=%s", sms_url, sms_tmpl)
  18. smsConfig.check_format = fmt.Sprintf("%s/check?mobile=%%s&code=%%s", sms_url)
  19. }
  20. var imageCaptchaConfig struct {
  21. // 验证码长度
  22. length int
  23. // 验证码缓存数量
  24. collect_num int
  25. // 验证码超时时间
  26. expiration time.Duration
  27. }
  28. // @Description 生成图片验证码
  29. // @Params width int 生成图片的宽高
  30. // @Return id string 验证码id
  31. // @Return image *bytes.Buffer 图片buffer
  32. // @Return err error 错误
  33. func GenerateImageCaptcha(width, height, length int) (string, *bytes.Buffer, error) {
  34. var captchaId string
  35. var image bytes.Buffer
  36. if width <= 0 {
  37. width = 240
  38. }
  39. if height <= 0 {
  40. height = 80
  41. }
  42. if length <= 0 {
  43. length = imageCaptchaConfig.length
  44. }
  45. captchaId = captcha.NewLen(length)
  46. err := captcha.WriteImage(&image, captchaId, width, height)
  47. return captchaId, &image, err
  48. }
  49. // @Description 图片验证码校验
  50. // @Param code string 验证码
  51. // @Param id 验证码id 手机号码
  52. // @Return result bool 验证结果 true 成功 false 失败
  53. func ImageCaptchaCheck(code, id string) bool {
  54. if code == "" || id == "" {
  55. return false
  56. }
  57. return captcha.VerifyString(id, code)
  58. }
  59. // @Description 设置图片验证码长度
  60. // @Param length int 验证码长度
  61. func SetImageCaptchaLength(length int) {
  62. if length != imageCaptchaConfig.length {
  63. imageCaptchaConfig.length = length
  64. }
  65. }
  66. // @Description 设置图片验证码超时时间
  67. // @Param expiration time.Duration 超时时间
  68. func SetImageCaptchaExpiration(expiration time.Duration) {
  69. if expiration != imageCaptchaConfig.expiration {
  70. imageCaptchaConfig.expiration = expiration
  71. memoryStore := captcha.NewMemoryStore(imageCaptchaConfig.collect_num, expiration)
  72. captcha.SetCustomStore(memoryStore)
  73. }
  74. }
  75. // @Description 设置图片验证码缓存数量
  76. // @Param collect_num int 缓存数量
  77. func SetImageCaptchaCollectNum(collect_num int) {
  78. if collect_num != imageCaptchaConfig.collect_num {
  79. imageCaptchaConfig.collect_num = collect_num
  80. memoryStore := captcha.NewMemoryStore(collect_num, imageCaptchaConfig.expiration)
  81. captcha.SetCustomStore(memoryStore)
  82. }
  83. }
  84. var smsConfig struct {
  85. send_format string
  86. check_format string
  87. }
  88. // @Description 发送短信验证码
  89. // @Param mobile string 手机号码
  90. // @Return data []byte 发送结果
  91. // @Return err error 错误
  92. func SendSmsCaptcha(mobile string) ([]byte, error) {
  93. if mobile == "" {
  94. return nil, errors.New("mobile is cannot empty")
  95. }
  96. sms_url := fmt.Sprintf(smsConfig.send_format, mobile)
  97. fmt.Println("------>sms_url", sms_url)
  98. data, err := NewHttpUtil().Get(sms_url, nil, nil)
  99. logs.Debug(string(data))
  100. if err != nil {
  101. logs.Error("send sms captcha failure:", err)
  102. }
  103. return data, err
  104. }
  105. // @Description 短信验证码校验
  106. // @Param captcha string 验证码
  107. // @Param mobile string 手机号码
  108. // @Return result bool 验证结果 true 成功 false 失败
  109. func SmsCaptchaCheck(captcha, mobile string) bool {
  110. if captcha == "" || mobile == "" {
  111. return false
  112. }
  113. sms_url := fmt.Sprintf(smsConfig.check_format, mobile, captcha)
  114. logs.Debug("-----sms_check_url-------")
  115. logs.Debug(sms_url)
  116. data, err := NewHttpUtil().Get(sms_url, nil, nil)
  117. return err == nil && string(data) != "0"
  118. }