user.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package user
  2. import (
  3. "context"
  4. "demo/configs"
  5. "demo/share"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "github.com/mojocn/base64Captcha"
  9. "github.com/spf13/cast"
  10. "log"
  11. "net/http"
  12. "regexp"
  13. "time"
  14. )
  15. var ctx = context.Background()
  16. func UserRouth(engine *gin.RouterGroup) {
  17. user := engine.Group("/user")
  18. {
  19. user.POST("/login", login)
  20. user.POST("sendSms", SendVerificationCode)
  21. user.GET("/captcha", VerificationCode)
  22. }
  23. }
  24. func VerificationCode(c *gin.Context) {
  25. // 配置
  26. driver := base64Captcha.NewDriverDigit(80, 240, 4, 0.5, 80)
  27. store := base64Captcha.DefaultMemStore
  28. captcha := base64Captcha.NewCaptcha(driver, store)
  29. // 生成验证码
  30. id, b64s, answer, err := captcha.Generate()
  31. if err != nil {
  32. c.JSON(500, gin.H{"error": err.Error()})
  33. return
  34. }
  35. fmt.Println(answer)
  36. err = configs.RedisDb.Set(ctx, "VerificationCode_"+id, answer, 0).Err()
  37. if err != nil {
  38. fmt.Println(err)
  39. }
  40. err = configs.RedisDb.Expire(ctx, "VerificationCode_"+id, 60*time.Second).Err()
  41. if err != nil {
  42. panic(err)
  43. }
  44. // 返回验证码图片
  45. c.JSON(200, gin.H{
  46. "message": "获取成功!!!",
  47. "expires": time.Now().Add(time.Minute * 5).Unix(),
  48. "code": 200,
  49. "data": map[string]string{"id": id, "image": b64s},
  50. })
  51. }
  52. func login(c *gin.Context) {
  53. data := share.GetJsonAnyParam(c)
  54. var user configs.User
  55. var err error
  56. username, _ := data("username")
  57. password, _ := data("password")
  58. // 定义正则表达式
  59. regexPattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
  60. // 编译正则表达式
  61. reg, err := regexp.Compile(regexPattern)
  62. if err != nil {
  63. fmt.Println("Error compiling regex:", err)
  64. return
  65. }
  66. matched := reg.MatchString(cast.ToString(username))
  67. user, err = logins(cast.ToString(username), cast.ToString(password), matched)
  68. if err == nil {
  69. token, err := share.GenerateToken(cast.ToString(user.Id))
  70. if err == nil {
  71. c.Header("auth-sign", token)
  72. c.JSON(http.StatusOK, gin.H{
  73. "code": 200,
  74. "message": "成功!!!",
  75. })
  76. } else {
  77. c.JSON(http.StatusOK, gin.H{
  78. "code": 400,
  79. "message": "生成token失败!!!",
  80. })
  81. }
  82. } else {
  83. c.JSON(http.StatusOK, gin.H{
  84. "code": 401,
  85. "message": "用户密码错误!!!",
  86. })
  87. }
  88. }
  89. // SendVerificationCode
  90. func SendVerificationCode(c *gin.Context) {
  91. data := share.GetJsonAnyParam(c)
  92. var err error
  93. username, _ := data("username")
  94. code, _ := data("code")
  95. codeId, _ := data("codeId")
  96. val, err := configs.RedisDb.Get(ctx, cast.ToString(codeId)).Result()
  97. if err != nil {
  98. log.Fatal(err)
  99. }
  100. if val != cast.ToString(code) {
  101. c.JSON(200, gin.H{"code": 400, "message": "验证码错误!!!"})
  102. c.Abort()
  103. return
  104. }
  105. // 定义正则表达式
  106. regexPattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
  107. // 编译正则表达式
  108. reg, err := regexp.Compile(regexPattern)
  109. if err != nil {
  110. fmt.Println("Error compiling regex:", err)
  111. return
  112. }
  113. matched := reg.MatchString(cast.ToString(username))
  114. sendSms(matched, cast.ToString(username))
  115. c.JSON(200, gin.H{"code": 200, "message": "发送成功!!!"})
  116. }