sys_captcha.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package api
  2. import (
  3. "fmt"
  4. "gin-vue-admin/controller/servers"
  5. "github.com/dchest/captcha"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // 获取图片验证码id
  9. // @Tags base
  10. // @Summary 生成验证码
  11. // @Security ApiKeyAuth
  12. // @accept application/json
  13. // @Produce application/json
  14. // @Param data body modelInterface.PageInfo true "生成验证码"
  15. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  16. // @Router /base/captcha [post]
  17. func Captcha(c *gin.Context) {
  18. captchaId := captcha.NewLen(6)
  19. if err:= captcha.Server(captcha.StdWidth,captcha.StdHeight);err != nil{
  20. servers.ReportFormat(c,true,fmt.Sprintf("验证码获取失败:%v",err),gin.H{})
  21. }else{
  22. servers.ReportFormat(c,true,"验证码获取成功",gin.H{
  23. "captchaId":captchaId,
  24. "picPath":"/base/captcha/"+captchaId+".png",
  25. })
  26. }
  27. }
  28. // @Tags base
  29. // @Summary 生成验证码图片路径
  30. // @Security ApiKeyAuth
  31. // @accept application/json
  32. // @Produce application/json
  33. // @Param data body modelInterface.PageInfo true "生成验证码图片路径"
  34. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  35. // @Router /base/captcha/:captchaId [get]
  36. func CaptchaImg(c *gin.Context) {
  37. servers.GinCapthcaServeHTTP(c.Writer, c.Request)
  38. }