CaptchaController.go 3.0 KB

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