go_dreamfactory/modules/friend/api_agree.go
2022-06-21 11:58:46 +08:00

105 lines
2.5 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
)
func (this *ApiComp) Agree_Check(session comm.IUserSession, req *pb.Friend_Agree_Req) (chk map[string]interface{}, code comm.ErrorCode) {
chk = make(map[string]interface{})
var err error
self := &pb.DB_FriendData{UId: session.GetUserId()}
//获取玩家自己好友数据
err = this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfNoData}
return
}
//同意的好友
agreeIds := []string{}
for _, friendId := range req.FriendIds {
if _, ok := utils.Find(self.FriendIds, friendId); !ok {
//不在好友列表中就加入
agreeIds = append(agreeIds, friendId)
}
}
chk["agreeIds"] = agreeIds
chk["self"] = self
return
}
//单个/批量同意
func (this *ApiComp) Agree(session comm.IUserSession, chk map[string]interface{}, req *pb.Friend_Agree_Req) (code pb.ErrorCode) {
var (
self *pb.DB_FriendData
rsp *pb.Friend_Agree_Rsp
optNum int32
)
defer func() {
rsp = &pb.Friend_Agree_Rsp{
Num: optNum,
}
err := session.SendMsg(string(this.module.GetType()), Friend_SubType_Agree, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
if v, ok := chk["self"]; !ok {
code = pb.ErrorCode_FriendTargetNoData
return
} else {
self = v.(*pb.DB_FriendData)
}
if agreeIds, ok := chk["agreeIds"]; ok {
//将目标加入到自己的好友列表中
for _, userId := range agreeIds.([]string) {
if _, ok := utils.Find(self.FriendIds, userId); !ok {
if self.FriendIds == nil {
self.FriendIds = []string{}
}
self.FriendIds = append(self.FriendIds, userId)
}
//双向添加:将自己加入到申请人的好友列表中
target := &pb.DB_FriendData{}
err := this.module.model_friend.GetObj(userId, target)
if target == nil || err != nil {
code = pb.ErrorCode_FriendTargetNoData
}
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.module.model_friend.SetObj(target.UId, target)
if err != nil {
code = pb.ErrorCode_DBError
}
//将目标从申请列表中删除
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
optNum++
}
//更新
err := this.module.model_friend.SetObj(self.UId, self)
if err != nil {
code = pb.ErrorCode_DBError
return
}
}
return
}