package app import ( "git.echol.cn/loser/st/server/global" "git.echol.cn/loser/st/server/middleware" "git.echol.cn/loser/st/server/model/app/request" "git.echol.cn/loser/st/server/model/common/response" "github.com/gin-gonic/gin" "go.uber.org/zap" ) type AuthApi struct{} // Register 前台用户注册 // @Tags App.Auth // @Summary 前台用户注册 // @accept application/json // @Produce application/json // @Param data body request.RegisterRequest true "用户注册信息" // @Success 200 {object} response.Response{msg=string} "注册成功" // @Router /app/auth/register [post] func (a *AuthApi) Register(c *gin.Context) { var req request.RegisterRequest err := c.ShouldBindJSON(&req) if err != nil { response.FailWithMessage(err.Error(), c) return } err = authService.Register(&req) if err != nil { global.GVA_LOG.Error("注册失败", zap.Error(err)) response.FailWithMessage(err.Error(), c) return } response.OkWithMessage("注册成功", c) } // Login 前台用户登录 // @Tags App.Auth // @Summary 前台用户登录 // @accept application/json // @Produce application/json // @Param data body request.LoginRequest true "用户登录信息" // @Success 200 {object} response.Response{data=response.LoginResponse,msg=string} "登录成功" // @Router /app/auth/login [post] func (a *AuthApi) Login(c *gin.Context) { var req request.LoginRequest err := c.ShouldBindJSON(&req) if err != nil { response.FailWithMessage(err.Error(), c) return } // 获取客户端 IP ip := c.ClientIP() result, err := authService.Login(&req, ip) if err != nil { global.GVA_LOG.Error("登录失败", zap.Error(err)) response.FailWithMessage(err.Error(), c) return } response.OkWithData(result, c) } // Logout 前台用户登出 // @Tags App.Auth // @Summary 前台用户登出 // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Success 200 {object} response.Response{msg=string} "登出成功" // @Router /app/auth/logout [post] func (a *AuthApi) Logout(c *gin.Context) { userID := middleware.GetAppUserID(c) token := middleware.GetToken(c) err := authService.Logout(userID, token) if err != nil { global.GVA_LOG.Error("登出失败", zap.Error(err)) response.FailWithMessage("登出失败", c) return } response.OkWithMessage("登出成功", c) } // RefreshToken 刷新 Token // @Tags App.Auth // @Summary 刷新 Token // @accept application/json // @Produce application/json // @Param data body request.RefreshTokenRequest true "刷新Token请求" // @Success 200 {object} response.Response{data=response.LoginResponse,msg=string} "刷新成功" // @Router /app/auth/refresh [post] func (a *AuthApi) RefreshToken(c *gin.Context) { var req request.RefreshTokenRequest err := c.ShouldBindJSON(&req) if err != nil { response.FailWithMessage(err.Error(), c) return } result, err := authService.RefreshToken(&req) if err != nil { global.GVA_LOG.Error("刷新 Token 失败", zap.Error(err)) response.FailWithMessage(err.Error(), c) return } response.OkWithData(result, c) } // GetUserInfo 获取当前登录用户信息 // @Tags App.Auth // @Summary 获取当前登录用户信息 // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Success 200 {object} response.Response{data=response.AppUserResponse,msg=string} "获取成功" // @Router /app/auth/userinfo [get] func (a *AuthApi) GetUserInfo(c *gin.Context) { userID := middleware.GetAppUserID(c) result, err := authService.GetUserInfo(userID) if err != nil { global.GVA_LOG.Error("获取用户信息失败", zap.Error(err)) response.FailWithMessage("获取用户信息失败", c) return } response.OkWithData(result, c) } // UpdateProfile 更新用户信息 // @Tags App.Auth // @Summary 更新用户信息 // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data body request.UpdateProfileRequest true "用户信息" // @Success 200 {object} response.Response{msg=string} "更新成功" // @Router /app/user/profile [put] func (a *AuthApi) UpdateProfile(c *gin.Context) { userID := middleware.GetAppUserID(c) var req request.UpdateProfileRequest err := c.ShouldBindJSON(&req) if err != nil { response.FailWithMessage(err.Error(), c) return } err = authService.UpdateProfile(userID, &req) if err != nil { global.GVA_LOG.Error("更新用户信息失败", zap.Error(err)) response.FailWithMessage(err.Error(), c) return } response.OkWithMessage("更新成功", c) } // ChangePassword 修改密码 // @Tags App.Auth // @Summary 修改密码 // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data body request.ChangePasswordRequest true "密码信息" // @Success 200 {object} response.Response{msg=string} "修改成功" // @Router /app/user/change-password [post] func (a *AuthApi) ChangePassword(c *gin.Context) { userID := middleware.GetAppUserID(c) var req request.ChangePasswordRequest err := c.ShouldBindJSON(&req) if err != nil { response.FailWithMessage(err.Error(), c) return } err = authService.ChangePassword(userID, &req) if err != nil { global.GVA_LOG.Error("修改密码失败", zap.Error(err)) response.FailWithMessage(err.Error(), c) return } response.OkWithMessage("修改成功", c) }