✨ 初始化项目
This commit is contained in:
305
account/api.go
Normal file
305
account/api.go
Normal file
@@ -0,0 +1,305 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/5/27 20:07
|
||||
* @Desc: 账号管理
|
||||
*/
|
||||
|
||||
package account
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.echol.cn/loser/tencent-im/internal/core"
|
||||
"git.echol.cn/loser/tencent-im/internal/enum"
|
||||
"git.echol.cn/loser/tencent-im/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
serviceAccount = "im_open_login_svc"
|
||||
serviceOpenIM = "openim"
|
||||
commandImportAccount = "account_import"
|
||||
commandImportAccounts = "multiaccount_import"
|
||||
commandDeleteAccounts = "account_delete"
|
||||
commandCheckAccounts = "account_check"
|
||||
commandKickAccount = "kick"
|
||||
commandQueryAccountsOnlineStatus = "query_online_status"
|
||||
|
||||
batchImportAccountsLimit = 100 // 导入账号限制
|
||||
batchDeleteAccountsLimit = 100 // 删除账号限制
|
||||
batchCheckAccountsLimit = 100 // 查询账号限制
|
||||
)
|
||||
|
||||
type API interface {
|
||||
// ImportAccount 导入单个帐号
|
||||
// 本接口用于将 App 自有帐号导入即时通信 IM 帐号系统,
|
||||
// 为该帐号创建一个对应的内部 ID,使该帐号能够使用即时通信 IM 服务。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1608
|
||||
ImportAccount(account *Account) (err error)
|
||||
|
||||
// ImportAccounts 导入多个帐号
|
||||
// 本接口用于批量将 App 自有帐号导入即时通信 IM 帐号系统,
|
||||
// 为该帐号创建一个对应的内部 ID,使该帐号能够使用即时通信 IM 服务。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/4919
|
||||
ImportAccounts(userIds ...string) (failUserIds []string, err error)
|
||||
|
||||
// DeleteAccount 删除账号
|
||||
// 本方法拓展于“删除多个帐号(DeleteAccounts)”方法。
|
||||
// 仅支持删除套餐包类型为 IM 体验版的帐号,其他类型的账号(如:TRTC、白板、专业版、旗舰版)无法删除。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/36443
|
||||
DeleteAccount(userId string) (err error)
|
||||
|
||||
// DeleteAccounts 删除多个帐号
|
||||
// 仅支持删除套餐包类型为 IM 体验版的帐号,其他类型的账号(如:TRTC、白板、专业版、旗舰版)无法删除。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/36443
|
||||
DeleteAccounts(userIds ...string) (results []*DeleteResult, err error)
|
||||
|
||||
// CheckAccount 查询帐号导入状态
|
||||
// 本方法拓展于“查询多个帐号导入状态(CheckAccounts)”方法。
|
||||
// 用于查询自有帐号是否已导入即时通信 IM。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/38417
|
||||
CheckAccount(userId string) (bool, error)
|
||||
|
||||
// CheckAccounts 查询多个帐号导入状态
|
||||
// 用于查询自有帐号是否已导入即时通信 IM,支持批量查询。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/38417
|
||||
CheckAccounts(userIds ...string) (results []*CheckResult, err error)
|
||||
|
||||
// KickAccount 使帐号登录状态失效
|
||||
// 本接口适用于将 App 用户帐号的登录状态(例如 UserSig)失效。
|
||||
// 例如,开发者判断一个用户为恶意帐号后,可以调用本接口将该用户当前的登录状态失效,这样用户使用历史 UserSig 登录即时通信 IM 会失败。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3853
|
||||
KickAccount(userId string) (err error)
|
||||
|
||||
// GetAccountOnlineState 查询帐号在线状态
|
||||
// 本方法拓展于“查询多个帐号在线状态(GetAccountsOnlineState)”方法。
|
||||
// 获取用户当前的登录状态。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2566
|
||||
GetAccountOnlineState(userId string, isNeedDetail ...bool) (*OnlineStatusResult, error)
|
||||
|
||||
// GetAccountsOnlineState 查询多个帐号在线状态
|
||||
// 获取用户当前的登录状态。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2566
|
||||
GetAccountsOnlineState(userIds []string, isNeedDetail ...bool) (ret *OnlineStatusRet, err error)
|
||||
}
|
||||
|
||||
type api struct {
|
||||
client core.Client
|
||||
}
|
||||
|
||||
func NewAPI(client core.Client) API {
|
||||
return &api{client: client}
|
||||
}
|
||||
|
||||
// ImportAccount 导入单个帐号
|
||||
// 本接口用于将 App 自有帐号导入即时通信 IM 帐号系统,
|
||||
// 为该帐号创建一个对应的内部 ID,使该帐号能够使用即时通信 IM 服务。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1608
|
||||
func (a *api) ImportAccount(account *Account) (err error) {
|
||||
if err = a.client.Post(serviceAccount, commandImportAccount, account, &types.ActionBaseResp{}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ImportAccounts 导入多个帐号
|
||||
// 本接口用于批量将 App 自有帐号导入即时通信 IM 帐号系统,
|
||||
// 为该帐号创建一个对应的内部 ID,使该帐号能够使用即时通信 IM 服务。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/4919
|
||||
func (a *api) ImportAccounts(userIds ...string) (failUserIds []string, err error) {
|
||||
if c := len(userIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the userid is not set")
|
||||
return
|
||||
} else if c > batchImportAccountsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of imported accounts cannot exceed %d", batchImportAccountsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &importAccountsReq{UserIds: userIds}
|
||||
resp := &importAccountsResp{}
|
||||
|
||||
if err = a.client.Post(serviceAccount, commandImportAccounts, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
failUserIds = resp.FailUserIds
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteAccount 删除账号
|
||||
// 本方法拓展于“删除多个帐号(DeleteAccounts)”方法。
|
||||
// 仅支持删除套餐包类型为 IM 体验版的帐号,其他类型的账号(如:TRTC、白板、专业版、旗舰版)无法删除。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/36443
|
||||
func (a *api) DeleteAccount(userId string) (err error) {
|
||||
results, err := a.DeleteAccounts(userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
if result.UserId == userId && result.ResultCode != enum.SuccessCode {
|
||||
return core.NewError(result.ResultCode, result.ResultInfo)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteAccounts 删除多个帐号
|
||||
// 仅支持删除套餐包类型为 IM 体验版的帐号,其他类型的账号(如:TRTC、白板、专业版、旗舰版)无法删除。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/36443
|
||||
func (a *api) DeleteAccounts(userIds ...string) (results []*DeleteResult, err error) {
|
||||
if c := len(userIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the userid is not set")
|
||||
return
|
||||
} else if c > batchDeleteAccountsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of deleted accounts cannot exceed %d", batchDeleteAccountsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &deleteAccountsReq{}
|
||||
resp := &deleteAccountsResp{}
|
||||
|
||||
for _, userId := range userIds {
|
||||
req.Deletes = append(req.Deletes, &accountItem{userId})
|
||||
}
|
||||
|
||||
if err = a.client.Post(serviceAccount, commandDeleteAccounts, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CheckAccount 查询帐号导入状态.
|
||||
// 本方法拓展于“查询多个帐号导入状态(CheckAccounts)”方法。
|
||||
// 用于查询自有帐号是否已导入即时通信 IM。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/38417
|
||||
func (a *api) CheckAccount(userId string) (bool, error) {
|
||||
results, err := a.CheckAccounts(userId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
if result.UserId == userId {
|
||||
if result.ResultCode != enum.SuccessCode {
|
||||
return false, core.NewError(result.ResultCode, result.ResultInfo)
|
||||
} else {
|
||||
return result.Status == ImportedStatusYes, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// CheckAccounts 查询多个帐号导入状态.
|
||||
// 用于查询自有帐号是否已导入即时通信 IM,支持批量查询。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/38417
|
||||
func (a *api) CheckAccounts(userIds ...string) (results []*CheckResult, err error) {
|
||||
if c := len(userIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the account is not set")
|
||||
return
|
||||
} else if c > batchCheckAccountsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of checked accounts cannot exceed %d", batchCheckAccountsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &checkAccountsReq{}
|
||||
resp := &checkAccountsResp{}
|
||||
|
||||
for _, userId := range userIds {
|
||||
req.Checks = append(req.Checks, &accountItem{userId})
|
||||
}
|
||||
|
||||
if err = a.client.Post(serviceAccount, commandCheckAccounts, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// KickAccount 失效帐号登录状态
|
||||
// 本接口适用于将 App 用户帐号的登录状态(例如 UserSig)失效。
|
||||
// 例如,开发者判断一个用户为恶意帐号后,可以调用本接口将该用户当前的登录状态失效,这样用户使用历史 UserSig 登录即时通信 IM 会失败。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3853
|
||||
func (a *api) KickAccount(userId string) (err error) {
|
||||
if err = a.client.Post(serviceAccount, commandKickAccount, &kickAccountReq{userId}, &types.ActionBaseResp{}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetAccountOnlineState 查询帐号在线状态
|
||||
// 本方法拓展于“查询多个帐号在线状态(GetAccountsOnlineState)”方法。
|
||||
// 获取用户当前的登录状态。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2566
|
||||
func (a *api) GetAccountOnlineState(userId string, isNeedDetail ...bool) (*OnlineStatusResult, error) {
|
||||
ret, err := a.GetAccountsOnlineState([]string{userId}, isNeedDetail...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, item := range ret.Errors {
|
||||
if item.UserId == userId && item.ErrorCode != enum.SuccessCode {
|
||||
return nil, core.NewError(item.ErrorCode, "account exception")
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range ret.Results {
|
||||
if item.UserId == userId {
|
||||
return &item, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetAccountsOnlineState 查询多个帐号在线状态
|
||||
// 获取用户当前的登录状态。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2566
|
||||
func (a *api) GetAccountsOnlineState(userIds []string, isNeedDetail ...bool) (ret *OnlineStatusRet, err error) {
|
||||
req := &queryAccountsOnlineStatusReq{UserIds: userIds}
|
||||
resp := &queryAccountsOnlineStatusResp{}
|
||||
|
||||
if len(isNeedDetail) > 0 && isNeedDetail[0] {
|
||||
req.IsNeedDetail = 1
|
||||
}
|
||||
|
||||
if err = a.client.Post(serviceOpenIM, commandQueryAccountsOnlineStatus, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &OnlineStatusRet{
|
||||
Results: resp.Results,
|
||||
Errors: resp.Errors,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
16
account/enum.go
Normal file
16
account/enum.go
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/9/7 11:09
|
||||
* @Desc: TODO
|
||||
*/
|
||||
|
||||
package account
|
||||
|
||||
// ImportedStatusType 导入状态
|
||||
type ImportedStatusType string
|
||||
|
||||
const (
|
||||
ImportedStatusNo ImportedStatusType = "NotImported" // 未导入
|
||||
ImportedStatusYes ImportedStatusType = "Imported" // 已导入
|
||||
)
|
117
account/types.go
Normal file
117
account/types.go
Normal file
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/5/29 17:36
|
||||
* @Desc: 账号管理数据类型
|
||||
*/
|
||||
|
||||
package account
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/tencent-im/internal/types"
|
||||
)
|
||||
|
||||
type (
|
||||
// Account 导入单个账号
|
||||
Account struct {
|
||||
UserId string `json:"Identifier"` // (必填)用户名,长度不超过32字节
|
||||
Nickname string `json:"Nick"` // (选填)用户昵称
|
||||
FaceUrl string `json:"FaceUrl"` // (选填)用户头像 URL
|
||||
}
|
||||
|
||||
// 批量导入账号(参数)
|
||||
importAccountsReq struct {
|
||||
UserIds []string `json:"Accounts"` // (必填)用户名,单个用户名长度不超过32字节,单次最多导入100个用户名
|
||||
}
|
||||
|
||||
// 批量导入账号(响应)
|
||||
importAccountsResp struct {
|
||||
types.ActionBaseResp
|
||||
FailUserIds []string `json:"FailAccounts"` // 导入失败的帐号列表
|
||||
}
|
||||
|
||||
// 账号项
|
||||
accountItem struct {
|
||||
UserId string `json:"UserID"` // 帐号的UserID
|
||||
}
|
||||
|
||||
// 删除多个帐号(请求)
|
||||
deleteAccountsReq struct {
|
||||
Deletes []*accountItem `json:"DeleteItem"` // 请求删除的帐号对象数组,单次请求最多支持100个帐号
|
||||
}
|
||||
|
||||
// 删除多个账号(响应)
|
||||
deleteAccountsResp struct {
|
||||
types.ActionBaseResp
|
||||
Results []*DeleteResult `json:"ResultItem"`
|
||||
}
|
||||
|
||||
// DeleteResult 删除多个账号结果项
|
||||
DeleteResult struct {
|
||||
ResultCode int `json:"ResultCode"` // 单个帐号的错误码,0表示成功,非0表示失败
|
||||
ResultInfo string `json:"ResultInfo"` // 单个帐号删除失败时的错误描述信息
|
||||
UserId string `json:"UserID"` // 请求删除的帐号的 UserID
|
||||
}
|
||||
|
||||
// 查询多个帐号(请求)
|
||||
checkAccountsReq struct {
|
||||
Checks []*accountItem `json:"CheckItem"` // (必填)请求检查的帐号对象数组,单次请求最多支持100个帐号
|
||||
}
|
||||
|
||||
// 查询多个帐号(响应)
|
||||
checkAccountsResp struct {
|
||||
types.ActionBaseResp
|
||||
Results []*CheckResult `json:"ResultItem"` // 检测结果
|
||||
}
|
||||
|
||||
// CheckResult 检测结果
|
||||
CheckResult struct {
|
||||
UserId string `json:"UserID"` // 请求检查的帐号的 UserID
|
||||
Status ImportedStatusType `json:"AccountStatus"` // 单个帐号的导入状态:Imported 表示已导入,NotImported 表示未导入
|
||||
ResultCode int `json:"ResultCode"` // 单个帐号的检查结果:0表示成功,非0表示失败
|
||||
ResultInfo string `json:"ResultInfo"` // 单个帐号检查失败时的错误描述信息
|
||||
}
|
||||
|
||||
// 失效帐号登录状态(请求)
|
||||
kickAccountReq struct {
|
||||
UserId string `json:"Identifier"` // (必填)用户名
|
||||
}
|
||||
|
||||
// 查询帐号在线状态(请求)
|
||||
queryAccountsOnlineStatusReq struct {
|
||||
UserIds []string `json:"To_Account"` // (必填)需要查询这些 UserID 的登录状态,一次最多查询500个 UserID 的状态
|
||||
IsNeedDetail int `json:"IsNeedDetail"` // (选填)是否需要返回详细的登录平台信息。0表示不需要,1表示需要
|
||||
}
|
||||
|
||||
// 查询帐号在线状态(响应)
|
||||
queryAccountsOnlineStatusResp struct {
|
||||
types.ActionBaseResp
|
||||
Results []OnlineStatusResult `json:"QueryResult"` // 用户在线状态结构化信息
|
||||
Errors []OnlineStatusError `json:"ErrorList"` // 状态查询失败的帐号列表,在此列表中的目标帐号,状态查询失败或目标帐号不存在。若状态全部查询成功,则 ErrorList 为空
|
||||
}
|
||||
|
||||
// OnlineStatusRet 在线状态结果
|
||||
OnlineStatusRet struct {
|
||||
Results []OnlineStatusResult // 用户在线状态结构化信息
|
||||
Errors []OnlineStatusError // 状态查询失败的帐号列表,在此列表中的目标帐号,状态查询失败或目标帐号不存在。若状态全部查询成功,则 ErrorList 为空
|
||||
}
|
||||
|
||||
// OnlineStatusPlatform 详细的登录平台信息
|
||||
OnlineStatusPlatform struct {
|
||||
Platform string `json:"Platform"` // 登录的平台类型。可能的返回值有:"iPhone", "Android", "Web", "PC", "iPad", "Mac"。
|
||||
Status string `json:"Status"` // 该登录平台的状态
|
||||
}
|
||||
|
||||
// OnlineStatusResult 用户在线状态结构化信息项
|
||||
OnlineStatusResult struct {
|
||||
UserId string `json:"To_Account"` // 用户的 UserID
|
||||
Status string `json:"Status"` // 用户状态,前台运行状态(Online)、后台运行状态(PushOnline)、未登录状态(Offline)
|
||||
Detail []OnlineStatusPlatform `json:"Detail"` // 详细的登录平台信息
|
||||
}
|
||||
|
||||
// OnlineStatusError 状态查询失败的帐号项
|
||||
OnlineStatusError struct {
|
||||
UserId string `json:"To_Account"` // 状态查询失败的目标帐号
|
||||
ErrorCode int `json:"ErrorCode"` // 状态查询失败的错误码,若目标帐号的错误码为70107,表示该帐号不存在
|
||||
}
|
||||
)
|
Reference in New Issue
Block a user