CaptchaController.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // _Image
  9. // @Title _Image
  10. // @Description 获取图片验证码
  11. // @Param w int 图片宽度
  12. // @Param h int 图片高度
  13. // @Param code string 保存的session编码
  14. // @Success 200 {object} Account
  15. // @Failure 403 :id is empty
  16. func Captcha_Image(c *entitys.CtrlContext) {
  17. width, _ := strconv.Atoi(c.Ctx.Query("w"))
  18. height, _ := strconv.Atoi(c.Ctx.Query("h"))
  19. length, _ := strconv.Atoi(c.Ctx.Query("l"))
  20. captchaId, image, err := utils.GenerateImageCaptcha(width, height, length)
  21. if err == nil {
  22. result := map[string]interface{}{
  23. "captcha_key": captchaId,
  24. "captcha_image": image.Bytes(),
  25. }
  26. c.Ctx.JSON(200, sysmodel.SysReturn{200, "", result})
  27. } else {
  28. c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  29. }
  30. }
  31. // _Sms
  32. // @Title _Sms
  33. // @Description 获取短信验证码
  34. // @Param mobile string false "手机号码"
  35. // @Success 200 {object} Account
  36. // @Failure 403 :id is empty
  37. func Captcha_Sms(c *entitys.CtrlContext) {
  38. mobile := c.Ctx.Query("mobile")
  39. if mobile == "" {
  40. c.Ctx.JSON(500, sysmodel.SysReturn{500, "mobile is cannot empty", nil})
  41. return
  42. }
  43. data, err := utils.SendSmsCaptcha(mobile)
  44. if err == nil {
  45. c.Ctx.JSON(200, sysmodel.SysReturn{200, "", string(data)})
  46. } else {
  47. c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  48. }
  49. }
  50. // _Check
  51. // @Title _Check
  52. // @Description 校验验证码
  53. // @Param type int false "验证类型 0:图片验证码 1:短信验证码"
  54. // @Param captcha string false "验证码"
  55. // @Param mobile string false "短信验证码的手机号码"
  56. // @Param key string false "图片验证码的key"
  57. // @Success 200 {object} Account
  58. // @Failure 403 :id is empty
  59. func Captcha_Check(c *entitys.CtrlContext) {
  60. check_type := c.Ctx.Query("type")
  61. captcha := c.Ctx.Query("captcha")
  62. if captcha == "" {
  63. c.Ctx.JSON(200, sysmodel.SysReturn{200, "验证码不能为空", nil})
  64. return
  65. }
  66. var check = false //校验结果
  67. switch check_type {
  68. case "0":
  69. captchaId := c.Ctx.Query("key")
  70. if captchaId == "" {
  71. c.Ctx.JSON(500, sysmodel.SysReturn{500, "验证码key不能为空", nil})
  72. return
  73. }
  74. check = utils.ImageCaptchaCheck(captcha, captchaId)
  75. case "1":
  76. mobile := c.Ctx.Query("mobile")
  77. if mobile == "" {
  78. c.Ctx.JSON(500, sysmodel.SysReturn{500, "手机号码不能为空", nil})
  79. return
  80. }
  81. check = utils.SmsCaptchaCheck(captcha, mobile)
  82. default:
  83. c.Ctx.JSON(500, sysmodel.SysReturn{500, "验证类型错误", nil})
  84. return
  85. }
  86. if check {
  87. c.Ctx.JSON(200, sysmodel.SysReturn{200, "验证成功", nil})
  88. } else {
  89. c.Ctx.JSON(500, sysmodel.SysReturn{500, "验证失败", nil})
  90. }
  91. }
  92. func __none_func_captcha__(params ... interface{}) bool {
  93. return true
  94. }