add 角色关联用户数
This commit is contained in:
parent
a96ea0b501
commit
6d177cf2d7
@ -22,6 +22,7 @@ type UserMenusRes struct {
|
||||
type UserSearchReq struct {
|
||||
g.Meta `path:"/user/list" tags:"用户管理" method:"get" summary:"用户列表"`
|
||||
DeptId string `p:"deptId"` //部门id
|
||||
RoleId uint `p:"roleId"`
|
||||
Mobile string `p:"mobile"`
|
||||
Status string `p:"status"`
|
||||
KeyWords string `p:"keyWords"`
|
||||
|
14
go.sum
14
go.sum
@ -218,8 +218,8 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
|
||||
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
|
||||
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@ -255,21 +255,21 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8-0.20211105212822-18b340fc7af2/go.mod h1:EFNZuWvGYxIRUEX+K8UmCFwYmZjqcrnq15ZuVldZkZ0=
|
||||
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
|
||||
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
|
37
internal/app/system/dao/extend_dao.go
Normal file
37
internal/app/system/dao/extend_dao.go
Normal file
@ -0,0 +1,37 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// 解决多表排除字段不能用的情况
|
||||
// 例:FieldsEx(internal.SysUser,"a","ex_col1","ex_col2")
|
||||
func FieldsEx(obj interface{}, preFix string, exFields ...string) []string {
|
||||
v := reflect.ValueOf(obj)
|
||||
columnsMethod := v.MethodByName("Columns")
|
||||
if !columnsMethod.IsValid() {
|
||||
return []string{}
|
||||
}
|
||||
result := columnsMethod.Call([]reflect.Value{})
|
||||
|
||||
var fields []string
|
||||
for i := 0; i < result[0].NumField(); i++ {
|
||||
f := result[0].Field(i)
|
||||
if f.Kind() == reflect.String {
|
||||
|
||||
if !isInArray(f.String(), exFields) {
|
||||
fields = append(fields, preFix+"."+f.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
func isInArray(str string, arr []string) bool {
|
||||
for _, v := range arr {
|
||||
if v == str {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@ -9,6 +9,7 @@ package sysRole
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
@ -52,7 +53,10 @@ func (s *sSysRole) GetRoleListSearch(ctx context.Context, req *system.RoleListRe
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
err = model.Page(res.CurrentPage, req.PageSize).Order("id asc").Scan(&res.List)
|
||||
model = model.As("a")
|
||||
model = model.LeftJoin("casbin_rule", "b", "b.v1 = a.id AND SUBSTR( b.v0, 1, 2 ) = 'u_' ")
|
||||
model = model.Group("a.id")
|
||||
err = model.Page(res.CurrentPage, req.PageSize).Order("id asc").Fields("a.*, count(b.v0) user_cnt").Scan(&res.List)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
})
|
||||
return
|
||||
|
@ -10,6 +10,8 @@ package sysUser
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gset"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
@ -29,7 +31,6 @@ import (
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/libUtils"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -305,6 +306,40 @@ func (s *sSysUser) GetPermissions(ctx context.Context, roleIds []uint) (userButt
|
||||
return
|
||||
}
|
||||
|
||||
func fieldsEx() []string {
|
||||
s := dao.SysUser.Columns()
|
||||
return []string{
|
||||
s.Id,
|
||||
s.UserName,
|
||||
s.Mobile,
|
||||
s.UserNickname,
|
||||
s.Birthday,
|
||||
s.UserStatus,
|
||||
s.UserEmail,
|
||||
s.Sex,
|
||||
s.Avatar,
|
||||
s.DeptId,
|
||||
s.Remark,
|
||||
s.IsAdmin,
|
||||
s.Address,
|
||||
s.Describe,
|
||||
s.LastLoginIp,
|
||||
s.LastLoginTime,
|
||||
s.CreatedAt,
|
||||
s.UpdatedAt,
|
||||
s.DeletedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func inSlice(field string, excludes []string) bool {
|
||||
for _, exclude := range excludes {
|
||||
if exclude == field {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// List 用户列表
|
||||
func (s *sSysUser) List(ctx context.Context, req *system.UserSearchReq) (total interface{}, userList []*entity.SysUser, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
@ -327,6 +362,11 @@ func (s *sSysUser) List(ctx context.Context, req *system.UserSearchReq) (total i
|
||||
if len(req.DateRange) > 0 {
|
||||
m = m.Where("created_at >=? AND created_at <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
|
||||
if req.RoleId > 0 {
|
||||
m = m.As("a").LeftJoin("casbin_rule", "b", "b.v0 = CONCAT('u_',a.id )")
|
||||
m = m.Where("v1 = ? and SUBSTR(v0,1,2) = 'u_'", req.RoleId)
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
@ -335,8 +375,15 @@ func (s *sSysUser) List(ctx context.Context, req *system.UserSearchReq) (total i
|
||||
}
|
||||
total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取用户数据失败")
|
||||
err = m.FieldsEx(dao.SysUser.Columns().UserPassword, dao.SysUser.Columns().UserSalt).
|
||||
Page(req.PageNum, req.PageSize).Order("id asc").Scan(&userList)
|
||||
|
||||
// 排除password字段
|
||||
if req.RoleId > 0 {
|
||||
m = m.Fields(fieldsEx())
|
||||
} else {
|
||||
m = m.FieldsEx(dao.SysUser.Columns().UserPassword, dao.SysUser.Columns().UserSalt)
|
||||
}
|
||||
|
||||
err = m.Page(req.PageNum, req.PageSize).Order("id asc").Scan(&userList)
|
||||
liberr.ErrIsNil(ctx, err, "获取用户列表失败")
|
||||
})
|
||||
return
|
||||
@ -701,7 +748,7 @@ func (s *sSysUser) HasAccessByDataWhere(ctx context.Context, where g.Map, uid in
|
||||
err := g.Try(ctx, func(ctx context.Context) {
|
||||
rec, err := dao.SysUser.Ctx(ctx).As("user").
|
||||
Fields("user.id").
|
||||
Where("user.id",uid).Where(where).One()
|
||||
Where("user.id", uid).Where(where).One()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
if rec.IsEmpty() {
|
||||
liberr.ErrIsNil(ctx, gerror.New("没有数据"))
|
||||
|
@ -18,4 +18,5 @@ type SysRole struct {
|
||||
DataScope uint `json:"dataScope" description:"数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限)"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" description:"创建时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" description:"更新时间"`
|
||||
UserCnt uint `json:"userCnt" description:"用户数量"`
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user