CaptchaUtils.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package captcha
  2. import (
  3. "github.com/mojocn/base64Captcha"
  4. "image/color"
  5. "time"
  6. "ulink-admin/config"
  7. )
  8. var result = base64Captcha.NewMemoryStore(200240, 3*time.Minute)
  9. type CaptchaInfo struct {
  10. Id string `json:"id" ` //id
  11. Image string `json:"image" ` //验证码
  12. }
  13. // mathConfig 生成图形化算术验证码配置
  14. func mathConfig() *base64Captcha.DriverMath {
  15. mathType := &base64Captcha.DriverMath{
  16. Height: 50,
  17. Width: 150,
  18. NoiseCount: 0,
  19. ShowLineOptions: base64Captcha.OptionShowHollowLine,
  20. /* BgColor: &color.RGBA{
  21. R: 40,
  22. G: 30,
  23. B: 89,
  24. A: 29,
  25. },*/
  26. Fonts: nil,
  27. }
  28. return mathType
  29. }
  30. // digitConfig 生成图形化数字验证码配置
  31. func digitConfig() *base64Captcha.DriverDigit {
  32. digitType := &base64Captcha.DriverDigit{
  33. Height: 50,
  34. Width: 100,
  35. Length: 5,
  36. MaxSkew: 0.45,
  37. DotCount: 80,
  38. }
  39. return digitType
  40. }
  41. // stringConfig 生成图形化字符串验证码配置
  42. func stringConfig() *base64Captcha.DriverString {
  43. stringType := &base64Captcha.DriverString{
  44. Height: 50,
  45. Width: 150,
  46. NoiseCount: 0,
  47. //ShowLineOptions: base64Captcha.OptionShowHollowLine | base64Captcha.OptionShowSlimeLine,
  48. Length: 4,
  49. Source: "23456789qwertyupasdfghjkzxcvb",
  50. BgColor: &color.RGBA{
  51. R: 40,
  52. G: 30,
  53. B: 89,
  54. A: 29,
  55. },
  56. Fonts: []string{"wqy-microhei.ttc"},
  57. }
  58. return stringType
  59. }
  60. // chineseConfig 生成图形化汉字验证码配置
  61. func chineseConfig() *base64Captcha.DriverChinese {
  62. chineseType := &base64Captcha.DriverChinese{
  63. Height: 50,
  64. Width: 100,
  65. NoiseCount: 0,
  66. ShowLineOptions: base64Captcha.OptionShowSlimeLine,
  67. Length: 2,
  68. Source: "设想,你在,处理,消费者,的音,频输,出音,频可,能无,论什,么都,没有,任何,输出,或者,它可,能是,单声道,立体声,或是,环绕立,体声的,不想要,的值",
  69. BgColor: &color.RGBA{
  70. R: 40,
  71. G: 30,
  72. B: 89,
  73. A: 29,
  74. },
  75. Fonts: nil,
  76. }
  77. return chineseType
  78. }
  79. // autoConfig 生成图形化数字音频验证码配置
  80. func autoConfig() *base64Captcha.DriverAudio {
  81. chineseType := &base64Captcha.DriverAudio{
  82. Length: 4,
  83. Language: "zh",
  84. }
  85. return chineseType
  86. }
  87. // @Result id 验证码id
  88. // @Result bse64s 图片base64编码
  89. // @Result err 错误
  90. func CreateCode() (string, string, error) {
  91. var driver base64Captcha.Driver
  92. // switch case分支中的方法为目录3的配置
  93. // switch case分支中的方法为目录3的配置
  94. // switch case分支中的方法为目录3的配置
  95. captchaConfig := config.GetCaptchaConfig()
  96. switch captchaConfig.Type {
  97. case "audio":
  98. driver = autoConfig()
  99. case "string":
  100. driver = stringConfig().ConvertFonts()
  101. case "math":
  102. driver = mathConfig()
  103. case "chinese":
  104. driver = chineseConfig()
  105. case "digit":
  106. driver = digitConfig()
  107. }
  108. if driver == nil {
  109. panic("生成验证码的类型没有配置,请在yaml文件中配置完再次重试启动项目")
  110. }
  111. // 创建验证码并传入创建的类型的配置,以及存储的对象
  112. //driver = driver.ConvertFonts()
  113. c := base64Captcha.NewCaptcha(driver, result)
  114. id, b64s, err := c.Generate()
  115. return id, b64s, err
  116. }
  117. // @Pram id 验证码id
  118. // @Pram VerifyValue 用户输入的答案
  119. // @Result true:正确,false:失败
  120. func VerifyCaptcha(id, VerifyValue string) bool {
  121. // result 为步骤1 创建的图片验证码存储对象
  122. return result.Verify(id, VerifyValue, true)
  123. }
  124. // @Pram codeId 验证码id
  125. // @Result 验证码答案
  126. func GetCodeAnswer(codeId string) string {
  127. // result 为步骤1 创建的图片验证码存储对象
  128. return result.Get(codeId, false)
  129. }