go_dreamfactory/modules/user/module.go
2022-07-04 18:16:03 +08:00

90 lines
1.8 KiB
Go

package user
import (
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/lego/core"
)
func NewModule() core.IModule {
m := new(User)
return m
}
type User struct {
modules.ModuleBase
api *apiComp
modelUser *ModelUser
modelSession *ModelSession
configure *modules.MCompConfigure
}
func (this *User) GetType() core.M_Modules {
return comm.ModuleUser
}
func (this *User) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
return
}
func (this *User) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelUser = this.RegisterComp(new(ModelUser)).(*ModelUser)
this.modelSession = this.RegisterComp(new(ModelSession)).(*ModelSession)
}
//获取用户数据
func (this *User) GetUser(uid string) *pb.DBUser {
user := this.modelUser.GetUser(uid)
if user.Id == "" {
return nil
}
return user
}
//获取用户会话
func (this *User) GetUserSession(uid string) *pb.CacheUser {
cuser := this.modelSession.getUserSession(uid)
if cuser.Uid == "" {
return nil
}
return cuser
}
//查询用户属性值 例如 金币 经验
func (this *User) QueryAttributeValue(uid string, attr string) (value int32) {
user := this.modelUser.GetUser(uid)
if user == nil {
return
}
switch attr {
case comm.ResGold:
return user.Gold
case comm.ResExp:
return user.Exp
}
return
}
//用户资源
func (this *User) AddAttributeValue(uid string, attr string, add int32) (code pb.ErrorCode) {
update := make(map[string]interface{})
switch attr {
case comm.ResGold:
update[comm.ResGold] = add
case comm.ResExp:
update[comm.ResExp] = add
}
if err := this.modelUser.updateUserAttr(uid, update); err != nil {
code = pb.ErrorCode_DBError
}
return
}