Add user info service

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2025-12-25 02:50:27 +08:00
parent 396ab10469
commit f8b6c1b1df
5 changed files with 45 additions and 3 deletions

View File

@@ -25,7 +25,7 @@ func (self *User) GetByEmail(email string) error {
return nil
}
func (self *User) GetByUserId(userId string) error {
func (self *User) GetByUserId(userId uuid.UUID) error {
if err := Database.Where("user_id = ?", userId).First(&self).Error; err != nil {
return err
}

View File

@@ -3,6 +3,7 @@ package server
import (
"nixcn-cms/service/auth"
"nixcn-cms/service/checkin"
"nixcn-cms/service/user"
"github.com/gin-gonic/gin"
)
@@ -12,4 +13,5 @@ func Router(e *gin.Engine) {
api := e.Group("/api/v1")
auth.Handler(api.Group("/auth"))
checkin.Handler(api.Group("/checkin"))
user.Handler(api.Group("/user"))
}

View File

@@ -1 +0,0 @@
package info

View File

@@ -1,4 +1,4 @@
package info
package user
import (
"nixcn-cms/internal/crypto/jwt"
@@ -8,4 +8,5 @@ import (
func Handler(r *gin.RouterGroup) {
r.Use(jwt.JWTAuth())
r.GET("/info", UserInfo)
}

40
service/user/userinfo.go Normal file
View File

@@ -0,0 +1,40 @@
package user
import (
"net/http"
"nixcn-cms/data"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func UserInfo(c *gin.Context) {
data := new(data.User)
userId, ok := c.Get("user_id")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{
"status": "user not found",
})
return
}
err := data.GetByUserId(userId.(uuid.UUID))
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"status": "user not found",
})
return
}
var checkinTime any = nil
if !data.Checkin.IsZero() {
checkinTime = data.Checkin
}
c.JSON(http.StatusOK, gin.H{
"user_id": data.UserId,
"email": data.Email,
"type": data.Type,
"nickname": data.Nickname,
"subtitle": data.Subtitle,
"avatar": data.Avatar,
"checkin": checkinTime,
})
}