//用户登录
func userLogin(session *link.Session, protoMsg protos.ProtoMsg) {
	rev_msg := protoMsg.Body.(*dbProto.DB_User_LoginC2S)
	userName := rev_msg.GetName()

	//先从缓存中读取
	dbUser := redisProxy.GetDBUserByUserName(userName)
	if dbUser == nil {
		//从数据库中获取
		dbUser, _ = dao.GetUserByUserName(userName)
		//将数据缓存到Redis
		redisProxy.SetDBUser(dbUser)
	}

	//返回消息
	sendProtoMsg := &dbProto.DB_User_LoginS2C{}
	if dbUser != nil {
		sendProtoMsg.ID = protos.Uint64(dbUser.ID)
		sendProtoMsg.Name = protos.String(dbUser.Name)
	}
	send_msg := dbProto.MarshalProtoMsg(protoMsg.Identification, sendProtoMsg)
	sendDBMsgToClient(session, send_msg)

	//更新最后登录时间
	if dbUser != nil {
		dbUser.LastLoginTime = time.Now().Unix()
		redisProxy.UpdateUserLastLoginTime(dbUser)
	}
}
Example #2
0
//登录
func (this UserModule) Login(userName string, session *link.Session) {
	onlineUser := module.Cache.GetOnlineUserByUserName(userName)
	if onlineUser != nil {
		if onlineUser.Session.Id() != session.Id() {
			//当前在线,但是连接不同,其他客户端连接,需通知当前客户端下线
			module.SendOtherLogin(onlineUser.Session)
			//替换Session
			module.Cache.RemoveOnlineUser(onlineUser.Session.Id())
			//登录成功处理
			success := loginSuccess(session, onlineUser.UserName, onlineUser.UserID)
			if success {
				module.SendLoginResult(onlineUser.UserID, session)
			} else {
				module.SendLoginResult(0, session)
			}
		}
	} else {
		cacheDbUser := redisProxy.GetDBUserByUserName(userName)
		if cacheDbUser != nil {
			this.UserLoginHandle(session, cacheDbUser.Name, cacheDbUser.ID)
		} else {
			dbProxy.UserLogin(session.Id(), userName)
		}
	}
}