更新接口chk

This commit is contained in:
zhaocy 2022-06-16 20:26:20 +08:00
parent bba410cb3c
commit 4da4c572e5
14 changed files with 284 additions and 168 deletions

View File

@ -190,15 +190,12 @@ func Test_Redis_Encoder_Hash(t *testing.T) {
// fmt.Printf("data:%v err:%v\n", data, err)
// redis.HSet("test:1003", "Name", "eeee")
name := ""
err := redis.HGet("test:103", "name", &name)
if err != nil {
fmt.Println(err)
}
fmt.Println(name)
redis.HMSet("",)
// name := ""
// err := redis.HGet("test:103", "name", &name)
// if err != nil {
// fmt.Println(err)
// }
// fmt.Println(name)
// data1 := map[string]*TestData{"li_1": {Name: "liwei2dao", Agr: 56}, "li_2": {Name: "liwei3dao", Agr: 78}}
// err := redis.HMSet("test:1004", data1)
@ -206,4 +203,15 @@ func Test_Redis_Encoder_Hash(t *testing.T) {
// data2 := make(map[string]*TestData)
// err = redis.HGetAll("test:1004", data2)
// fmt.Printf("data2:%v err:%v\n", data2, err)
// d1 := map[string]interface{}{"ll": "1", "age": 20}
// err := redis.HMSet("test:104", d1)
// fmt.Printf("err:%v\n", err)
// d2 := make(map[string]interface{})
// err := redis.HGetAll("test:105", d2)
// fmt.Printf("data2:%v err:%v\n", d2, err)
iskeep, err := redis.ExistsKey("test:105")
fmt.Printf("ok:%v err:%v\n", iskeep, err)
}

View File

@ -6,68 +6,78 @@ import (
"go_dreamfactory/utils"
)
func (this *ApiComp) Addblack_Check(session comm.IUserSession, req *pb.Friend_BlackAdd_Req) (result map[string]interface{}, code comm.ErrorCode) {
return
}
//加入黑名单
func (this *ApiComp) Addblack(session comm.IUserSession, chk map[string]interface{}, req *pb.Friend_BlackAdd_Req) (err error) {
func (this *ApiComp) Addblack_Check(session comm.IUserSession, req *pb.Friend_BlackAdd_Req) (chk map[string]interface{}, code comm.ErrorCode) {
chk = make(map[string]interface{})
var (
code pb.ErrorCode
self *pb.DB_FriendData
target *pb.DB_FriendData
rsp *pb.Friend_BlackAdd_Rsp
err error
blackNumMax = 50 //TODO 从配置中读取
)
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.Friend_BlackAdd_Rsp{
FriendId: req.FriendId,
UserId: session.GetUserId(),
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, rsp)
}()
self := &pb.DB_FriendData{UserId: session.GetUserId()}
target := &pb.DB_FriendData{UserId: req.FriendId}
err = this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfNoData}
return
}
err = this.module.model_friend.GetObj(req.FriendId, target)
if target == nil || err != nil {
code = pb.ErrorCode_FriendTargetNoData
code = comm.ErrorCode{Code: pb.ErrorCode_FriendTargetNoData}
return
}
//判断目标是否在好友列表里面
if _, ok := utils.Find(self.FriendIds, req.FriendId); ok {
code = pb.ErrorCode_FriendSelfBlackYet
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfBlackYet}
return
}
// 判断自己是否在对方的黑名单中
if _, ok := utils.Find(target.BlackIds, self.UserId); ok {
code = pb.ErrorCode_FriendTargetBlackYet
code = comm.ErrorCode{Code: pb.ErrorCode_FriendTargetBlackYet}
return
}
// 判断是否黑名单人数已满
if len(self.BlackIds) >= blackNumMax {
code = pb.ErrorCode_FriendBlackMax
code = comm.ErrorCode{Code: pb.ErrorCode_FriendBlackMax}
return
}
chk["self"] = self
chk["target"] = target
return
}
//加入黑名单
func (this *ApiComp) Addblack(session comm.IUserSession, chk map[string]interface{}, req *pb.Friend_BlackAdd_Req) (code pb.ErrorCode) {
var (
self *pb.DB_FriendData
rsp *pb.Friend_BlackAdd_Rsp
)
defer func() {
rsp = &pb.Friend_BlackAdd_Rsp{
FriendId: req.FriendId,
UserId: session.GetUserId(),
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, rsp)
}()
if v, ok := chk["self"]; ok {
self = v.(*pb.DB_FriendData)
//将目标加入黑名单
self.BlackIds = append(self.BlackIds, req.FriendId)
//更新黑名单
err = this.module.model_friend.SetObj(self.UserId, self)
err := this.module.model_friend.SetObj(self.UserId, self)
if err != nil {
code = pb.ErrorCode_DBError
return
}
}
return
}

