【查询用户详情增加角色信息】

This commit is contained in:
weizhihong 2023-09-01 16:10:29 +08:00
parent 370bc1e5fb
commit 2cf67f776d
3 changed files with 37 additions and 17 deletions

View File

@ -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)
}

View File

@ -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,
}
}

View File

@ -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
}
// 查询关联用户角色信息