158 lines
4.6 KiB
Go
158 lines
4.6 KiB
Go
package user
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
"go_dreamfactory/utils"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
// 参数校验
|
|
func (this *apiComp) LoginCheck(session comm.IUserSession, req *pb.UserLoginReq) (errdata *pb.ErrorData) {
|
|
if req.Account == "" {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_ReqParameterError,
|
|
Title: pb.ErrorCode_ReqParameterError.ToString(),
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 登录
|
|
func (this *apiComp) Login(session comm.IUserSession, req *pb.UserLoginReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
user *pb.DBUser
|
|
expand *pb.DBUserExpand
|
|
lastLoginTime int64
|
|
err error
|
|
tasks []*pb.BuriedParam = make([]*pb.BuriedParam, 0)
|
|
firstLogin bool
|
|
)
|
|
|
|
if errdata = this.LoginCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
|
|
//从mgo查询user
|
|
user, err = this.module.modelUser.FindByAccount(req.Sid, req.Account)
|
|
if err != nil && err != mongo.ErrNoDocuments {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if err == mongo.ErrNoDocuments {
|
|
//如果是新玩家,创建一条基础的数据,页面会引导进入创角页面
|
|
user = &pb.DBUser{
|
|
Sid: req.Sid,
|
|
Binduid: req.Account,
|
|
Lastloginip: session.GetIP(),
|
|
Skins: make([]string, 0),
|
|
Area: req.Area,
|
|
Channel: req.Channel,
|
|
Vcode: req.Vcode,
|
|
Vname: req.Vname,
|
|
}
|
|
err = this.module.modelUser.User_Create(user)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
firstLogin = true
|
|
expand = &pb.DBUserExpand{}
|
|
}
|
|
// 玩家是否已在线
|
|
if isession, ok := this.module.ModuleBase.GetUserSession(user.Uid); ok {
|
|
isession.SendMsg(string(this.module.GetType()), "othertermlogin", &pb.UserOtherTermLoginPush{Uid: user.Uid})
|
|
isession.SyncPush()
|
|
isession.UnBind()
|
|
this.module.PutUserSession(isession)
|
|
}
|
|
lastLoginTime = user.Logintime
|
|
user.Logintime = configure.Now().Unix()
|
|
user.Lastloginip = session.GetIP()
|
|
user.Offlinetime = 0
|
|
user.Area = req.Area
|
|
user.Channel = req.Channel
|
|
user.Vcode = req.Vcode
|
|
user.Vname = req.Vname
|
|
update := utils.StructToMap(user)
|
|
if expand, err = this.module.GetUserExpand(user.Uid); err != nil {
|
|
if err != mongo.ErrNoDocuments {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
if !utils.IsToday(lastLoginTime) { //当天第一次登录
|
|
user.Nologindays = int32(utils.DiffDays(lastLoginTime, configure.Now().Unix()))
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype8, 1))
|
|
this.module.modelExpand.Change(user.Uid, map[string]interface{}{
|
|
"loginAddCount": expand.LoginAddCount + 1,
|
|
"globalbuff": 0,
|
|
"consumPs": 0, // 重置每日消耗体力
|
|
"loginContinueCount": expand.LoginContinueCount + 1,
|
|
})
|
|
firstLogin = true
|
|
}
|
|
err = this.module.modelUser.Change(user.Uid, update)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype7, 1))
|
|
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype230, 1, int32(configure.Now().Weekday())))
|
|
//缓存user session
|
|
err = this.module.modelSession.addUserSession(user.Uid, session)
|
|
if err != nil {
|
|
this.module.Errorf("set user session err:%v", err)
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_UserSessionNobeing,
|
|
Title: pb.ErrorCode_UserSessionNobeing.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
session.SetSession(session.GetIP(), session.GetSessionId(), session.GetServiecTag(), session.GetGatewayServiceId(), user.Uid)
|
|
session.SendMsg(string(this.module.GetType()), UserSubTypeLogin, &pb.UserLoginResp{
|
|
Data: user,
|
|
Ex: expand,
|
|
TimeNow: configure.Now().Unix(),
|
|
})
|
|
|
|
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
|
|
event.TriggerEvent(comm.EventUserLogin, session)
|
|
if len(tasks) > 0 {
|
|
this.module.ModuleBuried.TriggerBuried(session.Clone(), tasks...)
|
|
}
|
|
if firstLogin {
|
|
this.module.ModuleFriend.ResetFriend(user.Uid)
|
|
this.module.modelSign.UserSign(session)
|
|
this.module.ModuleHero.CheckPeachReward(session, user.Ctime)
|
|
this.module.ModuleItems.InitItemBagData(session)
|
|
this.module.ModulePrivilege.CheckDailyPrivilegeMail(session) // 检查月卡每日邮件奖励发放
|
|
}
|
|
})
|
|
|
|
return
|
|
}
|