修改friend接口

This commit is contained in:
zhaocy 2022-06-16 15:12:45 +08:00
parent 6943458a0d
commit 24749bb718
7 changed files with 191 additions and 89 deletions

View File

@ -6,7 +6,30 @@ import (
"go_dreamfactory/utils"
)
func (this *ApiComp) Agree_Check(session comm.IUserSession, req *pb.FriendAgreeReq) (result map[string]interface{}, code pb.ErrorCode) {
func (this *ApiComp) Agree_Check(session comm.IUserSession, req *pb.FriendAgreeReq) (chk map[string]interface{}, code pb.ErrorCode) {
chk = make(map[string]interface{})
var err error
self := &pb.DB_FriendData{UserId: session.GetUserId()}
//获取玩家自己好友数据
err = this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
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
}
@ -28,39 +51,49 @@ func (this *ApiComp) Agree(session comm.IUserSession, chk map[string]interface{}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Agree, code, rsp)
}()
err = this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
if v, ok := chk["self"]; !ok {
code = pb.ErrorCode_FriendTargetNoData
return
} else {
self = v.(*pb.DB_FriendData)
}
//将申请人加入到自己的好友列表中
for _, userId := range req.FriendIds {
if _, ok := utils.Find(self.FriendIds, userId); !ok {
self.FriendIds = append(self.FriendIds, userId)
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.UserId); !ok {
if target.FriendIds == nil {
target.FriendIds = []string{}
}
target.FriendIds = append(target.FriendIds, self.UserId)
}
err = this.module.model_friend.SetObj(target.UserId, target, false, true)
if err != nil {
code = pb.ErrorCode_DBError
}
//将目标从申请列表中删除
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
optNum++
}
//更新
return this.module.model_friend.SetObj(self.UserId, self, false, true)
}
//将自己加入到申请人的好友列表中
for _, userId := range req.FriendIds {
var target *pb.DB_FriendData
err2 := this.module.model_friend.GetObj(userId, target)
if target == nil || err2 != nil {
code = pb.ErrorCode_FriendTargetNoData
continue
}
if _, ok := utils.Find(target.FriendIds, self.UserId); !ok {
target.FriendIds = append(target.FriendIds, self.UserId)
}
this.module.model_friend.SetObj(target.UserId, target, false, true)
}
//将申请人从申请列表中删除
for _, userId := range req.FriendIds {
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
optNum++
}
//更新
return this.module.model_friend.SetObj(self.UserId, self, false, true)
return nil
}

View File

