From 958e440b7ebfea00f39a6fbc2c1c474515f9f27f Mon Sep 17 00:00:00 2001 From: dengbiao Date: Tue, 26 Nov 2024 20:51:26 +0800 Subject: [PATCH] update --- app/db/im/model/group.go | 27 +++++++ app/hdl/im/hdl_group.go | 33 ++++++++ app/hdl/im/hdl_send_message.go | 58 ++++++++++++++ app/md/im/md_group.go | 57 ++++++++++++++ app/router/router.go | 3 + app/svc/im/svc_group.go | 140 +++++++++++++++++++++++++++++++++ 6 files changed, 318 insertions(+) create mode 100644 app/db/im/model/group.go create mode 100644 app/hdl/im/hdl_group.go create mode 100644 app/hdl/im/hdl_send_message.go create mode 100644 app/md/im/md_group.go create mode 100644 app/svc/im/svc_group.go diff --git a/app/db/im/model/group.go b/app/db/im/model/group.go new file mode 100644 index 0000000..b164d84 --- /dev/null +++ b/app/db/im/model/group.go @@ -0,0 +1,27 @@ +package model + +// Group 群组 +type Group struct { + Id int64 // 群组id + Name string // 组名 + AvatarUrl string // 头像 + Introduction string // 群简介 + UserNum int32 // 群组人数 + IsAllMemberBanned int32 // 是否全员禁言(1:是 2:否) + IsAllAddFriend int32 // 是否允许加好友(1:是 2:否) + Extra string // 附加字段 + CreateTime string // 创建时间 + UpdateTime string // 更新时间 +} + +type GroupUser struct { + Id int64 // 自增主键 + GroupId int64 // 群组id + UserId int64 // 用户id + MemberType int // 群组类型 + Remarks string // 备注 + Extra string // 附加属性 + Status int // 状态 + CreateTime string // 创建时间 + UpdateTime string // 更新时间 +} diff --git a/app/hdl/im/hdl_group.go b/app/hdl/im/hdl_group.go new file mode 100644 index 0000000..1240c87 --- /dev/null +++ b/app/hdl/im/hdl_group.go @@ -0,0 +1,33 @@ +package im + +import ( + "applet/app/e" + md "applet/app/md/im" + svc "applet/app/svc/im" + "github.com/gin-gonic/gin" +) + +// GroupList +// @Summary Im-群组(列表) +// @Tags 群组 +// @Description 群组(列表) +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @param req body md.GroupListReq true "相关参数" +// @Success 200 {object} md.GroupListResp "" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/im/groupList [POST] +func GroupList(c *gin.Context) { + var req *md.GroupListReq + if err1 := c.ShouldBindJSON(&req); err1 != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error()) + return + } + err, resp := svc.GroupList(*req) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, resp, nil) +} diff --git a/app/hdl/im/hdl_send_message.go b/app/hdl/im/hdl_send_message.go new file mode 100644 index 0000000..bbbf995 --- /dev/null +++ b/app/hdl/im/hdl_send_message.go @@ -0,0 +1,58 @@ +package im + +import ( + "applet/app/e" + md "applet/app/md/im" + svc "applet/app/svc/im" + "github.com/gin-gonic/gin" +) + +// BatchSendGroupMessage +// @Summary Im-发送群组消息 +// @Tags 发送群组消息 +// @Description 发送群组消息 +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @param req body md.GroupListReq true "相关参数" +// @Success 200 {object} md.GroupListResp "" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/im/batchSendGroupMessage [POST] +func BatchSendGroupMessage(c *gin.Context) { + var req *md.GroupListReq + if err1 := c.ShouldBindJSON(&req); err1 != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error()) + return + } + err, resp := svc.GroupList(*req) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, resp, nil) +} + +// BatchSendUserMessage +// @Summary Im-发送用户消息 +// @Tags 发送用户消息 +// @Description 发送用户消息 +// @Accept json +// @Produce json +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @param req body md.GroupListReq true "相关参数" +// @Success 200 {object} md.GroupListResp "" +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/im/batchSendUserMessage [POST] +func BatchSendUserMessage(c *gin.Context) { + var req *md.GroupListReq + if err1 := c.ShouldBindJSON(&req); err1 != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error()) + return + } + err, resp := svc.GroupList(*req) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + e.OutSuc(c, resp, nil) +} diff --git a/app/md/im/md_group.go b/app/md/im/md_group.go new file mode 100644 index 0000000..b0b6ea8 --- /dev/null +++ b/app/md/im/md_group.go @@ -0,0 +1,57 @@ +package md + +type GroupListReq struct { + Page int `json:"page"` + PageSize int `json:"page_size"` + Name string `json:"name"` //群名称 + GroupAdmin string `json:"group_admin"` //群主 + StartTimeStart string `json:"start_time_start" example:"创建时间-起始"` + StartTimeEnd string `json:"start_time_end" example:"创建时间-截止"` +} + +type GroupListResp struct { + Page int `json:"page"` + PageSize int `json:"page_size"` + Total int64 `json:"total"` + List []struct { + Id int64 // 群组id + Name string // 组名 + AvatarUrl string // 头像 + Introduction string // 群简介 + UserNum int32 // 群组人数 + IsAllMemberBanned int32 // 是否全员禁言(1:是 2:否) + IsAllAddFriend int32 // 是否允许加好友(1:是 2:否) + Extra string // 附加字段 + CreateTime string // 创建时间 + UpdateTime string // 更新时间 + PhoneNumber string // 群主账号 + Nickname string // 群主昵称 + } `json:"list"` +} + +type GroupUserListReq struct { + Page int `json:"page"` + PageSize int `json:"page_size"` + GroupId int `json:"name"` //群id + Phone string `json:"phone" example:"手机号"` //手机号 + Nickname string `json:"nickname" example:"昵称"` //昵称 +} + +type GroupUserListResp struct { + Page int `json:"page"` + PageSize int `json:"page_size"` + Total int64 `json:"total"` + List []struct { + Id int64 // 自增主键 + GroupId int64 // 群组id + UserId int64 // 用户id + MemberType int // 群组类型 + Remarks string // 备注 + Extra string // 附加属性 + Status int // 状态 + CreateTime string // 创建时间 + UpdateTime string // 更新时间 + PhoneNumber string // 账号 + Nickname string // 昵称 + } `json:"list"` +} diff --git a/app/router/router.go b/app/router/router.go index b03b96b..05cbe26 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -257,6 +257,9 @@ func rIm(r *gin.RouterGroup) { r.POST("/updateCustomerServiceMemo", im.UpdateCustomerServiceMemo) r.GET("/redPackageRecordsDetail", im.RedPackageRecordsDetail) r.POST("/pageSendRedPackageOrd", im.PageSendRedPackageOrd) + r.POST("/groupList", im.GroupList) + r.POST("/batchSendGroupMessage", im.BatchSendGroupMessage) + r.POST("/batchSendUserMessage", im.BatchSendUserMessage) } func rFinancialCenter(r *gin.RouterGroup) { diff --git a/app/svc/im/svc_group.go b/app/svc/im/svc_group.go new file mode 100644 index 0000000..c0952b3 --- /dev/null +++ b/app/svc/im/svc_group.go @@ -0,0 +1,140 @@ +package svc + +import ( + "applet/app/db" + "applet/app/db/im/model" + md "applet/app/md/im" + "errors" + "fmt" +) + +func GroupList(req md.GroupListReq) (err error, resp md.GroupListResp) { + sess := db.DbIm.Where("1=1") + var groupIds []int64 + if req.GroupAdmin != "" { + var user model.User + has, err := db.DbIm.Where("`phone_number`=?", req.GroupAdmin).Get(&user) + if err != nil { + return + } + if !has { + return errors.New("群主账号不存在"), resp + } + var groupUserList []*model.GroupUser + err = db.DbIm.Where("`user_id`=? and member_type= 1", user.Id).Find(&groupUserList) + if err != nil { + return + } + for _, v := range groupUserList { + groupIds = append(groupIds, v.GroupId) + } + } + + var mm []*model.Group + if len(groupIds) > 0 { + sess.In("id", groupIds) + } + resp.Total, err = sess.Limit(req.PageSize, (req.Page-1)*req.PageSize).Asc("id").FindAndCount(&mm) + if err != nil { + return + } + resp.Page = req.Page + resp.PageSize = req.PageSize + for _, v := range mm { + var tmp = struct { + Id int64 // 群组id + Name string // 组名 + AvatarUrl string // 头像 + Introduction string // 群简介 + UserNum int32 // 群组人数 + IsAllMemberBanned int32 // 是否全员禁言(1:是 2:否) + IsAllAddFriend int32 // 是否允许加好友(1:是 2:否) + Extra string // 附加字段 + CreateTime string // 创建时间 + UpdateTime string // 更新时间 + PhoneNumber string // 群主账号 + Nickname string // 群主昵称 + }{ + Id: v.Id, + Name: v.Name, + AvatarUrl: v.AvatarUrl, + Introduction: v.Introduction, + UserNum: v.UserNum, + IsAllMemberBanned: v.IsAllMemberBanned, + IsAllAddFriend: v.IsAllAddFriend, + Extra: v.Extra, + CreateTime: v.CreateTime, + UpdateTime: v.UpdateTime, + } + var groupUser model.GroupUser + has, err := db.DbIm.Where("`group_id`=? and member_type= 1", v.Id).Get(&groupUser) + if err != nil { + return + } + if has { + var user model.User + has, err = db.DbIm.ID(groupUser.UserId).Get(&user) + if err != nil { + return + } + if has { + tmp.PhoneNumber = user.PhoneNumber + tmp.Nickname = user.Nickname + } + } + } + return +} + +type GroupUserJoinUser struct { + model.GroupUser `xorm:"extends"` + model.User `xorm:"extends"` +} + +func GroupUserList(req md.GroupUserListReq) (err error, resp md.GroupUserListResp) { + sess := db.DbIm.Table("group_user").Where("1=1") + sess.Join("LEFT", "user", "group_user.user_id = user.id") + if req.Phone != "" { + sess.And("user.phone LIKE ?", "%"+req.Phone+"%") + } + if req.Nickname != "" { + sess.And("user.nickname LIKE ?", "%"+req.Nickname+"%") + } + var mm []*GroupUserJoinUser + resp.Total, err = sess.Limit(req.PageSize, (req.Page-1)*req.PageSize).Asc("group_user.id").FindAndCount(&mm) + sql, _ := sess.LastSQL() + fmt.Println("GroupUserList___SQL:::", sql) + if err != nil { + return + } + resp.Page = req.Page + resp.PageSize = req.PageSize + for _, v := range mm { + resp.List = append(resp.List, struct { + Id int64 // 自增主键 + GroupId int64 // 群组id + UserId int64 // 用户id + MemberType int // 群组类型 + Remarks string // 备注 + Extra string // 附加属性 + Status int // 状态 + CreateTime string // 创建时间 + UpdateTime string // 更新时间 + PhoneNumber string // 账号 + Nickname string // 昵称 + }{ + Id: v.GroupUser.Id, + GroupId: int64(v.GroupUser.GroupId), + UserId: v.GroupUser.UserId, + MemberType: v.GroupUser.MemberType, + Remarks: v.GroupUser.Remarks, + Extra: v.GroupUser.Extra, + Status: v.GroupUser.Status, + CreateTime: v.GroupUser.CreateTime, + UpdateTime: v.GroupUser.UpdateTime, + PhoneNumber: v.User.PhoneNumber, + Nickname: v.User.Nickname, + }) + } + return +}