go_dreamfactory/modules/friend/api_cross_search.go
2023-04-14 12:21:58 +08:00

69 lines
1.6 KiB
Go

package friend
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
)
//搜索
func (this *apiComp) SearchCheck(session comm.IUserSession, req *pb.FriendSearchReq) (code pb.ErrorCode) {
if req.NickName == "" {
code = pb.ErrorCode_FriendSearchNameEmpty
this.moduleFriend.Error("参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})
return
}
return
}
func (this *apiComp) Search(session comm.IUserSession, req *pb.FriendSearchReq) (code pb.ErrorCode, data *pb.ErrorData) {
if code = this.SearchCheck(session, req); code != pb.ErrorCode_Success {
return
}
uid := session.GetUserId()
resp := &pb.FriendSearchResp{}
users, err := this.moduleFriend.ModuleUser.SearchRmoteUser(req.NickName)
if err != nil {
code = pb.ErrorCode_DBError
this.moduleFriend.Error("搜索玩家",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "params", Value: req.NickName},
log.Field{Key: "err", Value: err.Error()},
)
return
}
for _, u := range users {
// 排除自己
if u.Uid == session.GetUserId() {
continue
}
base := &pb.FriendBase{
UserId: u.Uid,
NickName: u.Name,
Level: u.Lv,
Avatar: u.Avatar,
ServerId: u.Sid,
}
target := this.moduleFriend.modelFriend.GetFriend(u.Uid)
if target == nil {
continue
}
if _, ok := utils.Findx(target.ApplyIds, uid); ok {
base.IsApplied = true
}
resp.Friends = append(resp.Friends, base)
}
if err := session.SendMsg(string(this.moduleFriend.GetType()), FriendSubTypeSearch, resp); err != nil {
code = pb.ErrorCode_SystemError
}
return
}