mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: User/Me
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"cloudreve/models"
|
||||
"cloudreve/pkg/serializer"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gopkg.in/go-playground/validator.v8"
|
||||
)
|
||||
|
||||
@@ -47,3 +49,13 @@ func ErrorResponse(err error) serializer.Response {
|
||||
|
||||
return serializer.ParamErr("参数错误", err)
|
||||
}
|
||||
|
||||
// CurrentUser 获取当前用户
|
||||
func CurrentUser(c *gin.Context) *model.User {
|
||||
if user, _ := c.Get("user"); user != nil {
|
||||
if u, ok := user.(*model.User); ok {
|
||||
return u
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"cloudreve/pkg/serializer"
|
||||
"cloudreve/service/user"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -16,3 +17,11 @@ func UserLogin(c *gin.Context) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// UserMe 获取当前登录的用户
|
||||
func UserMe(c *gin.Context) {
|
||||
user := CurrentUser(c)
|
||||
res := serializer.BuildUserResponse(*user)
|
||||
c.JSON(200, res)
|
||||
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// InitRouter 初始化路由
|
||||
func InitRouter() *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
// 中间件
|
||||
r.Use(middleware.Session(conf.SystemConfig.SessionSecret))
|
||||
r.Use(middleware.CurrentUser())
|
||||
|
||||
// 顶层路由分组
|
||||
v3 := r.Group("/Api/V3")
|
||||
@@ -21,6 +23,19 @@ func InitRouter() *gin.Engine {
|
||||
// 用户登录
|
||||
v3.POST("User/Session", controllers.UserLogin)
|
||||
|
||||
// 需要登录保护的
|
||||
auth := v3.Group("")
|
||||
auth.Use(middleware.AuthRequired())
|
||||
{
|
||||
// 用户类
|
||||
user := auth.Group("User")
|
||||
{
|
||||
// 当前登录用户信息
|
||||
user.GET("Me", controllers.UserMe)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user