用的是一个单例来管理 ,数据是存在本地的xml文件里的格式如下

<?xml version="1.0" encoding = "utf-8" ?>
<rootNode>
<userinfo time="1449905923">
<account>abc002</account>
<password>a123456</password>
</userinfo>
<userinfo time="1430905758">
<account>abc001</account>
<password>a123456</password>
</userinfo>
</rootNode>

首先定义要用到的一些数信息

#include "AccountManager.h"
#include "external/tinyxml2/tinyxml2.h" #define ACCOUNT_FILE_NAME "userAccounts.xml"
#define ACCOUNT_SAVE_COUNT 5 //保存的账号数量
#define SECONDS_OF_DAY 86400 //一天的秒数
#define LIMIT_DAY 30LL //过效天数 AccountManager * AccountManager::m_pSelf = nullptr; struct LoginAccountInfo
{
string strAccount; //账号
string strPassword; //密码
string strDate; //最近登陆日期
};

在构造对象的时候获取以前保存的账号信息

AccountManager::AccountManager()
{
string strPath = FileUtils::getInstance()->getWritablePath() + ACCOUNT_FILE_NAME; if (FileUtils::getInstance()->isFileExist(strPath))
{
//提取账号信息到内存
tinyxml2::XMLDocument *pxmlDocument = new tinyxml2::XMLDocument;
pxmlDocument->LoadFile(strPath.c_str()); auto pxmlRootNode = pxmlDocument->RootElement(); auto pxmlNodeUinfo = pxmlRootNode->FirstChildElement();
while (pxmlNodeUinfo)
{
LoginAccountInfo accinfo;
accinfo.strDate = pxmlNodeUinfo->Attribute("time"); auto pxmlNodeAccunt = pxmlNodeUinfo->FirstChildElement();
accinfo.strAccount = pxmlNodeAccunt->GetText(); auto pxmlNodePassword = pxmlNodeAccunt->NextSiblingElement();
accinfo.strPassword = pxmlNodePassword->GetText(); m_vectorAccountsInfo.push_back(accinfo);
pxmlNodeUinfo = pxmlNodeUinfo->NextSiblingElement();
}
}
}

保存账号信息到文件

//保存账号到文件
void AccountManager::saveAccountInfoToFile(const string &strAccounts, const string &strPassword)
{
//排序账号
auto iteratorStr = m_vectorAccountsInfo.begin();
for (; iteratorStr != m_vectorAccountsInfo.end();++iteratorStr)
{
if ((*iteratorStr).strAccount == strAccounts)
{
m_vectorAccountsInfo.erase(iteratorStr);
break;
}
} //判断保存的账号是否超过5个
if (m_vectorAccountsInfo.size() >= ACCOUNT_SAVE_COUNT) m_vectorAccountsInfo.pop_back(); LoginAccountInfo accinfo;
accinfo.strAccount = strAccounts;
accinfo.strPassword = strPassword;
accinfo.strDate = getCurrentDate();
m_vectorAccountsInfo.insert(m_vectorAccountsInfo.begin(), accinfo); tinyxml2::XMLDocument *pxmlDoc = new tinyxml2::XMLDocument();
//声明
tinyxml2::XMLDeclaration *pxmlDeclare = pxmlDoc->NewDeclaration("xml version=\"1.0\" encoding = \"utf-8\" ");
assert(pxmlDeclare);
if (pxmlDeclare == nullptr) return;
pxmlDoc->LinkEndChild(pxmlDeclare); //根结点
tinyxml2::XMLElement *pxmlRootNode = pxmlDoc->NewElement("rootNode"); //添加账号子节点
for (size_t i = ; i < m_vectorAccountsInfo.size(); i++)
{
auto pxmlNodeUInfo = pxmlDoc->NewElement("userinfo");
pxmlNodeUInfo->SetAttribute("time", m_vectorAccountsInfo.at(i).strDate.c_str()); auto pxmlNodeAccount = pxmlDoc->NewElement("account");
pxmlNodeAccount->LinkEndChild(pxmlDoc->NewText(m_vectorAccountsInfo.at(i).strAccount.c_str()));
pxmlNodeUInfo->LinkEndChild(pxmlNodeAccount); auto pxmlNodePassword = pxmlDoc->NewElement("password");
pxmlNodePassword->LinkEndChild(pxmlDoc->NewText(m_vectorAccountsInfo.at(i).strPassword.c_str()));
pxmlNodeUInfo->LinkEndChild(pxmlNodePassword); pxmlRootNode->LinkEndChild(pxmlNodeUInfo);
}
pxmlDoc->LinkEndChild(pxmlRootNode); //保存到文件
if (pxmlDoc)
{
string strPath = FileUtils::getInstance()->getWritablePath() + ACCOUNT_FILE_NAME;
pxmlDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(strPath).c_str());
delete pxmlDoc;
}
}
//获取当前时间 总秒值
string AccountManager::getCurrentDate()
{
time_t llTimeStamp = time(nullptr); string strDate; strDate = StringUtils::format("%lld", llTimeStamp); return strDate;
}
//判断账号是否保存超过限制天数
bool AccountManager::IsAcccountOutOfDate(const string &strAccount)
{
for (auto accinfo : m_vectorAccountsInfo)
{
if (accinfo.strAccount == strAccount)
{
long long llLoginTime = atoll(accinfo.strDate.c_str());
long long llNowTime = atoll(getCurrentDate().c_str());
long long llDay = (llNowTime - llLoginTime) / SECONDS_OF_DAY; if (llDay > LIMIT_DAY)
{
return true;
}
return false;
}
}
return false;
}

