mini_user_info_logic.go
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package user
import (
"context"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/collection"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserInfoLogic {
return &MiniUserInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserInfoLogic) MiniUserInfo(req *types.MiniUserInfoRequest) (resp *types.MiniUserInfoResponse, err error) {
var (
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
user *domain.User
conn = l.svcCtx.DefaultDBConn()
accounts []*domain.User
companyMap = make(map[int64]*domain.Company)
roles []*domain.Role
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
return nil, xerr.NewErrMsgErr("用户不存在", err)
}
if _, roles, err = l.svcCtx.RoleRepository.Find(l.ctx, conn, domain.IndexCompanyId(user.CompanyId)().MustWithKV("ids", user.Roles)); err != nil {
return nil, xerr.NewErrMsgErr("角色不存在", err)
}
resp = &types.MiniUserInfoResponse{
User: &types.UserItem{
Id: user.Id,
Name: user.Name,
Avatar: lo.ToPtr(user.Avatar),
Position: user.Position,
AuditStatus: lo.ToPtr(user.AuditStatus),
Enable: user.Enable,
},
Accounts: make([]types.Account, 0),
Auths: make([]types.Auth, 0),
}
if company, _ := domain.LazyLoad(companyMap, l.ctx, conn, user.CompanyId, l.svcCtx.CompanyRepository.FindOne); company != nil {
resp.User.CompanyName = company.Name
resp.User.CompanyCode = company.Code
resp.User.CompanyLogo = lo.ToPtr(company.Logo)
}
_, accounts, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().MustWithKV("phone", user.Phone).MustWithKV("auditStatus", []int{domain.UserAuditStatusPassed}))
if err != nil {
return nil, xerr.NewErrMsgErr("用户不存在", err)
}
// 拥有的账号
for _, account := range accounts {
if account.Enable != domain.UserEnable {
continue
}
var (
companyName = ""
logo = ""
)
if company, _ := domain.LazyLoad(companyMap, l.ctx, conn, account.CompanyId, l.svcCtx.CompanyRepository.FindOne); company != nil {
companyName = company.Name
logo = company.Logo
}
resp.Accounts = append(resp.Accounts, types.Account{
CompanyId: account.CompanyId,
CompanyName: companyName,
Logo: logo,
UserId: account.Id,
Name: account.Name,
Position: account.Position,
})
}
// 拥有的权限
authSet := collection.NewSet()
for _, role := range roles {
for _, auth := range role.Auths {
if authSet.Contains(auth) {
continue
}
authSet.Add(auth)
if item := role.GetAuth(auth); item != nil {
resp.Auths = append(resp.Auths, types.Auth{
Id: item.Id,
Name: item.Name,
Code: item.Code,
})
}
}
}
return
}