Commit 316b492c authored by hujiebin's avatar hujiebin

feat:tengxunyun

parent 89c2baf1
...@@ -32,6 +32,9 @@ require ( ...@@ -32,6 +32,9 @@ require (
github.com/sirupsen/logrus v1.7.0 // indirect github.com/sirupsen/logrus v1.7.0 // indirect
github.com/spf13/cast v1.5.0 // indirect github.com/spf13/cast v1.5.0 // indirect
github.com/tealeg/xlsx v1.0.5 // indirect github.com/tealeg/xlsx v1.0.5 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.479 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ims v1.0.479 // indirect
github.com/tencentyun/tls-sig-api-v2-golang v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect gopkg.in/ini.v1 v1.63.2 // indirect
gorm.io/driver/mysql v1.4.3 // indirect gorm.io/driver/mysql v1.4.3 // indirect
......
...@@ -108,6 +108,16 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P ...@@ -108,6 +108,16 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.479 h1:3kwDb6p1J3LxmwnNgSSEheemPffo+vMewoDzKysYdig=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.479/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ims v1.0.479 h1:xDmo1rBmSJ7Hw3iOa6DQ/++z1d7pgsEVKrZno35zR7w=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ims v1.0.479/go.mod h1:3YlJ1g/Ko/iFgyBJdok+dRPTH6zRB6BBopAcFkbujPc=
github.com/tencentyun/tls-sig-api-v2-golang v1.0.0 h1:NavMw9XO2iCLv8hTKaJW2kTaGR2SdNljMABbe39yu6Q=
github.com/tencentyun/tls-sig-api-v2-golang v1.0.0/go.mod h1:u7WiArmCTXTaQAHJwAOaLgpJ5e2xdY5/cgMEy3ubL60=
github.com/tencentyun/tls-sig-api-v2-golang v1.1.0 h1:dWbCuoTaRWvcS/RWW/8RIAvLRA4jyDNdoAuBGC5Veqk=
github.com/tencentyun/tls-sig-api-v2-golang v1.1.0/go.mod h1:D/i1nQrP1hSXxxz+SAmjaprAwH3EhYaktJG6BObWnFE=
github.com/tencentyun/tls-sig-api-v2-golang v1.3.0 h1:mW4onVqOP6MqkJtTay74Tch6NvjwQRXeCuhUC/p6YYk=
github.com/tencentyun/tls-sig-api-v2-golang v1.3.0/go.mod h1:0L1MSijyEq6a74xZ02h0qKfN8y6RcvDiNabIZWXereE=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY= golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
......
package rpc
import (
"encoding/json"
"fmt"
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/consul"
"git.hilo.cn/hilo-common/resource/mysql"
"github.com/hashicorp/consul/api"
"math/rand"
"net"
"net/http"
)
const (
defaultFinanceConsulName = "hiloFinance"
defaultFinanceServerScheme = "http"
defaultFinanceServerAddr = "127.0.0.1:9030" // 默认内网转发,本地回环
)
var financeServerHost = []string{defaultFinanceServerAddr}
var financeTransport *http.Transport
func init() {
financeTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConnsPerHost: defaultMaxIdleConnsPerHost,
MaxIdleConns: defaultMaxIdleConns,
DialContext: (&net.Dialer{
Timeout: defaultDialTimeout,
KeepAlive: defaultKeepAliveTimeout,
}).DialContext,
IdleConnTimeout: defaultIdleConnTimeout,
ExpectContinueTimeout: defaultExpectContinueTimeout,
DisableKeepAlives: false,
}
go func() {
address := api.DefaultConfig().Address // 用consul api的default config
if err := consul.RegisterWatcher("services", nil, address, func(serviceStatus map[string]map[string][]string) {
if statusAddrs, ok := serviceStatus[defaultFinanceConsulName]; ok {
healthAddrs, _ := statusAddrs[api.HealthPassing]
l := len(healthAddrs)
if l > 0 {
mylogrus.MyLog.Infof("consul service update state:%v-%v", defaultFinanceConsulName, healthAddrs)
financeServerHost = healthAddrs
} else {
mylogrus.MyLog.Warnf("consul service update local state:%v-%v", defaultFinanceConsulName, defaultFinanceServerAddr)
financeServerHost = []string{defaultFinanceServerAddr} // 有其他问题都用默认的
}
for status := range statusAddrs {
if status == api.HealthPassing {
continue
}
mylogrus.MyLog.Warnf("consul service wrong state:%v-%v-%v", defaultFinanceConsulName, status, statusAddrs[status])
}
}
}); err != nil {
mylogrus.MyLog.Errorf("启动 consul 的watch监控失败")
}
}()
}
type CvSvip struct {
SvipLevel int `json:"svipLevel"`
Privileges []CVSvipPrivilege `json:"privileges"` // 特权
}
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"` // 用户开关
}
// 获取svip等级
func GetUserSvip(model *domain.Model, userId mysql.ID) (CvSvip, error) {
svip, err := MGetUserSvip(model, []mysql.ID{userId})
if err != nil {
return CvSvip{}, nil
}
return svip[userId], nil
}
// 批量获取svip等级
// map userId->level
func MGetUserSvipLevel(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]int, error) {
res := make(map[mysql.ID]int, len(userIds))
if len(userIds) <= 0 {
return res, nil
}
level, err := MGetUserSvip(model, userIds)
if err != nil {
return res, nil
}
for userId, svip := range level {
res[userId] = svip.SvipLevel
}
return res, nil
}
// 批量获取svip
func MGetUserSvip(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]CvSvip, error) {
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data map[mysql.ID]CvSvip
}
var res = make(map[mysql.ID]CvSvip)
if len(userIds) <= 0 {
return res, nil
}
var userIdsStr []string
for _, userId := range userIds {
userIdsStr = append(userIdsStr, fmt.Sprintf("%d", userId))
}
_url := fmt.Sprintf("%v://%v/inner/svip/users", defaultFinanceServerScheme, getFinanceHost())
resp, err := HttpGet(model, _url, nil, map[string][]string{
"ids": userIdsStr,
})
if err != nil {
model.Log.Errorf("MGetUserSvip fail:%v", err)
return res, err
}
response := new(Response)
if err = json.Unmarshal(resp, response); err != nil {
model.Log.Errorf("MGetUserSvip json fail:%v", err)
return res, err
}
for userId, svip := range response.Data {
res[userId] = CvSvip{
SvipLevel: svip.SvipLevel,
Privileges: response.Data[userId].Privileges,
}
}
return res, nil
}
func getFinanceHost() string {
l := len(financeServerHost)
r := rand.Intn(l) // 随机一个
mylogrus.MyLog.Infof("getHostFinance:%v---%v", r, financeServerHost[r])
return financeServerHost[r]
}
package tencentyun
var IM_INVALID_USER = "@TLS#NOT_FOUND"
type OnlineStatusType = uint
const (
// 在线状态
IM_STATUS_OFF_LINE OnlineStatusType = 0
IM_STATUS_PUSH_ON_LINE OnlineStatusType = 1
IM_STATUS_ON_LINE OnlineStatusType = 2
)
// TIM相关常量
const (
TAG_PROFILE_IM_LEVEL = "Tag_Profile_IM_Level"
TAG_PROFILE_IM_NICK = "Tag_Profile_IM_Nick"
TAG_PROFILE_IM_IMAGE = "Tag_Profile_IM_Image"
TAG_PROFILE_IM_HILO = "Tag_Profile_Custom_Hilo"
)
package tencentyun
import (
"encoding/json"
"git.hilo.cn/hilo-common/domain"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
v20201229 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ims/v20201229"
"golang.org/x/net/context"
"time"
)
const (
ImageModerationSecretId = "IKIDqbCXaNt97oEOHtDG3ZomUu3vXsvhDTR8"
ImageModerationSecretKey = "RdYCYeT0GFyPZS5QUHo43lCNHuFK8fut"
)
func ModerateImage(model *domain.Model, userId uint64, bizType, dataId, fileUrl string) (string, error) {
beginTime := time.Now()
credential := common.NewCredential(ImageModerationSecretId, ImageModerationSecretKey)
client, _ := v20201229.NewClient(credential, regions.Frankfurt, profile.NewClientProfile())
request := v20201229.NewImageModerationRequest()
request.BizType = &bizType
request.DataId = &dataId
request.FileUrl = &fileUrl
response, err := client.ImageModerationWithContext(context.Background(), request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
model.Log.Warnf("ModerateImage: An API error has returned: %s", err)
return "", err
}
if err != nil {
model.Log.Warnf("ModerateImage: An API error has returned: %s", err)
return "", err
}
endTime := time.Now()
model.Log.Infof("ModerateImage takes %d ms result: %s", endTime.Sub(beginTime).Milliseconds(), response.ToJsonString())
if response.Response != nil && response.Response.Label != nil && response.Response.Suggestion != nil {
rsp := *response.Response.Label
buf, err := json.Marshal(*response.Response)
if err == nil {
rsp = string(buf)
}
mr := TxModerateRecord{
UserId: userId,
DataId: dataId,
FileUrl: fileUrl,
Suggestion: *response.Response.Suggestion,
Response: rsp}
if err = model.Db.Create(&mr).Error; err != nil {
model.Log.Infof("Save TxModerateRecord err: %v", err)
}
}
return *response.Response.Suggestion, nil
}
package tencentyun
import "git.hilo.cn/hilo-common/resource/mysql"
type TxModerateRecord struct {
mysql.Entity
UserId uint64
DataId string
FileUrl string
Suggestion string
Response string
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment