From a253184f3c9a122b833b07960e358de7b71216db Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Thu, 13 Apr 2023 13:50:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5trtc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _const/enum/group_e/enum.go | 8 ++++++++ cv/group_cv/group.go | 1 + debug.ini | 3 +++ domain/model/group_m/groupInfo.go | 24 ++++++++++++++++++++++++ domain/service/group_s/group_op.go | 12 ++++++++++-- local.ini | 3 +++ mysql/trtc.sql | 9 +++++++++ release.ini | 3 +++ route/group_r/group_op.go | 29 ++++++++++++++++++++++++++++- test/group_power_test.go | 12 ++++++++++-- 10 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 mysql/trtc.sql diff --git a/_const/enum/group_e/enum.go b/_const/enum/group_e/enum.go index 10f7c5b..e93adf9 100644 --- a/_const/enum/group_e/enum.go +++ b/_const/enum/group_e/enum.go @@ -156,3 +156,11 @@ var GROUP_NOBLE4_JOIN_LIMIT uint = 1000 var GROUP_ROLE_PERSONAL_VIEW_LIMIT = 5 var GROUP_MANAGER_LIMIT uint = 30 var GROUP_ADMIN_LIMIT uint = 50 + +type GroupProvider = uint8 + +const ( + // 群组声音供应商 + GroupProvider_SW GroupProvider = 0 // 声网 + GroupProvider_TRTC GroupProvider = 1 // 腾讯trtc +) diff --git a/cv/group_cv/group.go b/cv/group_cv/group.go index 4661d4b..28cb11b 100644 --- a/cv/group_cv/group.go +++ b/cv/group_cv/group.go @@ -221,6 +221,7 @@ type GroupChannelId struct { Token string `json:"token"` AgoraId uint32 `json:"agoraId"` MicNumType uint8 `json:"micNumType"` + Provider uint8 `json:"provider"` // 供应商 1.声网 2.腾讯trtc } //国籍视图 diff --git a/debug.ini b/debug.ini index 976183c..4c5ff45 100644 --- a/debug.ini +++ b/debug.ini @@ -72,6 +72,9 @@ APP_CERTIFICATE=ff29c100a613433db82324e8411eabc8 CUSTOMER_KEY=6b132c0ff7164560a2bc53fda06ea85a CUSTOMER_SECRET=eedad2cd16d24834990d5450ace9f1ce CALLBACK_SECRET=n_ZizS_N8 +[TRTC] +APP_ID=1400548270 +APP_CERTIFICATE=321bd60f73096b059c7350f1cd97d51028850b34fa58c5c0d26bb4a19e783de8 [CHECKOUT] AUTHORIZATION=sk_test_9b5e771c-5a3f-4a8d-a4da-31b19bd43d83 URL=https://api.sandbox.checkout.com/hosted-payments diff --git a/domain/model/group_m/groupInfo.go b/domain/model/group_m/groupInfo.go index a6b4551..fe9b6f0 100644 --- a/domain/model/group_m/groupInfo.go +++ b/domain/model/group_m/groupInfo.go @@ -7,6 +7,7 @@ import ( "git.hilo.cn/hilo-common/utils" "github.com/bluele/gcache" "gorm.io/gorm" + "gorm.io/gorm/clause" "hilo-group/_const/enum/group_e" "hilo-group/myerr" "hilo-group/myerr/bizerr" @@ -566,3 +567,26 @@ func GetFamilyRooms(model *domain.Model, familyId uint64, pageSize, pageIndex in return res, nextIdx, hasNext, nil } + +func IsUseTRTC(model *domain.Model, imGroupId string) bool { + var count int64 + if err := model.DB().Table("group_trtc").Where("im_group_id=?", imGroupId).Count(&count).Error; err != nil { + model.Log.Errorf("IsUseTRTC err:%v, groupId:%s", err, imGroupId) + return false + } + return count > 0 +} + +type GroupTrtc struct { + mysql.Entity + ImGroupId mysql.Str +} + +// 初始化trtc房间 +func InitTRTC(model *domain.Model, imGroupId string) error { + if err := model.DB().Model(GroupTrtc{}).Clauses(clause.OnConflict{DoNothing: true}).Create(&GroupTrtc{ImGroupId: imGroupId}).Error; err != nil { + model.Log.Errorf("InitTRTC err:%v, groupId:%s", err, imGroupId) + return err + } + return nil +} diff --git a/domain/service/group_s/group_op.go b/domain/service/group_s/group_op.go index c547f75..6618d9e 100644 --- a/domain/service/group_s/group_op.go +++ b/domain/service/group_s/group_op.go @@ -6,11 +6,13 @@ import ( "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mylogrus" + "git.hilo.cn/hilo-common/resource/config" "git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/resource/redisCli" "git.hilo.cn/hilo-common/rpc" "git.hilo.cn/hilo-common/sdk/agora" "git.hilo.cn/hilo-common/sdk/tencentyun" + "git.hilo.cn/hilo-common/sdk/trtc" "git.hilo.cn/hilo-common/utils" "gorm.io/gorm" "hilo-group/_const/enum/group_e" @@ -115,7 +117,7 @@ func (s *GroupService) LeaveGroupMember(model *domain.Model, groupId string, use } //进入房间, 返回channelId, err -func (s *GroupService) GroupIn(userId uint64, externalId string, groupUuid string, password, imei, ip string) (string, string, error) { +func (s *GroupService) GroupIn(userId uint64, externalId string, groupUuid string, password, imei, ip string, provider group_e.GroupProvider) (string, string, error) { var channelId string var token string err := s.svc.Transactional(func() error { @@ -171,7 +173,13 @@ func (s *GroupService) GroupIn(userId uint64, externalId string, groupUuid strin if err != nil { return err } - channelId, token, err = agora.CreateGroupAgora(groupInfo.ChannelId, uint32(userId)) + if provider == group_e.GroupProvider_TRTC { + channelId = groupInfo.ChannelId + token = trtc.CreateGroupTRTCUserSig(userId, config.GetTRTCConfig()) + model.Log.Infof("enter trtc group userId:%v, groupId:%v", userId, groupUuid) + } else { + channelId, token, err = agora.CreateGroupAgora(groupInfo.ChannelId, uint32(userId)) + } if err != nil { return err } else { diff --git a/local.ini b/local.ini index d46aed4..94db393 100644 --- a/local.ini +++ b/local.ini @@ -72,6 +72,9 @@ APP_CERTIFICATE=ff29c100a613433db82324e8411eabc8 CUSTOMER_KEY=6b132c0ff7164560a2bc53fda06ea85a CUSTOMER_SECRET=eedad2cd16d24834990d5450ace9f1ce CALLBACK_SECRET=n_ZizS_N8 +[TRTC] +APP_ID=1400548270 +APP_CERTIFICATE=321bd60f73096b059c7350f1cd97d51028850b34fa58c5c0d26bb4a19e783de8 [CHECKOUT] AUTHORIZATION=sk_test_9b5e771c-5a3f-4a8d-a4da-31b19bd43d83 URL=https://api.sandbox.checkout.com/hosted-payments diff --git a/mysql/trtc.sql b/mysql/trtc.sql new file mode 100644 index 0000000..fdc3fe8 --- /dev/null +++ b/mysql/trtc.sql @@ -0,0 +1,9 @@ +-CREATE TABLE `group_trtc` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', + `im_group_id` char(32) COLLATE utf8mb4_unicode_ci NOT NULL, + `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `im_group_id` (`im_group_id`), + KEY `created_time` (`created_time`) USING BTREE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='使用trtc的群组'; \ No newline at end of file diff --git a/release.ini b/release.ini index 4e7a568..610fc60 100644 --- a/release.ini +++ b/release.ini @@ -72,6 +72,9 @@ APP_ID=6291d069123642d9929a49c734c50719 APP_CERTIFICATE=d5de40350aa54e60bcdce90c71e9598a CUSTOMER_KEY=6b132c0ff7164560a2bc53fda06ea85a CUSTOMER_SECRET=eedad2cd16d24834990d5450ace9f1ce +[TRTC] +APP_ID=40000066 +APP_CERTIFICATE=3ab68ea5bddc8774d90b8c764ae71188914bd5fd06f30b28790c51e44ca7885c [CHECKOUT] AUTHORIZATION=sk_fca6e213-b7df-4bd7-99f4-7c0a9f7c778c URL=https://api.checkout.com/hosted-payments diff --git a/route/group_r/group_op.go b/route/group_r/group_op.go index bb62b80..f1d7d60 100644 --- a/route/group_r/group_op.go +++ b/route/group_r/group_op.go @@ -1751,7 +1751,33 @@ func GroupIn(c *gin.Context) (*mycontext.MyContext, error) { } model.Log.Infof("GroupIn ip userId:%v,imGroupId:%v,ip:%v,imei:%v", userId, groupId, ip, imei) - if channelId, token, err := group_s.NewGroupService(myContext).GroupIn(userId, externalId, groupId, password, imei, ip); err != nil { + provider := group_e.GroupProvider_SW + if group_m.IsUseTRTC(model, groupId) { + provider = group_e.GroupProvider_TRTC + } else { + if time.Now().Unix() > 1683820800 { // todo 1683820800=5月2号,等过审再修改 + roomUser, err := group_m.GetRoomOnlineUser(myContext, groupId) + if err == nil && roomUser.Total == 0 { + if err := group_m.InitTRTC(model, groupId); err != nil { + return myContext, err + } + provider = group_e.GroupProvider_TRTC + model.Log.Infof("auto shift trtc room groupId:%v,%+v-%v", groupId, roomUser, err) + } + } + } + if provider == group_e.GroupProvider_TRTC { + // 版本升级提示,旧版本(3.6.0以下),提示升级 + _, major, minor, _, err := req.GetAppVersion(c) + if err != nil { + return myContext, err + } + if (major < 3) || (major == 3 && minor < 6) { + return myContext, bizerr.UpgradeRequired + } + } + + if channelId, token, err := group_s.NewGroupService(myContext).GroupIn(userId, externalId, groupId, password, imei, ip, provider); err != nil { return myContext, err } else { // 加入房间缓存 @@ -1767,6 +1793,7 @@ func GroupIn(c *gin.Context) (*mycontext.MyContext, error) { Token: token, AgoraId: uint32(userId), MicNumType: gi.MicNumType, + Provider: provider, }) // v2.26及以后,客户端自己加TIM群,不再由服务器代加 diff --git a/test/group_power_test.go b/test/group_power_test.go index b0d364a..c830099 100644 --- a/test/group_power_test.go +++ b/test/group_power_test.go @@ -1,17 +1,25 @@ package test import ( + "fmt" "git.hilo.cn/hilo-common/domain" "hilo-group/domain/model/groupPower_m" + "hilo-group/domain/model/group_m" "testing" ) func TestGetAllGroupPowerOwner(t *testing.T) { - owners, err := groupPower_m.BatchGetAllGroupPowerUsers(domain.CreateModelNil()) + owners, _, err := groupPower_m.BatchGetAllGroupPowerUsers(domain.CreateModelNil(), 1, 1) println(owners, err) } func TestGetGroupPowerMonthStartTop1(t *testing.T) { res, err := groupPower_m.GetGroupPowerMonthStartTop1(domain.CreateModelNil(), 250) - println(res,err) + println(res, err) +} + +func TestIsUseTRTC(t *testing.T) { + if group_m.IsUseTRTC(domain.CreateModelNil(), "@TGS#3I57DMRIB") { + fmt.Println("true--------------") + } } -- 2.22.0