CaptchaController.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package partial
  2. import (
  3. sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
  5. "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
  6. "strconv"
  7. )
  8. const captcha_session = "image_captcha"
  9. // _Image
  10. // @Title _Image
  11. // @Description 获取图片验证码
  12. // @Param w int 图片宽度
  13. // @Param h int 图片高度
  14. // @Param code string 保存的session编码
  15. // @Success 200 {object} Account
  16. // @Failure 403 :id is empty
  17. func Captcha_Image(c *entitys.CtrlContext) {
  18. width, _ := strconv.Atoi(c.Ctx.Query("w"))
  19. height, _ := strconv.Atoi(c.Ctx.Query("h"))
  20. length, _ := strconv.Atoi(c.Ctx.Query("l"))
  21. captchaKey := captcha_session + c.Ctx.Query("code")
  22. captchaId, image, err := utils.GenerateImageCaptcha(width, height, length)
  23. if err == nil {
  24. utils.SetSession(c.Ctx, captchaKey, captchaId)
  25. c.Ctx.Header("Cache-Control", "no-cache, no-store, must-revalidate")
  26. c.Ctx.Header("Pragma", "no-cache")
  27. c.Ctx.Header("Expires", "0")
  28. c.Ctx.Header("Content-Type", "image/png")
  29. c.Ctx.Writer.Write(image.Bytes())
  30. c.Ctx.JSON(200, sysmodel.SysReturn{200, "", nil})
  31. } else {
  32. utils.SetSession(c.Ctx, captchaKey, "")
  33. c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  34. }
  35. }
  36. // _Sms
  37. // @Title _Sms
  38. // @Description 获取短信验证码
  39. // @Param mobile string false "手机号码"
  40. // @Success 200 {object} Account
  41. // @Failure 403 :id is empty
  42. func Captcha_Sms(c *entitys.CtrlContext) {
  43. mobile := c.Ctx.Query("mobile")
  44. if mobile == "" {
  45. c.Ctx.JSON(500, sysmodel.SysReturn{500, "mobile is cannot empty", nil})
  46. return
  47. }
  48. data, err := utils.SendSmsCaptcha(mobile)
  49. if err == nil {
  50. c.Ctx.JSON(200, sysmodel.SysReturn{200, "", string(data)})
  51. } else {
  52. c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  53. }
  54. }
  55. // _Check
  56. // @Title _Check
  57. // @Description 校验验证码
  58. // @Param type int false "验证类型 0:图片验证码 1:短信验证码"
  59. // @Param captcha string false "验证码"
  60. // @Param mobile string false "手机号码 短信验证码时传"
  61. // @Success 200 {object} Account
  62. // @Failure 403 :id is empty
  63. func Captcha_Check(c *entitys.CtrlContext) {
  64. check_type := c.Ctx.Query("type")
  65. captcha := c.Ctx.Query("captcha")
  66. if captcha == "" {
  67. c.Ctx.JSON(200, sysmodel.SysReturn{200, "验证码不能为空", nil})
  68. return
  69. }
  70. var check = false //校验结果
  71. switch check_type {
  72. case "0":
  73. captchaKey := captcha_session + c.Ctx.Query("code")
  74. captchaId := utils.GetSession(c.Ctx, captchaKey, "").(string)
  75. check = utils.ImageCaptchaCheck(captcha, captchaId)
  76. if check {
  77. c.Ctx.Set(captchaKey, "")
  78. }
  79. case "1":
  80. mobile := c.Ctx.Query("mobile")
  81. if mobile == "" {
  82. c.Ctx.JSON(500, sysmodel.SysReturn{500, "手机号码不能为空", nil})
  83. return
  84. }
  85. check= utils.SmsCaptchaCheck(captcha, mobile)
  86. default:
  87. c.Ctx.JSON(500, sysmodel.SysReturn{500, "验证类型错误", nil})
  88. return
  89. }
  90. if check {
  91. c.Ctx.JSON(200, sysmodel.SysReturn{200, "验证成功", nil})
  92. }else{
  93. c.Ctx.JSON(500, sysmodel.SysReturn{500, "验证失败", nil})
  94. }
  95. }
  96. func __none_func_captcha__(params ... interface{}) bool {
  97. return true
  98. }