go_dreamfactory/modules/user/api_login.go

221 lines
6.5 KiB
Go

package user
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"go_dreamfactory/utils"
"time"
"go.mongodb.org/mongo-driver/bson"
"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 (
group *UserGroupData
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 {
if group, err = this.getGroup(); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Message: err.Error(),
}
return
}
//如果是新玩家,创建一条基础的数据,页面会引导进入创角页面
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,
Moonlv: 1,
Resreplies: make(map[int32]int64),
Group: group.CurrGroup,
}
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 user.Ban {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_UserAccountBan,
Title: pb.ErrorCode_UserAccountBan.ToString(),
Message: fmt.Errorf("UserAccountBan").Error(),
}
return
}
// 玩家是否已在线
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) { //当天第一次登录
update := make(map[string]interface{}, 0)
user.Nologindays = int32(utils.DiffDays(lastLoginTime, configure.Now().Unix()))
tasks = append(tasks, comm.GetBuriedParam(comm.Rtype8, 1))
update["loginAddCount"] = expand.LoginAddCount + 1
update["globalbuff"] = 0
update["consumPs"] = 0
update["loginContinueCount"] = expand.LoginContinueCount + 1
this.module.modelExpand.ChangeUserExpand(user.Uid, update)
firstLogin = true
}
err = this.module.modelUser.Change(user.Uid, update)
err = this.module.modelUser.updateUserAttr(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, user.Group)
if user.CurSkin == "" {
user.CurSkin = this.module.ModuleTools.GetGlobalConf().Initper.T
}
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) {
// if !user.Created { // 没创角就没必要通知
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) //检查月卡每日邮件奖励发放
if user.Created {
this.module.modelUser.ResAutoReplies(session, user.Resreplies)
}
}
})
return
}
func (this *apiComp) getGroup() (group *UserGroupData, err error) {
group = &UserGroupData{
CurrGroup: 1,
CurrGroupNum: 0,
STime: configure.Now().Unix(),
ETime: configure.Now().Add(time.Hour * 24 * time.Duration(this.options.GroupMaxIntervalDay)).Unix(),
}
if err = this.module.ModuleTools.GetAndUpdateGlobalData(UserGroupDataCoonfKey, group, bson.M{"$inc": bson.M{"currgroupnum": 1}}); err != nil && err != mongo.ErrNoDocuments {
return
}
err = nil
if group.CurrGroupNum >= this.options.GroupMaxPlayerNum || configure.Now().After(time.Unix(group.ETime, 0)) {
group.CurrGroup++
group.CurrGroupNum = 0
group.STime = configure.Now().Unix()
group.ETime = configure.Now().Add(time.Hour * 24 * time.Duration(this.options.GroupMaxIntervalDay)).Unix()
if err = this.module.ModuleTools.UpdateGlobalData(UserGroupDataCoonfKey, map[string]interface{}{
"currcroup": group.CurrGroup,
"currgroupnum": group.CurrGroupNum,
"stime": group.STime,
"etime": group.ETime,
}); err != nil {
return
}
}
return
}