68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"filestoragesystem/internal/middleware"
|
|
"filestoragesystem/internal/utils"
|
|
)
|
|
|
|
// UserController 用户管理控制器(个人资料)
|
|
type UserController struct {
|
|
*Base
|
|
}
|
|
|
|
// NewUserController 创建用户控制器
|
|
func NewUserController(b *Base) *UserController { return &UserController{Base: b} }
|
|
|
|
// GetProfile 获取个人资料
|
|
func (ctl *UserController) GetProfile(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
u, err := ctl.svc.User.GetProfile(userID)
|
|
if err != nil {
|
|
utils.NotFound(c, "用户不存在")
|
|
return
|
|
}
|
|
utils.OK(c, u)
|
|
}
|
|
|
|
type updateProfileReq struct {
|
|
Email string `json:"email"`
|
|
}
|
|
|
|
// UpdateProfile 更新个人资料
|
|
func (ctl *UserController) UpdateProfile(c *gin.Context) {
|
|
var req updateProfileReq
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
userID := middleware.CurrentUserID(c)
|
|
if err := ctl.svc.User.UpdateProfile(userID, req.Email); err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "update", "user", userID, "", true, "")
|
|
utils.OKMsg(c, "资料已更新")
|
|
}
|
|
|
|
type changePasswordReq struct {
|
|
OldPassword string `json:"old_password" binding:"required"`
|
|
NewPassword string `json:"new_password" binding:"required"`
|
|
}
|
|
|
|
// ChangePassword 修改密码
|
|
func (ctl *UserController) ChangePassword(c *gin.Context) {
|
|
var req changePasswordReq
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
userID := middleware.CurrentUserID(c)
|
|
if err := ctl.svc.User.ChangePassword(userID, req.OldPassword, req.NewPassword); err != nil {
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "update", "user", userID, "password", false, err.Error())
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "update", "user", userID, "password", true, "")
|
|
utils.OKMsg(c, "密码已修改")
|
|
}
|