| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package user
- import (
- "context"
- "demo/configs"
- "demo/share"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/mojocn/base64Captcha"
- "github.com/spf13/cast"
- "log"
- "net/http"
- "regexp"
- "time"
- )
- var ctx = context.Background()
- func UserRouth(engine *gin.RouterGroup) {
- user := engine.Group("/user")
- {
- user.POST("/login", login)
- user.POST("sendSms", SendVerificationCode)
- user.GET("/captcha", VerificationCode)
- }
- }
- func VerificationCode(c *gin.Context) {
- // 配置
- driver := base64Captcha.NewDriverDigit(80, 240, 4, 0.5, 80)
- store := base64Captcha.DefaultMemStore
- captcha := base64Captcha.NewCaptcha(driver, store)
- // 生成验证码
- id, b64s, answer, err := captcha.Generate()
- if err != nil {
- c.JSON(500, gin.H{"error": err.Error()})
- return
- }
- fmt.Println(answer)
- err = configs.RedisDb.Set(ctx, "VerificationCode_"+id, answer, 0).Err()
- if err != nil {
- fmt.Println(err)
- }
- err = configs.RedisDb.Expire(ctx, "VerificationCode_"+id, 60*time.Second).Err()
- if err != nil {
- panic(err)
- }
- // 返回验证码图片
- c.JSON(200, gin.H{
- "message": "获取成功!!!",
- "expires": time.Now().Add(time.Minute * 5).Unix(),
- "code": 200,
- "data": map[string]string{"id": id, "image": b64s},
- })
- }
- func login(c *gin.Context) {
- data := share.GetJsonAnyParam(c)
- var user configs.User
- var err error
- username, _ := data("username")
- password, _ := data("password")
- // 定义正则表达式
- regexPattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
- // 编译正则表达式
- reg, err := regexp.Compile(regexPattern)
- if err != nil {
- fmt.Println("Error compiling regex:", err)
- return
- }
- matched := reg.MatchString(cast.ToString(username))
- user, err = logins(cast.ToString(username), cast.ToString(password), matched)
- if err == nil {
- token, err := share.GenerateToken(cast.ToString(user.Id))
- if err == nil {
- c.Header("auth-sign", token)
- c.JSON(http.StatusOK, gin.H{
- "code": 200,
- "message": "成功!!!",
- })
- } else {
- c.JSON(http.StatusOK, gin.H{
- "code": 400,
- "message": "生成token失败!!!",
- })
- }
- } else {
- c.JSON(http.StatusOK, gin.H{
- "code": 401,
- "message": "用户密码错误!!!",
- })
- }
- }
- // SendVerificationCode
- func SendVerificationCode(c *gin.Context) {
- data := share.GetJsonAnyParam(c)
- var err error
- username, _ := data("username")
- code, _ := data("code")
- codeId, _ := data("codeId")
- val, err := configs.RedisDb.Get(ctx, cast.ToString(codeId)).Result()
- if err != nil {
- log.Fatal(err)
- }
- if val != cast.ToString(code) {
- c.JSON(200, gin.H{"code": 400, "message": "验证码错误!!!"})
- c.Abort()
- return
- }
- // 定义正则表达式
- regexPattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
- // 编译正则表达式
- reg, err := regexp.Compile(regexPattern)
- if err != nil {
- fmt.Println("Error compiling regex:", err)
- return
- }
- matched := reg.MatchString(cast.ToString(username))
- sendSms(matched, cast.ToString(username))
- c.JSON(200, gin.H{"code": 200, "message": "发送成功!!!"})
- }
|