From 5197d9d74e526c278b3e6d7d8d52cbddd0a128f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 13 Aug 2022 17:04:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=E5=AE=8C=E6=88=90=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=80=BC=E8=AE=BE=E5=AE=9A=E7=94=A8=E6=88=B7=E7=9A=84=E8=AF=A6?= =?UTF-8?q?=E7=BB=86=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/user.go | 27 +++++++++++++++++++++++---- model/user_detail.go | 4 ++-- repository/user.go | 13 +++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/controller/user.go b/controller/user.go index 36ea0f6..65e3eee 100644 --- a/controller/user.go +++ b/controller/user.go @@ -8,7 +8,6 @@ import ( "electricity_bill_calc/response" "electricity_bill_calc/security" "electricity_bill_calc/service" - "errors" "net/http" "strconv" "time" @@ -34,6 +33,7 @@ func InitializeUserController(router *gin.Engine) { UserController.Router.GET("/accounts", security.OPSAuthorize, UserController.ListPagedUser) UserController.Router.PUT("/account/enabled/state", security.OPSAuthorize, UserController.SwitchUserEnabling) UserController.Router.POST("/account", security.OPSAuthorize, UserController.CreateOPSAndManagementAccount) + UserController.Router.GET("/account/:uid", security.MustAuthenticated, UserController.GetUserDetail) } type LoginFormData struct { @@ -93,11 +93,11 @@ func (_UserController) InvalidUserPassword(c *gin.Context) { result := response.NewResult(c) targetUserId := c.Param("uid") verifyCode, err := service.UserService.InvalidUserPassword(targetUserId) - if errors.Is(err, &exceptions.NotFoundError{}) { + if _, ok := err.(exceptions.NotFoundError); ok { result.NotFound("未找到指定用户。") return } - if errors.Is(err, &exceptions.UnsuccessfulOperationError{}) { + if _, ok := err.(exceptions.UnsuccessfulOperationError); ok { result.Error(500, "未能成功更新用户的密码。") return } @@ -119,7 +119,7 @@ func (_UserController) ResetUserPassword(c *gin.Context) { resetForm := new(ResetPasswordFormData) c.BindJSON(resetForm) verified, err := service.UserService.VerifyUserPassword(resetForm.Username, resetForm.VerifyCode) - if errors.Is(err, &exceptions.NotFoundError{}) { + if _, ok := err.(exceptions.NotFoundError); ok { result.NotFound("指定的用户不存在。") return } @@ -237,3 +237,22 @@ func (_UserController) CreateOPSAndManagementAccount(c *gin.Context) { } result.Json(http.StatusOK, http.StatusOK, "用户已经成功创建。", gin.H{"verify": verifyCode}) } + +func (_UserController) GetUserDetail(c *gin.Context) { + result := response.NewResult(c) + targetUserId := c.Param("uid") + exists, err := service.UserService.IsUserExists(targetUserId) + if !exists { + result.NotFound("指定的用户不存在。") + return + } + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + } + userDetail, err := repository.UserRepo.FetchUserDetail(targetUserId) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Json(http.StatusOK, http.StatusOK, "用户详细信息已获取到。", gin.H{"user": userDetail}) +} diff --git a/model/user_detail.go b/model/user_detail.go index d76c6b0..f7c7aef 100644 --- a/model/user_detail.go +++ b/model/user_detail.go @@ -37,10 +37,10 @@ func (JoinedUserDetail) TableName() string { } type FullJoinedUserDetail struct { - User `xorm:"extends"` UserDetail `xorm:"extends"` + User `xorm:"extends"` } func (FullJoinedUserDetail) TableName() string { - return "user" + return "user_detail" } diff --git a/repository/user.go b/repository/user.go index 3c0204d..5c850dc 100644 --- a/repository/user.go +++ b/repository/user.go @@ -85,3 +85,16 @@ func (_UserRepository) ListUserDetail(keyword string, userType int, userState *b Find(&users) return users, total, err } + +func (_UserRepository) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, error) { + user := new(model.FullJoinedUserDetail) + has, err := global.DBConn. + Table("user_detail").Alias("d"). + Join("INNER", []string{"user", "u"}, "d.id=u.id"). + ID(uid). + Get(user) + if has { + return user, nil + } + return nil, err +}