2023-07-14 10:16:58 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-10-12 10:10:23 +08:00
|
|
|
"log/slog"
|
2023-08-30 09:28:21 +08:00
|
|
|
"net/http"
|
|
|
|
|
2023-07-14 10:16:58 +08:00
|
|
|
jwt "github.com/appleboy/gin-jwt/v2"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-07-28 11:15:44 +08:00
|
|
|
"joylink.club/bj-rtsts-server/db/model"
|
2023-07-14 10:16:58 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
|
|
|
"joylink.club/bj-rtsts-server/middleware"
|
|
|
|
"joylink.club/bj-rtsts-server/service"
|
2023-10-20 15:08:47 +08:00
|
|
|
"joylink.club/bj-rtsts-server/sys_error"
|
2023-07-14 10:16:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitUserRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
|
|
|
unauthed := api.Group("/v1/user")
|
2023-07-14 16:28:47 +08:00
|
|
|
unauthed.POST("/login", login(authMiddleware))
|
2023-07-28 11:15:44 +08:00
|
|
|
unauthed.POST("/register", register)
|
2023-07-14 10:16:58 +08:00
|
|
|
|
2023-08-30 09:28:21 +08:00
|
|
|
authed := api.Group("/v1/user").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
|
2023-07-14 10:16:58 +08:00
|
|
|
authed.GET("/paging", pageQueryUser)
|
2023-07-28 11:15:44 +08:00
|
|
|
authed.GET("/current", findUserInfo)
|
2024-10-09 18:02:42 +08:00
|
|
|
|
2023-07-28 11:15:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 用户注册
|
|
|
|
//
|
|
|
|
// @Summary 用户注册
|
|
|
|
// @Description 用户注册
|
|
|
|
// @Tags 用户Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param loginInfo body dto.RegisterUser true "用户注册信息"
|
|
|
|
// @Success 200 {object} string
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/user/register [post]
|
|
|
|
func register(c *gin.Context) {
|
|
|
|
user := &dto.RegisterUser{}
|
|
|
|
err := c.BindJSON(user)
|
|
|
|
if err != nil {
|
2023-10-20 15:08:47 +08:00
|
|
|
panic(sys_error.New("注册失败,参数错误", err))
|
|
|
|
}
|
|
|
|
err = service.Register(user)
|
|
|
|
if err != nil {
|
|
|
|
panic(sys_error.New("注册失败", err))
|
2023-07-28 11:15:44 +08:00
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, "ok")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取当前登录用户信息
|
|
|
|
//
|
|
|
|
// @Summary 获取当前登录用户信息
|
|
|
|
// @Description 获取当前登录用户信息
|
|
|
|
// @Tags 用户Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
//
|
|
|
|
// @Param Authorization header string true "JWT Token"
|
|
|
|
//
|
2023-09-01 16:10:29 +08:00
|
|
|
// @Success 200 {object} dto.UserRspDto
|
2023-07-28 11:15:44 +08:00
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/user/current [get]
|
|
|
|
func findUserInfo(c *gin.Context) {
|
|
|
|
user, _ := c.Get(middleware.IdentityKey)
|
|
|
|
userId := user.(*model.User).ID
|
|
|
|
userInfo := service.FindUserInfo(userId)
|
|
|
|
c.JSON(http.StatusOK, userInfo)
|
2023-07-14 10:16:58 +08:00
|
|
|
}
|
|
|
|
|
2023-07-14 16:28:47 +08:00
|
|
|
// 用户登录
|
|
|
|
//
|
|
|
|
// @Summary 用户登录
|
|
|
|
// @Description 用户登录
|
|
|
|
// @Tags 用户Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param loginInfo body dto.LoginDto true "登录信息"
|
|
|
|
// @Success 200 {object} dto.TokenRespDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/user/login [post]
|
|
|
|
func login(authMiddleware *jwt.GinJWTMiddleware) func(*gin.Context) {
|
|
|
|
return authMiddleware.LoginHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// 分页查询用户
|
|
|
|
//
|
|
|
|
// @Summary 分页查询用户
|
|
|
|
//
|
|
|
|
// @Security JwtAuth
|
|
|
|
//
|
|
|
|
// @Description 可以通过用户名称过滤,分页查询用户
|
|
|
|
// @Tags 用户Api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id query dto.PageUserReqDto true "用户查询条件"
|
|
|
|
// @Success 200 {object} dto.PageDto
|
|
|
|
// @Failure 401 {object} dto.ErrorDto
|
|
|
|
// @Failure 404 {object} dto.ErrorDto
|
|
|
|
// @Failure 500 {object} dto.ErrorDto
|
|
|
|
// @Router /api/v1/user/paging [get]
|
2023-07-14 10:16:58 +08:00
|
|
|
func pageQueryUser(c *gin.Context) {
|
|
|
|
user, _ := c.Get(middleware.IdentityKey)
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("分页查询用户", user)
|
2023-07-14 10:16:58 +08:00
|
|
|
req := dto.PageUserReqDto{}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Warn("分页查询参数绑定错误,使用默认参数", err)
|
2023-07-14 10:16:58 +08:00
|
|
|
req.Default()
|
|
|
|
}
|
2023-10-12 10:10:23 +08:00
|
|
|
slog.Debug("分页查用户参数", req)
|
2023-07-14 10:16:58 +08:00
|
|
|
page, _ := service.PagingQueryUser(&req)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, page)
|
|
|
|
}
|