66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
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,
|
|
})
|
|
}
|