-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathim.go
212 lines (191 loc) · 4.92 KB
/
im.go
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* @Author: fuxiao
* @Author: [email protected]
* @Date: 2021/5/27 20:29
* @Desc: 腾讯云IM
*/
package im
import (
"sync"
"time"
"github.com/dobyte/tencent-im/account"
"github.com/dobyte/tencent-im/callback"
"github.com/dobyte/tencent-im/group"
"github.com/dobyte/tencent-im/internal/core"
"github.com/dobyte/tencent-im/internal/sign"
"github.com/dobyte/tencent-im/mute"
"github.com/dobyte/tencent-im/operation"
"github.com/dobyte/tencent-im/private"
"github.com/dobyte/tencent-im/profile"
"github.com/dobyte/tencent-im/push"
"github.com/dobyte/tencent-im/recentcontact"
"github.com/dobyte/tencent-im/sns"
)
type Error = core.Error
type (
IM interface {
// GetUserSig 获取UserSig签名
GetUserSig(userId string, expiration ...int) UserSig
// SNS 获取关系链管理接口
SNS() sns.API
// Mute 获取全局禁言管理接口
Mute() mute.API
// Push 获取全员推送接口
Push() push.API
// Group 获取群组管理接口
Group() group.API
// Account 获取账号管理接口
Account() account.API
// Profile 获取资料管理接口
Profile() profile.API
// Private 获取私聊消息接口
Private() private.API
// Operation 获取运营管理接口
Operation() operation.API
// RecentContact 获取最近联系人接口
RecentContact() recentcontact.API
// Callback 获取回调接口
Callback() callback.Callback
}
Options struct {
AppId int // 应用SDKAppID,可在即时通信 IM 控制台 的应用卡片中获取。
AppSecret string // 密钥信息,可在即时通信 IM 控制台 的应用详情页面中获取,具体操作请参见 获取密钥
UserId string // 用户ID
Expiration int // UserSig过期时间
}
UserSig struct {
UserSig string // 用户签名
ExpireAt int64 // 签名过期时间
}
im struct {
opt *Options
client core.Client
sns struct {
once sync.Once
instance sns.API
}
mute struct {
once sync.Once
instance mute.API
}
push struct {
once sync.Once
instance push.API
}
group struct {
once sync.Once
instance group.API
}
account struct {
once sync.Once
instance account.API
}
profile struct {
once sync.Once
instance profile.API
}
private struct {
once sync.Once
instance private.API
}
operation struct {
once sync.Once
instance operation.API
}
recentcontact struct {
once sync.Once
instance recentcontact.API
}
callback struct {
once sync.Once
instance callback.Callback
}
}
)
func NewIM(opt *Options) IM {
return &im{opt: opt, client: core.NewClient(&core.Options{
AppId: opt.AppId,
AppSecret: opt.AppSecret,
UserId: opt.UserId,
Expiration: opt.Expiration,
})}
}
// GetUserSig 获取UserSig签名
func (i *im) GetUserSig(userId string, expiration ...int) UserSig {
if len(expiration) == 0 {
expiration = append(expiration, i.opt.Expiration)
}
userSig, _ := sign.GenUserSig(i.opt.AppId, i.opt.AppSecret, userId, expiration[0])
expireAt := time.Now().Add(time.Duration(i.opt.Expiration) * time.Second).Unix()
return UserSig{UserSig: userSig, ExpireAt: expireAt}
}
// SNS 获取关系链管理接口ok
func (i *im) SNS() sns.API {
i.sns.once.Do(func() {
i.sns.instance = sns.NewAPI(i.client)
})
return i.sns.instance
}
// Mute 获取全局禁言管理接口ok
func (i *im) Mute() mute.API {
i.mute.once.Do(func() {
i.mute.instance = mute.NewAPI(i.client)
})
return i.mute.instance
}
// Push 获取全员推送接口
func (i *im) Push() push.API {
i.push.once.Do(func() {
i.push.instance = push.NewAPI(i.client)
})
return i.push.instance
}
// Group 获取群组管理接口
func (i *im) Group() group.API {
i.group.once.Do(func() {
i.group.instance = group.NewAPI(i.client)
})
return i.group.instance
}
// Account 获取账号管理接口ok
func (i *im) Account() account.API {
i.account.once.Do(func() {
i.account.instance = account.NewAPI(i.client)
})
return i.account.instance
}
// Profile 获取资料管理接口ok
func (i *im) Profile() profile.API {
i.profile.once.Do(func() {
i.profile.instance = profile.NewAPI(i.client)
})
return i.profile.instance
}
// Private 获取私聊消息接口ok
func (i *im) Private() private.API {
i.private.once.Do(func() {
i.private.instance = private.NewAPI(i.client)
})
return i.private.instance
}
// Operation 获取运营管理接口ok
func (i *im) Operation() operation.API {
i.operation.once.Do(func() {
i.operation.instance = operation.NewAPI(i.client)
})
return i.operation.instance
}
// RecentContact 获取最近联系人接口ok
func (i *im) RecentContact() recentcontact.API {
i.recentcontact.once.Do(func() {
i.recentcontact.instance = recentcontact.NewAPI(i.client)
})
return i.recentcontact.instance
}
// Callback 获取回调接口
func (i *im) Callback() callback.Callback {
i.callback.once.Do(func() {
i.callback.instance = callback.NewCallback(i.opt.AppId)
})
return i.callback.instance
}