pg_im_service.go
4.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/im"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
"time"
)
// PgImService 网易云信IM服务
type PgImService struct {
transactionContext *pgTransaction.TransactionContext
}
func (ptr *PgImService) InitOrUpdateUserIMInfo(userId int64, flag int) (*domain.Im, error) {
var (
checkImRequest *im.CheckImRequest = &im.CheckImRequest{}
IsCreated = false
checkImResponse *im.CheckImResponse
)
userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": fmt.Sprintf("%v", userId)})
if err != nil || userBase == nil || userBase.Status != int(domain.UserStatusEnable) {
return nil, fmt.Errorf("账号不存在")
}
if userBase.Im != nil && len(userBase.Im.Accid) > 0 {
IsCreated = true
if flag == domain.RefreshWhenNotExists {
return userBase.Im, nil
}
} else {
id, err := repository.IdWorker.NextId()
if err != nil {
return nil, err
}
userBase.Im = &domain.Im{
Accid: fmt.Sprintf("%v", id),
}
}
checkImRequest = &im.CheckImRequest{
UserId: userId,
ImId: userBase.Im.Accid,
Uname: userBase.UserInfo.UserName,
CustomerImId: fmt.Sprintf("%v", userBase.Im.CsAccountId),
IsCreated: IsCreated,
}
if checkImResponse, err = CheckIm(checkImRequest); err != nil {
return nil, err
}
if len(userBase.Im.CsAccountId) == 0 {
userBase.Im.CsAccountId = fmt.Sprintf("%v", getRandomCustomerAccount(userId))
}
userBase.Im.ImToken = checkImResponse.ImToken
userBase.LastLogIn(time.Now())
if userBase, err = userBaseRepository.Save(userBase); err != nil {
}
return userBase.Im, nil
}
// 检查ImToken
func CheckIm(request *im.CheckImRequest) (rsp *im.CheckImResponse, err error) {
var ()
rsp = &im.CheckImResponse{}
if !request.IsCreated {
if err = imCreate(request, rsp); err != nil {
return
}
} else {
if err = imUpdate(request, rsp); err != nil {
return
}
}
if err = imRefreshToken(request, rsp); err != nil {
return
}
return
}
//create
func imCreate(request *im.CheckImRequest, rsp *im.CheckImResponse) (err error) {
var (
param im.UserCreate = im.UserCreate{
Accid: request.ImId,
Name: request.Uname,
Icon: request.Icon,
}
out *im.UserTokenResult
)
if out, err = im.CallCreate(param); err != nil {
return
}
if out.Code != 200 || (out.Info.Accid != request.ImId) {
return im.ErrorFailCall
}
rsp.ImToken = out.Info.Token
return
}
//update user info
func imUpdate(request *im.CheckImRequest, rsp *im.CheckImResponse) (err error) {
var (
param im.UserUpdate = im.UserUpdate{
Accid: request.ImId,
Name: request.Uname,
Icon: request.Icon,
}
out *im.BaseResp
)
if out, err = im.CallUpdate(param); err != nil {
return
}
if out.Code != 200 {
return im.ErrorFailCall
}
return
}
//refresh token
func imRefreshToken(request *im.CheckImRequest, rsp *im.CheckImResponse) (err error) {
var (
param im.UserRefreshToken = im.UserRefreshToken{
Accid: request.ImId,
}
out *im.UserTokenResult
)
if out, err = im.CallRefreshToken(param); err != nil {
return
}
if out.Code != 200 || (out.Info.Accid != request.ImId) {
return im.ErrorFailCall
}
rsp.ImToken = out.Info.Token
return
}
// 获取客服id
func getRandomCustomerAccount(userId int64) int64 {
total, customers := len(constant.CUSTOMER_ACCOUNT), constant.CUSTOMER_ACCOUNT
if total == 0 {
return 0
}
index := (int(userId) & 0xFF) % int(total)
if int(index) < len(customers) {
return customers[index]
}
return customers[0]
}
func NewPgImService(transactionContext *pgTransaction.TransactionContext) (*PgImService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgImService{
transactionContext: transactionContext,
}, nil
}
}