@ -15,8 +15,11 @@ import (
"github.com/xormplus/xorm"
"strconv"
"strings"
"time"
)
var db = DbUtil . Engine
//人员信息结构
type Person struct {
Success bool ` json:"success" `
@ -447,3 +450,159 @@ func AddGroupMember(groupId string, personList string, personIdCookie string, id
return nil
}
/ * *
功 能 : 向 融 云 同 步 人 员
作 者 : 吴 缤
日 期 : 2021 - 0 9 - 13
* /
func SyncRongYunUser ( ) {
ts , inc := getRongYunSync ( "person" )
sql := "select person_id,person_name,last_update_time from t_base_person where b_use=1 and identity_id=5 and ((last_update_time=? and person_id>?) or last_update_time>?) order by last_update_time,person_id limit 200"
res , _ := db . QueryString ( sql , ts , inc , ts )
count := len ( res ) - 1
for i , v := range res {
personId := v [ "person_id" ]
personName := v [ "person_name" ]
updateTs := v [ "last_update_time" ]
RongYunGetToken ( personId , "5" , personName , "" )
if i == count {
updateRongYunSync ( "person" , updateTs , personId )
}
}
// 每1分钟执行一次
time . AfterFunc ( 1 * time . Minute , SyncRongYunUser )
}
/ * *
功 能 : 向 融 云 同 步 群 组 和 人 员 关 系
作 者 : 吴 缤
日 期 : 2021 - 0 9 - 13
* /
func SyncRongYunGroup ( ) {
ts , inc := getRongYunSync ( "group" )
sql := "select t1.id,t1.group_id,t2.group_name,t1.person_id,t1.identity_id,t1.last_update_time from t_base_group_member_new t1 inner join t_base_group_new t2 on t1.group_id=t2.id where t1.identity_id=5 and t1.state_id=1 and t1.b_use=1 and ((t1.last_update_time=? and t1.id>?) or t1.last_update_time>?) order by t1.last_update_time,t1.id limit 200"
res , _ := db . QueryString ( sql , ts , inc , ts )
count := len ( res ) - 1
for i , v := range res {
personId := v [ "person_id" ]
identityId := v [ "identity_id" ]
groupId := v [ "group_id" ]
groupName := v [ "group_name" ]
updateTs := v [ "last_update_time" ]
id := v [ "id" ]
RongYunJoinGroup ( personId , identityId , groupId , groupName )
if i == count {
updateRongYunSync ( "group" , updateTs , id )
}
}
// 每1分钟执行一次
time . AfterFunc ( 1 * time . Minute , SyncRongYunGroup )
}
/ * *
功 能 : 融 云 加 入 群 组
作 者 : 吴 缤
日 期 : 2021 - 0 9 - 13
* /
func RongYunJoinGroup ( personId , identityId , groupId , groupName string ) error {
//获取云平台配置文件中的融云AK和SK
configMap , configErr := CommonUtil . GetRongYunConfig ( )
if configErr != nil {
return configErr
}
ak := configMap [ "app_key" ]
sk := configMap [ "app_secret" ]
suffix := configMap [ "rong_server_name" ]
rc := sdk . NewRongCloud ( ak , sk )
group := [ ] sdk . Group { sdk . Group { ID : groupId + suffix , Name : groupName } }
err := rc . GroupSync (
personId + "_" + identityId + suffix ,
group ,
)
return err
}
/ * *
功 能 : 根 据 分 类 获 取 时 间 戳 和 自 增 ID
作 者 : 吴 缤
日 期 : 2021 - 0 9 - 13
* /
func getRongYunSync ( classify string ) ( string , string ) {
record := make ( xorm . Record )
sql := "select ts,inc from t_dsmin_rongyun_sync where classify=?"
db . SQL ( sql , classify ) . Get ( & record )
ts := record [ "ts" ] . String ( )
inc := record [ "inc" ] . String ( )
return ts , inc
}
/ * *
功 能 : 更 新 时 间 戳 和 自 增 ID
作 者 : 吴 缤
日 期 : 2021 - 0 9 - 13
* /
func updateRongYunSync ( classify , ts , inc string ) {
sql := "update t_dsmin_rongyun_sync set ts=?,inc=? where classify=?"
db . Exec ( sql , ts , inc , classify )
}
/ * *
功 能 : 发 送 系 统 消 息
作 者 : 吴 缤
日 期 : 2021 - 0 9 - 17
* /
func SendSystemMsg ( fromId , toId , content string ) error {
req := httplib . Post ( fmt . Sprintf ( "%s/message/system/publish.json" , ConfigUtil . RongYunApiUri ) )
addHeader ( req )
req . Param ( "fromUserId" , fromId )
req . Param ( "toUserId" , toId )
req . Param ( "objectName" , "RC:TxtMsg" )
req . Param ( "content" , content )
resStr , err := req . String ( )
if err != nil {
return err
}
resMap := make ( map [ string ] interface { } )
errJson := json . Unmarshal ( [ ] byte ( resStr ) , & resMap )
if errJson != nil {
return errJson
}
if resMap [ "code" ] . ( float64 ) != 200 {
return errors . New ( resMap [ "errorMessage" ] . ( string ) )
}
return nil
}
/ * *
功 能 : 调 用 融 云 API 时 增 加 头 信 息
作 者 : 吴 缤
日 期 : 2021 - 0 9 - 17
* /
func addHeader ( req * httplib . BeegoHTTPRequest ) {
configMap , configErr := CommonUtil . GetRongYunConfig ( )
if configErr != nil {
fmt . Println ( "获取融云配置文件失败!" )
return
}
ak := configMap [ "app_key" ]
sk := configMap [ "app_secret" ]
nonce := CommonUtil . GetSixRandom ( )
timestamp := CommonUtil . GetTimeStamp ( )
req . Header ( "RC-App-Key" , ak )
req . Header ( "RC-Nonce" , nonce )
req . Header ( "RC-Timestamp" , timestamp )
req . Header ( "RC-Signature" , CommonUtil . SHA1 ( sk + nonce + timestamp ) )
}