captcha_util.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "github.com/gin-gonic/gin"
  11. //"github.com/gin-contrib/sessions"
  12. )
  13. func init() {
  14. imageCaptchaConfig.length = captcha.DefaultLen
  15. imageCaptchaConfig.collect_num = captcha.CollectNum
  16. imageCaptchaConfig.expiration = captcha.Expiration
  17. sms_url := config.AppConfig.GetKey("sms_url")
  18. sms_tmpl := config.AppConfig.GetKey("sms_tmpl")
  19. smsConfig.send_format = fmt.Sprintf("%s/send?mobile=%%s&name=%s", sms_url, sms_tmpl)
  20. smsConfig.check_format = fmt.Sprintf("%s/check?mobile=%%s&code=%%s", sms_url)
  21. }
  22. var imageCaptchaConfig struct {
  23. // 验证码长度
  24. length int
  25. // 验证码缓存数量
  26. collect_num int
  27. // 验证码超时时间
  28. expiration time.Duration
  29. }
  30. // @Description 生成图片验证码
  31. // @Params width int 生成图片的宽高
  32. // @Return id string 验证码id
  33. // @Return image *bytes.Buffer 图片buffer
  34. // @Return err error 错误
  35. func GenerateImageCaptcha(width, height, length int) (string, *bytes.Buffer, error) {
  36. var captchaId string
  37. var image bytes.Buffer
  38. if width <= 0 {
  39. width = 240
  40. }
  41. if height <= 0 {
  42. height = 80
  43. }
  44. if length <= 0 {
  45. length = imageCaptchaConfig.length
  46. }
  47. captchaId = captcha.NewLen(length)
  48. err := captcha.WriteImage(&image, captchaId, width, height)
  49. return captchaId, &image, err
  50. }
  51. // @Description 图片验证码校验
  52. // @Param code string 验证码
  53. // @Param id 验证码id 手机号码
  54. // @Return result bool 验证结果 true 成功 false 失败
  55. func ImageCaptchaCheck(code, id string) bool {
  56. if code == "" || id == "" {
  57. return false
  58. }
  59. return captcha.VerifyString(id, code)
  60. }
  61. // @Description 设置图片验证码长度
  62. // @Param length int 验证码长度
  63. func SetImageCaptchaLength(length int) {
  64. if length != imageCaptchaConfig.length {
  65. imageCaptchaConfig.length = length
  66. }
  67. }
  68. // @Description 设置图片验证码超时时间
  69. // @Param expiration time.Duration 超时时间
  70. func SetImageCaptchaExpiration(expiration time.Duration) {
  71. if expiration != imageCaptchaConfig.expiration {
  72. imageCaptchaConfig.expiration = expiration
  73. memoryStore := captcha.NewMemoryStore(imageCaptchaConfig.collect_num, expiration)
  74. captcha.SetCustomStore(memoryStore)
  75. }
  76. }
  77. // @Description 设置图片验证码缓存数量
  78. // @Param collect_num int 缓存数量
  79. func SetImageCaptchaCollectNum(collect_num int) {
  80. if collect_num != imageCaptchaConfig.collect_num {
  81. imageCaptchaConfig.collect_num = collect_num
  82. memoryStore := captcha.NewMemoryStore(collect_num, imageCaptchaConfig.expiration)
  83. captcha.SetCustomStore(memoryStore)
  84. }
  85. }
  86. var smsConfig struct {
  87. send_format string
  88. check_format string
  89. }
  90. // @Description 发送短信验证码
  91. // @Param mobile string 手机号码
  92. // @Return data []byte 发送结果
  93. // @Return err error 错误
  94. func SendSmsCaptcha(mobile string) ([]byte, error) {
  95. if mobile == "" {
  96. return nil, errors.New("mobile is cannot empty")
  97. }
  98. sms_url := fmt.Sprintf(smsConfig.send_format, mobile)
  99. logs.Debug("-----sms_send_url-------")
  100. logs.Debug(sms_url)
  101. data, err := NewHttpUtil().Get(sms_url, nil, nil)
  102. logs.Debug(string(data))
  103. if err != nil {
  104. logs.Error("send sms captcha failure:", err)
  105. }
  106. return data, err
  107. }
  108. // @Description 短信验证码校验
  109. // @Param captcha string 验证码
  110. // @Param mobile string 手机号码
  111. // @Return result bool 验证结果 true 成功 false 失败
  112. func SmsCaptchaCheck(captcha, mobile string) bool {
  113. if captcha == "" || mobile == "" {
  114. return false
  115. }
  116. sms_url := fmt.Sprintf(smsConfig.check_format, mobile, captcha)
  117. logs.Debug("-----sms_check_url-------")
  118. logs.Debug(sms_url)
  119. data, err := NewHttpUtil().Get(sms_url, nil, nil)
  120. return err == nil && string(data) != "0"
  121. }
  122. // @Description 设置session
  123. // @Param ctx *gin.Context gin上下文
  124. // @Param key string 键
  125. // @Param value string 值
  126. func SetSession(ctx *gin.Context, key, value interface{}) {
  127. /*session := sessions.Default(ctx)
  128. session.Set(key, value)
  129. session.Save()*/
  130. }
  131. // @Description 获取session
  132. // @Param ctx *gin.Context gin上下文
  133. // @Param key string 键
  134. // @Param value string 值
  135. func GetSession(ctx *gin.Context, key interface{}, defalutValue ...interface{}) interface{} {
  136. return nil
  137. /*session := sessions.Default(ctx)
  138. value := session.Get(key)
  139. if value == nil && len(defalutValue) > 0 {
  140. return defalutValue[0]
  141. }
  142. return value*/
  143. }