1 Star 0 Fork 0

h79/goim

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
chat
chat.go
msg.go
internal
protobuffs
rpc
.gitignore
LICENSE
Makefile
README.md
client.go
config.go
context.go
error.go
event.go
go.mod
group.go
im.go
model.go
process.go
request.go
response.go
serve.go
servers.go
user.go
version.go
work.go
克隆/下载
msg.go 2.30 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2年前 . init
package chat
import (
"encoding/json"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/common/stringutil"
)
type MemberId uint64
func (m MemberId) String() string {
return stringutil.UInt64ToString(uint64(m))
}
func (m MemberId) Int() uint64 {
return uint64(m)
}
func (m MemberId) Int32() int32 {
return int32(m)
}
type Message struct {
// 消息的来源ID(如果是私聊,则是接收用户id,如果是群聊,则是群组id)
Id int64 `json:"id"`
FromId MemberId `json:"fromid"` // 消息的发送者id
ToId MemberId `json:"toid"` // 接收者id
Content string `json:"content"` // 消息
Type string `json:"type"` // "group", "friend",聊天类型
Avatar string `json:"avatar"` //消息来源用户头像
UserName string `json:"username"` //消息来源用户名
Mine bool `json:"mine"` //是否我发送的消息,如果为true,则会显示在右方
}
func (chat *Message) TypeInt32() int32 {
if chat.Type == "friend" {
return 0
}
return 1
}
func (chat *Message) IsFriend() bool {
return chat.Type == "friend"
}
func (chat *Message) IsGroup() bool {
return chat.Type == "group"
}
func (chat *Message) HasValid() error {
if chat.Id > 0 && chat.FromId > 0 && len(chat.Content) > 0 {
return nil
}
return result.RErrParam
}
type GroupModel struct {
GroupId int64 `json:"id"`
Member []Member `json:"members,omitempty"`
}
type Member struct {
UserId MemberId `json:"id"`
Name string `json:"name,omitempty"`
}
type Event int32
const (
KChatCreateGroup = Event(iota) //创建群
KChatJoinGroup //进群
KChatLeaveGroup //退群
KChatRemoveGroup //解散群
KChatMessage //发消息
)
type Package struct {
Event Event `json:"event"`
Content string `json:"content"` //json格式化
}
func (pack *Package) HasValid() error {
if pack.Event < KChatCreateGroup || pack.Event > KChatMessage {
return result.RErrNotSupport
}
if len(pack.Content) <= 0 {
return result.RErrParam
}
return nil
}
func (pack *Package) ToChatMsg() *Message {
chat := &Message{}
_ = json.Unmarshal([]byte(pack.Content), chat)
return chat
}
func (pack *Package) ToChatGroup() *GroupModel {
chat := &GroupModel{}
_ = json.Unmarshal([]byte(pack.Content), chat)
return chat
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goim.git
git@gitee.com:h79/goim.git
h79
goim
goim
v0.1.19

搜索帮助