初始化项目

This commit is contained in:
2022-09-27 11:31:23 +08:00
parent b4dc3c7305
commit 533ede4f66
54 changed files with 12011 additions and 25 deletions

170
internal/entity/mesage.go Normal file
View File

@@ -0,0 +1,170 @@
/**
* @Author: Echo
* @Author:1711788888@qq.com
* @Date: 2021/9/3 18:19
* @Desc: 基础消息实体
*/
package entity
import (
"errors"
"math/rand"
"git.echol.cn/loser/tencent-im/internal/enum"
"git.echol.cn/loser/tencent-im/internal/types"
)
var (
errInvalidMsgContent = errors.New("invalid message content")
errInvalidMsgLifeTime = errors.New("invalid message life time")
errNotSetMsgContent = errors.New("message content is not set")
)
type Message struct {
sender string // 发送方UserId
lifeTime int // 消息离线保存时长单位最长为7天604800秒
random uint32 // 消息随机数,由随机函数产生
body []*types.MsgBody // 消息体
offlinePush *offlinePush // 推送实体
}
// SetSender 设置发送方UserId
func (m *Message) SetSender(userId string) {
m.sender = userId
}
// GetSender 获取发送者
func (m *Message) GetSender() string {
return m.sender
}
// SetLifeTime 设置消息离线保存时长
func (m *Message) SetLifeTime(lifeTime int) {
m.lifeTime = lifeTime
}
// GetLifeTime 获取消息离线保存时长
func (m *Message) GetLifeTime() int {
return m.lifeTime
}
// SetRandom 设置消息随机数
func (m *Message) SetRandom(random uint32) {
m.random = random
}
// GetRandom 获取消息随机数
func (m *Message) GetRandom() uint32 {
if m.random == 0 {
m.random = rand.Uint32()
}
return m.random
}
// AddContent 添加消息内容(添加会累加之前的消息内容)
func (m *Message) AddContent(msgContent ...interface{}) {
if m.body == nil {
m.body = make([]*types.MsgBody, 0)
}
if len(msgContent) > 0 {
var msgType string
for _, content := range msgContent {
switch content.(type) {
case types.MsgTextContent, *types.MsgTextContent:
msgType = enum.MsgText
case types.MsgLocationContent, *types.MsgLocationContent:
msgType = enum.MsgLocation
case types.MsgFaceContent, *types.MsgFaceContent:
msgType = enum.MsgFace
case types.MsgCustomContent, *types.MsgCustomContent:
msgType = enum.MsgCustom
case types.MsgSoundContent, *types.MsgSoundContent:
msgType = enum.MsgSound
case types.MsgImageContent, *types.MsgImageContent:
msgType = enum.MsgImage
case types.MsgFileContent, *types.MsgFileContent:
msgType = enum.MsgFile
case types.MsgVideoContent, *types.MsgVideoContent:
msgType = enum.MsgVideo
default:
msgType = ""
}
m.body = append(m.body, &types.MsgBody{
MsgType: msgType,
MsgContent: content,
})
}
}
}
// SetContent 设置消息内容(设置会冲掉之前的消息内容)
func (m *Message) SetContent(msgContent ...interface{}) {
if m.body != nil {
m.body = m.body[0:0]
}
m.AddContent(msgContent...)
}
// GetBody 获取消息体
func (m *Message) GetBody() []*types.MsgBody {
return m.body
}
// OfflinePush 新建离线推送对象
func (m *Message) OfflinePush() *offlinePush {
if m.offlinePush == nil {
m.offlinePush = newOfflinePush()
}
return m.offlinePush
}
// GetOfflinePushInfo 获取离线推送消息
func (m *Message) GetOfflinePushInfo() *types.OfflinePushInfo {
if m.offlinePush == nil {
return nil
}
return &types.OfflinePushInfo{
PushFlag: m.offlinePush.pushFlag,
Title: m.offlinePush.title,
Desc: m.offlinePush.desc,
Ext: m.offlinePush.ext,
AndroidInfo: m.offlinePush.androidInfo,
ApnsInfo: m.offlinePush.apnsInfo,
}
}
// CheckLifeTimeArgError 检测参数错误
func (m *Message) CheckLifeTimeArgError() error {
if m.body != nil && len(m.body) > 0 {
for _, item := range m.body {
if item.MsgType == "" {
return errInvalidMsgContent
}
}
} else {
return errNotSetMsgContent
}
return nil
}
// CheckBodyArgError 检测参数错误
func (m *Message) CheckBodyArgError() error {
if m.body != nil && len(m.body) > 0 {
for _, item := range m.body {
if item.MsgType == "" {
return errInvalidMsgContent
}
}
} else {
return errNotSetMsgContent
}
return nil
}

