From c4b3eeb394ade7df4717158a075a5a139b7d94e0 Mon Sep 17 00:00:00 2001 From: lijun <2021141461138@stu.scu.edu.cn> Date: Tue, 21 May 2024 15:13:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8C=E4=B8=80=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=92=8C=E9=82=AE=E7=AE=B1=E9=87=8D=E5=A4=8D=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/user.go | 9 ++++++--- handler/user.go | 7 +++++-- service/userService.go | 11 ++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/dao/user.go b/dao/user.go index 0da1907..2786d3d 100644 --- a/dao/user.go +++ b/dao/user.go @@ -1,9 +1,12 @@ package dao -import "fmt" +import ( + "fmt" + "gorm.io/gorm" +) type User struct { - ID int `gorm:"primaryKey column:id"` + gorm.Model Name string `gorm:"column:name"` Age int `gorm:"column:age"` Email string `gorm:"column:email"` @@ -13,7 +16,7 @@ type User struct { UpdateTime string `gorm:"column:update_time"` } -func CreateUser(name string, password, email string) int { +func CreateUser(name string, password, email string) uint { user := User{Name: name, Email: email, Password: password} DB.Create(&user) return user.ID diff --git a/handler/user.go b/handler/user.go index afa36f5..413444e 100644 --- a/handler/user.go +++ b/handler/user.go @@ -26,7 +26,6 @@ type RLReq struct { } func loginHandler(c *gin.Context) { - var req_data RLReq tokenString := "" if err := c.ShouldBindJSON(&req_data); err == nil { @@ -50,7 +49,7 @@ func loginHandler(c *gin.Context) { } worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟 // 返回令牌 - c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": 0, "message": "success"}) + c.JSON(200, gin.H{"code": 0, "message": "success", "token": tokenString}) } else { c.JSON(400, gin.H{"error": "user not found", "code": 1, "message": "error"}) } @@ -69,6 +68,10 @@ func registerHandler(c *gin.Context) { hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值 req_data.Password = hex.EncodeToString(hasher.Sum(nil)) // 生成密码的 MD5 散列值 } + if service.ContainsUser(req_data.User, req_data.Email) == true { + c.JSON(400, gin.H{"error": "user already exists", "code": 1, "message": "error"}) + return + } id := service.CreateUser(req_data.User, req_data.Password, req_data.Email) // 生成 JWT 令牌 token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ diff --git a/service/userService.go b/service/userService.go index d5e5d1e..86d8be6 100644 --- a/service/userService.go +++ b/service/userService.go @@ -5,7 +5,7 @@ import ( "videoplayer/dao" ) -func CreateUser(name string, password, email string) int { +func CreateUser(name string, password, email string) uint { return dao.CreateUser(name, password, email) } @@ -23,3 +23,12 @@ func GetUser(name, password string) dao.User { } return dao.User{} } + +func ContainsUser(name, email string) bool { + user := dao.FindUserByName(name) + user2 := dao.FindUserByEmail(email) + if user.ID != 0 || user2.ID != 0 { + return true + } + return false +}