diff --git a/dao/im.go b/dao/im.go index 1e83c43..f2dcfea 100644 --- a/dao/im.go +++ b/dao/im.go @@ -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 +} diff --git a/handler/im.go b/handler/im.go index ffc6d04..974e323 100644 --- a/handler/im.go +++ b/handler/im.go @@ -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") diff --git a/service/imService.go b/service/imService.go index 4001b58..8f5ffe9 100644 --- a/service/imService.go +++ b/service/imService.go @@ -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 +}