拆分模块方法
This commit is contained in:
parent
e387e9b8e5
commit
b053cde0a3
@ -1,79 +0,0 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/lego/sys/redis"
|
||||
"go_dreamfactory/sys/cache"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Action string
|
||||
|
||||
const (
|
||||
INSERT Action = "insert"
|
||||
UPDATE Action = "update"
|
||||
DELETE Action = "delete"
|
||||
)
|
||||
|
||||
type DBModel struct {
|
||||
*MComp_DBComp
|
||||
pbData proto.Message
|
||||
prefix string //rediskey前缀
|
||||
tableName string
|
||||
}
|
||||
|
||||
func NewDBModel(c *MComp_DBComp) *DBModel {
|
||||
return &DBModel{MComp_DBComp: c}
|
||||
}
|
||||
|
||||
func (m *DBModel) SetData(model proto.Message) *DBModel {
|
||||
m.pbData = model
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *DBModel) GetData() proto.Message {
|
||||
return m.pbData
|
||||
}
|
||||
|
||||
func (m *DBModel) SetPrefix(prefix string) {
|
||||
m.prefix = prefix
|
||||
}
|
||||
|
||||
func (m *DBModel) SetTableName(tableName string) {
|
||||
m.tableName = tableName
|
||||
}
|
||||
|
||||
func (m *DBModel) OnChange(key string) {
|
||||
m.MComp_DBComp.InsertModelLogs(m.tableName, key, m.pbData)
|
||||
}
|
||||
|
||||
func (m *DBModel) Set(key string, act Action) {
|
||||
|
||||
//调用写入mongo日志接口
|
||||
switch act {
|
||||
case INSERT:
|
||||
m.MComp_DBComp.InsertModelLogs(m.tableName, key, m.pbData)
|
||||
case UPDATE:
|
||||
m.MComp_DBComp.UpdateModelLogs(m.tableName, key, bson.M{"_id": key}, m.pbData)
|
||||
case DELETE:
|
||||
m.MComp_DBComp.DeleteModelLogs(m.tableName, key, bson.M{"_id": key})
|
||||
}
|
||||
}
|
||||
|
||||
//获取数据
|
||||
func (m *DBModel) Get(uid string) {
|
||||
if m.prefix == "" {
|
||||
log.Errorf("get data err,because prefix not setting")
|
||||
return
|
||||
}
|
||||
err := cache.Redis().Get(fmt.Sprintf("%s:%s", m.prefix, uid), m.pbData)
|
||||
if err != nil {
|
||||
if err.Error() != redis.RedisNil.Error() {
|
||||
log.Errorf("err:%v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
29
modules/friend/api.go
Normal file
29
modules/friend/api.go
Normal file
@ -0,0 +1,29 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/modules"
|
||||
)
|
||||
|
||||
const (
|
||||
Friend_SubType_List = "list"
|
||||
Friend_SubType_Apply = "apply"
|
||||
Friend_SubType_ApplyList = "applylist"
|
||||
Friend_SubType_AddBlack = "addblack"
|
||||
Friend_SubType_DelBlack = "delblack"
|
||||
Friend_SubType_Blacklist = "blacklist"
|
||||
Friend_SubType_Agree = "agree"
|
||||
Friend_SubType_Refuse = "refuse"
|
||||
Friend_SubType_Search = "search"
|
||||
)
|
||||
|
||||
type ApiComp struct {
|
||||
modules.MComp_GateComp
|
||||
module *Friend
|
||||
}
|
||||
|
||||
func (this *ApiComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
this.MComp_GateComp.Init(service, module, comp, options)
|
||||
this.module = module.(*Friend)
|
||||
return
|
||||
}
|
70
modules/friend/api_addblack.go
Normal file
70
modules/friend/api_addblack.go
Normal file
@ -0,0 +1,70 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/utils"
|
||||
)
|
||||
|
||||
//加入黑名单
|
||||
func (this *ApiComp) Addblack(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackAddReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
target *pb.Cache_FriendData
|
||||
rsp *pb.FriendBlackAddRsp
|
||||
blackNumMax = 50 //TODO 从配置中读取
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendBlackAddRsp{
|
||||
FriendId: req.FriendId,
|
||||
UserId: session.GetUserId(),
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
target, err = cache.Defsys.Friend_Get(req.FriendId)
|
||||
if target == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendTargetNoData
|
||||
return
|
||||
}
|
||||
|
||||
//判断目标是否在好友列表里面
|
||||
if _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
|
||||
code = pb.ErrorCode_FriendSelfBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
// 判断自己是否在对方的黑名单中
|
||||
if _, ok := utils.Find(target.BlackIds, self.UserId); ok {
|
||||
code = pb.ErrorCode_FriendTargetBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否黑名单人数已满
|
||||
if len(self.BlackIds) >= blackNumMax {
|
||||
code = pb.ErrorCode_FriendBlackMax
|
||||
return
|
||||
}
|
||||
|
||||
//将目标加入黑名单
|
||||
self.BlackIds = append(self.BlackIds, req.FriendId)
|
||||
//更新黑名单
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
72
modules/friend/api_agree.go
Normal file
72
modules/friend/api_agree.go
Normal file
@ -0,0 +1,72 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/utils"
|
||||
)
|
||||
|
||||
//单个/批量同意
|
||||
func (this *ApiComp) Agree(ctx context.Context, session comm.IUserSession, req *pb.FriendAgreeReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendAgreeRsp
|
||||
optNum int32
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendAgreeRsp{
|
||||
Num: optNum,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Agree, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//将申请人加入到自己的好友列表中
|
||||
for _, userId := range req.FriendIds {
|
||||
if _, ok := utils.Find(self.FriendIds, userId); !ok {
|
||||
self.FriendIds = append(self.FriendIds, userId)
|
||||
}
|
||||
}
|
||||
|
||||
//将自己加入到申请人的好友列表中
|
||||
for _, userId := range req.FriendIds {
|
||||
target, err2 := cache.Defsys.Friend_Get(userId)
|
||||
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)
|
||||
}
|
||||
err = cache.Defsys.Friend_Update(target)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//将申请人从申请列表中删除
|
||||
for _, userId := range req.FriendIds {
|
||||
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
|
||||
optNum++
|
||||
}
|
||||
|
||||
//更新
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
107
modules/friend/api_apply.go
Normal file
107
modules/friend/api_apply.go
Normal file
@ -0,0 +1,107 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/utils"
|
||||
)
|
||||
|
||||
//好友申请
|
||||
func (this *ApiComp) Apply(ctx context.Context, session comm.IUserSession, req *pb.FriendApplyReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
target *pb.Cache_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)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
target, err = cache.Defsys.Friend_Get(req.FriendId)
|
||||
if target == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendTargetNoData
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否是自己
|
||||
if req.FriendId == session.GetUserId() {
|
||||
code = pb.ErrorCode_FriendNotSelf
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否超过最大好友数量
|
||||
//TODO 最大数从全局配置中获取
|
||||
var max int32 = 50
|
||||
total := cache.Defsys.Friend_Total(session.GetUserId())
|
||||
if total >= max {
|
||||
code = pb.ErrorCode_FriendSelfMax
|
||||
return
|
||||
}
|
||||
|
||||
//判断对方是否也超过最大好友数量
|
||||
ttotal := cache.Defsys.Friend_Total(req.FriendId)
|
||||
if ttotal >= max {
|
||||
code = pb.ErrorCode_FriendTargetMax
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否是好友
|
||||
if _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
|
||||
code = pb.ErrorCode_FriendYet
|
||||
return
|
||||
}
|
||||
|
||||
//判断自己是否在目标用户的申请列表中
|
||||
if _, ok := utils.Find(target.ApplyIds, self.UserId); ok {
|
||||
code = pb.ErrorCode_FriendApplyYet
|
||||
return
|
||||
}
|
||||
|
||||
//判断目标用户是否在黑名单中
|
||||
if _, ok := utils.Find(self.BlackIds, req.FriendId); ok {
|
||||
code = pb.ErrorCode_FriendSelfBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否在对方的黑名单中
|
||||
if _, ok := utils.Find(target.BlackIds, self.UserId); ok {
|
||||
code = pb.ErrorCode_FriendTargetBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
//将自己加入到目标用户的申请列表中
|
||||
target.ApplyIds = append(target.ApplyIds, self.UserId)
|
||||
err = cache.Defsys.Friend_Update(&pb.Cache_FriendData{
|
||||
UserId: req.FriendId,
|
||||
ApplyIds: target.ApplyIds,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("firend Apply err:%v", err)
|
||||
code = pb.ErrorCode_FriendApplyError
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
42
modules/friend/api_applylist.go
Normal file
42
modules/friend/api_applylist.go
Normal file
@ -0,0 +1,42 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
)
|
||||
|
||||
//申请列表
|
||||
func (this *ApiComp) ApplyList(ctx context.Context, session comm.IUserSession, req *pb.FriendApplyListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendApplyListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendApplyListRsp{
|
||||
List: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_ApplyList, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.ApplyIds {
|
||||
//TODO 组装FriendBase明细数据
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
42
modules/friend/api_blacklist.go
Normal file
42
modules/friend/api_blacklist.go
Normal file
@ -0,0 +1,42 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
)
|
||||
|
||||
//黑名单
|
||||
func (this *ApiComp) Blacklist(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendBlackListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendBlackListRsp{
|
||||
Friends: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Blacklist, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.BlackIds {
|
||||
//TODO 完善FriendBase信息
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
12
modules/friend/api_del.go
Normal file
12
modules/friend/api_del.go
Normal file
@ -0,0 +1,12 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
)
|
||||
|
||||
//删除好友
|
||||
func (this *ApiComp) Del(ctx context.Context, session comm.IUserSession, req *pb.FriendDelReq) error {
|
||||
return nil
|
||||
}
|
42
modules/friend/api_delblack.go
Normal file
42
modules/friend/api_delblack.go
Normal file
@ -0,0 +1,42 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/utils"
|
||||
)
|
||||
|
||||
//删除黑名单
|
||||
func (this *ApiComp) delblack(ctx context.Context, session comm.IUserSession, req *pb.FriendDelBlackReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendDelBlackRsp
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendDelBlackRsp{
|
||||
FriendId: req.FriendId,
|
||||
UserId: session.GetUserId(),
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, code, rsp)
|
||||
}()
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//从黑名单列表中删除目标
|
||||
self.BlackIds = utils.DeleteString(self.BlackIds, req.FriendId)
|
||||
//更新黑名单
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
41
modules/friend/api_list.go
Normal file
41
modules/friend/api_list.go
Normal file
@ -0,0 +1,41 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
)
|
||||
|
||||
//好友列表
|
||||
func (this *ApiComp) List(ctx context.Context, session comm.IUserSession, req *pb.FriendListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendListRsp{
|
||||
List: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_List, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.FriendIds {
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
48
modules/friend/api_refuse.go
Normal file
48
modules/friend/api_refuse.go
Normal file
@ -0,0 +1,48 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/utils"
|
||||
)
|
||||
|
||||
//单个/批量拒绝
|
||||
func (this *ApiComp) Refuse(ctx context.Context, session comm.IUserSession, req *pb.FriendRefuseReq) (err error) {
|
||||
//将申请人从申请列表中删除
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendAgreeRsp
|
||||
optNum int32
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendAgreeRsp{
|
||||
Num: optNum,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Refuse, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//将申请人从申请列表中删除
|
||||
for _, userId := range req.FriendIds {
|
||||
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
|
||||
optNum++
|
||||
}
|
||||
|
||||
//更新
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
33
modules/friend/api_search.go
Normal file
33
modules/friend/api_search.go
Normal file
@ -0,0 +1,33 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/pb"
|
||||
)
|
||||
|
||||
//搜索
|
||||
func (this *ApiComp) Search(ctx context.Context, session comm.IUserSession, req *pb.FriendSearchReq) error {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
rsp *pb.FriendSearchRsp
|
||||
friend *pb.FriendBase
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendSearchRsp{
|
||||
Friend: friend,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Search, code, rsp)
|
||||
}()
|
||||
|
||||
user :=this.module.model_friend.Frined_FindCond(req.NickName)
|
||||
if user != nil {
|
||||
friend = &pb.FriendBase{
|
||||
UserId: user.Uid,
|
||||
NickName: user.Name,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,471 +0,0 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
"go_dreamfactory/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
Friend_SubType_List = "list"
|
||||
Friend_SubType_Apply = "apply"
|
||||
Friend_SubType_ApplyList = "applylist"
|
||||
Friend_SubType_AddBlack = "addblack"
|
||||
Friend_SubType_DelBlack = "delblack"
|
||||
Friend_SubType_Blacklist = "blacklist"
|
||||
Friend_SubType_Agree = "agree"
|
||||
Friend_SubType_Refuse = "refuse"
|
||||
Friend_SubType_Search = "search"
|
||||
)
|
||||
|
||||
type FriendComp struct {
|
||||
modules.MComp_GateComp
|
||||
module *Friend
|
||||
}
|
||||
|
||||
func (this *FriendComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
this.MComp_GateComp.Init(service, module, comp, options)
|
||||
this.module = module.(*Friend)
|
||||
return
|
||||
}
|
||||
|
||||
//搜索
|
||||
func (this *FriendComp) Search(ctx context.Context, session comm.IUserSession, req *pb.FriendSearchReq) error {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
rsp *pb.FriendSearchRsp
|
||||
friend *pb.FriendBase
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendSearchRsp{
|
||||
Friend: friend,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Search, code, rsp)
|
||||
}()
|
||||
|
||||
user := db.Defsys.Frined_FindCond(req.NickName)
|
||||
if user != nil {
|
||||
friend = &pb.FriendBase{
|
||||
UserId: user.Uid,
|
||||
NickName: user.Name,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//好友申请
|
||||
func (this *FriendComp) Apply(ctx context.Context, session comm.IUserSession, req *pb.FriendApplyReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
target *pb.Cache_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)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
target, err = cache.Defsys.Friend_Get(req.FriendId)
|
||||
if target == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendTargetNoData
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否是自己
|
||||
if req.FriendId == session.GetUserId() {
|
||||
code = pb.ErrorCode_FriendNotSelf
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否超过最大好友数量
|
||||
//TODO 最大数从全局配置中获取
|
||||
var max int32 = 50
|
||||
total := cache.Defsys.Friend_Total(session.GetUserId())
|
||||
if total >= max {
|
||||
code = pb.ErrorCode_FriendSelfMax
|
||||
return
|
||||
}
|
||||
|
||||
//判断对方是否也超过最大好友数量
|
||||
ttotal := cache.Defsys.Friend_Total(req.FriendId)
|
||||
if ttotal >= max {
|
||||
code = pb.ErrorCode_FriendTargetMax
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否是好友
|
||||
if _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
|
||||
code = pb.ErrorCode_FriendYet
|
||||
return
|
||||
}
|
||||
|
||||
//判断自己是否在目标用户的申请列表中
|
||||
if _, ok := utils.Find(target.ApplyIds, self.UserId); ok {
|
||||
code = pb.ErrorCode_FriendApplyYet
|
||||
return
|
||||
}
|
||||
|
||||
//判断目标用户是否在黑名单中
|
||||
if _, ok := utils.Find(self.BlackIds, req.FriendId); ok {
|
||||
code = pb.ErrorCode_FriendSelfBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
//判断是否在对方的黑名单中
|
||||
if _, ok := utils.Find(target.BlackIds, self.UserId); ok {
|
||||
code = pb.ErrorCode_FriendTargetBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
//将自己加入到目标用户的申请列表中
|
||||
target.ApplyIds = append(target.ApplyIds, self.UserId)
|
||||
err = cache.Defsys.Friend_Update(&pb.Cache_FriendData{
|
||||
UserId: req.FriendId,
|
||||
ApplyIds: target.ApplyIds,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("firend Apply err:%v", err)
|
||||
code = pb.ErrorCode_FriendApplyError
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//申请列表
|
||||
func (this *FriendComp) ApplyList(ctx context.Context, session comm.IUserSession, req *pb.FriendApplyListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendApplyListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendApplyListRsp{
|
||||
List: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_ApplyList, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.ApplyIds {
|
||||
//TODO 组装FriendBase明细数据
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//删除好友
|
||||
func (this *FriendComp) Del(ctx context.Context, session comm.IUserSession, req *pb.FriendDelReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//好友列表
|
||||
func (this *FriendComp) List(ctx context.Context, session comm.IUserSession, req *pb.FriendListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendListRsp{
|
||||
List: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_List, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.FriendIds {
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//单个/批量同意
|
||||
func (this *FriendComp) Agree(ctx context.Context, session comm.IUserSession, req *pb.FriendAgreeReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendAgreeRsp
|
||||
optNum int32
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendAgreeRsp{
|
||||
Num: optNum,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Agree, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//将申请人加入到自己的好友列表中
|
||||
for _, userId := range req.FriendIds {
|
||||
if _, ok := utils.Find(self.FriendIds, userId); !ok {
|
||||
self.FriendIds = append(self.FriendIds, userId)
|
||||
}
|
||||
}
|
||||
|
||||
//将自己加入到申请人的好友列表中
|
||||
for _, userId := range req.FriendIds {
|
||||
target, err2 := cache.Defsys.Friend_Get(userId)
|
||||
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)
|
||||
}
|
||||
err = cache.Defsys.Friend_Update(target)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//将申请人从申请列表中删除
|
||||
for _, userId := range req.FriendIds {
|
||||
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
|
||||
optNum++
|
||||
}
|
||||
|
||||
//更新
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//单个/批量拒绝
|
||||
func (this *FriendComp) Refuse(ctx context.Context, session comm.IUserSession, req *pb.FriendRefuseReq) (err error) {
|
||||
//将申请人从申请列表中删除
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendAgreeRsp
|
||||
optNum int32
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendAgreeRsp{
|
||||
Num: optNum,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Refuse, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//将申请人从申请列表中删除
|
||||
for _, userId := range req.FriendIds {
|
||||
self.ApplyIds = utils.DeleteString(self.ApplyIds, userId)
|
||||
optNum++
|
||||
}
|
||||
|
||||
//更新
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//赠送或接收
|
||||
func (this *FriendComp) ReceSend(ctx context.Context, session comm.IUserSession, req *pb.FriendReceiveReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//好友详情
|
||||
func (this *FriendComp) Detail(ctx context.Context, session comm.IUserSession, req *pb.FriendTotalReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//黑名单
|
||||
func (this *FriendComp) Blacklist(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackListReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendBlackListRsp
|
||||
list []*pb.FriendBase
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendBlackListRsp{
|
||||
Friends: list,
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_Blacklist, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
for _, userId := range self.BlackIds {
|
||||
//TODO 完善FriendBase信息
|
||||
list = append(list, &pb.FriendBase{
|
||||
UserId: userId,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//加入黑名单
|
||||
func (this *FriendComp) Addblack(ctx context.Context, session comm.IUserSession, req *pb.FriendBlackAddReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
target *pb.Cache_FriendData
|
||||
rsp *pb.FriendBlackAddRsp
|
||||
blackNumMax = 50 //TODO 从配置中读取
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendBlackAddRsp{
|
||||
FriendId: req.FriendId,
|
||||
UserId: session.GetUserId(),
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, code, rsp)
|
||||
}()
|
||||
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
target, err = cache.Defsys.Friend_Get(req.FriendId)
|
||||
if target == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendTargetNoData
|
||||
return
|
||||
}
|
||||
|
||||
//判断目标是否在好友列表里面
|
||||
if _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
|
||||
code = pb.ErrorCode_FriendSelfBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
// 判断自己是否在对方的黑名单中
|
||||
if _, ok := utils.Find(target.BlackIds, self.UserId); ok {
|
||||
code = pb.ErrorCode_FriendTargetBlackYet
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否黑名单人数已满
|
||||
if len(self.BlackIds) >= blackNumMax {
|
||||
code = pb.ErrorCode_FriendBlackMax
|
||||
return
|
||||
}
|
||||
|
||||
//将目标加入黑名单
|
||||
self.BlackIds = append(self.BlackIds, req.FriendId)
|
||||
//更新黑名单
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//删除黑名单
|
||||
func (this *FriendComp) delblack(ctx context.Context, session comm.IUserSession, req *pb.FriendDelBlackReq) (err error) {
|
||||
var (
|
||||
code pb.ErrorCode
|
||||
self *pb.Cache_FriendData
|
||||
rsp *pb.FriendDelBlackRsp
|
||||
)
|
||||
defer func() {
|
||||
if code == pb.ErrorCode_Success {
|
||||
rsp = &pb.FriendDelBlackRsp{
|
||||
FriendId: req.FriendId,
|
||||
UserId: session.GetUserId(),
|
||||
}
|
||||
}
|
||||
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, code, rsp)
|
||||
}()
|
||||
self, err = cache.Defsys.Friend_Get(session.GetUserId())
|
||||
if self == nil || err != nil {
|
||||
code = pb.ErrorCode_FriendSelfNoData
|
||||
return
|
||||
}
|
||||
|
||||
//从黑名单列表中删除目标
|
||||
self.BlackIds = utils.DeleteString(self.BlackIds, req.FriendId)
|
||||
//更新黑名单
|
||||
err = cache.Defsys.Friend_Update(self)
|
||||
if err != nil {
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
55
modules/friend/model_friend.go
Normal file
55
modules/friend/model_friend.go
Normal file
@ -0,0 +1,55 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const (
|
||||
DB_FriendTable core.SqlTable = "friend"
|
||||
DB_UserTable core.SqlTable = "user" //用户表
|
||||
)
|
||||
|
||||
type ModelFriend struct {
|
||||
modules.Model_Comp
|
||||
}
|
||||
|
||||
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"
|
||||
return
|
||||
}
|
||||
|
||||
//好友
|
||||
func (this *ModelFriend) Friend_SaveOrUpdate(data *pb.Cache_FriendData) (err error) {
|
||||
err = this.DB.FindOneAndUpdate(DB_FriendTable,
|
||||
bson.M{"_id": data.UserId},
|
||||
bson.M{"$set": bson.M{
|
||||
"friendids": data.FriendIds,
|
||||
"applyids": data.ApplyIds}},
|
||||
options.FindOneAndUpdate().SetUpsert(true)).Err()
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
_, err = this.DB.InsertOne(DB_FriendTable, data)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *ModelFriend) Frined_FindCond(nickName string) *pb.DB_UserData {
|
||||
var user *pb.DB_UserData
|
||||
err := this.DB.FindOne(DB_UserTable, bson.M{
|
||||
"name": nickName,
|
||||
}).Decode(&user)
|
||||
if err != nil {
|
||||
log.Errorf("findCond err:%v", err)
|
||||
return nil
|
||||
}
|
||||
return user
|
||||
}
|
@ -14,7 +14,8 @@ func NewModule() core.IModule {
|
||||
|
||||
type Friend struct {
|
||||
modules.ModuleBase
|
||||
friend_comp *FriendComp
|
||||
friend_comp *ApiComp
|
||||
model_friend *ModelFriend
|
||||
}
|
||||
|
||||
func (this *Friend) GetType() core.M_Modules {
|
||||
@ -29,5 +30,6 @@ func (this *Friend) Init(service core.IService, module core.IModule, options cor
|
||||
|
||||
func (this *Friend) OnInstallComp() {
|
||||
this.ModuleBase.OnInstallComp()
|
||||
this.friend_comp = this.RegisterComp(new(FriendComp)).(*FriendComp)
|
||||
this.friend_comp = this.RegisterComp(new(ApiComp)).(*ApiComp)
|
||||
this.model_friend = this.RegisterComp(new(ModelFriend)).(*ModelFriend)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/core/cbase"
|
||||
@ -9,7 +10,9 @@ import (
|
||||
"go_dreamfactory/lego/sys/redis"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
"reflect"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
@ -25,6 +28,7 @@ type Model_Comp struct {
|
||||
cbase.ModuleCompBase
|
||||
Redis redis.ISys
|
||||
DB mgo.ISys
|
||||
Prefix string //redis key前缀
|
||||
}
|
||||
|
||||
//组件初始化接口
|
||||
@ -32,7 +36,6 @@ func (this *Model_Comp) Init(service core.IService, module core.IModule, comp co
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
this.Redis = cache.Redis()
|
||||
this.DB = db.Mgo()
|
||||
NewDBModel(this)
|
||||
return
|
||||
}
|
||||
func (this *Model_Comp) Start() (err error) {
|
||||
@ -93,3 +96,40 @@ func (this *Model_Comp) UpdateModelLogs(table string, uID string, where interfac
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
//更新缓存字段
|
||||
func (this *Model_Comp) Set(uid string, v map[string]interface{}, isnew bool) error {
|
||||
err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.Prefix, uid), v)
|
||||
if err != nil {
|
||||
log.Errorf("set err:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if isnew {
|
||||
return this.InsertModelLogs(this.Prefix, uid, v)
|
||||
} else {
|
||||
return this.UpdateModelLogs(this.Prefix, uid, bson.M{"_id": uid}, v)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//获取对象数据
|
||||
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...)
|
||||
}
|
||||
|
||||
//获取字段数据
|
||||
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)
|
||||
}
|
||||
|
||||
//删除一条数据
|
||||
func (this *Model_Comp) Del(uid string) error {
|
||||
err := this.Redis.HDel(fmt.Sprintf("%s:%s", this.Prefix, uid))
|
||||
if err != nil {
|
||||
log.Errorf("del err:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return this.DeleteModelLogs(this.Prefix, uid, bson.M{"_id": uid})
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func (this *ModuleBase) SendMsgToUser(mainType, subType string, msg proto.Messag
|
||||
SubType: subType,
|
||||
Data: data,
|
||||
}, reply); err != nil {
|
||||
log.Errorf("SendMsgToUser%d:%s [%s.%s] err:%v", user.UserData.Uid, user.SessionId, mainType, subType, err)
|
||||
log.Errorf("SendMsgToUser%d:%s [%s.%s] err:%v", user.Uid, user.SessionId, mainType, subType, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if err := db.OnInit(nil, db.Set_MongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), db.Set_MongodbDatabase("dreamfactory")); err != nil {
|
||||
fmt.Printf("err:%v\n", err)
|
||||
return
|
||||
}
|
||||
if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil {
|
||||
fmt.Printf("err:%v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer os.Exit(m.Run())
|
||||
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
userModel := NewUserModel()
|
||||
userModel.SetData(&pb.DB_UserData{Name: "aaaaa"})
|
||||
userModel.Set("1_62a729fc0e01ab2819553242", modules.INSERT)
|
||||
// userModel.Set("1_62a729fc0e01ab2819553242",)
|
||||
// fmt.Printf("%v", userModel.GetData().(*pb.Cache_UserData))
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
// um := NewUserModel("1_62a729fc0e01ab2819553242")
|
||||
|
||||
}
|
17
modules/user/model_user.go
Normal file
17
modules/user/model_user.go
Normal file
@ -0,0 +1,17 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/modules"
|
||||
)
|
||||
|
||||
type ModelUser struct {
|
||||
modules.Model_Comp
|
||||
}
|
||||
|
||||
func (this *ModelUser) 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 = "user"
|
||||
return
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ type User struct {
|
||||
modules.ModuleBase
|
||||
login_comp *LoginComp
|
||||
user_comp *UserComp
|
||||
modelUser *ModelUser
|
||||
}
|
||||
|
||||
func (this *User) GetType() core.M_Modules {
|
||||
@ -32,4 +33,5 @@ func (this *User) OnInstallComp() {
|
||||
this.ModuleBase.OnInstallComp()
|
||||
this.login_comp = this.RegisterComp(new(LoginComp)).(*LoginComp)
|
||||
this.user_comp = this.RegisterComp(new(UserComp)).(*UserComp)
|
||||
this.modelUser = this.RegisterComp(new(ModelUser)).(*ModelUser)
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package user
|
@ -5,12 +5,10 @@ import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/utils"
|
||||
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/event"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -31,26 +29,22 @@ func (this *UserComp) Init(service core.IService, module core.IModule, comp core
|
||||
//创角
|
||||
func (this *UserComp) Create(ctx context.Context, session comm.IUserSession, req *pb.UserCreateReq) (err error) {
|
||||
defer utils.TraceFunc(session.GetUserId(), string(this.module.GetType()), User_SubType_Create, req, nil)
|
||||
user := cache.Defsys.Get(session.GetUserId())
|
||||
|
||||
var code pb.ErrorCode
|
||||
defer func() {
|
||||
session.SendMsg(string(this.module.GetType()), User_SubType_Create, code, &pb.UserCreateRsp{})
|
||||
}()
|
||||
if user == nil {
|
||||
log.Errorf("user no exist")
|
||||
code = pb.ErrorCode_UserSessionNobeing
|
||||
return
|
||||
}
|
||||
|
||||
user.Name = req.NickName
|
||||
err = cache.Defsys.Update(user)
|
||||
//更新昵称
|
||||
update := map[string]interface{}{
|
||||
"name": req.NickName,
|
||||
}
|
||||
err = this.module.modelUser.Set(session.GetUserId(), update)
|
||||
if err != nil {
|
||||
log.Errorf("cache update err")
|
||||
code = pb.ErrorCode_DBError
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
event.RegisterGO(comm.Event_CreateUser, session.GetUserId())
|
||||
return nil
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
)
|
||||
|
||||
type UserModel struct {
|
||||
modules.DBModel
|
||||
}
|
||||
|
||||
func NewUserModel() *UserModel {
|
||||
model := new(UserModel)
|
||||
model.SetPrefix("user") //每个Model有自己的唯一key
|
||||
model.SetTableName("user")
|
||||
model.SetData(&pb.Cache_UserData{}) //设置具体数据类型
|
||||
// model.Get(uid) //获取model数据
|
||||
return model
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/pb"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const (
|
||||
DB_FriendTable core.SqlTable = "friend"
|
||||
)
|
||||
|
||||
type IFriend interface {
|
||||
Friend_SaveOrUpdate(data *pb.Cache_FriendData) (err error)
|
||||
Frined_FindCond(nickName string) *pb.DB_UserData
|
||||
}
|
||||
|
||||
//好友
|
||||
func (this *DB) Friend_SaveOrUpdate(data *pb.Cache_FriendData) (err error) {
|
||||
err = this.mgo.FindOneAndUpdate(DB_FriendTable,
|
||||
bson.M{"_id": data.UserId},
|
||||
bson.M{"$set": bson.M{
|
||||
"friendids": data.FriendIds,
|
||||
"applyids": data.ApplyIds}},
|
||||
options.FindOneAndUpdate().SetUpsert(true)).Err()
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
_, err = this.mgo.InsertOne(DB_FriendTable, data)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *DB) Frined_FindCond(nickName string) *pb.DB_UserData {
|
||||
var user *pb.DB_UserData
|
||||
err := this.mgo.FindOne(DB_UserTable, bson.M{
|
||||
"nicename": nickName,
|
||||
}).Decode(&user)
|
||||
if err != nil {
|
||||
log.Errorf("findCond err:%v", err)
|
||||
return nil
|
||||
}
|
||||
return user
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/pb"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFriendAdd(t *testing.T) {
|
||||
err := db.Friend_SaveOrUpdate(&pb.Cache_FriendData{
|
||||
UserId: "629f159310d6970846f7ef26",
|
||||
FriendIds: []string{
|
||||
"629f147e3d276120561bfa18",
|
||||
},
|
||||
})
|
||||
|
||||
require.Nil(t, err, nil)
|
||||
}
|
||||
|
||||
func TestFriendFindCond(t *testing.T) {
|
||||
user := db.Frined_FindCond("乐谷5")
|
||||
require.NotNil(t, user, nil)
|
||||
|
||||
fmt.Printf("%v", user)
|
||||
}
|
Loading…
Reference in New Issue
Block a user