@ -8,38 +8,19 @@ import (
)
func (this *ApiComp) Apply_Check(session comm.IUserSession, req *pb.FriendApplyReq) (chk map[string]interface{}, code pb.ErrorCode) {
return
}
//好友申请
func (this *ApiComp) Apply(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendApplyReq) (err error) {
var (
code pb.ErrorCode
self *pb.DB_FriendData
target *pb.DB_FriendData
rsp *pb.FriendApplyRsp
)
defer func() {
utils.TraceFunc(session.GetUserId(), string(this.module.GetType()), Friend_SubType_Apply, req, rsp)
}()
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.FriendApplyRsp{
UserId: session.GetUserId(),
FriendId: req.FriendId,
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Apply, code, rsp)
}()
chk = make(map[string]interface{})
var err error
self := &pb.DB_FriendData{UserId: session.GetUserId()}
target := &pb.DB_FriendData{UserId: req.FriendId}
//获取玩家自己好友数据
err = this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
return
}
//获取好友数据
err = this.module.model_friend.GetObj(req.FriendId, target)
if target == nil || err != nil {
code = pb.ErrorCode_FriendTargetNoData
@ -92,12 +73,47 @@ func (this *ApiComp) Apply(session comm.IUserSession, chk map[string]interface{}
return
}
chk["self"] = self
chk["target"] = target
return
}
//好友申请
func (this *ApiComp) Apply(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendApplyReq) (err error) {
var (
code pb.ErrorCode
target *pb.DB_FriendData
rsp *pb.FriendApplyRsp
)
defer func() {
utils.TraceFunc(session.GetUserId(), string(this.module.GetType()), Friend_SubType_Apply, req, rsp)
}()
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.FriendApplyRsp{
UserId: session.GetUserId(),
FriendId: req.FriendId,
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Apply, code, rsp)
}()
if v, ok := chk["target"]; !ok {
code = pb.ErrorCode_FriendTargetNoData
return
} else {
target = v.(*pb.DB_FriendData)
}
//将自己加入到目标用户的申请列表中
target.ApplyIds = append(target.ApplyIds, self.UserId)
err = this.module.model_friend.SetObj(req.FriendId, &pb.DB_FriendData{
UserId: req.FriendId,
ApplyIds: target.ApplyIds,
}, false, true)
if target.ApplyIds == nil {
target.ApplyIds = []string{}
}
target.ApplyIds = append(target.ApplyIds, session.GetUserId())
err = this.module.model_friend.SetObj(req.FriendId, target, false, true)
if err != nil {
log.Errorf("firend Apply err:%v", err)

View File

@ -7,6 +7,28 @@ import (
)
func (this *ApiComp) Refuse_Check(session comm.IUserSession, req *pb.FriendRefuseReq) (chk map[string]interface{}, code pb.ErrorCode) {
chk = make(map[string]interface{})
var err error
self := &pb.DB_FriendData{UserId: session.GetUserId()}
//获取玩家自己好友数据
err = this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
return
}
//拒绝的Ids
refuseIds := []string{}
for _, friendId := range req.FriendIds {
if _, ok := utils.Find(self.ApplyIds, friendId); ok {
refuseIds = append(refuseIds, friendId)
}
}
chk["self"] = self
chk["refuseIds"] = refuseIds
return
}
@ -28,18 +50,24 @@ func (this *ApiComp) Refuse(session comm.IUserSession, chk map[string]interface{
session.SendMsg(string(this.module.GetType()), Friend_SubType_Refuse, code, rsp)
}()
err = this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
if v, ok := chk["self"]; !ok {
code = pb.ErrorCode_FriendTargetNoData
return
}
} else {
self = v.(*pb.DB_FriendData)
//将申请人从申请列表中删除
for _, userId := range req.FriendIds {
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
optNum++
}
if v, ok := chk["refuseIds"]; ok {
//将申请人从申请列表中删除
for _, userId := range v.([]string) {
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
optNum++
}
//更新
if optNum > 0 {
return this.module.model_friend.SetObj(self.UserId, self, false, true)
}
//更新
return this.module.model_friend.SetObj(self.UserId, self, false, true)
}
}
return nil
}

View File

@ -22,7 +22,7 @@ type ModelFriend struct {
func (this *ModelFriend) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.Model_Comp.Init(service, module, comp, options)
this.Prefix = "friend"
this.TableName = "friend"
return
}

View File

