From 88c15d7d2e88c584a41a47758b8dc866450a75e4 Mon Sep 17 00:00:00 2001 From: lin <550210817@qq.com> Date: Wed, 10 Apr 2024 16:41:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/contact_service.go | 407 ------------------------------------- service/user_service.go | 399 ++++++++++++++++++++++++++++++++++++ 2 files changed, 399 insertions(+), 407 deletions(-) diff --git a/service/contact_service.go b/service/contact_service.go index 5778d74..6d43c33 100644 --- a/service/contact_service.go +++ b/service/contact_service.go @@ -1,408 +1 @@ package service - -import ( - "DiTing-Go/dal" - "DiTing-Go/dal/model" - "DiTing-Go/dal/query" - domainEnum "DiTing-Go/domain/enum" - "DiTing-Go/domain/vo/req" - resp2 "DiTing-Go/domain/vo/resp" - "DiTing-Go/global" - cursorUtils "DiTing-Go/pkg/cursor" - "DiTing-Go/pkg/enum" - "DiTing-Go/pkg/resp" - "bytes" - "context" - "github.com/gin-gonic/gin" - "github.com/goccy/go-json" - "github.com/jinzhu/copier" - "io" - "log" - "strconv" -) - -var q *query.Query = global.Query - -// ApplyFriend 添加好友 -// -// @Summary 添加好友 -// @Produce json -// @Param uid body int true "好友uid" -// @Param msg body string true "验证消息" -// @Success 200 {object} resp.ResponseData "成功" -// @Failure 500 {object} resp.ResponseData "内部错误" -// @Router /api/contact/add [post] -func ApplyFriend(c *gin.Context) { - uid := c.GetInt64("uid") - applyReq := req.UserApplyReq{} - data, err := c.GetRawData() - c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) - if err != nil { //ShouldBind()会自动推导 - resp.ErrorResponse(c, "参数错误") - return - } - json.Unmarshal(data, &applyReq) - friendUid := applyReq.Uid - //检查用户是否存在 - user, err := query.User.WithContext(context.Background()).Where(query.User.ID.Eq(friendUid)).First() - if user == nil { - resp.ErrorResponse(c, "用户不存在") - c.Abort() - return - } - // 检查是否已经是好友关系 - if isFriend := isFriend(c, uid, friendUid); isFriend { - resp.ErrorResponse(c, "好友已存在") - c.Abort() - return - } - // 检查是否已经发送过好友请求 - friendApply, err := query.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(uid), query.UserApply.TargetID.Eq(friendUid)).First() - if err != nil && err.Error() != "record not found" { - resp.ErrorResponse(c, "参数错误") - c.Abort() - return - } - if friendApply != nil { - resp.ErrorResponse(c, "已发送过好友请求,请等待对方同意") - c.Abort() - return - } - // 检查对方是否给我们发送过好友请求,如果是,直接同意 - apply, err := query.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(friendUid), query.UserApply.TargetID.Eq(uid)).First() - if err != nil && err.Error() != "record not found" { - resp.ErrorResponse(c, "参数错误") - c.Abort() - return - } - if apply != nil { - Agree(c) - return - } - // 发送好友请求 - err = query.UserApply.WithContext(context.Background()).Create(&model.UserApply{ - UID: uid, - TargetID: friendUid, - Msg: applyReq.Msg, - Status: enum.NO, - ReadStatus: enum.NO, - }) - if err != nil { - resp.ErrorResponse(c, "参数错误") - c.Abort() - return - } - // 发送好友申请事件 - global.Bus.Publish(domainEnum.FriendApplyEvent, model.UserApply{ - UID: uid, - TargetID: friendUid, - Msg: applyReq.Msg, - Status: enum.NO, - ReadStatus: enum.NO, - }) - resp.SuccessResponseWithMsg(c, "success") - c.Abort() - return -} - -// DeleteFriend 删除好友 -// -// @Summary 删除好友 -// @Produce json -// @Param uid body int true "好友uid" -// @Success 200 {object} resp.ResponseData "成功" -// @Failure 500 {object} resp.ResponseData "内部错误" -// @Router /api/contact/delete [delete] -func DeleteFriend(c *gin.Context) { - uid := c.GetInt64("uid") - friend := req.UidReq{} - if err := c.ShouldBind(&friend); err != nil { //ShouldBind()会自动推导 - resp.ErrorResponse(c, "参数错误") - return - } - friendUid := friend.Uid - // 检查是否为好友 - if isFriend := isFriend(c, uid, friendUid); isFriend { - // 事务 - err := q.Transaction(func(tx *query.Query) error { - // 删除好友关系 - if _, err := tx.UserFriend.WithContext(context.Background()).Where(query.UserFriend.UID.Eq(uid), query.UserFriend.FriendUID.Eq(friendUid)).Delete(); err != nil { - return err - } - if _, err := tx.UserFriend.WithContext(context.Background()).Where(query.UserFriend.UID.Eq(friendUid), query.UserFriend.FriendUID.Eq(uid)).Delete(); err != nil { - return err - } - // 删除好友申请 - if _, err := tx.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(uid), query.UserApply.TargetID.Eq(friendUid)).Delete(); err != nil { - return err - } - if _, err := tx.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(friendUid), query.UserApply.TargetID.Eq(uid)).Delete(); err != nil { - return err - } - return nil - }) - if err != nil { - resp.ErrorResponse(c, "删除失败") - c.Abort() - return - } - } - resp.SuccessResponseWithMsg(c, "success") - return -} - -// IsFriend 是否为好友关系 -// -// @Summary 是否为好友关系 -// @Produce json -// @Param uid body int true "好友uid" -// @Success 200 {object} resp.ResponseData "成功" -// @Failure 500 {object} resp.ResponseData "内部错误" -// @Router /api/contact/isFriend/:friendUid [get] -func IsFriend(c *gin.Context) { - uid := c.GetInt64("uid") - friendUid, _ := strconv.ParseInt(c.Param("friendUid"), 10, 64) - resp.SuccessResponse(c, isFriend(c, uid, int64(friendUid))) -} - -func isFriend(c *gin.Context, uid, friendUid int64) bool { - // 检查是否已经是好友关系 - friend, err := query.UserFriend.WithContext(context.Background()).Where(query.UserFriend.UID.Eq(uid), query.UserFriend.FriendUID.Eq(friendUid)).First() - if err != nil && err.Error() != "record not found" { - resp.ErrorResponse(c, "参数错误") - c.Abort() - } - if friend == nil { - return false - } - return true -} - -// Agree 同意好友申请 -// -// @Summary 同意好友申请 -// @Produce json -// @Param uid body int true "好友uid" -// @Success 200 {object} resp.ResponseData "成功" -// @Failure 500 {object} resp.ResponseData "内部错误" -// @Router /api/contact/delete [put] -func Agree(c *gin.Context) { - ctx := context.Background() - userApply := query.UserApply - userApplyQ := userApply.WithContext(ctx) - uid := c.GetInt64("uid") - - friend := req.UidReq{} - if err := c.ShouldBind(&friend); err != nil { //ShouldBind()会自动推导 - resp.ErrorResponse(c, "参数错误") - c.Abort() - return - } - - friendUid := friend.Uid - // 检查是否存在好友申请且状态为待审批 - apply, err := userApplyQ.Where(userApply.UID.Eq(friendUid), userApply.TargetID.Eq(uid), userApply.Status.Eq(enum.NO)).First() - if err != nil && err.Error() != "record not found" { - resp.ErrorResponse(c, "参数错误") - c.Abort() - } - if apply == nil { - resp.ErrorResponse(c, "好友申请不存在") - c.Abort() - return - } - - // 同意好友请求 - apply.Status = enum.YES - // 事务 - tx := q.Begin() - userApplyTx := tx.UserApply.WithContext(context.Background()) - userFriendTx := tx.UserFriend.WithContext(context.Background()) - if _, err = userApplyTx.Where(userApply.UID.Eq(friendUid), userApply.TargetID.Eq(uid)).Updates(apply); err != nil { - if err := tx.Rollback(); err != nil { - log.Fatalln("事务回滚失败", err.Error()) - } - resp.ErrorResponse(c, "系统正忙请稍后再试~") - c.Abort() - return - } - var userFriends = []*model.UserFriend{ - { - UID: uid, - FriendUID: friendUid, - }, - { - UID: friendUid, - FriendUID: uid, - }, - } - if err = userFriendTx.Create(userFriends...); err != nil { - if err := tx.Rollback(); err != nil { - log.Fatalln("事务回滚失败", err.Error()) - } - resp.ErrorResponse(c, "系统正忙请稍后再试~") - c.Abort() - return - } - if err := tx.Commit(); err != nil { - resp.ErrorResponse(c, "系统正忙请稍后再试~") - c.Abort() - log.Fatalln("事务提交失败", err.Error()) - return - } - // 发送新好友事件 - global.Bus.Publish(domainEnum.FriendNewEvent, model.UserFriend{ - UID: uid, - FriendUID: friendUid, - }) - resp.SuccessResponseWithMsg(c, "success") - c.Abort() - return -} - -// GetApplyList 获取用户好友申请列表 -// -// @Summary 获取用户好友申请列表 -// @Produce json -// @Security ApiKeyAuth -// @Param last_id query int true "last_id" -// @Param limit query int true "limit" -// @Success 200 {object} resp.ResponseData "成功" -// @Failure 500 {object} resp.ResponseData "内部错误" -// @Router /api/contact/getApplyList [get] -func GetApplyList(c *gin.Context) { - - ctx := context.Background() - - uid := c.GetInt64("uid") - var n int - - u := query.User - - var uids = make([]int64, 0) - var usersVO = make([]resp2.UserApplyResp, 0) - - // 默认值 - var cursor *string = nil - var pageSize int = 20 - //pageSize, _ = strconv.Atoi(c.Query("page_size")) - pageRequest := cursorUtils.PageReq{ - Cursor: cursor, - PageSize: pageSize, - } - if err := c.ShouldBindQuery(&pageRequest); err != nil { //ShouldBind()会自动推导 - resp.ErrorResponse(c, "参数错误") - return - } - - // 获取 UserApply 表中 TargetID 等于 uid(登录用户ID)的用户ID集合,采用游标分页 - db := dal.DB - userApplys := make([]model.UserApply, 0) - condition := []string{"target_id=?", strconv.FormatInt(uid, 10)} - pageResp, err := cursorUtils.Paginate(db, pageRequest, &userApplys, "create_time", false, condition...) - if err != nil { - // todo 添加日志系统 - log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) - resp.ErrorResponse(c, "系统繁忙,亲稍后再试") - return - } - - n = len(userApplys) - for i := 0; i < n; i++ { - uids = append(uids, userApplys[i].UID) - } - - // 根据 uids 集合查询 User 表 - users, err := u.WithContext(ctx).Select(u.ID, u.Name, u.Avatar).Where(u.ID.In(uids...)).Find() - if err != nil { - // todo 添加日志系统 - log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) - resp.ErrorResponse(c, "系统繁忙,亲稍后再试") - return - } - - if len(users) != len(userApplys) { - log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) - resp.ErrorResponse(c, "系统繁忙,亲稍后再试") - } - // 数据转换 - for i := 0; i < len(users); i++ { - var userVO resp2.UserApplyResp - _ = copier.Copy(&userVO, &users[i]) - userVO.Msg = userApplys[i].Msg - userVO.Status = userApplys[i].Status - usersVO = append(usersVO, userVO) - } - pageResp.Data = usersVO - - resp.SuccessResponse(c, pageResp) -} - -// UnreadApplyNum 好友申请未读数量 -// -// @Summary 好友申请未读数量 -// @Success 200 {object} resp.ResponseData "成功" -// @Failure 500 {object} resp.ResponseData "内部错误" -// @Router /api/public/login [post] -func UnreadApplyNum(c *gin.Context) { - ctx := context.Background() - - uid := c.GetInt64("uid") - - ua := query.UserApply - - // 获取 UserApply 表中 TargetID 等于 uid(登录用户ID)的用户ID集合 - num, err := ua.WithContext(ctx).Where(ua.TargetID.Eq(uid), ua.ReadStatus.Eq(enum.NO), ua.Status.Eq(enum.NO)).Count() - if err != nil { - log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) - resp.ErrorResponse(c, "系统繁忙,亲稍后再试") - c.Abort() - return - } - resp.SuccessResponse(c, num) -} - -// GetContactList 获取好友列表 -// -// @Summary 获取好友列表 -// @Produce json -// @Success 200 {object} resp.ResponseData "成功" -// @Failure 500 {object} resp.ResponseData "内部错误" -// @Router /api/contact/getContactList [get] -func GetContactList(c *gin.Context) { - ctx := context.Background() - uid := c.GetInt64("uid") - // 获取好友列表 - userFriend := query.UserFriend - // 获取 UserFriend 表中 uid = uid 的好友的uid组成的集合 - // select friend_uid from user_friend where uid = ? - friendIDs, err := userFriend.WithContext(ctx).Select(userFriend.FriendUID).Where(userFriend.UID.Eq(uid)).Find() - // 将friendIDs转换为切片 - // TODO 实现游标翻页 - friendIDsSlice := make([]int64, 0) - for _, id := range friendIDs { - friendIDsSlice = append(friendIDsSlice, id.FriendUID) - } - if err != nil { - // todo 添加日志系统 - log.Printf("SQL查询错误, 错误信息为 : %v", err) - resp.ErrorResponse(c, "出现错误,未能获取联系人列表") - return - } - - // 获取好友信息 - users := query.User - // select id , name , avatar from user where id in (...) and status = 0 - friendList, err := users.WithContext(ctx).Select(users.ID, users.Name, users.Avatar).Where(users.ID.In(friendIDsSlice...), users.Status.Eq(0)).Find() - if err != nil { - // todo 添加日志系统 - log.Printf("SQL查询错误, 错误信息为 : %v", err) - resp.ErrorResponse(c, "出现错误,未能获取联系人信息") - return - } - - // 数据转换 - friendListVO := make([]resp2.UserContactResp, 0) - _ = copier.Copy(&friendListVO, &friendList) - resp.SuccessResponse(c, friendListVO) -} diff --git a/service/user_service.go b/service/user_service.go index 2704156..e29fca6 100644 --- a/service/user_service.go +++ b/service/user_service.go @@ -1,13 +1,26 @@ package service import ( + "DiTing-Go/dal" "DiTing-Go/dal/model" "DiTing-Go/dal/query" + domainEnum "DiTing-Go/domain/enum" + "DiTing-Go/domain/vo/req" + resp2 "DiTing-Go/domain/vo/resp" + "DiTing-Go/global" + cursorUtils "DiTing-Go/pkg/cursor" + "DiTing-Go/pkg/enum" "DiTing-Go/pkg/resp" _ "DiTing-Go/pkg/setting" "DiTing-Go/pkg/utils" + "bytes" "context" "github.com/gin-gonic/gin" + "github.com/goccy/go-json" + "github.com/jinzhu/copier" + "io" + "log" + "strconv" ) // Register 用户注册 @@ -69,3 +82,389 @@ func Login(c *gin.Context) { token, _ := utils.GenerateToken(user.ID) resp.SuccessResponse(c, token) } + +var q *query.Query = global.Query + +// ApplyFriend 添加好友 +// +// @Summary 添加好友 +// @Produce json +// @Param uid body int true "好友uid" +// @Param msg body string true "验证消息" +// @Success 200 {object} resp.ResponseData "成功" +// @Failure 500 {object} resp.ResponseData "内部错误" +// @Router /api/contact/add [post] +func ApplyFriend(c *gin.Context) { + uid := c.GetInt64("uid") + applyReq := req.UserApplyReq{} + data, err := c.GetRawData() + c.Request.Body = io.NopCloser(bytes.NewBuffer(data)) + if err != nil { //ShouldBind()会自动推导 + resp.ErrorResponse(c, "参数错误") + return + } + json.Unmarshal(data, &applyReq) + friendUid := applyReq.Uid + //检查用户是否存在 + user, err := query.User.WithContext(context.Background()).Where(query.User.ID.Eq(friendUid)).First() + if user == nil { + resp.ErrorResponse(c, "用户不存在") + c.Abort() + return + } + // 检查是否已经是好友关系 + if isFriend := isFriend(c, uid, friendUid); isFriend { + resp.ErrorResponse(c, "好友已存在") + c.Abort() + return + } + // 检查是否已经发送过好友请求 + friendApply, err := query.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(uid), query.UserApply.TargetID.Eq(friendUid)).First() + if err != nil && err.Error() != "record not found" { + resp.ErrorResponse(c, "参数错误") + c.Abort() + return + } + if friendApply != nil { + resp.ErrorResponse(c, "已发送过好友请求,请等待对方同意") + c.Abort() + return + } + // 检查对方是否给我们发送过好友请求,如果是,直接同意 + apply, err := query.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(friendUid), query.UserApply.TargetID.Eq(uid)).First() + if err != nil && err.Error() != "record not found" { + resp.ErrorResponse(c, "参数错误") + c.Abort() + return + } + if apply != nil { + Agree(c) + return + } + // 发送好友请求 + err = query.UserApply.WithContext(context.Background()).Create(&model.UserApply{ + UID: uid, + TargetID: friendUid, + Msg: applyReq.Msg, + Status: enum.NO, + ReadStatus: enum.NO, + }) + if err != nil { + resp.ErrorResponse(c, "参数错误") + c.Abort() + return + } + // 发送好友申请事件 + global.Bus.Publish(domainEnum.FriendApplyEvent, model.UserApply{ + UID: uid, + TargetID: friendUid, + Msg: applyReq.Msg, + Status: enum.NO, + ReadStatus: enum.NO, + }) + resp.SuccessResponseWithMsg(c, "success") + c.Abort() + return +} + +// DeleteFriend 删除好友 +// +// @Summary 删除好友 +// @Produce json +// @Param uid body int true "好友uid" +// @Success 200 {object} resp.ResponseData "成功" +// @Failure 500 {object} resp.ResponseData "内部错误" +// @Router /api/contact/delete [delete] +func DeleteFriend(c *gin.Context) { + uid := c.GetInt64("uid") + friend := req.UidReq{} + if err := c.ShouldBind(&friend); err != nil { //ShouldBind()会自动推导 + resp.ErrorResponse(c, "参数错误") + return + } + friendUid := friend.Uid + // 检查是否为好友 + if isFriend := isFriend(c, uid, friendUid); isFriend { + // 事务 + err := q.Transaction(func(tx *query.Query) error { + // 删除好友关系 + if _, err := tx.UserFriend.WithContext(context.Background()).Where(query.UserFriend.UID.Eq(uid), query.UserFriend.FriendUID.Eq(friendUid)).Delete(); err != nil { + return err + } + if _, err := tx.UserFriend.WithContext(context.Background()).Where(query.UserFriend.UID.Eq(friendUid), query.UserFriend.FriendUID.Eq(uid)).Delete(); err != nil { + return err + } + // 删除好友申请 + if _, err := tx.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(uid), query.UserApply.TargetID.Eq(friendUid)).Delete(); err != nil { + return err + } + if _, err := tx.UserApply.WithContext(context.Background()).Where(query.UserApply.UID.Eq(friendUid), query.UserApply.TargetID.Eq(uid)).Delete(); err != nil { + return err + } + return nil + }) + if err != nil { + resp.ErrorResponse(c, "删除失败") + c.Abort() + return + } + } + resp.SuccessResponseWithMsg(c, "success") + return +} + +// IsFriend 是否为好友关系 +// +// @Summary 是否为好友关系 +// @Produce json +// @Param uid body int true "好友uid" +// @Success 200 {object} resp.ResponseData "成功" +// @Failure 500 {object} resp.ResponseData "内部错误" +// @Router /api/contact/isFriend/:friendUid [get] +func IsFriend(c *gin.Context) { + uid := c.GetInt64("uid") + friendUid, _ := strconv.ParseInt(c.Param("friendUid"), 10, 64) + resp.SuccessResponse(c, isFriend(c, uid, int64(friendUid))) +} + +func isFriend(c *gin.Context, uid, friendUid int64) bool { + // 检查是否已经是好友关系 + friend, err := query.UserFriend.WithContext(context.Background()).Where(query.UserFriend.UID.Eq(uid), query.UserFriend.FriendUID.Eq(friendUid)).First() + if err != nil && err.Error() != "record not found" { + resp.ErrorResponse(c, "参数错误") + c.Abort() + } + if friend == nil { + return false + } + return true +} + +// Agree 同意好友申请 +// +// @Summary 同意好友申请 +// @Produce json +// @Param uid body int true "好友uid" +// @Success 200 {object} resp.ResponseData "成功" +// @Failure 500 {object} resp.ResponseData "内部错误" +// @Router /api/contact/delete [put] +func Agree(c *gin.Context) { + ctx := context.Background() + userApply := query.UserApply + userApplyQ := userApply.WithContext(ctx) + uid := c.GetInt64("uid") + + friend := req.UidReq{} + if err := c.ShouldBind(&friend); err != nil { //ShouldBind()会自动推导 + resp.ErrorResponse(c, "参数错误") + c.Abort() + return + } + + friendUid := friend.Uid + // 检查是否存在好友申请且状态为待审批 + apply, err := userApplyQ.Where(userApply.UID.Eq(friendUid), userApply.TargetID.Eq(uid), userApply.Status.Eq(enum.NO)).First() + if err != nil && err.Error() != "record not found" { + resp.ErrorResponse(c, "参数错误") + c.Abort() + } + if apply == nil { + resp.ErrorResponse(c, "好友申请不存在") + c.Abort() + return + } + + // 同意好友请求 + apply.Status = enum.YES + // 事务 + tx := q.Begin() + userApplyTx := tx.UserApply.WithContext(context.Background()) + userFriendTx := tx.UserFriend.WithContext(context.Background()) + if _, err = userApplyTx.Where(userApply.UID.Eq(friendUid), userApply.TargetID.Eq(uid)).Updates(apply); err != nil { + if err := tx.Rollback(); err != nil { + log.Fatalln("事务回滚失败", err.Error()) + } + resp.ErrorResponse(c, "系统正忙请稍后再试~") + c.Abort() + return + } + var userFriends = []*model.UserFriend{ + { + UID: uid, + FriendUID: friendUid, + }, + { + UID: friendUid, + FriendUID: uid, + }, + } + if err = userFriendTx.Create(userFriends...); err != nil { + if err := tx.Rollback(); err != nil { + log.Fatalln("事务回滚失败", err.Error()) + } + resp.ErrorResponse(c, "系统正忙请稍后再试~") + c.Abort() + return + } + if err := tx.Commit(); err != nil { + resp.ErrorResponse(c, "系统正忙请稍后再试~") + c.Abort() + log.Fatalln("事务提交失败", err.Error()) + return + } + // 发送新好友事件 + global.Bus.Publish(domainEnum.FriendNewEvent, model.UserFriend{ + UID: uid, + FriendUID: friendUid, + }) + resp.SuccessResponseWithMsg(c, "success") + c.Abort() + return +} + +// GetApplyList 获取用户好友申请列表 +// +// @Summary 获取用户好友申请列表 +// @Produce json +// @Security ApiKeyAuth +// @Param last_id query int true "last_id" +// @Param limit query int true "limit" +// @Success 200 {object} resp.ResponseData "成功" +// @Failure 500 {object} resp.ResponseData "内部错误" +// @Router /api/contact/getApplyList [get] +func GetApplyList(c *gin.Context) { + + ctx := context.Background() + + uid := c.GetInt64("uid") + var n int + + u := query.User + + var uids = make([]int64, 0) + var usersVO = make([]resp2.UserApplyResp, 0) + + // 默认值 + var cursor *string = nil + var pageSize int = 20 + //pageSize, _ = strconv.Atoi(c.Query("page_size")) + pageRequest := cursorUtils.PageReq{ + Cursor: cursor, + PageSize: pageSize, + } + if err := c.ShouldBindQuery(&pageRequest); err != nil { //ShouldBind()会自动推导 + resp.ErrorResponse(c, "参数错误") + return + } + + // 获取 UserApply 表中 TargetID 等于 uid(登录用户ID)的用户ID集合,采用游标分页 + db := dal.DB + userApplys := make([]model.UserApply, 0) + condition := []string{"target_id=?", strconv.FormatInt(uid, 10)} + pageResp, err := cursorUtils.Paginate(db, pageRequest, &userApplys, "create_time", false, condition...) + if err != nil { + // todo 添加日志系统 + log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) + resp.ErrorResponse(c, "系统繁忙,亲稍后再试") + return + } + + n = len(userApplys) + for i := 0; i < n; i++ { + uids = append(uids, userApplys[i].UID) + } + + // 根据 uids 集合查询 User 表 + users, err := u.WithContext(ctx).Select(u.ID, u.Name, u.Avatar).Where(u.ID.In(uids...)).Find() + if err != nil { + // todo 添加日志系统 + log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) + resp.ErrorResponse(c, "系统繁忙,亲稍后再试") + return + } + + if len(users) != len(userApplys) { + log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) + resp.ErrorResponse(c, "系统繁忙,亲稍后再试") + } + // 数据转换 + for i := 0; i < len(users); i++ { + var userVO resp2.UserApplyResp + _ = copier.Copy(&userVO, &users[i]) + userVO.Msg = userApplys[i].Msg + userVO.Status = userApplys[i].Status + usersVO = append(usersVO, userVO) + } + pageResp.Data = usersVO + + resp.SuccessResponse(c, pageResp) +} + +// UnreadApplyNum 好友申请未读数量 +// +// @Summary 好友申请未读数量 +// @Success 200 {object} resp.ResponseData "成功" +// @Failure 500 {object} resp.ResponseData "内部错误" +// @Router /api/public/login [post] +func UnreadApplyNum(c *gin.Context) { + ctx := context.Background() + + uid := c.GetInt64("uid") + + ua := query.UserApply + + // 获取 UserApply 表中 TargetID 等于 uid(登录用户ID)的用户ID集合 + num, err := ua.WithContext(ctx).Where(ua.TargetID.Eq(uid), ua.ReadStatus.Eq(enum.NO), ua.Status.Eq(enum.NO)).Count() + if err != nil { + log.Printf("DB excete Sql happen [ERROR], err msg is : %v", err) + resp.ErrorResponse(c, "系统繁忙,亲稍后再试") + c.Abort() + return + } + resp.SuccessResponse(c, num) +} + +// GetContactList 获取好友列表 +// +// @Summary 获取好友列表 +// @Produce json +// @Success 200 {object} resp.ResponseData "成功" +// @Failure 500 {object} resp.ResponseData "内部错误" +// @Router /api/contact/getContactList [get] +func GetContactList(c *gin.Context) { + ctx := context.Background() + uid := c.GetInt64("uid") + // 获取好友列表 + userFriend := query.UserFriend + // 获取 UserFriend 表中 uid = uid 的好友的uid组成的集合 + // select friend_uid from user_friend where uid = ? + friendIDs, err := userFriend.WithContext(ctx).Select(userFriend.FriendUID).Where(userFriend.UID.Eq(uid)).Find() + // 将friendIDs转换为切片 + // TODO 实现游标翻页 + friendIDsSlice := make([]int64, 0) + for _, id := range friendIDs { + friendIDsSlice = append(friendIDsSlice, id.FriendUID) + } + if err != nil { + // todo 添加日志系统 + log.Printf("SQL查询错误, 错误信息为 : %v", err) + resp.ErrorResponse(c, "出现错误,未能获取联系人列表") + return + } + + // 获取好友信息 + users := query.User + // select id , name , avatar from user where id in (...) and status = 0 + friendList, err := users.WithContext(ctx).Select(users.ID, users.Name, users.Avatar).Where(users.ID.In(friendIDsSlice...), users.Status.Eq(0)).Find() + if err != nil { + // todo 添加日志系统 + log.Printf("SQL查询错误, 错误信息为 : %v", err) + resp.ErrorResponse(c, "出现错误,未能获取联系人信息") + return + } + + // 数据转换 + friendListVO := make([]resp2.UserContactResp, 0) + _ = copier.Copy(&friendListVO, &friendList) + resp.SuccessResponse(c, friendListVO) +}