🔥 重写

This commit is contained in:
李寻欢 2022-01-11 10:14:11 +08:00
parent 50403947b8
commit 4b70b60922
11 changed files with 263 additions and 198 deletions

27
api/account.go Normal file
View File

@ -0,0 +1,27 @@
package api
import (
"gitee.ltd/lxh/txim/api/param"
"gitee.ltd/lxh/txim/common"
)
// AccountImport 导入单个账号
func AccountImport(r *param.AccountImportReq) (*param.AccountImportRes, error) {
a := param.AccountImportRes{}
err := Api(common.AccountImport, r, &a)
return &a, err
}
// AccountCheck 检查账号是否存在
func AccountCheck(r *param.AccountCheckReq) (*param.AccountCheckRes, error) {
a := param.AccountCheckRes{}
err := Api(common.AccountCheck, r, &a)
return &a, err
}
// AccountDelete 账号删除
func AccountDelete(r *param.AccountDeleteReq) (*param.AccountDeleteRes, error) {
a := param.AccountDeleteRes{}
err := Api(common.AccountDelete, r, &a)
return &a, err
}

View File

@ -3,136 +3,22 @@ package api
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
. "gitee.ltd/lxh/txim"
"gitee.ltd/lxh/txim/common"
"math/rand" "math/rand"
"github.com/imroc/req" "github.com/imroc/req"
"github.com/tencentyun/tls-sig-api-v2-golang/tencentyun"
) )
const ( func Api(p common.Api, in, out interface{}) error {
API_HOST = "console.tim.qq.com" host := fmt.Sprintf("https://%s/%s/%s/%s", ApiHost, ApiVersion, p.ServiceName, p.Command)
API_VERSION = "v4"
)
var g struct { appId, identifier := GetInfo()
sdkAppID int
identifier string
key string
}
func Init(sdkAppID int, key, identifier string) {
g.sdkAppID = sdkAppID
g.key = key
g.identifier = identifier
}
func UserSig(identifier string, expire int) string {
sig, _ := tencentyun.GenUserSig(g.sdkAppID, g.key, identifier, expire)
return sig
}
//单个账号导入
type AccountImportReq struct {
Identifier string `json:"Identifier"` //:"test",
Nick string `json:"Nick"` //:"test",
FaceUrl string `json:"FaceUrl"` //:"http://www.qq.com"
}
type AccountImportAns struct{}
func AccountImport(r *AccountImportReq) (*AccountImportAns, error) {
a := AccountImportAns{}
err := Api("im_open_login_svc", "account_import", r, &a)
return &a, err
}
//账号查询
type AccountCheckReq struct {
CheckItem []*AccountCheckReqItem `json:"CheckItem"`
}
type AccountCheckReqItem struct {
UserID string `json:"UserID"`
}
type AccountCheckAns struct {
ResultItem []*AccountCheckAnsItem `json:"ResultItem"`
}
type AccountCheckAnsItem struct {
UserID string `json:"UserID"`
ResultCode int `json:"ResultCode"`
ResultInfo string `json:"ResultInfo"`
AccountStatus string `json:"AccountStatus"`
}
func AccountCheck(r *AccountCheckReq) (*AccountCheckAns, error) {
a := AccountCheckAns{}
err := Api("im_open_login_svc", "account_check", r, &a)
return &a, err
}
//账号删除
type AccountDeleteReq struct {
DeleteItem []*AccountDeleteReqItem `json:"DeleteItem"`
}
type AccountDeleteReqItem struct {
UserID string `json:"UserID"`
}
type AccountDeleteAns struct {
ResultItem []*AccountDeleteAnsItem `json:"ResultItem"`
}
type AccountDeleteAnsItem struct {
UserID string `json:"UserID"`
ResultCode int `json:"ResultCode"`
ResultInfo string `json:"ResultInfo"`
}
func AccountDelete(r *AccountDeleteReq) (*AccountDeleteAns, error) {
a := AccountDeleteAns{}
err := Api("im_open_login_svc", "account_delete", r, &a)
return &a, err
}
//建群
//https://cloud.tencent.com/document/product/269/1615
type GroupCreateReq struct {
OwnerAccount string `json:"Owner_Account"`
Type string `json:"Type"`
Name string `json:"Name"`
ApplyJoinOption string `json:"ApplyJoinOption"`
}
type GroupCreateAns struct {
GroupId string `json:"GroupId"`
}
func GroupCreate(r *GroupCreateReq) (*GroupCreateAns, error) {
a := GroupCreateAns{}
err := Api("group_open_http_svc", "create_group", r, &a)
return &a, err
}
//资料设置
type ProfileSetReq struct {
FromAccount string `json:"From_Account"`
ProfileItem []*ProfileSetReqItem `json:"ProfileItem"`
}
type ProfileSetReqItem struct {
Tag string `json:"Tag"`
Value interface{} `json:"Value"`
}
type ProfileSetAns struct{}
func ProfileSet(r *ProfileSetReq) (*ProfileSetAns, error) {
a := ProfileSetAns{}
err := Api("profile", "portrait_set", r, &a)
return &a, err
}
func Api(servicename, command string, in, out interface{}) error {
host := fmt.Sprintf("https://%s/%s/%s/%s", API_HOST, API_VERSION, servicename, command)
query := req.QueryParam{ query := req.QueryParam{
"sdkappid": g.sdkAppID, "sdkappid": appId,
"identifier": g.identifier, "identifier": identifier,
"usersig": UserSig(g.identifier, 5*60), "usersig": UserSig(identifier, 5*60),
"random": rand.Uint32(), "random": rand.Uint32(),
"contenttype": "json", "contenttype": "json",
} }

14
api/group.go Normal file
View File

@ -0,0 +1,14 @@
package api
import (
"gitee.ltd/lxh/txim/api/param"
"gitee.ltd/lxh/txim/common"
)
// GroupCreate 建群
//https://cloud.tencent.com/document/product/269/1615
func GroupCreate(r *param.GroupCreateReq) (*param.GroupCreateRes, error) {
a := param.GroupCreateRes{}
err := Api(common.GroupCreate, r, &a)
return &a, err
}

56
api/param/request.go Normal file
View File

@ -0,0 +1,56 @@
package param
// AccountImportReq 单个账号导入
type AccountImportReq struct {
Identifier string `json:"Identifier"` // 用户名长度不超过32字节
Nick string `json:"Nick"` // 用户昵称
FaceUrl string `json:"FaceUrl"` // 用户头像 URL
}
// ========================================================
// AccountCheckReq 账号查询
type AccountCheckReq struct {
CheckItem []*AccountCheckReqItem `json:"CheckItem"`
}
// AccountCheckReqItem 账号查询条件
type AccountCheckReqItem struct {
UserID string `json:"UserID"`
}
// ========================================================
// AccountDeleteReq 账号删除
type AccountDeleteReq struct {
DeleteItem []*AccountDeleteReqItem `json:"DeleteItem"`
}
// AccountDeleteReqItem 账号删除条件
type AccountDeleteReqItem struct {
UserID string `json:"UserID"`
}
// ========================================================
// GroupCreateReq 建群
type GroupCreateReq struct {
OwnerAccount string `json:"Owner_Account"`
Type string `json:"Type"`
Name string `json:"Name"`
ApplyJoinOption string `json:"ApplyJoinOption"`
}
// ========================================================
// ProfileSetReq 资料设置
type ProfileSetReq struct {
FromAccount string `json:"From_Account"`
ProfileItem []*ProfileSetReqItem `json:"ProfileItem"`
}
// ProfileSetReqItem 资料设置条件
type ProfileSetReqItem struct {
Tag string `json:"Tag"`
Value interface{} `json:"Value"`
}

45
api/param/response.go Normal file
View File

@ -0,0 +1,45 @@
package param
// AccountImportRes 导入单个账号返回
type AccountImportRes struct{}
// ===============================================================
// AccountCheckRes 查询账号列表返回
type AccountCheckRes struct {
ResultItem []*AccountCheckResItem `json:"ResultItem"`
}
// AccountCheckResItem 账号查询结果
type AccountCheckResItem struct {
UserID string `json:"UserID"`
ResultCode int `json:"ResultCode"`
ResultInfo string `json:"ResultInfo"`
AccountStatus string `json:"AccountStatus"`
}
// ===============================================================
// AccountDeleteRes 账号删除返回
type AccountDeleteRes struct {
ResultItem []*AccountDeleteResItem `json:"ResultItem"`
}
// AccountDeleteResItem 账号删除结果
type AccountDeleteResItem struct {
UserID string `json:"UserID"`
ResultCode int `json:"ResultCode"`
ResultInfo string `json:"ResultInfo"`
}
// ===============================================================
// GroupCreateRes 建群返回
type GroupCreateRes struct {
GroupId string `json:"GroupId"`
}
// ===============================================================
// ProfileSetRes 资料设置返回
type ProfileSetRes struct{}

13
api/profile.go Normal file
View File

@ -0,0 +1,13 @@
package api
import (
"gitee.ltd/lxh/txim/api/param"
"gitee.ltd/lxh/txim/common"
)
// ProfileSet 资料设置
func ProfileSet(r *param.ProfileSetReq) (*param.ProfileSetRes, error) {
a := param.ProfileSetRes{}
err := Api(common.ProfileSet, r, &a)
return &a, err
}

14
callback/param.go Normal file
View File

@ -0,0 +1,14 @@
package callback
import "gitee.ltd/lxh/txim/common"
// TencentCallbackQuery 腾讯云回调Query参数
type TencentCallbackQuery struct {
CallbackCommand common.TxIMCallbackCommand `json:"CallbackCommand" form:"CallbackCommand"` // 回调命令字
ClientIP string `json:"ClientIP" form:"ClientIP"` // 客户端IP
OptPlatform string `json:"OptPlatform" form:"OptPlatform"` // 客户端平台
RequestId string `json:"RequestId" form:"RequestId"` // 请求ID
RetryId int `json:"RetryId" form:"RetryId"` // 重试次数
SdkAppid string `json:"SdkAppid" form:"SdkAppid"` // 应用ID
ContentType string `json:"contenttype" form:"contenttype"` // 内容类型
}

14
common/api.go Normal file
View File

@ -0,0 +1,14 @@
package common
type Api struct {
ServiceName string // 服务名称
Command string // 接口名称
}
var (
AccountImport Api = Api{"im_open_login_svc", "account_import"}
AccountCheck Api = Api{"im_open_login_svc", "account_check"}
AccountDelete Api = Api{"im_open_login_svc", "account_delete"}
GroupCreate Api = Api{"group_open_http_svc", "create_group"}
ProfileSet Api = Api{"profile", "portrait_set"}
)

40
common/callback.go Normal file
View File

@ -0,0 +1,40 @@
package common
// TxIMCallbackCommand 回调消息类型
type TxIMCallbackCommand string
const (
// 在线状态
SnsStateChange TxIMCallbackCommand = "Sns.StateChange" // 状态变更
// 资料关系链
SnsCallbackPrevFriendAdd TxIMCallbackCommand = "Sns.CallbackPrevFriendAdd" // 添加好友之前回调
SnsCallbackPrevFriendResponse TxIMCallbackCommand = "Sns.CallbackPrevFriendResponse" // 添加好友回应之前回调
SnsCallbackFriendAdd TxIMCallbackCommand = "Sns.CallbackFriendAdd" // 添加好友之后回调
SnsCallbackFriendDelete TxIMCallbackCommand = "Sns.CallbackFriendDelete" // 删除好友之后回调
SnsCallbackBlackListAdd TxIMCallbackCommand = "Sns.CallbackBlackListAdd" // 添加黑名单之后回调
SnsCallbackBlackListDelete TxIMCallbackCommand = "Sns.CallbackBlackListDelete" // 删除黑名单之后回调
// 资料关系链
C2CCallbackBeforeSendMsg TxIMCallbackCommand = "C2C.CallbackBeforeSendMsg" // 发单聊消息之前回调
C2CCallbackAfterSendMsg TxIMCallbackCommand = "C2C.CallbackAfterSendMsg" // 发单聊消息之后回调
C2CCallbackAfterMsgReport TxIMCallbackCommand = "C2C.CallbackAfterMsgReport" // 单聊消息已读上报后回调
C2CCallbackAfterMsgWithDraw TxIMCallbackCommand = "C2C.CallbackAfterMsgWithDraw" // 单聊消息撤回后回调
// 群组系统
GroupCallbackBeforeCreateGroup TxIMCallbackCommand = "Group.CallbackBeforeCreateGroup" // 创建群组之前回调
GroupCallbackAfterCreateGroup TxIMCallbackCommand = "Group.CallbackAfterCreateGroup" // 创建群组之后回调
GroupCallbackBeforeApplyJoinGroup TxIMCallbackCommand = "Group.CallbackBeforeApplyJoinGroup" // 申请入群之前回调
GroupCallbackBeforeInviteJoinGroup TxIMCallbackCommand = "Group.CallbackBeforeInviteJoinGroup" // 拉人入群之前回调
GroupCallbackAfterNewMemberJoin TxIMCallbackCommand = "Group.CallbackAfterNewMemberJoin" // 新成员入群之后回调
GroupCallbackAfterMemberExit TxIMCallbackCommand = "Group.CallbackAfterMemberExit" // 群成员离开之后回调
GroupCallbackBeforeSendMsg TxIMCallbackCommand = "Group.CallbackBeforeSendMsg" // 群内发言之前回调
GroupCallbackAfterSendMsg TxIMCallbackCommand = "Group.CallbackAfterSendMsg" // 群内发言之后回调
GroupCallbackAfterGroupFull TxIMCallbackCommand = "Group.CallbackAfterGroupFull" // 群组满员之后回调
GroupCallbackAfterGroupDestroyed TxIMCallbackCommand = "Group.CallbackAfterGroupDestroyed" // 群组解散之后回调
GroupCallbackAfterGroupInfoChanged TxIMCallbackCommand = "Group.CallbackAfterGroupInfoChanged" // 群组资料修改之后回调
)

76
demo.go
View File

@ -1,76 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
txim "gitee.ltd/lxh/txim/api"
)
func main() {
sdkAppId, _ := strconv.Atoi(os.Getenv("tximid"))
sdkKey := os.Getenv("tximkey")
txim.Init(sdkAppId, sdkKey, "administrator")
// 生成签名
// sig := txim.UserSig("administrator", 300)
// fmt.Println("签名:", sig)
var (
data interface{}
dataBytes []byte
err error
)
//创建房间
// data, err = txim.GroupCreate(&txim.GroupCreateReq{
// OwnerAccount: "administrator",
// Type: "ChatRoom",
// Name: "测试聊天室",
// ApplyJoinOption: "FreeAccess",
// })
// dataBytes, _ = json.Marshal(data)
// fmt.Printf("%s,%v\n", string(dataBytes), err)
//创建用户
// data, err = txim.AccountImport(&txim.AccountImportReq{
// Identifier: "test",
// Nick: "test1",
// })
// dataBytes, _ = json.Marshal(data)
// fmt.Printf("%s,%v\n", string(dataBytes), err)
//查询用户
data, err = txim.AccountCheck(&txim.AccountCheckReq{
CheckItem: []*txim.AccountCheckReqItem{
{UserID: "test"},
},
})
dataBytes, _ = json.Marshal(data)
fmt.Printf("%s,%v\n", string(dataBytes), err)
//删除用户
// data, err = txim.AccountDelete(&txim.AccountDeleteReq{
// DeleteItem: []*txim.AccountDeleteReqItem{
// {UserID: "test"},
// },
// })
// dataBytes, _ = json.Marshal(data)
// fmt.Printf("%s,%v\n", string(dataBytes), err)
//设置用户资料
// data, err = txim.ProfileSet(&txim.ProfileSetReq{
// FromAccount: "test1",
// ProfileItem: []*txim.ProfileSetReqItem{
// {
// Tag: "Tag_Profile_IM_Nick",
// Value: "test1",
// },
// },
// })
// dataStr, _ := json.Marshal(data)
// fmt.Printf("%+v,%v\n", dataStr, err)
}

32
txim.go Normal file
View File

@ -0,0 +1,32 @@
package txim
import "github.com/tencentyun/tls-sig-api-v2-golang/tencentyun"
const (
ApiHost = "console.tim.qq.com" // API服务器地址
ApiVersion = "v4" // API版本号
)
var g struct {
sdkAppID int
identifier string
key string
}
// GetInfo 获取SDK AppID等信息
func GetInfo() (int, string) {
return g.sdkAppID, g.identifier
}
// Init 初始化SDK
func Init(sdkAppID int, key, identifier string) {
g.sdkAppID = sdkAppID
g.key = key
g.identifier = identifier
}
// UserSig 生成签名
func UserSig(identifier string, expire int) string {
sig, _ := tencentyun.GenUserSig(g.sdkAppID, g.key, identifier, expire)
return sig
}