@ -14,6 +14,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"google.golang.org/protobuf/proto"
)
@ -23,9 +24,9 @@ DB组件也封装进来
*/
type Model_Comp struct {
cbase.ModuleCompBase
Redis redis.ISys
DB mgo.ISys
Prefix string //redis key前缀
Redis redis.ISys
DB mgo.ISys
TableName string //redis key前缀
}
const (
@ -98,8 +99,9 @@ func (this *Model_Comp) UpdateModelLogs(table string, uID string, where bson.M,
return err
}
//设置缓存JSON格式数据
func (this *Model_Comp) SetObj(uid string, data proto.Message, isnew, islog bool) error {
err := this.Redis.Set(fmt.Sprintf("%s:%s", this.Prefix, uid), data, 0)
err := this.Redis.Set(fmt.Sprintf("%s:%s", this.TableName, uid), data, 0)
if err != nil {
log.Errorf("set err:%v", err)
return err
@ -107,9 +109,9 @@ func (this *Model_Comp) SetObj(uid string, data proto.Message, isnew, islog bool
if islog {
if isnew {
return this.InsertModelLogs(this.Prefix, uid, data)
return this.InsertModelLogs(this.TableName, uid, data)
} else {
return this.UpdateModelLogs(this.Prefix, uid, bson.M{"_id": uid}, data)
return this.UpdateModelLogs(this.TableName, uid, bson.M{"_id": uid}, data)
}
}
return nil
@ -118,7 +120,7 @@ func (this *Model_Comp) SetObj(uid string, data proto.Message, isnew, islog bool
//更新缓存字段
//isnew true insertlog , false updatelog
func (this *Model_Comp) Set(uid string, v map[string]interface{}, isnew, islog bool) error {
err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.Prefix, uid), v)
err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.TableName, uid), v)
if err != nil {
log.Errorf("set err:%v", err)
return err
@ -126,35 +128,57 @@ func (this *Model_Comp) Set(uid string, v map[string]interface{}, isnew, islog b
if islog {
if isnew {
return this.InsertModelLogs(this.Prefix, uid, v)
return this.InsertModelLogs(this.TableName, uid, v)
} else {
return this.UpdateModelLogs(this.Prefix, uid, bson.M{"_id": uid}, v)
return this.UpdateModelLogs(this.TableName, uid, bson.M{"uid": uid}, v)
}
}
return nil
}
func (this *Model_Comp) GetObj(uid string, v interface{}) error {
return this.Redis.Get(fmt.Sprintf("%s:%s", this.Prefix, uid), v)
//获取缓存JSON数据
func (this *Model_Comp) GetObj(uid string, v proto.Message) error {
err := this.Redis.Get(fmt.Sprintf("%s:%s", this.TableName, uid), v)
if err != nil {
if err == redis.RedisNil {
//query from mgo
err = this.DB.FindOne(core.SqlTable(this.TableName), bson.M{"_id": uid}).Decode(v)
if err != nil {
//no record
if err == mongo.ErrNoDocuments {
_, err = this.DB.InsertOne(core.SqlTable(this.TableName), v)
if err != nil {
log.Errorf("insert err: %v", err)
return err
}
//set cache
return this.SetObj(uid, v, true, false)
}
}
} else {
log.Errorf("get cache err: %v", err)
}
}
return err
}
//获取对象数据
func (this *Model_Comp) Get(uid string, v reflect.Type, fields ...string) ([]interface{}, error) {
return this.Redis.HMGet(fmt.Sprintf("%s:%s", this.Prefix, uid), v, fields...)
return this.Redis.HMGet(fmt.Sprintf("%s:%s", this.TableName, uid), v, fields...)
}
//获取字段数据
func (this *Model_Comp) GetField(uid string, field string, v interface{}) error {
return this.Redis.HGet(fmt.Sprintf("%s:%s", this.Prefix, uid), field, v)
return this.Redis.HGet(fmt.Sprintf("%s:%s", this.TableName, uid), field, v)
}
//删除一条数据
func (this *Model_Comp) Del(uid string) error {
err := this.Redis.HDel(fmt.Sprintf("%s:%s", this.Prefix, uid))
err := this.Redis.HDel(fmt.Sprintf("%s:%s", this.TableName, uid))
if err != nil {
log.Errorf("del err:%v", err)
return err
}
return this.DeleteModelLogs(this.Prefix, uid, bson.M{"_id": uid})
return this.DeleteModelLogs(this.TableName, uid, bson.M{"_id": uid})
}

View File

@ -39,11 +39,13 @@ func (this *Api_Comp) Register(c *engine.Context) {
if err == nil {
err := this.module.modelUser.User_Create(&pb.DB_UserData{
Binduid: req.Account,
Sid: req.Sid,
})
if err != nil {
log.Errorf("create user err: %v", err)
rsp.Code = pb.ErrorCode_SqlExecutionError
}
rsp.Account = req.Account
rsp.Code = pb.ErrorCode_Success
} else {
rsp.Code = pb.ErrorCode_ReqParameterError

View File

@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"go_dreamfactory/modules/dbservice"
"go_dreamfactory/modules/friend"
"go_dreamfactory/modules/mail"
"go_dreamfactory/modules/pack"
@ -41,7 +40,7 @@ func main() {
pack.NewModule(),
mail.NewModule(),
friend.NewModule(),
dbservice.NewModule(),
// dbservice.NewModule(),
)
}