go_dreamfactory/modules/friend/api_cross_agree.go

112 lines
2.9 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"google.golang.org/protobuf/proto"
)
//单个/批量同意
func (this *apiComp) AgreeCheck(session comm.IUserSession, req *pb.FriendAgreeReq) (code pb.ErrorCode) {
if len(req.FriendIds) == 0 {
code = pb.ErrorCode_ReqParameterError
this.moduleFriend.Error("好友审批同意参数错误", log.Fields{"uid": session.GetUserId(), "params": req})
}
return
}
func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (code pb.ErrorCode, data proto.Message) {
if code = this.AgreeCheck(session, req); code != pb.ErrorCode_Success {
return
}
var (
err error
self *pb.DBFriend
target *pb.DBFriend
optNum int32
)
uid := session.GetUserId()
//获取玩家自己好友数据
self = this.moduleFriend.modelFriend.GetFriend(uid)
if self == nil {
code = pb.ErrorCode_FriendSelfNoData
return
}
//同意的好友
agreeIds := []string{}
for _, friendId := range req.FriendIds {
// 验证friendId是否有效
user := this.moduleFriend.ModuleUser.GetUser(friendId)
if user == nil {
continue
}
if _, ok := utils.Find(self.FriendIds, friendId); !ok {
//不在好友列表中就加入
agreeIds = append(agreeIds, friendId)
}
}
//将目标加入到自己的好友列表中
for _, userId := range agreeIds {
if _, ok := utils.Find(self.FriendIds, userId); !ok {
if self.FriendIds == nil {
self.FriendIds = []string{}
}
self.FriendIds = append(self.FriendIds, userId)
}
//双向添加:将自己加入到申请人的好友列表中
target = this.moduleFriend.modelFriend.GetFriend(userId)
if target == nil {
code = pb.ErrorCode_FriendTargetNoData
return
}
if _, ok := utils.Find(target.FriendIds, self.Uid); !ok {
if target.FriendIds == nil {
target.FriendIds = []string{}
}
target.FriendIds = append(target.FriendIds, self.Uid)
}
err = this.moduleFriend.modelFriend.Change(target.Uid, map[string]interface{}{
"friendIds": target.FriendIds,
})
if err != nil {
code = pb.ErrorCode_DBError
this.moduleFriend.Error("好友审批同意", log.Fields{"uid": uid, "params": req.FriendIds, "err": err.Error()})
return
}
//将目标从申请列表中删除
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
optNum++
}
//更新
err = this.moduleFriend.modelFriend.Change(self.Uid, map[string]interface{}{
"applyIds": self.ApplyIds,
"friendIds": self.FriendIds,
})
if err != nil {
code = pb.ErrorCode_DBError
return
}
// 拥有xx个好友
this.moduleFriend.ModuleRtask.SendToRtask(session, comm.Rtype10, int32(len(agreeIds)))
resp := &pb.FriendAgreeResp{
Num: optNum,
}
if err := session.SendMsg(string(this.moduleFriend.GetType()), FriendSubTypeAgree, resp); err != nil {
code = pb.ErrorCode_SystemError
return
}
return
}