diff --git a/controllers/user/userInfo.go b/controllers/user/userInfo.go new file mode 100644 index 0000000..0c6294d --- /dev/null +++ b/controllers/user/userInfo.go @@ -0,0 +1,65 @@ +package user + +import ( + "net/http" + "toutoukan/init/databaseInit" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" + "time" +) + +// UserInfo 表结构体,用于 GORM 查询 +// 确保字段名与数据库列名一致 +type UserInfo struct { + Uid string `gorm:"column:uid" json:"uid"` + Telephone string `gorm:"column:telephone" json:"telephone"` + Password string `gorm:"column:password" json:"-"` // 不在 JSON 中返回密码 + AvatarURL string `gorm:"column:avatar_url" json:"avatar_url"` + Gender int `gorm:"column:gender" json:"gender"` + Birthdate *time.Time `gorm:"column:birthdate-date;type:datetime;nullable"` + CreatedTime time.Time `gorm:"column:createdtime" json:"created_time"` + UpdatedTime time.Time `gorm:"column:updatedtime" json:"updated_time"` + Bio string `gorm:"column:bio" json:"bio"` + Username string `gorm:"column:username" json:"username"` + TotalPoints int `gorm:"column:total_points" json:"total_points"` +} + +// UserReq 定义请求中用户ID的结构体 +// GetUserInfo 获取用户所有信息 +func GetUserInfo(c *gin.Context) { + var req UserReq + + // 1. 解析并验证请求参数 + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "参数解析失败", + "detail": err.Error(), + }) + return + } + + // 2. 根据 UID 查询用户基本信息 + var userInfo UserInfo + if err := databaseInit.UserDB.Table("user_info"). + Where("uid = ?", req.Uid). + First(&userInfo).Error; err != nil { + + if err == gorm.ErrRecordNotFound { + c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"}) + } else { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "查询用户失败", + "detail": err.Error(), + }) + } + return + } + + // 3. 返回成功响应 + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "获取用户信息成功", + "data": userInfo, + }) +} diff --git a/controllers/user/userLogin.go b/controllers/user/userLogin.go index 762c5b1..f673c01 100644 --- a/controllers/user/userLogin.go +++ b/controllers/user/userLogin.go @@ -179,18 +179,6 @@ func pkcs7Unpad(data []byte) []byte { } // 定义与表结构对应的用户模型 -type UserInfo struct { - Uid string `gorm:"column:uid;primaryKey"` - Telephone string `gorm:"column:telephone"` - Password string `gorm:"column:password"` - AvatarUrl string `gorm:"column:avatar_url"` - Gender int `gorm:"column:gender"` - Birthdate *time.Time `gorm:"column:birthdate-date;type:datetime;nullable"` - CreatedTime time.Time `gorm:"column:createdtime;type:datetime"` - UpdatedTime time.Time `gorm:"column:updatedtime;type:datetime"` - Bio string `gorm:"column:bio"` - Username string `gorm:"column:username"` -} // 自定义表名 func (UserInfo) TableName() string { diff --git a/controllers/user/userScore.go b/controllers/user/userScore.go new file mode 100644 index 0000000..8bb7290 --- /dev/null +++ b/controllers/user/userScore.go @@ -0,0 +1,57 @@ +package user + +import ( + "database/sql" + "net/http" + "toutoukan/init/databaseInit" + + "github.com/gin-gonic/gin" +) + +// UserReq 定义了请求中用户ID的结构体 +type UserReq struct { + Uid string `json:"uid" binding:"required,min=1,max=50"` +} + +func GetScore(c *gin.Context) { + var req UserReq + + // 1. 解析并验证请求参数 + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "参数解析失败", + "detail": err.Error(), + }) + return + } + + // 2. 根据 user_id 查询用户总积分 + // 使用 sql.NullInt64 来处理 SUM 函数可能返回 NULL 的情况 + var totalPoints sql.NullInt64 + + if err := databaseInit.UserDB.Table("user_points"). + Where("user_id = ?", req.Uid). + Select("SUM(points_change)"). + Scan(&totalPoints).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "查询用户积分失败: " + err.Error(), + }) + return + } + + // 3. 返回查询结果 + // 检查 totalPoints 是否为 NULL,如果是则返回 0 + finalPoints := 0 + if totalPoints.Valid { + finalPoints = int(totalPoints.Int64) + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "查询成功", + "data": gin.H{ + "user_id": req.Uid, + "total_points": finalPoints, + }, + }) +}