diff --git a/api/user.go b/api/user.go index 011be5a..5348f5f 100644 --- a/api/user.go +++ b/api/user.go @@ -53,14 +53,12 @@ func register(c *gin.Context) { // // @Param Authorization header string true "JWT Token" // -// @Success 200 {object} dto.RegisterUser +// @Success 200 {object} dto.UserRspDto // @Failure 500 {object} dto.ErrorDto // @Router /api/v1/user/current [get] func findUserInfo(c *gin.Context) { user, _ := c.Get(middleware.IdentityKey) - //createId := user.(model.User).ID userId := user.(*model.User).ID - userInfo := service.FindUserInfo(userId) c.JSON(http.StatusOK, userInfo) } diff --git a/dto/user.go b/dto/user.go index 2d6f663..bca12b8 100644 --- a/dto/user.go +++ b/dto/user.go @@ -24,3 +24,12 @@ type UserRspDto struct { RegisterTime time.Time `json:"register_time" form:"register_time"` // 注册时间 Roles []*AuthRoleRspDto `json:"roles" form:"roles"` } + +func ConvertFromUserDto(u *model.User) *UserRspDto { + return &UserRspDto{ + ID: u.ID, + Name: u.Name, + Mobile: u.Mobile, + RegisterTime: u.RegisterTime, + } +} diff --git a/service/user.go b/service/user.go index b5371bb..a47f250 100644 --- a/service/user.go +++ b/service/user.go @@ -47,23 +47,36 @@ func Register(user *dto.RegisterUser) { u.Save(user) } -func FindUserInfo(userId int32) *dto.RegisterUser { - defer func() { - err := recover() - if err != nil { - zap.S().Warn("用户详情查找错误 userId:"+string(userId), err) - panic(err) - } - - }() - u := dbquery.User - uq := u.Where() - uq = uq.Where(u.ID.Eq(userId)) - user, _ := uq.First() +func FindUserInfo(userId int32) *dto.UserRspDto { + user, _ := dbquery.User.Where(dbquery.User.ID.Eq(userId)).First() if user == nil { panic(dto.ErrorDto{Code: dto.DataNotExist, Message: "没有获取到对应的用户信息"}) } - return user + rspUser := dto.ConvertFromUserDto(user) + // 查找关联关系 + aru := dbquery.AuthRoleUser + arus, err1 := aru.Distinct(aru.Rid).Select(aru.Rid).Where(aru.UID.Eq(user.ID)).Find() + if err1 != nil { + panic(dto.ErrorDto{Code: dto.DataNotExist, Message: err1.Error()}) + } + if len(arus) == 0 { + return rspUser + } + rids := make([]int32, len(arus)) + for i, v := range arus { + rids[i] = v.Rid + } + roles, err2 := dbquery.AuthRole.Where(dbquery.AuthRole.ID.In(rids...)).Find() + if err2 != nil { + panic(dto.ErrorDto{Code: dto.DataNotExist, Message: err2.Error()}) + } + if len(roles) == 0 { + return rspUser + } + for _, r := range roles { + rspUser.Roles = append(rspUser.Roles, &dto.AuthRoleRspDto{Id: r.ID, Name: r.Name}) + } + return rspUser } // 查询关联用户角色信息