添加获取好友列表及加入群组

This commit is contained in:
junleea 2024-08-07 15:31:25 +08:00
parent 63dee0c857
commit cd42b443f2
3 changed files with 41 additions and 0 deletions

View File

@ -176,3 +176,21 @@ func FindFriend(from_user_id, to_user_id int) []Friend {
DB.Debug().Where("user_id = ? and friend_id = ?", from_user_id, to_user_id).Find(&friends)
return friends
}
type FriendRet struct {
ID int `json:"id"` //用户id
Username string `json:"username"` //用户名
Avatar string `json:"avatar"` //头像
}
func FindFriends(user_id int) []FriendRet {
var friends []FriendRet
DB.Debug().Raw("select users.id, users.username, users.avatar from users join friends on users.id = friends.friend_id where friends.user_id = ?", user_id).Scan(&friends)
return friends
}
func FindGroups(user_id int) []Group {
var groups []Group
DB.Debug().Raw("select groups.* from groups join group_users on groups.id = group_users.group_id where group_users.user_id = ?", user_id).Scan(&groups)
return groups
}

View File

@ -66,6 +66,7 @@ func SetUpIMGroup(router *gin.Engine) {
imGroup.POST("/accept_invite", AcceptInvite)
imGroup.POST("/create_group", CreateGroup)
imGroup.GET("/sse_msg", ServerSendMsg)
imGroup.POST("/get_friend_list", GetFriendList) //获取好友列表,包括群聊
}
func generateRandomHexString(length int) (string, error) {
bytes := make([]byte, length/2) // 16字节的字符串需要32个十六进制字符即16个字节
@ -74,6 +75,13 @@ func generateRandomHexString(length int) (string, error) {
}
return hex.EncodeToString(bytes), nil
}
func GetFriendList(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
data := service.GetFriendList(user_id)
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": data, "message": "success"})
}
func GetMessage(c *gin.Context) {
var req Message
user_id, _ := c.Get("id")

View File

@ -108,3 +108,18 @@ func CreateGroup(groupName, groupInfo, groupType, groupIcon string, user_id int)
err, id := dao.CreateGroup(groupName, groupInfo, groupType, groupIcon, user_id)
return err, id
}
type FGRet struct {
Friends []dao.FriendRet `json:"friends"`
Groups []dao.Group `json:"groups"`
}
func GetFriendList(user_id int) FGRet {
//获取好友id和群id
friends := dao.FindFriends(user_id)
groups := dao.FindGroups(user_id)
var fg FGRet
fg.Friends = friends
fg.Groups = groups
return fg
}