Compare commits
3 Commits
b8e69234ee
...
5b7d9f8cea
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5b7d9f8cea | ||
![]() |
1c1aec3cc8 | ||
![]() |
b804856510 |
@ -37,6 +37,7 @@ type ISC_GateRouteComp interface {
|
||||
//用户会话
|
||||
type IUserSession interface {
|
||||
GetSessionId() string
|
||||
GetUserId() uint32
|
||||
GetIP() string
|
||||
GetGatewayServiceId() string
|
||||
Build(uid uint32) (err error)
|
||||
|
@ -31,6 +31,10 @@ type UserSession struct {
|
||||
func (this *UserSession) GetSessionId() string {
|
||||
return this.SessionId
|
||||
}
|
||||
|
||||
func (this *UserSession) GetUserId() uint32 {
|
||||
return this.UserId
|
||||
}
|
||||
func (this *UserSession) GetIP() string {
|
||||
return this.IP
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ type LoginComp struct {
|
||||
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)
|
||||
|
||||
db_user, err := db.Defsys.FindUserByAccount(req.Name)
|
||||
db_user, err := db.Defsys.User_FindUserByAccount(req.Name)
|
||||
if err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
return err
|
||||
@ -29,7 +29,7 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
|
||||
|
||||
if db_user.UserId == 0 {
|
||||
db_user.Account = req.Name
|
||||
err = db.Defsys.CreateUser(db_user)
|
||||
err = db.Defsys.User_CreateUser(db_user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -42,7 +42,7 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req
|
||||
GatewayServiceId: session.GetGatewayServiceId(),
|
||||
UserData: db_user,
|
||||
}
|
||||
err = cache.Defsys.UpdateUser(cache_user)
|
||||
err = cache.Defsys.User_UpdateUser(cache_user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
7
sys/cache/core.go
vendored
7
sys/cache/core.go
vendored
@ -1,10 +1,9 @@
|
||||
package cache
|
||||
|
||||
import "go_dreamfactory/pb"
|
||||
|
||||
type (
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
7
sys/cache/mail.go
vendored
Normal file
7
sys/cache/mail.go
vendored
Normal 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
7
sys/cache/pack.go
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
package cache
|
||||
|
||||
const ( //Redis
|
||||
Redis_PackCache string = "pack:%d"
|
||||
)
|
||||
|
||||
type IPack interface{}
|
9
sys/cache/user.go
vendored
9
sys/cache/user.go
vendored
@ -9,6 +9,11 @@ const ( //Redis
|
||||
Redis_UserCache string = "user:%d" //会话列表
|
||||
)
|
||||
|
||||
func (this *Cache) UpdateUser(data *pb.Cache_UserData) (err error) {
|
||||
return this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.UserId), data, 0)
|
||||
type IUser interface {
|
||||
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
|
||||
}
|
||||
|
4
sys/cache/user_test.go
vendored
4
sys/cache/user_test.go
vendored
@ -28,10 +28,10 @@ func TestUpdateUser(t *testing.T) {
|
||||
SessionId: "1",
|
||||
GatewayServiceId: "work",
|
||||
UserData: &pb.DB_UserData{
|
||||
UserId: 1,
|
||||
UserId: 1,
|
||||
Account: "aaa",
|
||||
},
|
||||
}
|
||||
err := cache.UpdateUser(user)
|
||||
err := cache.User_UpdateUser(user)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
@ -1,13 +1,10 @@
|
||||
package db
|
||||
|
||||
import "go_dreamfactory/pb"
|
||||
|
||||
type (
|
||||
ISys interface {
|
||||
FindUserByAccount(account string) (*pb.DB_UserData, error)
|
||||
FindUserById(id uint32) (*pb.DB_UserData, error)
|
||||
CreateUser(user *pb.DB_UserData) error
|
||||
UpdateUser(data *pb.DB_UserData) (err error)
|
||||
IUser
|
||||
IPack
|
||||
IMail
|
||||
}
|
||||
)
|
||||
|
||||
|
4
sys/db/mail.go
Normal file
4
sys/db/mail.go
Normal file
@ -0,0 +1,4 @@
|
||||
package db
|
||||
|
||||
type IMail interface {
|
||||
}
|
4
sys/db/pack.go
Normal file
4
sys/db/pack.go
Normal file
@ -0,0 +1,4 @@
|
||||
package db
|
||||
|
||||
type IPack interface {
|
||||
}
|
@ -18,11 +18,18 @@ const ( //Redis
|
||||
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 {
|
||||
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{
|
||||
{"account", account},
|
||||
}
|
||||
@ -32,7 +39,7 @@ func (this *DB) FindUserByAccount(account string) (*pb.DB_UserData, error) {
|
||||
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{
|
||||
{"_id", id},
|
||||
}
|
||||
@ -42,7 +49,7 @@ func (this *DB) FindUserById(id uint32) (*pb.DB_UserData, error) {
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (this *DB) CreateUser(user *pb.DB_UserData) error {
|
||||
func (this *DB) User_CreateUser(user *pb.DB_UserData) error {
|
||||
userId := &UserId{}
|
||||
err := this.mgo.FindOneAndDelete(DB_UserIdTable, bson.M{}).Decode(userId)
|
||||
if err != nil {
|
||||
@ -54,6 +61,20 @@ func (this *DB) CreateUser(user *pb.DB_UserData) error {
|
||||
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) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*60)
|
||||
@ -83,17 +104,3 @@ func (this DB) checkUserIdInit() (err error) {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -32,16 +32,16 @@ func TestCreate(t *testing.T) {
|
||||
Email: "1111@legu.com",
|
||||
}
|
||||
|
||||
err := db.CreateUser(user)
|
||||
err := db.User_CreateUser(user)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestFindOne(t *testing.T) {
|
||||
user, err := db.FindUserById(1)
|
||||
user, err := db.User_FindUserById(1)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "legu1", user.Account)
|
||||
|
||||
user2, err := db.FindUserByAccount("legu1")
|
||||
user2, err := db.User_FindUserByAccount("legu1")
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "legu1", user2.Account)
|
||||
|
||||
@ -52,7 +52,7 @@ func TestUpdate(t *testing.T) {
|
||||
UserId: 10001,
|
||||
Email: "new@qq.com",
|
||||
}
|
||||
err := db.UpdateUser(user)
|
||||
err := db.User_UpdateUser(user)
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.Equal(t, "new@qq.com", user.Email)
|
||||
|
Loading…
Reference in New Issue
Block a user