Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
75970f92
Commit
75970f92
authored
Mar 03, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:迁移代码
parent
2ca0565e
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
861 additions
and
3 deletions
+861
-3
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
util.go
resource/redisCli/util.go
+35
-0
finance.go
rpc/finance.go
+23
-3
fix_user_svip.go
script/fix_user_svip.go
+91
-0
agora.go
sdk/agora/agora.go
+652
-0
time.go
utils/time.go
+5
-0
url.go
utils/url.go
+1
-0
utils.go
utils/utils.go
+51
-0
No files found.
go.mod
View file @
75970f92
...
...
@@ -3,6 +3,7 @@ module git.hilo.cn/hilo-common
go 1.17
require (
github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src v0.0.0-20200910100525-12b7f1b63a6a // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1274 // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
...
...
go.sum
View file @
75970f92
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src v0.0.0-20200910100525-12b7f1b63a6a h1:+/ILLIcuKbrW4kyQ0V574aV9skXRivrcmlg0IBk7apY=
github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src v0.0.0-20200910100525-12b7f1b63a6a/go.mod h1:4bXIK0ntDk9CqAXobmomWd7dedbfNv/aaIpmpqqzt+A=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1274 h1:u48e7I7h/BY5uDP8xiIFNaUkdTVk7hjj/Sucg8FrxNU=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1274/go.mod h1:9CMdKNL3ynIGPpfTcdwTvIm8SGuAZYYC4jFVSSvE1YQ=
...
...
resource/redisCli/util.go
0 → 100644
View file @
75970f92
package
redisCli
import
(
"context"
"git.hilo.cn/hilo-common/mylogrus"
"time"
)
//这个用户避免多个服务器并发问题。
func
SetNX
(
key
string
,
value
interface
{},
expiration
time
.
Duration
,
callBack
func
())
{
flag
,
err
:=
RedisClient
.
SetNX
(
context
.
Background
(),
key
,
value
,
expiration
)
.
Result
()
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"key:%v lock start setNx err: %v"
,
key
,
err
)
}
if
!
flag
{
mylogrus
.
MyLog
.
Infof
(
"key:%v lock setNx has lock"
,
key
)
return
}
mylogrus
.
MyLog
.
Infof
(
"key:%v lock setNx begin"
,
key
)
callBack
()
//执行结束之后,移除key
//RedisClient.Del(context.Background(), key)
mylogrus
.
MyLog
.
Infof
(
"key:%v lock setNx end"
,
key
)
}
func
Lock
(
key
string
,
expiration
time
.
Duration
)
bool
{
flag
,
err
:=
RedisClient
.
SetNX
(
context
.
Background
(),
key
,
1
,
expiration
)
.
Result
()
if
err
!=
nil
{
return
false
}
if
!
flag
{
return
false
}
return
true
}
rpc/finance.go
View file @
75970f92
...
...
@@ -52,9 +52,10 @@ type CvSvip struct {
}
type
CVSvipPrivilege
struct
{
Type
int
`json:"type"`
// 1.专属勋章, 2.专属标识, 3.设备和IP踢出房间, 4.隐藏在线, 5.禁止跟随, 6.炫彩昵称, 7.隐藏礼物墙, 8.隐藏访客记录, 9.排行榜隐身, 10.房间防踢, 11.房间防抱下麦
CanSwitch
bool
`json:"canSwitch"`
// 能否开关
UserSwitch
bool
`json:"userSwitch"`
// 用户开关
Type
int
`json:"type"`
// 1.专属勋章, 2.专属标识, 3.设备和IP踢出房间, 4.隐藏在线, 5.禁止跟随, 6.炫彩昵称, 7.隐藏礼物墙, 8.隐藏访客记录, 9.排行榜隐身, 10.房间防踢, 11.房间防抱下麦
CanSwitch
bool
`json:"canSwitch"`
// 能否开关
UserSwitch
bool
`json:"userSwitch"`
// 用户开关
MysteryCode
string
`json:"mysteryCode,omitempty"`
// 神秘人代码,特权17的专属代码
}
// 获取svip等级
...
...
@@ -120,6 +121,25 @@ func MGetUserSvip(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]CvSvip,
return
res
,
nil
}
// 拷贝客户端用到的privileges赋值
// 避免推送大包体
// 目前暂时只需要 svip7.神秘人特权
func
CopySimpleSvip
(
svip
CvSvip
)
CvSvip
{
var
simpleSvip
CvSvip
simpleSvip
.
SvipLevel
=
svip
.
SvipLevel
for
_
,
p
:=
range
svip
.
Privileges
{
if
p
.
Type
==
17
{
// 神秘人代码,特权17的专属代码
simpleSvip
.
Privileges
=
append
(
simpleSvip
.
Privileges
,
CVSvipPrivilege
{
Type
:
p
.
Type
,
CanSwitch
:
p
.
CanSwitch
,
UserSwitch
:
p
.
UserSwitch
,
MysteryCode
:
p
.
MysteryCode
,
})
}
}
return
simpleSvip
}
func
getFinanceHost
()
string
{
l
:=
len
(
financeServerHost
)
r
:=
rand
.
Intn
(
l
)
// 随机一个
...
...
script/fix_user_svip.go
0 → 100644
View file @
75970f92
package
main
import
(
"fmt"
"git.hilo.cn/hilo-common/script/model"
"git.hilo.cn/hilo-common/script/mysql"
)
type
UserSvip
struct
{
UserId
uint64
Code
string
Points
int64
Level
int
AcPoints
int64
Diamond
int64
}
var
bt1
=
"2023-01-04 16:42:58"
var
et1
=
"2023-03-04 00:00:00"
func
cal
(
level
int
)
int64
{
switch
level
{
case
1
:
return
50000
case
2
:
return
300000
case
3
:
return
1000000
case
4
:
return
2000000
case
5
:
return
5000000
case
6
:
return
10000000
case
7
:
return
30000000
}
return
0
}
func
main
()
{
var
userSvip
[]
UserSvip
if
err
:=
mysql
.
ProdReadOnlyDB
.
Model
(
UserSvip
{})
.
Where
(
"level > 0"
)
.
Find
(
&
userSvip
)
.
Error
;
err
!=
nil
{
panic
(
err
)
}
for
i
,
v
:=
range
userSvip
{
_
,
userSvip
[
i
]
.
Diamond
,
_
=
GetChargeMoneyDiamond2
(
v
.
UserId
)
userSvip
[
i
]
.
AcPoints
=
v
.
Points
+
cal
(
v
.
Level
)
var
user
model
.
User
if
err
:=
mysql
.
ProdReadOnlyDB
.
Model
(
model
.
User
{})
.
Where
(
"id = ? "
,
v
.
UserId
)
.
First
(
&
user
)
.
Error
;
err
!=
nil
{
panic
(
err
)
}
userSvip
[
i
]
.
Code
=
user
.
Code
}
fmt
.
Printf
(
"UserId,Points,Level,AcPoints,Diamond
\n
"
)
for
_
,
v
:=
range
userSvip
{
fmt
.
Printf
(
"%v,%v,%v,%v,%v
\n
"
,
v
.
Code
,
v
.
Points
,
v
.
Level
,
v
.
AcPoints
,
v
.
Diamond
)
}
}
func
GetChargeMoneyDiamond2
(
uid
uint64
)
(
int64
,
int64
,
error
)
{
type
R
struct
{
Money
int64
Diamond
int64
}
var
money
R
if
err
:=
mysql
.
ProdReadOnlyDB
.
Table
(
"diamond_account_detail AS d"
)
.
Joins
(
"INNER JOIN pay_order AS p ON d.origin_id = p.id"
)
.
Where
(
"d.user_id in (?) AND operate_type in (?) AND add_reduce = 1"
,
uid
,
[]
int
{
int
(
4
),
int
(
42
),
int
(
55
),
int
(
68
)})
.
Where
(
"d.created_time >= ? AND d.created_time < ?"
,
bt1
,
et1
)
.
Order
(
"d.user_id"
)
.
Select
(
"SUM(p.price) as money,SUM(d.num) as diamond"
)
.
Scan
(
&
money
)
.
Error
;
err
!=
nil
{
return
0
,
0
,
err
}
type
R2
struct
{
Price
int64
Diamond
int64
}
var
money2
R2
if
err
:=
mysql
.
ProdReadOnlyDB
.
Table
(
"diamond_account_detail AS d"
)
.
Joins
(
"INNER JOIN dealer_transfer_detail AS t ON d.origin_id = t.id"
)
.
Where
(
"d.user_id in (?) AND operate_type = ? AND add_reduce = 1"
,
uid
,
26
)
.
Select
(
"SUM(t.dollar) as price,SUM(d.num) as diamond"
)
.
Where
(
"d.created_time >= ? AND d.created_time < ?"
,
bt1
,
et1
)
.
Order
(
"d.user_id"
)
.
Scan
(
&
money2
)
.
Error
;
err
!=
nil
{
return
0
,
0
,
err
}
return
money
.
Money
+
money2
.
Price
,
money
.
Diamond
+
money2
.
Diamond
,
nil
}
sdk/agora/agora.go
0 → 100644
View file @
75970f92
package
agora
import
(
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/config"
rtctokenbuilder
"github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src/RtcTokenBuilder"
"io/ioutil"
"net/http"
"strconv"
"time"
)
const
urlBase
=
"https://api.agora.io/v1/apps/"
//建立MatchConfirm
func
CreateAgora
(
channelId
string
,
expireTimestamp
uint32
,
agoraId1
uint32
,
agoraId2
uint32
)
(
string
,
string
,
uint32
,
string
,
uint32
,
error
)
{
appId
:=
config
.
GetAgoraAppId
()
appCertificate
:=
config
.
GetAgoraAppCertificate
()
//channelId := strings.Replace(uuid.NewV4().String(), "-", "", -1)
//expireTimeInSeconds := uint32(60)
//currentTimestamp := uint32(time.Now().UTC().Unix())
//expireTimestamp := currentTimestamp + expireTimeInSeconds
token1
,
err
:=
rtctokenbuilder
.
BuildTokenWithUID
(
appId
,
appCertificate
,
channelId
,
agoraId1
,
rtctokenbuilder
.
RoleAttendee
,
expireTimestamp
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Printf
(
"Fail to obtain token for user 1 %v"
,
err
.
Error
())
return
""
,
""
,
0
,
""
,
0
,
err
}
token2
,
err
:=
rtctokenbuilder
.
BuildTokenWithUID
(
appId
,
appCertificate
,
channelId
,
agoraId2
,
rtctokenbuilder
.
RoleAttendee
,
expireTimestamp
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Printf
(
"Fail to obtain token for user 2 %v"
,
err
.
Error
())
return
""
,
""
,
0
,
""
,
0
,
err
}
return
channelId
,
token1
,
agoraId1
,
token2
,
agoraId2
,
nil
}
//创建群组的声网通道,token有效时长:一个星期, 参数:userId uint32, channelId,业务保证唯一性。返回:channelId, token, err
func
CreateGroupAgora
(
channelId
string
,
uid
uint32
)
(
string
,
string
,
error
)
{
appId
:=
config
.
GetAgoraAppId
()
appCertificate
:=
config
.
GetAgoraAppCertificate
()
//channelId := strings.Replace(uuid.NewV4().String(), "-", "", -1)
expireTimeInSeconds
:=
uint32
(
60
*
60
*
24
*
7
)
//一个星期
currentTimestamp
:=
uint32
(
time
.
Now
()
.
UTC
()
.
Unix
())
expireTimestamp
:=
currentTimestamp
+
expireTimeInSeconds
token
,
err
:=
rtctokenbuilder
.
BuildTokenWithUID
(
appId
,
appCertificate
,
channelId
,
uid
,
rtctokenbuilder
.
RoleAttendee
,
expireTimestamp
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Printf
(
"Fail to obtain token for user 1 %v"
,
err
.
Error
())
return
""
,
""
,
err
}
return
channelId
,
token
,
nil
}
// 生成RESTful API 凭证
func
createCredential
()
string
{
customerKey
:=
config
.
GetAgoraCustomerKey
()
customerSecret
:=
config
.
GetAgoraCustomerSecret
()
// 拼接客户 ID 和客户密钥并使用 base64 进行编码
plainCredentials
:=
customerKey
+
":"
+
customerSecret
base64Credentials
:=
base64
.
StdEncoding
.
EncodeToString
([]
byte
(
plainCredentials
))
mylogrus
.
MyLog
.
Infof
(
"agora customerKey :%s customerSecret:%s encodeString:%s"
,
customerKey
,
customerSecret
,
base64Credentials
)
return
base64Credentials
}
type
AcquireResourceBody
struct
{
ResourceId
string
`json:"resourceId"`
}
type
StartResourceBody
struct
{
Sid
string
`json:"sid"`
ResourceId
string
`json:"resourceId"`
}
type
FileListItem
struct
{
Filename
string
`json:"filename"`
TrackType
string
`json:"trackType"`
Uid
string
`json:"uid"`
MixedAllUser
bool
`json:"mixedAllUser"`
IsPlayable
bool
`json:"isPlayable"`
SliceStartTime
int64
`json:"sliceStartTime"`
}
type
ServerResponse
struct
{
FileListMode
string
`json:"fileListMode"`
FileList
[]
FileListItem
`json:"fileList"`
UploadingStatus
string
`json:"uploadingStatus"`
}
type
StopResponseBody
struct
{
ResourceId
string
`json:"resourceId"`
Sid
string
`json:"sid"`
ServerResponse
ServerResponse
`json:"serverResponse"`
}
func
generateUid
()
uint32
{
// 1609430400就是2021-01-01 00:00:00
return
uint32
(
time
.
Now
()
.
Unix
()
-
1609430400
)
}
func
GetRecordPathPrefix
()
string
{
return
"nextvideo/agoraRecording/"
}
//获取资源Id
func
acquireResourceId
(
cname
string
)
(
string
,
uint32
,
error
)
{
appId
:=
config
.
GetAgoraAppId
()
url
:=
urlBase
+
appId
+
"/cloud_recording/acquire"
clientRequest
:=
make
(
map
[
string
]
interface
{})
clientRequest
[
"resourceExpiredHour"
]
=
24
clientRequest
[
"scene"
]
=
2
// 申请延时转码资源
params
:=
make
(
map
[
string
]
interface
{})
params
[
"cname"
]
=
cname
uid
:=
generateUid
()
params
[
"uid"
]
=
strconv
.
FormatUint
(
uint64
(
uid
),
10
)
params
[
"clientRequest"
]
=
clientRequest
bytesData
,
err
:=
json
.
Marshal
(
params
)
if
err
!=
nil
{
return
""
,
0
,
err
}
reader
:=
bytes
.
NewReader
(
bytesData
)
//mylogrus.MyLog.Infof("agora api:acquire url:%v, param:%v", url, string(bytesData))
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
reader
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:acquire request err:%v"
,
err
)
return
""
,
0
,
err
// handle error
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:acquire do err:%v"
,
err
)
return
""
,
0
,
err
}
defer
resp
.
Body
.
Close
()
mylogrus
.
MyLog
.
Infof
(
"req header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
""
,
0
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
mylogrus
.
MyLog
.
Infof
(
"agora api:acquire resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:acquire resp err:%v"
,
err
)
return
""
,
0
,
err
}
var
resourceBody
AcquireResourceBody
if
err
:=
json
.
Unmarshal
(
body
,
&
resourceBody
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:acquire resp toStruts err:%v, body:%v"
,
err
,
string
(
body
))
return
""
,
0
,
err
}
mylogrus
.
MyLog
.
Infof
(
"agora api:acquire resp success sid:%v"
,
resourceBody
.
ResourceId
)
return
resourceBody
.
ResourceId
,
uid
,
nil
}
func
StartRecording
(
token
string
,
cname
string
)
(
string
,
string
,
uint32
,
error
)
{
mylogrus
.
MyLog
.
Infof
(
"agora start recording, token: %s, cname: %s"
,
token
,
cname
)
resourceId
,
uid
,
err
:=
acquireResourceId
(
cname
)
if
err
!=
nil
{
return
""
,
""
,
0
,
err
}
appId
:=
config
.
GetAgoraAppId
()
url
:=
urlBase
+
appId
+
"/cloud_recording/resourceid/"
+
resourceId
+
"/mode/individual/start"
appsCollection
:=
make
(
map
[
string
]
interface
{})
appsCollection
[
"combinationPolicy"
]
=
"postpone_transcoding"
// 打开延时转码
recordingConfig
:=
map
[
string
]
interface
{}{
"channelType"
:
0
,
"streamTypes"
:
2
,
"subscribeUidGroup"
:
0
,
"maxIdleTime"
:
5
,
}
/*
snapshotConfig := map[string]interface{}{
"captureInterval": 5,
"fileType": []string{"jpg"},
}*/
storageConfig
:=
map
[
string
]
interface
{}{
"vendor"
:
2
,
"region"
:
6
,
"bucket"
:
config
.
GetConfigOss
()
.
OSS_BUCKET
,
"accessKey"
:
config
.
GetConfigOss
()
.
OSS_ACCESS_KEY_ID
,
"secretKey"
:
config
.
GetConfigOss
()
.
OSS_ACCESS_KEY_SECRET
,
"fileNamePrefix"
:
[]
string
{
"nextvideo"
,
"agoraRecording"
},
}
clientRequest
:=
map
[
string
]
interface
{}{
"token"
:
token
,
"appsCollection"
:
appsCollection
,
"recordingConfig"
:
recordingConfig
,
//"snapshotConfig": snapshotConfig,
"storageConfig"
:
storageConfig
,
}
params
:=
make
(
map
[
string
]
interface
{})
params
[
"cname"
]
=
cname
params
[
"uid"
]
=
strconv
.
FormatUint
(
uint64
(
uid
),
10
)
params
[
"clientRequest"
]
=
clientRequest
bytesData
,
err
:=
json
.
Marshal
(
params
)
if
err
!=
nil
{
return
""
,
""
,
0
,
err
}
reader
:=
bytes
.
NewReader
(
bytesData
)
mylogrus
.
MyLog
.
Infof
(
"agora start recording, url:%s, param:%s"
,
url
,
string
(
bytesData
))
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
reader
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:start request err:%v"
,
err
)
return
""
,
""
,
0
,
err
// handle error
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:start do err:%v"
,
err
)
return
""
,
""
,
0
,
err
}
defer
resp
.
Body
.
Close
()
mylogrus
.
MyLog
.
Infof
(
"req header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
""
,
""
,
0
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
mylogrus
.
MyLog
.
Infof
(
"agora api:start resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:start resp err:%v"
,
err
)
return
""
,
""
,
0
,
err
}
var
resourceBody
StartResourceBody
if
err
:=
json
.
Unmarshal
(
body
,
&
resourceBody
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:start resp toStruts err:%v, body:%v"
,
err
,
string
(
body
))
return
""
,
""
,
0
,
err
}
return
resourceId
,
resourceBody
.
Sid
,
uid
,
nil
}
func
StopRecording
(
token
,
resourceId
,
cname
,
sid
string
,
uid
uint32
)
(
*
StopResponseBody
,
error
)
{
mylogrus
.
MyLog
.
Debugf
(
"agora stop recording token: %s, resourceId: %s, cname: %s, sid: %s"
,
token
,
resourceId
,
cname
,
sid
)
appId
:=
config
.
GetAgoraAppId
()
url
:=
urlBase
+
appId
+
"/cloud_recording/resourceid/"
+
resourceId
+
"/sid/"
+
sid
+
"/mode/individual/stop"
clientRequest
:=
map
[
string
]
interface
{}{}
params
:=
make
(
map
[
string
]
interface
{})
params
[
"cname"
]
=
cname
params
[
"uid"
]
=
strconv
.
FormatUint
(
uint64
(
uid
),
10
)
params
[
"clientRequest"
]
=
clientRequest
bytesData
,
err
:=
json
.
Marshal
(
params
)
if
err
!=
nil
{
return
nil
,
err
}
reader
:=
bytes
.
NewReader
(
bytesData
)
mylogrus
.
MyLog
.
Debugf
(
"agora stop recording, url:%s, param:%s"
,
url
,
string
(
bytesData
))
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
reader
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:stop request err:%v"
,
err
)
return
nil
,
err
// handle error
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:stop do err:%v"
,
err
)
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
mylogrus
.
MyLog
.
Debugf
(
"req header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
mylogrus
.
MyLog
.
Debugf
(
"agora api:stop resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:stop resp err:%v"
,
err
)
return
nil
,
err
}
var
stopResponseBody
StopResponseBody
if
err
:=
json
.
Unmarshal
(
body
,
&
stopResponseBody
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:stop resp toStruts err:%v, body:%v"
,
err
,
string
(
body
))
return
nil
,
err
}
return
&
stopResponseBody
,
nil
}
func
Query
(
resourceId
,
sid
string
)
(
int
,
error
)
{
appId
:=
config
.
GetAgoraAppId
()
url
:=
urlBase
+
appId
+
"/cloud_recording/resourceid/"
+
resourceId
+
"/sid/"
+
sid
+
"/mode/individual/query"
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:acquire request err:%v"
,
err
)
return
0
,
err
// handle error
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:acquire do err:%v"
,
err
)
return
0
,
err
}
defer
resp
.
Body
.
Close
()
mylogrus
.
MyLog
.
Infof
(
"req header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
resp
.
StatusCode
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
mylogrus
.
MyLog
.
Infof
(
"agora api:acquire resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora api:acquire resp err:%v"
,
err
)
return
resp
.
StatusCode
,
err
}
mylogrus
.
MyLog
.
Info
(
body
)
return
0
,
nil
}
type
KickingResponseBody
struct
{
Status
string
`json:"status"`
Id
uint64
`json:"id"`
}
//增加封禁封禁规则
func
AddKickingGroupRule
(
channelId
,
uid
,
ip
string
)
(
*
KickingResponseBody
,
error
)
{
mylogrus
.
MyLog
.
Infoln
(
"agora AddKickingGroupRule channelId: %s, %s, %s"
,
channelId
,
uid
,
ip
)
appId
:=
config
.
GetAgoraAppId
()
url
:=
"https://api.agora.io/dev/v1/kicking-rule"
params
:=
make
(
map
[
string
]
interface
{})
params
[
"appid"
]
=
appId
params
[
"cname"
]
=
channelId
params
[
"uid"
]
=
uid
params
[
"ip"
]
=
ip
if
len
(
channelId
)
>
0
{
params
[
"time"
]
=
60
*
24
//封禁时长,不用担心,因为封禁之后,业务能控制不生产token
}
else
{
params
[
"time"
]
=
60
}
params
[
"privileges"
]
=
[]
string
{
"publish_audio"
}
bytesData
,
err
:=
json
.
Marshal
(
params
)
if
err
!=
nil
{
return
nil
,
err
}
reader
:=
bytes
.
NewReader
(
bytesData
)
mylogrus
.
MyLog
.
Infof
(
"agora AddKickingGroupRule, url:%v, param:%s"
,
url
,
string
(
bytesData
))
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
reader
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora AddKickingGroupRule api:stop request err:%v"
,
err
)
return
nil
,
err
// handle error
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora AddKickingGroupRule api:stop do err:%v"
,
err
)
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
mylogrus
.
MyLog
.
Infof
(
"req AddKickingGroupRule header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
mylogrus
.
MyLog
.
Debugf
(
"agora AddKickingGroupRule api:stop resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora AddKickingGroupRule api:stop resp err:%v"
,
err
)
return
nil
,
err
}
var
kickingResponseBody
KickingResponseBody
if
err
:=
json
.
Unmarshal
(
body
,
&
kickingResponseBody
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora AddKickingGroupRule api:stop resp toStruts err:%v, body:%v"
,
err
,
string
(
body
))
return
nil
,
err
}
return
&
kickingResponseBody
,
nil
}
//删除封禁频道规则
func
DelKickingRule
(
agoraRuleId
uint64
)
(
*
KickingResponseBody
,
error
)
{
mylogrus
.
MyLog
.
Infof
(
"agora DelKickingRule agoraRuleId: %s"
,
agoraRuleId
)
appId
:=
config
.
GetAgoraAppId
()
url
:=
"https://api.agora.io/dev/v1/kicking-rule"
params
:=
make
(
map
[
string
]
interface
{})
params
[
"appid"
]
=
appId
params
[
"id"
]
=
agoraRuleId
bytesData
,
err
:=
json
.
Marshal
(
params
)
if
err
!=
nil
{
return
nil
,
err
}
reader
:=
bytes
.
NewReader
(
bytesData
)
mylogrus
.
MyLog
.
Infof
(
"agora DelKickingRule, url:%v, param:%s"
,
url
,
string
(
bytesData
))
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
"DELETE"
,
url
,
reader
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora DelKickingRule api:stop request err:%v"
,
err
)
return
nil
,
err
// handle error
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora DelKickingRule api:stop do err:%v"
,
err
)
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
mylogrus
.
MyLog
.
Info
(
"req DelKickingRule header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
mylogrus
.
MyLog
.
Debugf
(
"agora DelKickingRule api:stop resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora DelKickingRule api:stop resp err:%v"
,
err
)
return
nil
,
err
}
var
kickingResponseBody
KickingResponseBody
if
err
:=
json
.
Unmarshal
(
body
,
&
kickingResponseBody
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora DelKickingRule api:stop resp toStruts err:%v, body:%v"
,
err
,
string
(
body
))
return
nil
,
err
}
return
&
kickingResponseBody
,
nil
}
type
ruleElem
struct
{
Id
int64
`json:"id"`
Uid
string
`json:"uid"`
StrUid
bool
`json:"str_uid"`
Cname
string
`json:"cname"`
Ip
*
string
`json:"ip"`
Ts
time
.
Time
`json:"ts"`
Privileges
[]
string
`json:"privileges"`
ClientIp
string
`json:"clientIp"`
Reason
*
string
`json:"reason"`
CreateAt
time
.
Time
`json:"createAt"`
UpdateAt
time
.
Time
`json:"updateAt"`
}
type
GetKickingResponseBody
struct
{
Status
string
`json:"status"`
Rules
[]
ruleElem
`json:"rules"`
}
// 查询封禁规则
func
GetKickingRule
()
(
*
GetKickingResponseBody
,
error
)
{
mylogrus
.
MyLog
.
Infoln
(
"agora GetKickingRule"
)
appId
:=
config
.
GetAgoraAppId
()
url
:=
"https://api.agora.io/dev/v1/kicking-rule?appid="
+
appId
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora GetKickingRule api:stop request err:%v"
,
err
)
return
nil
,
err
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora GetKickingRule api:stop do err:%v"
,
err
)
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
mylogrus
.
MyLog
.
Infof
(
"req GetKickingRule header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
mylogrus
.
MyLog
.
Debugf
(
"agora GetKickingRule api:stop resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora GetKickingRule api:stop resp err:%v"
,
err
)
return
nil
,
err
}
var
getResponse
GetKickingResponseBody
if
err
:=
json
.
Unmarshal
(
body
,
&
getResponse
);
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"agora GetKickingRule api:stop resp toStruts err:%v, body:%v"
,
err
,
string
(
body
))
return
nil
,
err
}
return
&
getResponse
,
nil
}
type
GetUserStatusRsp
struct
{
Success
bool
`json:"success"`
Data
struct
{
Join
int64
`json:"join"`
Uid
int64
`json:"uid"`
InChannel
bool
`json:"in_channel"`
Platform
int
`json:"platform"`
Role
int
`json:"role"`
}
`json:"data"`
}
// 查询用户状态
func
GetUserStatus
(
uid
int
,
chanName
string
)
(
*
GetUserStatusRsp
,
error
)
{
logger
:=
mylogrus
.
MyLog
.
WithField
(
"agora"
,
"GetUserStatus"
)
appId
:=
config
.
GetAgoraAppId
()
url
:=
"https://api.agora.io/dev/v1/channel/user/property/"
+
appId
+
"/"
+
strconv
.
Itoa
(
uid
)
+
"/"
+
chanName
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
logger
.
Errorf
(
"NewRequest err:%v"
,
err
)
return
nil
,
err
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
logger
.
Errorf
(
"Do err:%v"
,
err
)
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
logger
.
Infof
(
"header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
logger
.
Infof
(
"resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
logger
.
Errorf
(
"resp err:%v"
,
err
)
return
nil
,
err
}
var
getResponse
GetUserStatusRsp
if
err
:=
json
.
Unmarshal
(
body
,
&
getResponse
);
err
!=
nil
{
logger
.
Errorf
(
"unmarshal err:%v, body:%v"
,
err
,
string
(
body
))
return
nil
,
err
}
logger
.
Infof
(
"Rsp: %+v"
,
getResponse
)
return
&
getResponse
,
nil
}
type
GetUserListRsp
struct
{
Success
bool
`json:"success"`
Data
struct
{
ChannelExist
bool
`json:"channel_exist"`
Mode
int
`json:"mode"`
Broadcasters
[]
uint64
`json:"broadcasters"`
Audience
[]
uint64
`json:"audience"`
AudienceTotal
uint
`json:"audience_total"`
}
`json:"data"`
}
// 查询用户列表
func
GetUserList
(
chanName
string
)
(
*
GetUserListRsp
,
error
)
{
logger
:=
mylogrus
.
MyLog
.
WithField
(
"agora"
,
"GetUserList"
)
appId
:=
config
.
GetAgoraAppId
()
url
:=
"https://api.agora.io/dev/v1/channel/user/"
+
appId
+
"/"
+
chanName
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
logger
.
Errorf
(
"NewRequest err:%v"
,
err
)
return
nil
,
err
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json;charset=utf-8"
)
req
.
Header
.
Set
(
"Authorization"
,
"Basic "
+
createCredential
())
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
logger
.
Errorf
(
"Do err:%v"
,
err
)
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
logger
.
Infof
(
"header: %v, StatusCode = %d, %s"
,
req
.
Header
,
resp
.
StatusCode
,
resp
.
Status
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
fmt
.
Errorf
(
"%w"
,
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
logger
.
Infof
(
"resp body:%v"
,
string
(
body
))
if
err
!=
nil
{
logger
.
Errorf
(
"resp err:%v"
,
err
)
return
nil
,
err
}
var
getResponse
GetUserListRsp
if
err
:=
json
.
Unmarshal
(
body
,
&
getResponse
);
err
!=
nil
{
logger
.
Errorf
(
"unmarshal err:%v, body:%v"
,
err
,
string
(
body
))
return
nil
,
err
}
logger
.
Infof
(
"Rsp: %+v"
,
getResponse
)
return
&
getResponse
,
nil
}
utils/time.go
View file @
75970f92
...
...
@@ -6,6 +6,7 @@ const DEFAULT_LANG = "en"
const
DATETIME_FORMAT
=
"2006-01-02 15:04:05"
const
COMPACT_DATE_FORMAT
=
"20060102"
const
DATE_FORMAT
=
"2006-01-02"
const
COMPACT_MONTH_FORMAT
=
"200601"
func
GetZeroTime
(
t
time
.
Time
)
time
.
Time
{
return
time
.
Date
(
t
.
Year
(),
t
.
Month
(),
t
.
Day
(),
0
,
0
,
0
,
0
,
t
.
Location
())
...
...
@@ -22,3 +23,7 @@ func GetLastDayOfWeek(t time.Time, n time.Weekday) time.Time {
}
return
t
.
AddDate
(
0
,
0
,
-
(
int
(
weekDay
)))
}
func
GetMonday
(
t
time
.
Time
)
time
.
Time
{
return
GetLastDayOfWeek
(
t
,
time
.
Monday
)
}
utils/url.go
View file @
75970f92
...
...
@@ -6,6 +6,7 @@ import (
)
const
DefaultAvatarMan
=
"hilo/manager/ea48b62d54a24a709de3c38702c89995.png"
const
DEFAULT_NICK
=
"Hilo No.%s"
// 补全url,区分处理oss和aws两种情况
func
MakeFullUrl
(
url
string
)
string
{
...
...
utils/utils.go
View file @
75970f92
...
...
@@ -2,6 +2,8 @@ package utils
import
(
"encoding/json"
"git.hilo.cn/hilo-common/resource/mysql"
"time"
)
// 去除slice中的重复元素
...
...
@@ -32,3 +34,52 @@ func SliceToMapUInt64(s []uint64) map[uint64]struct{} {
}
return
m
}
func
IfLogoutStr
(
condition
bool
,
trueVal
,
falseVal
string
)
string
{
if
condition
{
return
trueVal
}
return
falseVal
}
func
IfLogoutNick
(
condition
bool
,
code
string
,
nick
string
)
string
{
if
condition
{
return
"Hilo No."
+
code
}
return
nick
}
func
IfLogout
(
logoutTime
int64
)
bool
{
return
logoutTime
>
0
&&
time
.
Now
()
.
Unix
()
>
logoutTime
}
func
BirthdayToUint64
(
birthday
*
mysql
.
Timestamp
)
*
uint64
{
if
*
birthday
==
0
{
return
nil
}
return
(
*
uint64
)(
birthday
)
}
//空字符串转成nil
func
StrNil
(
msg
string
)
*
string
{
if
msg
==
""
{
return
nil
}
return
&
msg
}
func
TypeToUint8
(
t
*
mysql
.
Type
)
*
uint8
{
if
*
t
==
0
{
return
nil
}
else
{
return
(
*
uint8
)(
t
)
}
}
func
StrToString
(
str
*
mysql
.
Str
)
*
string
{
return
(
*
string
)(
str
)
}
func
NumToUint32
(
num
*
mysql
.
Num
)
*
uint32
{
return
(
*
uint32
)(
num
)
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment