im.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
162
163
package auth
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
"strconv"
"time"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/im"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
)
// 更新用户 IM INFO
func InitOrUpdateUserIMInfo(userId int64, name string, ctx *transaction.TransactionContext) (imInfo *domain.ImInfo, err error) {
var (
ImInfoRepository, _ = factory.CreateImInfoRepository(ctx)
checkImRequest *protocol.CheckImRequest = &protocol.CheckImRequest{}
IsCreated = false
checkImResponse *protocol.CheckImResponse
)
var errFind error
imInfo, errFind = ImInfoRepository.FindOne(map[string]interface{}{"user_id": userId})
// 异常
if errFind != nil && errFind != domain.QueryNoRow {
err = errFind
return
}
// 不存在
if errFind == domain.QueryNoRow {
imInfo = &domain.ImInfo{
UserId: userId,
CreateTime: time.Now(),
}
}
// 已存在
if errFind == nil && imInfo != nil {
IsCreated = true
}
if len(imInfo.ImId) == 0 {
id, _ := utils.NewSnowflakeId()
imInfo.ImId = fmt.Sprintf("%v", id)
}
checkImRequest = &protocol.CheckImRequest{
UserId: imInfo.UserId,
ImId: imInfo.ImId,
Uname: name,
CustomerImId: fmt.Sprintf("%v", imInfo.CustomerImId),
IsCreated: IsCreated,
}
if checkImResponse, err = CheckIm(checkImRequest); err != nil {
return
}
if imInfo.CustomerImId == 0 {
imInfo.CustomerImId = getRandomCustomerAccount(userId, ctx)
}
imInfo.ImToken = checkImResponse.ImToken
imInfo.UpdateTime = time.Now()
if _, err = ImInfoRepository.Save(imInfo); err != nil {
return
}
return
}
// 检查ImToken
func CheckIm(request *protocol.CheckImRequest) (rsp *protocol.CheckImResponse, err error) {
var ()
rsp = &protocol.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 *protocol.CheckImRequest, rsp *protocol.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 *protocol.CheckImRequest, rsp *protocol.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 *protocol.CheckImRequest, rsp *protocol.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, ctx *transaction.TransactionContext) (acid int64) {
ImCustomerServiceRepository, _ := factory.CreateImCustomerServiceRepository(ctx)
total, customers, err := ImCustomerServiceRepository.Find(map[string]interface{}{"sortById": domain.ASC})
if err != nil {
log.Error(err)
return 0
}
if total == 0 {
return 0
}
index := userId % total
if int(index) < len(customers) {
acid, _ = strconv.ParseInt(customers[index].ImId, 10, 64)
return
}
acid, _ = strconv.ParseInt(customers[0].ImId, 10, 64)
return
}