View File

@ -42,12 +42,14 @@ func (this *ApiComp) Agree(session comm.IUserSession, chk map[string]interface{}
)
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.Friend_Agree_Rsp{
Num: optNum,
}
err := session.SendMsg(string(this.module.GetType()), Friend_SubType_Agree, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Agree, rsp)
}()
if v, ok := chk["self"]; !ok {

View File

@ -96,7 +96,11 @@ func (this *ApiComp) Apply(session comm.IUserSession, chk map[string]interface{}
FriendId: req.FriendId,
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Apply, rsp)
err := session.SendMsg(string(this.module.GetType()), Friend_SubType_Apply, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
if v, ok := chk["target"]; !ok {

View File

@ -6,6 +6,14 @@ import (
)
func (this *ApiComp) ApplyList_Check(session comm.IUserSession, req *pb.Friend_ApplyList_Req) (chk map[string]interface{}, code comm.ErrorCode) {
chk = make(map[string]interface{})
self := &pb.DB_FriendData{UserId: session.GetUserId()}
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfNoData}
return
}
chk["self"] = self
return
}
@ -26,18 +34,15 @@ func (this *ApiComp) ApplyList(session comm.IUserSession, chk map[string]interfa
session.SendMsg(string(this.module.GetType()), Friend_SubType_ApplyList, rsp)
}()
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
return
}
if v, ok := chk["self"]; ok {
self = v.(*pb.DB_FriendData)
for _, userId := range self.ApplyIds {
//TODO 组装FriendBase明细数据
list = append(list, &pb.FriendBase{
UserId: userId,
})
}
}
return
}

View File

@ -6,6 +6,14 @@ import (
)
func (this *ApiComp) Blacklist_Check(session comm.IUserSession, req *pb.Friend_BlackList_Req) (chk map[string]interface{}, code comm.ErrorCode) {
chk = make(map[string]interface{})
self := &pb.DB_FriendData{UserId: session.GetUserId()}
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfNoData}
return
}
chk["self"] = self
return
}
@ -18,26 +26,26 @@ func (this *ApiComp) Blacklist(session comm.IUserSession, chk map[string]interfa
)
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.Friend_BlackList_Rsp{
Friends: list,
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Blacklist, rsp)
}()
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
err := session.SendMsg(string(this.module.GetType()), Friend_SubType_Blacklist, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
if v, ok := chk["self"]; ok {
self = v.(*pb.DB_FriendData)
for _, userId := range self.BlackIds {
//TODO 完善FriendBase信息
list = append(list, &pb.FriendBase{
UserId: userId,
})
}
}
return
}

View File

@ -7,6 +7,14 @@ import (
)
func (this *ApiComp) Delblack_Check(session comm.IUserSession, req *pb.Friend_DelBlack_Req) (chk map[string]interface{}, code comm.ErrorCode) {
chk = make(map[string]interface{})
self := &pb.DB_FriendData{UserId: session.GetUserId()}
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfNoData}
return
}
chk["self"] = self
return
}
@ -23,21 +31,24 @@ func (this *ApiComp) Delblack(session comm.IUserSession, chk map[string]interfac
UserId: session.GetUserId(),
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, rsp)
}()
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
err := session.SendMsg(string(this.module.GetType()), Friend_SubType_AddBlack, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
if v, ok := chk["self"]; ok {
self = v.(*pb.DB_FriendData)
//从黑名单列表中删除目标
self.BlackIds = utils.DeleteString(self.BlackIds, req.FriendId)
//更新黑名单
err = this.module.model_friend.SetObj(self.UserId, self)
err := this.module.model_friend.SetObj(self.UserId, self)
if err != nil {
code = pb.ErrorCode_DBError
return
}
}
return
}

