Merge branch 'dev' of http://git.legu.cc/liwei_3d/go_dreamfactory into dev
This commit is contained in:
commit
277198351a
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package dbservice
|
||||
|
||||
import (
|
||||
"go_dreamfactory/modules"
|
||||
@ -9,13 +9,13 @@ import (
|
||||
type Api_Comp struct {
|
||||
modules.MComp_GateComp
|
||||
service core.IService
|
||||
module *Model
|
||||
module *DBService
|
||||
}
|
||||
|
||||
func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
this.MComp_GateComp.Init(service, module, comp, options)
|
||||
this.service = service
|
||||
this.module = module.(*Model)
|
||||
this.module = module.(*DBService)
|
||||
return
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package dbservice
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package dbservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -14,6 +14,11 @@ import (
|
||||
|
||||
const (
|
||||
WriteMaxNum uint32 = 1000 //一次性最处理条数
|
||||
ErrorMaxNum uint32 = 5 // 数据库操作最大失败次数
|
||||
)
|
||||
|
||||
var (
|
||||
ErrorLogCount = make(map[string]uint32, 0)
|
||||
)
|
||||
|
||||
type DB_Comp struct {
|
||||
@ -32,7 +37,7 @@ type QueryStruct struct {
|
||||
}
|
||||
|
||||
const (
|
||||
DB_ModelTable core.SqlTable = "model"
|
||||
DB_ModelTable core.SqlTable = "model_log"
|
||||
)
|
||||
|
||||
type IModel interface {
|
||||
@ -53,13 +58,12 @@ func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_delID := make([]string, 0) // 处理完成要删除的id
|
||||
for _data.Next(context.TODO()) { // 处理删除逻辑
|
||||
|
||||
data := &comm.Autogenerated{}
|
||||
if err = _data.Decode(data); err == nil {
|
||||
_delID = append(_delID, data.ID)
|
||||
} else {
|
||||
if err = _data.Decode(data); err != nil {
|
||||
log.Errorf("Decode Data err : %v", err)
|
||||
continue
|
||||
}
|
||||
@ -67,7 +71,7 @@ func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) {
|
||||
if data.Act == string(comm.LogHandleType_Insert) {
|
||||
if len(data.D) < 2 { // 参数校验
|
||||
log.Errorf("parameter len _id : %s,uid : %s d.len:%v", data.ID, data.UID, len(data.D))
|
||||
break
|
||||
continue
|
||||
}
|
||||
_obj := bson.M{}
|
||||
for _, v := range data.D[1].(bson.D) {
|
||||
@ -78,29 +82,49 @@ func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) {
|
||||
_, err := this.DB.InsertOne(core.SqlTable(_key), _obj)
|
||||
if err != nil {
|
||||
log.Errorf("insert %s db err:%v", (core.SqlTable(_key)), err)
|
||||
ErrorLogCount[data.ID]++
|
||||
if ErrorLogCount[data.ID] >= ErrorMaxNum { // 实在是写失败了那就删除吧
|
||||
log.Errorf("insert db err max num %s db err:%v", data.ID, err)
|
||||
_, err = this.DB.DeleteOne(DB_ModelTable, bson.M{"_id": data.ID})
|
||||
if err != nil {
|
||||
log.Errorf("insert %s db err:%v", data.ID, err)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
// _, err = this.DB.DeleteOne(DB_ModelTable, bson.M{"_id": data.ID})
|
||||
// if err != nil {
|
||||
// log.Errorf("insert %s db err:%v", data.ID, err)
|
||||
// log.Errorf("delete %s db err:%v", data.ID, err)
|
||||
// }
|
||||
} else if data.Act == string(comm.LogHandleType_Delete) {
|
||||
if len(data.D) < 2 { // 参数校验
|
||||
log.Errorf("parameter len _id : %s,uid : %s d.len:%v", data.ID, data.UID, len(data.D))
|
||||
break
|
||||
continue
|
||||
}
|
||||
_key := data.D[0].(string)
|
||||
_objKey := make([]string, 0)
|
||||
for _, v := range data.D[1].(bson.D) {
|
||||
_objKey = append(_objKey, v.Value.(string))
|
||||
}
|
||||
_, err = this.DB.DeleteMany(data.D[0].(core.SqlTable), bson.M{"_id": bson.M{"$in": _objKey}}, options.Delete())
|
||||
_, err = this.DB.DeleteMany(core.SqlTable(_key), bson.M{"_id": bson.M{"$in": _objKey}}, options.Delete())
|
||||
if err != nil {
|
||||
log.Errorf("delete %s db err:%v", data.D[0].(core.SqlTable), err)
|
||||
log.Errorf("delete %s db err:%v", core.SqlTable(_key), err)
|
||||
ErrorLogCount[data.ID]++
|
||||
if ErrorLogCount[data.ID] >= ErrorMaxNum { // 实在是写失败了那就删除吧
|
||||
log.Errorf("del db err max num %s db err:%v", data.ID, err)
|
||||
_, err = this.DB.DeleteOne(DB_ModelTable, bson.M{"_id": data.ID})
|
||||
if err != nil {
|
||||
log.Errorf("insert %s db err:%v", data.ID, err)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
} else { // update
|
||||
if len(data.D) < 3 { // 参数校验
|
||||
log.Errorf("parameter len _id : %s,uid : %s d.len:%v", data.ID, data.UID, len(data.D))
|
||||
break
|
||||
continue
|
||||
}
|
||||
_key := data.D[0].(string)
|
||||
where := data.D[1].(bson.D)
|
||||
_obj := &QueryStruct{}
|
||||
for _, v := range where {
|
||||
@ -110,9 +134,22 @@ func (this *DB_Comp) Model_UpdateDBByLog(uid string) (err error) {
|
||||
for _, v := range query {
|
||||
_obj.Query[v.Key] = v
|
||||
}
|
||||
this.DB.UpdateMany(data.D[0].(core.SqlTable), _obj.Selector, _obj.Query)
|
||||
_, err := this.DB.UpdateMany(core.SqlTable(_key), _obj.Selector, _obj.Query)
|
||||
if err != nil {
|
||||
log.Errorf("Update %s db err:%v", core.SqlTable(_key), err)
|
||||
ErrorLogCount[data.ID]++
|
||||
if ErrorLogCount[data.ID] >= ErrorMaxNum { // 超过一定次数写失败了那就删除吧
|
||||
log.Errorf("update db err max num %s db err:%v", data.ID, err)
|
||||
_, err = this.DB.DeleteOne(DB_ModelTable, bson.M{"_id": data.ID})
|
||||
if err != nil {
|
||||
log.Errorf("insert %s db err:%v", data.ID, err)
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
_delID = append(_delID, data.ID) // 都操作都成功了记录要删除的key
|
||||
}
|
||||
// 批量删除已处理的数据
|
||||
_, err = this.DB.DeleteMany(DB_ModelTable, bson.M{"_id": bson.M{"$in": _delID}}, options.Delete())
|
||||
if err != nil {
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package dbservice
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
@ -9,12 +9,12 @@ import (
|
||||
type DBService_Comp struct {
|
||||
cbase.ModuleCompBase
|
||||
task chan string
|
||||
module *Model
|
||||
module *DBService
|
||||
}
|
||||
|
||||
func (this *DBService_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
this.module = module.(*Model)
|
||||
this.module = module.(*DBService)
|
||||
return
|
||||
}
|
||||
|
48
modules/dbservice/mail_test.go
Normal file
48
modules/dbservice/mail_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package dbservice
|
||||
|
||||
import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/pb"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
var module = new(DBService)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
for i := 0; i < 50000; i++ {
|
||||
//go func() {
|
||||
_mail := &pb.DB_MailData{
|
||||
ObjId: primitive.NewObjectID().Hex(),
|
||||
UserId: "uid123",
|
||||
Title: "系统邮件",
|
||||
|
||||
Contex: "恭喜获得专属礼包一份",
|
||||
CreateTime: uint64(time.Now().Unix()),
|
||||
DueTime: uint64(time.Now().Unix()) + 30*24*3600,
|
||||
Check: false,
|
||||
Reward: false,
|
||||
}
|
||||
//db.InsertModelLogs("mail", "uid123", _mail)
|
||||
//InsertModelLogs("mail", "uid123", _mail)
|
||||
data := &comm.Autogenerated{
|
||||
ID: primitive.NewObjectID().Hex(),
|
||||
UID: "uid123",
|
||||
Act: string(comm.LogHandleType_Insert),
|
||||
}
|
||||
data.D = append(data.D, "mail") // D[0]
|
||||
data.D = append(data.D, _mail) // D[1]
|
||||
|
||||
_, err1 := module.db_comp.DB.InsertOne("model_log", data)
|
||||
if err1 != nil {
|
||||
log.Errorf("insert model db err %v", err1)
|
||||
}
|
||||
//}()
|
||||
}
|
||||
time.Sleep(time.Second * 10)
|
||||
defer os.Exit(m.Run())
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package dbservice
|
||||
|
||||
import (
|
||||
"go_dreamfactory/comm"
|
||||
@ -7,11 +7,11 @@ import (
|
||||
)
|
||||
|
||||
func NewModule() core.IModule {
|
||||
m := new(Model)
|
||||
m := new(DBService)
|
||||
return m
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
type DBService struct {
|
||||
modules.ModuleBase
|
||||
api_comp *Api_Comp
|
||||
db_comp *DB_Comp
|
||||
@ -19,17 +19,17 @@ type Model struct {
|
||||
configure_comp *Configure_Comp
|
||||
}
|
||||
|
||||
func (this *Model) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
||||
func (this *DBService) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
||||
err = this.ModuleBase.Init(service, module, options)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (this *Model) GetType() core.M_Modules {
|
||||
func (this *DBService) GetType() core.M_Modules {
|
||||
return comm.SM_LogModelModule
|
||||
}
|
||||
|
||||
func (this *Model) OnInstallComp() {
|
||||
func (this *DBService) OnInstallComp() {
|
||||
this.ModuleBase.OnInstallComp()
|
||||
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
||||
this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp)
|
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,25 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/sys/log"
|
||||
"go_dreamfactory/pb"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCreatemoudles(t *testing.T) {
|
||||
_mail := &pb.DB_MailData{
|
||||
|
||||
UserId: "uid123",
|
||||
Title: "系统邮件",
|
||||
|
||||
Contex: "恭喜获得专属礼包一份",
|
||||
CreateTime: uint64(time.Now().Unix()),
|
||||
DueTime: uint64(time.Now().Unix()) + 30*24*3600,
|
||||
Check: false,
|
||||
Reward: false,
|
||||
}
|
||||
// obj.InsertModelLogs("mail", "uid123", _mail)
|
||||
|
||||
log.Debugf("insert : %v", _mail)
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/core/cbase"
|
||||
@ -9,14 +10,12 @@ 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"
|
||||
)
|
||||
|
||||
const (
|
||||
DB_ModelTable core.SqlTable = "model"
|
||||
)
|
||||
|
||||
/*
|
||||
基础组件 缓存组件 读写缓存数据
|
||||
DB组件也封装进来
|
||||
@ -25,8 +24,13 @@ type Model_Comp struct {
|
||||
cbase.ModuleCompBase
|
||||
Redis redis.ISys
|
||||
DB mgo.ISys
|
||||
Prefix string //redis key前缀
|
||||
}
|
||||
|
||||
const (
|
||||
DB_ModelTable core.SqlTable = "model_log"
|
||||
)
|
||||
|
||||
//组件初始化接口
|
||||
func (this *Model_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
this.ModuleCompBase.Init(service, module, comp, options)
|
||||
@ -92,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
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
"go_dreamfactory/utils"
|
||||
"time"
|
||||
@ -23,11 +22,13 @@ import (
|
||||
type LoginComp struct {
|
||||
modules.MComp_GateComp
|
||||
service base.IRPCXService
|
||||
model *User
|
||||
}
|
||||
|
||||
func (this *LoginComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||
this.MComp_GateComp.Init(service, module, comp, options)
|
||||
this.service = service.(base.IRPCXService)
|
||||
this.model = module.(*User)
|
||||
return
|
||||
}
|
||||
|
||||
@ -60,13 +61,14 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if db_user != nil {
|
||||
session.SendMsg("user", "login", code, &pb.UserLoginResp{
|
||||
Data: &pb.Cache_UserData{
|
||||
UserData: db_user,
|
||||
Uid: db_user.Id,
|
||||
},
|
||||
})
|
||||
|
||||
event.TriggerEvent(comm.Event_UserLogin, db_user.Uid)
|
||||
}
|
||||
}()
|
||||
|
||||
if !utils.ValidSecretKey(req.Sec) {
|
||||
@ -83,6 +85,11 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
|
||||
}
|
||||
}
|
||||
|
||||
cache_user := &pb.Cache_UserData{
|
||||
SessionId: session.GetSessionId(),
|
||||
GatewayServiceId: session.GetGatewayServiceId(),
|
||||
}
|
||||
|
||||
//如果是新玩家,创建一条基础的数据,页面会引导进入创角页面
|
||||
if db_user == nil {
|
||||
err = db.Defsys.User_Create(user)
|
||||
@ -91,20 +98,21 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
|
||||
return
|
||||
}
|
||||
session.Bind(user.Uid, this.service.GetId())
|
||||
cache_user.Uid = user.Uid
|
||||
} else {
|
||||
session.Bind(db_user.Uid, this.service.GetId())
|
||||
cache_user.Uid = db_user.Uid
|
||||
}
|
||||
|
||||
cache_user := &pb.Cache_UserData{
|
||||
SessionId: session.GetSessionId(),
|
||||
GatewayServiceId: session.GetGatewayServiceId(),
|
||||
UserData: db_user,
|
||||
}
|
||||
err = cache.Defsys.Update(cache_user)
|
||||
if err != nil {
|
||||
log.Errorf("update cache err:%v", err)
|
||||
return err
|
||||
}
|
||||
// data := map[string]interface{}{
|
||||
// "sessionId": cache_user.SessionId,
|
||||
// "gatewayServiceId": cache_user.GatewayServiceId,
|
||||
// }
|
||||
// this.model.ModuleBas.Set(cache_user.Uid, data)
|
||||
// err = cache.Defsys.Update(cache_user)
|
||||
// if err != nil {
|
||||
// log.Errorf("update cache err:%v", err)
|
||||
// return err
|
||||
// }
|
||||
|
||||
return
|
||||
}
|
||||
|
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)
|
||||
}
|
||||
|
@ -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,24 +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.UserData.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
|
||||
}
|
||||
|
@ -2,9 +2,10 @@ syntax = "proto3";
|
||||
option go_package = ".;pb";
|
||||
|
||||
message Cache_UserData {
|
||||
string uid = 1;
|
||||
string SessionId = 2;
|
||||
string GatewayServiceId = 3;
|
||||
DB_UserData UserData = 4;
|
||||
// DB_UserData UserData = 4;
|
||||
}
|
||||
|
||||
message DB_UserData {
|
||||
|
@ -25,9 +25,9 @@ type Cache_UserData struct {
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Uid string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid,omitempty"`
|
||||
SessionId string `protobuf:"bytes,2,opt,name=SessionId,proto3" json:"SessionId,omitempty"`
|
||||
GatewayServiceId string `protobuf:"bytes,3,opt,name=GatewayServiceId,proto3" json:"GatewayServiceId,omitempty"`
|
||||
UserData *DB_UserData `protobuf:"bytes,4,opt,name=UserData,proto3" json:"UserData,omitempty"`
|
||||
GatewayServiceId string `protobuf:"bytes,3,opt,name=GatewayServiceId,proto3" json:"GatewayServiceId,omitempty"` // DB_UserData UserData = 4;
|
||||
}
|
||||
|
||||
func (x *Cache_UserData) Reset() {
|
||||
@ -62,6 +62,13 @@ func (*Cache_UserData) Descriptor() ([]byte, []int) {
|
||||
return file_user_user_db_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *Cache_UserData) GetUid() string {
|
||||
if x != nil {
|
||||
return x.Uid
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Cache_UserData) GetSessionId() string {
|
||||
if x != nil {
|
||||
return x.SessionId
|
||||
@ -76,13 +83,6 @@ func (x *Cache_UserData) GetGatewayServiceId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Cache_UserData) GetUserData() *DB_UserData {
|
||||
if x != nil {
|
||||
return x.UserData
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type DB_UserData struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
@ -222,35 +222,34 @@ var File_user_user_db_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_user_user_db_proto_rawDesc = []byte{
|
||||
0x0a, 0x12, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x64, 0x62, 0x2e, 0x70,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x22, 0x84, 0x01, 0x0a, 0x0e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55,
|
||||
0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x1c, 0x0a, 0x09, 0x53, 0x65, 0x73, 0x73, 0x69,
|
||||
0x6f, 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x53, 0x65, 0x73, 0x73,
|
||||
0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79,
|
||||
0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49,
|
||||
0x64, 0x12, 0x28, 0x0a, 0x08, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x18, 0x04, 0x20,
|
||||
0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74,
|
||||
0x61, 0x52, 0x08, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x22, 0xaf, 0x02, 0x0a, 0x0b,
|
||||
0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x69,
|
||||
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75,
|
||||
0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a,
|
||||
0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69,
|
||||
0x64, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e,
|
||||
0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12,
|
||||
0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x73, 0x69,
|
||||
0x64, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x69, 0x70, 0x18, 0x07, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x69, 0x70, 0x12, 0x20, 0x0a,
|
||||
0x0b, 0x6c, 0x61, 0x73, 0x74, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x69, 0x70, 0x18, 0x08, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x0b, 0x6c, 0x61, 0x73, 0x74, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x69, 0x70, 0x12,
|
||||
0x14, 0x0a, 0x05, 0x63, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05,
|
||||
0x63, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x74, 0x69,
|
||||
0x6d, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x74,
|
||||
0x69, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x50, 0x6f, 0x69,
|
||||
0x6e, 0x74, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64,
|
||||
0x50, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18,
|
||||
0x0c, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x42, 0x06, 0x5a,
|
||||
0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x22, 0x6c, 0x0a, 0x0e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73,
|
||||
0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x53, 0x65, 0x73, 0x73,
|
||||
0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x53, 0x65, 0x73,
|
||||
0x73, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61,
|
||||
0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x10, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65,
|
||||
0x49, 0x64, 0x22, 0xaf, 0x02, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61,
|
||||
0x74, 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02,
|
||||
0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x03, 0x75, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x69, 0x6e, 0x64,
|
||||
0x75, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x69, 0x6e, 0x64, 0x75,
|
||||
0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x06, 0x20,
|
||||
0x01, 0x28, 0x05, 0x52, 0x03, 0x73, 0x69, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x72, 0x65, 0x61,
|
||||
0x74, 0x65, 0x69, 0x70, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x72, 0x65, 0x61,
|
||||
0x74, 0x65, 0x69, 0x70, 0x12, 0x20, 0x0a, 0x0b, 0x6c, 0x61, 0x73, 0x74, 0x6c, 0x6f, 0x67, 0x69,
|
||||
0x6e, 0x69, 0x70, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x6c, 0x61, 0x73, 0x74, 0x6c,
|
||||
0x6f, 0x67, 0x69, 0x6e, 0x69, 0x70, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x74, 0x69, 0x6d, 0x65, 0x18,
|
||||
0x09, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x63, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09,
|
||||
0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x03, 0x52,
|
||||
0x09, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x46, 0x72,
|
||||
0x69, 0x65, 0x6e, 0x64, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x05, 0x52,
|
||||
0x0b, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06,
|
||||
0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x61, 0x76,
|
||||
0x61, 0x74, 0x61, 0x72, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
@ -271,12 +270,11 @@ var file_user_user_db_proto_goTypes = []interface{}{
|
||||
(*DB_UserData)(nil), // 1: DB_UserData
|
||||
}
|
||||
var file_user_user_db_proto_depIdxs = []int32{
|
||||
1, // 0: Cache_UserData.UserData:type_name -> DB_UserData
|
||||
1, // [1:1] is the sub-list for method output_type
|
||||
1, // [1:1] is the sub-list for method input_type
|
||||
1, // [1:1] is the sub-list for extension type_name
|
||||
1, // [1:1] is the sub-list for extension extendee
|
||||
0, // [0:1] is the sub-list for field type_name
|
||||
0, // [0:0] is the sub-list for method output_type
|
||||
0, // [0:0] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_user_user_db_proto_init() }
|
||||
|
@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"go_dreamfactory/modules/dbservice"
|
||||
"go_dreamfactory/modules/friend"
|
||||
"go_dreamfactory/modules/mail"
|
||||
"go_dreamfactory/modules/model"
|
||||
"go_dreamfactory/modules/pack"
|
||||
"go_dreamfactory/modules/user"
|
||||
"go_dreamfactory/services"
|
||||
@ -41,7 +41,7 @@ func main() {
|
||||
pack.NewModule(),
|
||||
mail.NewModule(),
|
||||
friend.NewModule(),
|
||||
model.NewModule(),
|
||||
dbservice.NewModule(),
|
||||
)
|
||||
|
||||
}
|
||||
|
1
sys/cache/core.go
vendored
1
sys/cache/core.go
vendored
@ -11,6 +11,7 @@ type (
|
||||
Redis() redis.ISys
|
||||
IUser //户模块的相关缓存接口
|
||||
IFriend //好友相关的缓存接口
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
|
2
sys/cache/init_test.go
vendored
2
sys/cache/init_test.go
vendored
@ -47,7 +47,7 @@ func TestMain(m *testing.M) {
|
||||
data.D = append(data.D, "mail") // D[0]
|
||||
data.D = append(data.D, _mail) // D[1]
|
||||
|
||||
_, err1 := db.Defsys.Mgo().InsertOne("model", data)
|
||||
_, err1 := db.Defsys.Mgo().InsertOne("model_log", data)
|
||||
if err1 != nil {
|
||||
log.Errorf("insert model db err %v", err1)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/mgo"
|
||||
)
|
||||
|
||||
@ -24,6 +25,12 @@ func (this *DB) init() (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *DB) Mgo() mgo.ISys {
|
||||
return this.mgo
|
||||
}
|
||||
|
||||
func (this *DB) Table(tableName string) *DB {
|
||||
this.mgo.Collection(core.SqlTable(tableName)).Database()
|
||||
return this
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
@ -16,6 +16,8 @@ func TestCreate(t *testing.T) {
|
||||
Sid: 1,
|
||||
}
|
||||
|
||||
|
||||
|
||||
err := db.User_Create(user)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user