下面是UI部分会用到的数据获取,UI部分用了TableView

//获取所有账号
vector<string> AccountManager::getAllAccount()
{
vector<string> vectorAccounts; for (auto accinfo : m_vectorAccountsInfo)
{
vectorAccounts.push_back(accinfo.strAccount);
} return vectorAccounts;
}

以上只是数据管理,UI部分就不上代码了,大概思路就是登陆界面默认加载填充账号密码的时候判断账号是否超过给定天数(这里是30天,最上面宏定义),获取数据列表的时候把所有账号填充到定义有tableview的那个UI,登陆的时候刷新账号信息,把最近登陆的排在最前面(代码里面有写),然后保存到本地。

cocos2d 保存最近登陆多个账号最多一个月的更多相关文章

  1. python日志按天分割,保存近一个月日志,日志自动清理

    python日志按天分割,保存近一个月日志 import os import logging import re from logging.handlers import TimedRotatingF ...

  2. php 账号不能同时登陆,当其它地方登陆时,当前账号失效

    解决的思路是每当用户登陆时我们必需记录当前的用户id和session_id,如果有人在其它地方用此账号登陆时,我们把此用户id对应的session_id的session文件删除,并重新记录当前的ses ...

  3. java的web项目中使用cookie保存用户登陆信息

    本文转自:http://lever0066.iteye.com/blog/1735963 最近在编写论坛系统的实现,其中就涉及到用户登陆后保持会话直到浏览器关闭,同时可以使用cookie保存登陆信息以 ...

  4. laravel框架——保存用户登陆信息(session)

    public function inlog(Request $request) { //获取表单提交的数据 $input = $request->all(); //根本获取的数据去数据库中查询 ...

  5. jQuery cookie插件保存用户登陆信息

    通过jquery cookie插件保存用户登录信息. 代码: <html>  <head>  <title>cookies.html</title>  ...

  6. 如何禁止浏览器自动填充非登陆input的账号和密码?

    发现浏览器填充密码的方式,那就是,找到页面上第一个type为password的input填充.发现了这个规律后,很自然的就想到了,是不是可以在真正的password前面加一个隐藏的password,形 ...

  7. Ionic Cordova Sqlite 实现保存用户名登陆

    1.添加sqlite 组件 cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage.git --save 2. ...

  8. ie浏览器多开-----同时登陆多个账号

    1.在电脑桌面右键 找到 新建快捷方式 在上图输入框中输入 "C:\Program Files\Internet Explorer\iexplore.exe" -noframeme ...

  9. #写一个登陆的程序 ( 1.最多登录失败3次 2.登陆成功,提示欢迎XX登录,今天的日期是XXX,程序结束 3.要检验输入是否为空,账户和密码不能为空 4.账户不区分大小写)

    import datetime import MySQLdb today=datetime.datetime.today() username=str(input('请输入账户:')) passwd1 ...

随机推荐

  1. 开源.NET FTP组件edtFTPnet 用法

    edtFTPnet官方网站:http://www.enterprisedt.com/products/edtftpnet/ 目前最新版本为2.2.3,下载后在bin目录中找到edtFTPnet.dll ...

  2. yii学习笔记

    学而不思则罔,思而不学则殆,适度的总结有利于学习效果的提升. 以前就是埋头看书很少动手所以学习效果不好. 学习yii的原因是自己基本功差,但是yii的学习本身也需要成本

  3. Response.End()在Webform和ASP.NET MVC下的表现差异

    前几天在博问中看到一个问题--Response.End()后,是否停止执行?MVC与WebForm不一致.看到LZ的描述后,虽然奇怪于为何用Response.End()而不用return方式去控制流程 ...

  4. [Prodinner项目]学习分享_第三部分_Service层(业务逻辑层)

    前两节讲到怎样生成一个Model和怎样将Model映射到数据库,这一节将讲到业务逻辑层,也就是Service层. 1.Prodinner架构已经构建好的,基本的增删改查. 假设,我现在想操作第二节中讲 ...

  5. Knockout学习笔记之一

    1.  四大关键理念: A. DeclarativeBindings(声明式绑定) Easily associate DOM elements with model data using a conc ...

  6. 使用QTP测试Web对象

    加载Web插件先启动QTP,再启动浏览器,否则Web元素识别不了最新版本QTP11支持的浏览器:IE:6.7.8Firefox:3.0.x.3.5.QTP支持直接访问DOM(Document Obje ...

  7. C:上台阶

    总时间限制: 1000ms 内存限制: 65536kB描述楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可以一步上2阶,也可以一步上3阶,编程计算共有多少种不同的走法. ...

  8. IE11里边form拦截失效,永远被弹回登录页

    现象描述: 1.在某些服务器上发布了程序以后,用IE11去浏览程序(试了多台电脑都一样),发现总是登录不进去,因为登录之后总是被立即反弹回登录页面,就像是登录后写入的票据瞬间丢失一样. 2.但是同一套 ...

  9. NSComparisonResul、NSNotFound、NSEnumerationOptions......的用处

    NSNotFound 定义一个值,用于指示请求项找不到或不存在.Defines a value that indicates that an item requested couldn’t be fo ...

  10. Js获取下拉框当前选择项的文本和值

    现在有一个Id为AreaId的下拉框,要获取它当前选择项的文本和值有以下方法: <span class="red">* </span> 地       区: ...