system_user_info_logic.go
3.1 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
package user
import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"strconv"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemUserInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUserInfoLogic {
return &SystemUserInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemUserInfoLogic) SystemUserInfo(req *types.SystemUserInfoRequest) (resp *types.SystemUserInfoResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
response *authlib.DataUserMe
company *domain.Company
companyId int64
code = tool.Krand(6, tool.KC_RAND_KIND_ALL)
)
if response, err = l.svcCtx.ApiAuthService.MeInfo(l.ctx, authlib.RequestUserMeQuery{
Token: req.Token,
}); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
companyId, _ = strconv.ParseInt(response.CurrentCompany.ID, 10, 64)
resp = &types.SystemUserInfoResponse{}
resp.UserName = response.User.NickName
resp.UserId, _ = strconv.ParseInt(response.User.ID, 10, 64)
resp.Avatar = response.User.Avatar
resp.CompanyName = response.CurrentCompany.Name
resp.CompanyId = companyId
resp.Code = code
if companyId != userToken.CompanyId {
return nil, xerr.NewErrMsgErr("获取用户资料失败", fmt.Errorf("当前登录公司信息不匹配"))
}
company, err = l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, userToken.CompanyId)
// 新建公司
if err == domain.ErrNotFound {
company = &domain.Company{
Id: companyId,
Name: response.CurrentCompany.Name,
Logo: response.CurrentCompany.Logo,
Code: code,
}
if company, err = l.svcCtx.CompanyRepository.Insert(l.ctx, conn, company); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
err = nil
return
}
if err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
resp.Code = company.Code
// 更新公司
if response.CurrentCompany != nil {
var changed bool
if response.CurrentCompany.Name != "" && response.CurrentCompany.Name != company.Name {
company.Name = response.CurrentCompany.Name
changed = true
}
if response.CurrentCompany.Logo != "" && response.CurrentCompany.Logo != company.Logo {
company.Logo = response.CurrentCompany.Logo
changed = true
}
if changed {
if company, err = l.svcCtx.CompanyRepository.UpdateWithVersion(l.ctx, conn, company); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
}
}
return
}