View File

@@ -0,0 +1,150 @@
/**
* @Author: Echo
* @Author:1711788888@qq.com
* @Date: 2021/9/3 18:47
* @Desc: 离线推送
*/
package entity
import (
"git.echol.cn/loser/tencent-im/internal/conv"
"git.echol.cn/loser/tencent-im/internal/types"
)
type offlinePush struct {
pushFlag int // 推送标识。0表示推送1表示不离线推送。
title string // 离线推送标题。该字段为 iOS 和 Android 共用。
desc string // 离线推送内容。
ext string // 离线推送透传内容。
androidInfo *types.AndroidInfo // Android离线推送消息
apnsInfo *types.ApnsInfo // IOS离线推送消息
}
func newOfflinePush() *offlinePush {
return &offlinePush{}
}
// SetPushFlag 设置推送消息
func (o *offlinePush) SetPushFlag(pushFlag types.PushFlag) {
o.pushFlag = int(pushFlag)
}
// SetTitle 设置离线推送标题
func (o *offlinePush) SetTitle(title string) {
o.title = title
}
// SetDesc 设置离线推送内容
func (o *offlinePush) SetDesc(desc string) {
o.desc = desc
}
// SetExt 设置离线推送透传内容
func (o *offlinePush) SetExt(ext interface{}) {
o.ext = conv.String(ext)
}
// SetAndroidSound 设置Android离线推送声音文件路径
func (o *offlinePush) SetAndroidSound(sound string) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.Sound = sound
}
// SetAndroidHuaWeiChannelId 设置华为手机 EMUI 10.0 及以上的通知渠道字段
func (o *offlinePush) SetAndroidHuaWeiChannelId(channelId string) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.HuaWeiChannelID = channelId
}
// SetAndroidXiaoMiChannelId 设置小米手机 MIUI 10 及以上的通知类别Channel适配字段
func (o *offlinePush) SetAndroidXiaoMiChannelId(channelId string) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.XiaoMiChannelID = channelId
}
// SetAndroidOppoChannelId 设置OPPO手机 Android 8.0 及以上的 NotificationChannel 通知适配字段
func (o *offlinePush) SetAndroidOppoChannelId(channelId string) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.OPPOChannelID = channelId
}
// SetAndroidGoogleChannelId 设置Google 手机 Android 8.0 及以上的通知渠道字段
func (o *offlinePush) SetAndroidGoogleChannelId(channelId string) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.GoogleChannelID = channelId
}
// SetAndroidVivoClassification 设置VIVO 手机推送消息分类“0”代表运营消息“1”代表系统消息不填默认为1
func (o *offlinePush) SetAndroidVivoClassification(classification types.VivoClassification) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.VIVOClassification = int(classification)
}
// SetAndroidHuaWeiImportance 设置华为推送通知消息分类
func (o *offlinePush) SetAndroidHuaWeiImportance(importance types.HuaWeiImportance) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.HuaWeiImportance = string(importance)
}
// SetAndroidExtAsHuaweiIntentParam 设置在控制台配置华为推送为“打开应用内指定页面”的前提下传“1”表示将透传内容 Ext 作为 Intent 的参数“0”表示将透传内容 Ext 作为 Action 参数。不填默认为0。
func (o *offlinePush) SetAndroidExtAsHuaweiIntentParam(param types.HuaweiIntentParam) {
if o.androidInfo == nil {
o.androidInfo = &types.AndroidInfo{}
}
o.androidInfo.ExtAsHuaweiIntentParam = int(param)
}
// SetApnsBadgeMode 设置IOS徽章计数模式
func (o *offlinePush) SetApnsBadgeMode(badgeMode types.BadgeMode) {
if o.apnsInfo == nil {
o.apnsInfo = &types.ApnsInfo{}
}
o.apnsInfo.BadgeMode = int(badgeMode)
}
// SetApnsTitle 设置APNs推送的标题
func (o *offlinePush) SetApnsTitle(title string) {
if o.apnsInfo == nil {
o.apnsInfo = &types.ApnsInfo{}
}
o.apnsInfo.Title = title
}
// SetApnsSubTitle 设置APNs推送的子标题
func (o *offlinePush) SetApnsSubTitle(subTitle string) {
if o.apnsInfo == nil {
o.apnsInfo = &types.ApnsInfo{}
}
o.apnsInfo.SubTitle = subTitle
}
// SetApnsImage 设置APNs携带的图片地址
func (o *offlinePush) SetApnsImage(image string) {
if o.apnsInfo == nil {
o.apnsInfo = &types.ApnsInfo{}
}
o.apnsInfo.Image = image
}
// SetApnsMutableContent 设置iOS10的推送扩展开关
func (o *offlinePush) SetApnsMutableContent(mutable types.MutableContent) {
if o.apnsInfo == nil {
o.apnsInfo = &types.ApnsInfo{}
}
o.apnsInfo.MutableContent = int(mutable)
}

