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 +}