Merge remote-tracking branch 'origin/liwei' into zhaocy

This commit is contained in:
zhaocy 2022-06-06 14:44:34 +08:00
commit 5b7d9f8cea
13 changed files with 72 additions and 39 deletions

View File

@ -37,6 +37,7 @@ type ISC_GateRouteComp interface {
//用户会话 //用户会话
type IUserSession interface { type IUserSession interface {
GetSessionId() string GetSessionId() string
GetUserId() uint32
GetIP() string GetIP() string
GetGatewayServiceId() string GetGatewayServiceId() string
Build(uid uint32) (err error) Build(uid uint32) (err error)

View File

@ -31,6 +31,10 @@ type UserSession struct {
func (this *UserSession) GetSessionId() string { func (this *UserSession) GetSessionId() string {
return this.SessionId return this.SessionId
} }
func (this *UserSession) GetUserId() uint32 {
return this.UserId
}
func (this *UserSession) GetIP() string { func (this *UserSession) GetIP() string {
return this.IP return this.IP
} }

View File

@ -20,7 +20,7 @@ type LoginComp struct {
func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req *pb.UserLoginReq) error { func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req *pb.UserLoginReq) error {
log.Debugf("User - Login: session:%v rsp:%v", session.ToString(), req) log.Debugf("User - Login: session:%v rsp:%v", session.ToString(), req)
db_user, err := db.Defsys.FindUserByAccount(req.Name) db_user, err := db.Defsys.User_FindUserByAccount(req.Name)
if err != nil { if err != nil {
if err != mongo.ErrNoDocuments { if err != mongo.ErrNoDocuments {
return err return err
@ -29,7 +29,7 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
if db_user.UserId == 0 { if db_user.UserId == 0 {
db_user.Account = req.Name db_user.Account = req.Name
err = db.Defsys.CreateUser(db_user) err = db.Defsys.User_CreateUser(db_user)
if err != nil { if err != nil {
return err return err
} }
@ -42,7 +42,7 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
GatewayServiceId: session.GetGatewayServiceId(), GatewayServiceId: session.GetGatewayServiceId(),
UserData: db_user, UserData: db_user,
} }
err = cache.Defsys.UpdateUser(cache_user) err = cache.Defsys.User_UpdateUser(cache_user)
if err != nil { if err != nil {
return err return err
} }

7
sys/cache/core.go vendored
View File

@ -1,10 +1,9 @@
package cache package cache
import "go_dreamfactory/pb"
type ( type (
ISys interface { ISys interface {
UpdateUser(data *pb.Cache_UserData) (err error) IUser
IPack
} }
) )
@ -29,5 +28,3 @@ func NewSys(option ...Option) (sys ISys, err error) {
Defsys, err = newSys(options) Defsys, err = newSys(options)
return return
} }

7
sys/cache/mail.go vendored Normal file
View File

@ -0,0 +1,7 @@
package cache
const ( //Redis
Redis_mailCache string = "mail:%d"
)
type IMail interface{}

7
sys/cache/pack.go vendored Normal file
View File

@ -0,0 +1,7 @@
package cache
const ( //Redis
Redis_PackCache string = "pack:%d"
)
type IPack interface{}

9
sys/cache/user.go vendored
View File

@ -9,6 +9,11 @@ const ( //Redis
Redis_UserCache string = "user:%d" //会话列表 Redis_UserCache string = "user:%d" //会话列表
) )
func (this *Cache) UpdateUser(data *pb.Cache_UserData) (err error) { type IUser interface {
return this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.UserId), data, 0) User_UpdateUser(data *pb.Cache_UserData) (err error)
}
func (this *Cache) User_UpdateUser(data *pb.Cache_UserData) (err error) {
err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.UserId), data, -1)
return
} }

View File

@ -32,6 +32,6 @@ func TestUpdateUser(t *testing.T) {
Account: "aaa", Account: "aaa",
}, },
} }
err := cache.UpdateUser(user) err := cache.User_UpdateUser(user)
require.Nil(t, err) require.Nil(t, err)
} }

View File

@ -1,13 +1,10 @@
package db package db
import "go_dreamfactory/pb"
type ( type (
ISys interface { ISys interface {
FindUserByAccount(account string) (*pb.DB_UserData, error) IUser
FindUserById(id uint32) (*pb.DB_UserData, error) IPack
CreateUser(user *pb.DB_UserData) error IMail
UpdateUser(data *pb.DB_UserData) (err error)
} }
) )

4
sys/db/mail.go Normal file
View File