View File

@ -6,6 +6,14 @@ import (
)
func (this *ApiComp) List_Check(session comm.IUserSession, req *pb.Friend_List_Req) (chk map[string]interface{}, code comm.ErrorCode) {
chk = make(map[string]interface{})
self := &pb.DB_FriendData{UserId: session.GetUserId()}
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSelfNoData}
return
}
chk["self"] = self
return
}
@ -18,25 +26,24 @@ func (this *ApiComp) List(session comm.IUserSession, chk map[string]interface{},
)
defer func() {
if code == pb.ErrorCode_Success {
rsp = &pb.Friend_List_Rsp{
List: list,
}
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_List, rsp)
}()
err := this.module.model_friend.GetObj(session.GetUserId(), self)
if self == nil || err != nil {
code = pb.ErrorCode_FriendSelfNoData
err := session.SendMsg(string(this.module.GetType()), Friend_SubType_List, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
if v, ok := chk["self"]; ok {
self = v.(*pb.DB_FriendData)
for _, userId := range self.FriendIds {
list = append(list, &pb.FriendBase{
UserId: userId,
})
}
}
return
}

View File

@ -45,7 +45,11 @@ func (this *ApiComp) Refuse(session comm.IUserSession, chk map[string]interface{
Num: optNum,
}
session.SendMsg(string(this.module.GetType()), Friend_SubType_Refuse, rsp)
err := session.SendMsg(string(this.module.GetType()), Friend_SubType_Refuse, rsp)
if err != nil {
code = pb.ErrorCode_SystemError
return
}
}()
if v, ok := chk["self"]; !ok {

View File

@ -6,6 +6,10 @@ import (
)
func (this *ApiComp) Search_Check(session comm.IUserSession, req *pb.Friend_Search_Req) (chk map[string]interface{}, code comm.ErrorCode) {
if req.NickName == "" {
code = comm.ErrorCode{Code: pb.ErrorCode_FriendSearchNameEmpty}
return
}
return
}

View File

@ -44,6 +44,11 @@ func (this *Model_Comp) Start() (err error) {
err = this.ModuleCompBase.Start()
return
}
func (this *Model_Comp) ukey(uid string) string {
return fmt.Sprintf("%s:%s", this.TableName, uid)
}
func (this *Model_Comp) InsertModelLogs(table string, uID string, target interface{}) (err error) {
data := &comm.Autogenerated{
@ -103,7 +108,7 @@ func (this *Model_Comp) UpdateModelLogs(table string, uID string, where bson.M,
//data 值允许protobuf格式的对象
// attrs 操作可选项目 eg.传入WithDisabledMgoLog() 表示关闭日志,否则开启;WithND() 传入表示插入操作不传表示更新前提不能传入传入WithDisabledMgoLog()
func (this *Model_Comp) SetObj(uid string, data proto.Message, attrs ...*cache.OperationAttr) error {
err := this.Redis.Set(fmt.Sprintf("%s:%s", this.TableName, uid), data, 0)
err := this.Redis.Set(this.ukey(uid), data, 0)
if err != nil {
log.Errorf("set err:%v", err)
return err
@ -118,7 +123,7 @@ func (this *Model_Comp) SetObj(uid string, data proto.Message, attrs ...*cache.O
// attrs 操作可选项目 eg.传入WithDisabledMgoLog() 表示关闭日志,否则开启;WithND() 传入表示插入操作不传表示更新前提不能传入传入WithDisabledMgoLog()
//如果更新数据uid作为where条件之一如果检索结果不能确定唯一此时data 必需是map[string]interface{}类型必需包含_id 字段
func (this *Model_Comp) SetHM(uid string, data interface{}, attrs ...*cache.OperationAttr) error {
err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.TableName, uid), data)
err := this.Redis.HMSet(this.ukey(uid), data)
if err != nil {
log.Errorf("SetHM err: %v", err)
return err
@ -130,7 +135,7 @@ func (this *Model_Comp) SetHM(uid string, data interface{}, attrs ...*cache.Oper
//缓存一个字段的数据
//如果更新数据uid作为where条件之一如果检索结果不能确定唯一此时data 必需是map[string]interface{}类型必需包含_id 字段
func (this *Model_Comp) SetH(uid string, field string, data interface{}, attrs ...*cache.OperationAttr) error {
err := this.Redis.HSet(fmt.Sprintf("%s:%s", this.TableName, uid), field, data)
err := this.Redis.HSet(this.ukey(uid), field, data)
if err != nil {
log.Errorf("SetH err %v", err)
return err
@ -140,7 +145,7 @@ func (this *Model_Comp) SetH(uid string, field string, data interface{}, attrs .
//获取缓存JSON数据
func (this *Model_Comp) GetObj(uid string, v proto.Message) error {
err := this.Redis.Get(fmt.Sprintf("%s:%s", this.TableName, uid), v)
err := this.Redis.Get(this.ukey(uid), v)
if err != nil {
if err == redis.RedisNil {
//query from mgo
@ -164,34 +169,77 @@ func (this *Model_Comp) GetObj(uid string, v proto.Message) error {
return err
}
//获取对象数据
//data
//获取对象所有字段数据
//data类型map或protobuf
func (this *Model_Comp) GetHM(uid string, data interface{}) error {
return this.Redis.HGetAll(fmt.Sprintf("%s:%s", this.TableName, uid), data)
ok, err := this.Redis.ExistsKey(this.ukey(uid))
if err != nil {
log.Errorf("key no exist %v", this.ukey(uid))
return err
}
if ok {
return this.Redis.HGetAll(this.ukey(uid), data)
} else {
filter := bson.M{"uid": uid}
c, err2 := this.DB.Find(core.SqlTable(this.TableName), filter)
if err2 != nil {
log.Errorf("GetHM-find err:%v", err)
return err
}
err2 = c.Decode(data)
if err2 != nil {
log.Errorf("GetHM-find decode err:%v", err)
return err
}
//update cache without mgolog
return this.SetHM(this.ukey(uid), data, cache.WithDisabledMgoLog())
}
return nil
}
//获取字段数据 缓存存储的数据为hashmap时
func (this *Model_Comp) GetH(uid string, field string, v interface{}) error {
return this.Redis.HGet(fmt.Sprintf("%s:%s", this.TableName, uid), field, v)
return this.Redis.HGet(this.ukey(uid), field, v)
}
//删除一条数据
func (this *Model_Comp) Del(uid string) error {
err := this.Redis.HDel(fmt.Sprintf("%s:%s", this.TableName, uid))
func (this *Model_Comp) DelH(uid string) error {
err := this.Redis.HDel(this.ukey(uid))
if err != nil {
log.Errorf("del err:%v", err)
return err
}
return this.DeleteModelLogs(this.TableName, uid, bson.M{"_id": uid})
return this.DeleteModelLogs(this.TableName, uid, bson.M{"uid": uid})
}
//删除缓存字段
func (this *Model_Comp) DelHF(uid string, fields ...string) error {
err := this.Redis.HDel(this.ukey(uid), fields...)
if err != nil {
log.Errorf("DelHF err: %v", err)
return err
}
//get new data after delete
data := make(map[string]interface{})
err = this.Redis.HGetAll(this.ukey(uid), data)
if err != nil {
log.Errorf("DelHF-HGetAll err: %v", err)
return err
}
//cache with mgolog
return this.SetHM(this.ukey(uid), data)
}
//日志操作可选项
func (this *Model_Comp) logOpt(uid string, data interface{}, attrs ...*cache.OperationAttr) error {
ret := cache.OperationAttrs(attrs).Find(cache.ATTR_MGOLOG).Unwrap_Or(nil)
if ret == nil {
if ret == nil { //启用mgolog
ir := cache.OperationAttrs(attrs).Find(cache.ATTR_INSERT).Unwrap_Or(nil)
if ir != nil && ir.(bool) {
if ir == nil { //updte opt
where := bson.M{"uid": uid}
if reflect.ValueOf(data).Kind() == reflect.Map {
if m, ok := data.(map[string]interface{}); ok {
@ -201,7 +249,7 @@ func (this *Model_Comp) logOpt(uid string, data interface{}, attrs ...*cache.Ope
}
}
return this.UpdateModelLogs(this.TableName, uid, where, data)
} else {
} else { //insert opt
return this.InsertModelLogs(this.TableName, uid, data)
}
}

View File

@ -53,6 +53,7 @@ const (
ErrorCode_FriendTargetBlackYet ErrorCode = 1108 //已在对方的黑名单中
ErrorCode_FriendApplyError ErrorCode = 1109 //申请失败
ErrorCode_FriendBlackMax ErrorCode = 1110 //黑名单最大数量
ErrorCode_FriendSearchNameEmpty ErrorCode = 1111 //查询昵称为空
)
// Enum value maps for ErrorCode.
@ -86,6 +87,7 @@ var (
1108: "FriendTargetBlackYet",
1109: "FriendApplyError",
1110: "FriendBlackMax",
1111: "FriendSearchNameEmpty",
}
ErrorCode_value = map[string]int32{
"Success": 0,
@ -116,6 +118,7 @@ var (
"FriendTargetBlackYet": 1108,
"FriendApplyError": 1109,
"FriendBlackMax": 1110,
"FriendSearchNameEmpty": 1111,
}
)
@ -150,7 +153,7 @@ var File_errorcode_proto protoreflect.FileDescriptor
var file_errorcode_proto_rawDesc = []byte{
0x0a, 0x0f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x2a, 0xc9, 0x04, 0x0a, 0x09, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12,
0x6f, 0x2a, 0xe5, 0x04, 0x0a, 0x09, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12,
0x0b, 0x0a, 0x07, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d,
0x4e, 0x6f, 0x46, 0x69, 0x6e, 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x10, 0x0a, 0x12,
0x1b, 0x0a, 0x17, 0x4e, 0x6f, 0x46, 0x69, 0x6e, 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65,
@ -186,8 +189,10 @@ var file_errorcode_proto_rawDesc = []byte{
0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x42, 0x6c, 0x61, 0x63, 0x6b, 0x59, 0x65, 0x74, 0x10, 0xd4,
0x08, 0x12, 0x15, 0x0a, 0x10, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x41, 0x70, 0x70, 0x6c, 0x79,
0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0xd5, 0x08, 0x12, 0x13, 0x0a, 0x0e, 0x46, 0x72, 0x69, 0x65,
0x6e, 0x64, 0x42, 0x6c, 0x61, 0x63, 0x6b, 0x4d, 0x61, 0x78, 0x10, 0xd6, 0x08, 0x42, 0x06, 0x5a,
0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x6e, 0x64, 0x42, 0x6c, 0x61, 0x63, 0x6b, 0x4d, 0x61, 0x78, 0x10, 0xd6, 0x08, 0x12, 0x1a, 0x0a,
0x15, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x4e, 0x61, 0x6d,
0x65, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x10, 0xd7, 0x08, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70,
0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@ -1,7 +1,6 @@
syntax = "proto3";
option go_package = ".;pb";
enum ErrorCode {
Success = 0; //
NoFindService = 10; //
@ -35,4 +34,5 @@ enum ErrorCode {
FriendTargetBlackYet = 1108; //
FriendApplyError = 1109; //
FriendBlackMax = 1110; //
FriendSearchNameEmpty = 1111; //
}

View File

@ -42,6 +42,6 @@ func WithDisabledMgoLog() *OperationAttr {
func WithND() *OperationAttr {
return &OperationAttr{
Name: ATTR_INSERT,
Value: true,
Value: empty{},
}
}