330
internal/entity/user.go Normal file
View File

@@ -0,0 +1,330 @@
/**
* @Author: Echo
* @Email:1711788888@qq.com
* @Date: 2021/8/30 4:23 下午
* @Desc: 用户
*/
package entity
import (
"fmt"
"strconv"
"strings"
"time"
"git.echol.cn/loser/tencent-im/internal/core"
"git.echol.cn/loser/tencent-im/internal/enum"
"git.echol.cn/loser/tencent-im/internal/types"
)
type User struct {
userId string
attrs map[string]interface{}
err error
}
// SetUserId 设置用户账号
func (u *User) SetUserId(userId string) {
u.userId = userId
}
// GetUserId 获取用户账号
func (u *User) GetUserId() string {
return u.userId
}
// SetNickname 设置昵称
func (u *User) SetNickname(nickname string) {
u.SetAttr(enum.StandardAttrNickname, nickname)
}
// GetNickname 获取昵称
func (u *User) GetNickname() (nickname string, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrNickname); exist {
nickname = v.(string)
}
return
}
// SetGender 设置性别
func (u *User) SetGender(gender types.GenderType) {
u.SetAttr(enum.StandardAttrGender, gender)
}
// GetGender 获取性别
func (u *User) GetGender() (gender types.GenderType, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrGender); exist {
gender = types.GenderType(v.(string))
}
return
}
// SetBirthday 设置生日
func (u *User) SetBirthday(birthday time.Time) {
b, _ := strconv.Atoi(birthday.Format("20060102"))
u.SetAttr(enum.StandardAttrBirthday, b)
}
// GetBirthday 获取昵称
func (u *User) GetBirthday() (birthday time.Time, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrBirthday); exist {
if val := v.(string); val != "" {
birthday, _ = time.Parse("20060102", val)
}
}
return
}
// SetLocation 设置所在地
func (u *User) SetLocation(country uint32, province uint32, city uint32, region uint32) {
var (
str string
location = []uint32{country, province, city, region}
builder strings.Builder
)
builder.Grow(16)
for _, v := range location {
str = strconv.Itoa(int(v))
if len(str) > 4 {
u.SetError(enum.InvalidParamsCode, "invalid location params")
break
}
builder.WriteString(strings.Repeat("0", 4-len(str)))
builder.WriteString(str)
}
u.SetAttr(enum.StandardAttrLocation, builder.String())
}
// GetLocation 获取所在地
func (u *User) GetLocation() (country uint32, province uint32, city uint32, region uint32, exist bool) {
var v interface{}
if v, exist = u.attrs[enum.StandardAttrLocation]; exist {
str := v.(string)
if len(str) != 16 {
exist = false
return
}
if c, err := strconv.Atoi(str[0:4]); err != nil || c < 0 {
exist = false
return
} else {
country = uint32(c)
}
if c, err := strconv.Atoi(str[4:8]); err != nil || c < 0 {
exist = false
return
} else {
province = uint32(c)
}
if c, err := strconv.Atoi(str[8:12]); err != nil || c < 0 {
exist = false
return
} else {
city = uint32(c)
}
if c, err := strconv.Atoi(str[12:16]); err != nil || c < 0 {
exist = false
return
} else {
region = uint32(c)
}
}
return
}
// SetSignature 设置个性签名
func (u *User) SetSignature(signature string) {
u.SetAttr(enum.StandardAttrSignature, signature)
}
// GetSignature 获取个性签名
func (u *User) GetSignature() (signature string, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrSignature); exist {
signature = v.(string)
}
return
}
// SetAllowType 设置加好友验证方式
func (u *User) SetAllowType(allowType types.AllowType) {
u.SetAttr(enum.StandardAttrAllowType, allowType)
}
// GetAllowType 获取加好友验证方式
func (u *User) GetAllowType() (allowType types.AllowType, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrAllowType); exist {
allowType = types.AllowType(v.(string))
}
return
}
// SetLanguage 设置语言
func (u *User) SetLanguage(language uint) {
u.SetAttr(enum.StandardAttrLanguage, language)
}
// GetLanguage 获取语言
func (u *User) GetLanguage() (language uint, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrLanguage); exist {
language = uint(v.(float64))
}
return
}
// SetAvatar 设置头像URL
func (u *User) SetAvatar(avatar string) {
u.SetAttr(enum.StandardAttrAvatar, avatar)
}
// GetAvatar 获取头像URL
func (u *User) GetAvatar() (avatar string, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrAvatar); exist {
avatar = v.(string)
}
return
}
// SetMsgSettings 设置消息设置
func (u *User) SetMsgSettings(settings uint) {
u.SetAttr(enum.StandardAttrMsgSettings, settings)
}
// GetMsgSettings 获取消息设置
func (u *User) GetMsgSettings() (settings uint, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrMsgSettings); exist {
settings = uint(v.(float64))
}
return
}
// SetAdminForbidType 设置管理员禁止加好友标识
func (u *User) SetAdminForbidType(forbidType types.AdminForbidType) {
u.SetAttr(enum.StandardAttrAdminForbidType, forbidType)
}
// GetAdminForbidType 获取管理员禁止加好友标识
func (u *User) GetAdminForbidType() (forbidType types.AdminForbidType, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrAdminForbidType); exist {
forbidType = types.AdminForbidType(v.(string))
}
return
}
// SetLevel 设置等级
func (u *User) SetLevel(level uint) {
u.SetAttr(enum.StandardAttrLevel, level)
}
// GetLevel 获取等级
func (u *User) GetLevel() (level uint, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrLevel); exist {
level = uint(v.(float64))
}
return
}
// SetRole 设置角色
func (u *User) SetRole(role uint) {
u.SetAttr(enum.StandardAttrRole, role)
}
// GetRole 获取角色
func (u *User) GetRole() (role uint, exist bool) {
var v interface{}
if v, exist = u.GetAttr(enum.StandardAttrRole); exist {
role = uint(v.(float64))
}
return
}
// SetCustomAttr 设置自定义属性
func (u *User) SetCustomAttr(name string, value interface{}) {
u.SetAttr(fmt.Sprintf("%s_%s", enum.CustomAttrPrefix, name), value)
}
// GetCustomAttr 获取自定义属性
func (u *User) GetCustomAttr(name string) (val interface{}, exist bool) {
val, exist = u.GetAttr(fmt.Sprintf("%s_%s", enum.CustomAttrPrefix, name))
return
}
// IsValid 检测用户是否有效
func (u *User) IsValid() bool {
return u.err == nil
}
// SetError 设置异常错误
func (u *User) SetError(code int, message string) {
if code != enum.SuccessCode {
u.err = core.NewError(code, message)
}
}
// GetError 获取异常错误
func (u *User) GetError() error {
return u.err
}
// SetAttr 设置属性
func (u *User) SetAttr(name string, value interface{}) {
if u.attrs == nil {
u.attrs = make(map[string]interface{})
}
u.attrs[name] = value
}
// GetAttr 获取属性
func (u *User) GetAttr(name string) (value interface{}, exist bool) {
value, exist = u.attrs[name]
return
}
// GetAttrs 获取所有属性
func (u *User) GetAttrs() map[string]interface{} {
return u.attrs
}