@ -0,0 +1,4 @@
package db
type IMail interface {
}

4
sys/db/pack.go Normal file
View File

@ -0,0 +1,4 @@
package db
type IPack interface {
}

View File

@ -18,11 +18,18 @@ const ( //Redis
DB_UserIdTable core.SqlTable = "userid" //用户id表 DB_UserIdTable core.SqlTable = "userid" //用户id表
) )
type IUser interface {
User_FindUserByAccount(account string) (*pb.DB_UserData, error)
User_FindUserById(id uint32) (*pb.DB_UserData, error)
User_CreateUser(user *pb.DB_UserData) error
User_UpdateUser(data *pb.DB_UserData) (err error)
}
type UserId struct { type UserId struct {
UserId uint32 `bson:"_id"` UserId uint32 `bson:"_id"`
} }
func (this *DB) FindUserByAccount(account string) (*pb.DB_UserData, error) { func (this *DB) User_FindUserByAccount(account string) (*pb.DB_UserData, error) {
filter := bson.D{ filter := bson.D{
{"account", account}, {"account", account},
} }
@ -32,7 +39,7 @@ func (this *DB) FindUserByAccount(account string) (*pb.DB_UserData, error) {
return user, err return user, err
} }
func (this *DB) FindUserById(id uint32) (*pb.DB_UserData, error) { func (this *DB) User_FindUserById(id uint32) (*pb.DB_UserData, error) {
filter := bson.D{ filter := bson.D{
{"_id", id}, {"_id", id},
} }
@ -42,7 +49,7 @@ func (this *DB) FindUserById(id uint32) (*pb.DB_UserData, error) {
return user, err return user, err
} }
func (this *DB) CreateUser(user *pb.DB_UserData) error { func (this *DB) User_CreateUser(user *pb.DB_UserData) error {
userId := &UserId{} userId := &UserId{}
err := this.mgo.FindOneAndDelete(DB_UserIdTable, bson.M{}).Decode(userId) err := this.mgo.FindOneAndDelete(DB_UserIdTable, bson.M{}).Decode(userId)
if err != nil { if err != nil {
@ -54,6 +61,20 @@ func (this *DB) CreateUser(user *pb.DB_UserData) error {
return err return err
} }
//更新用户数据到DB
func (this *DB) User_UpdateUser(data *pb.DB_UserData) error {
err := this.mgo.FindOneAndUpdate(
DB_UserTable,
bson.M{"_id": data.UserId},
bson.M{"$set": bson.M{
"niceName": data.NiceName,
"email": data.Email,
}},
options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After),
).Decode(data)
return err
}
//校验数据库初始化工作是否完成 //校验数据库初始化工作是否完成
func (this DB) checkUserIdInit() (err error) { func (this DB) checkUserIdInit() (err error) {
ctx, _ := context.WithTimeout(context.Background(), time.Second*60) ctx, _ := context.WithTimeout(context.Background(), time.Second*60)
@ -83,17 +104,3 @@ func (this DB) checkUserIdInit() (err error) {
} }
return return
} }
//更新用户数据到DB
func (this *DB) UpdateUser(data *pb.DB_UserData) error {
err := this.mgo.FindOneAndUpdate(
DB_UserTable,
bson.M{"_id": data.UserId},
bson.M{"$set": bson.M{
"niceName": data.NiceName,
"email": data.Email,
}},
options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After),
).Decode(data)
return err
}

View File

@ -32,16 +32,16 @@ func TestCreate(t *testing.T) {
Email: "1111@legu.com", Email: "1111@legu.com",
} }
err := db.CreateUser(user) err := db.User_CreateUser(user)
require.Nil(t, err) require.Nil(t, err)
} }
func TestFindOne(t *testing.T) { func TestFindOne(t *testing.T) {
user, err := db.FindUserById(1) user, err := db.User_FindUserById(1)
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, "legu1", user.Account) assert.Equal(t, "legu1", user.Account)
user2, err := db.FindUserByAccount("legu1") user2, err := db.User_FindUserByAccount("legu1")
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, "legu1", user2.Account) assert.Equal(t, "legu1", user2.Account)
@ -52,7 +52,7 @@ func TestUpdate(t *testing.T) {
UserId: 10001, UserId: 10001,
Email: "new@qq.com", Email: "new@qq.com",
} }
err := db.UpdateUser(user) err := db.User_UpdateUser(user)
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, "new@qq.com", user.Email) assert.Equal(t, "new@qq.com", user.Email)