cocos2d 保存最近登陆多个账号最多一个月
用的是一个单例来管理 ,数据是存在本地的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 保存最近登陆多个账号最多一个月的更多相关文章
- python日志按天分割,保存近一个月日志,日志自动清理
python日志按天分割,保存近一个月日志 import os import logging import re from logging.handlers import TimedRotatingF ...
- php 账号不能同时登陆,当其它地方登陆时,当前账号失效
解决的思路是每当用户登陆时我们必需记录当前的用户id和session_id,如果有人在其它地方用此账号登陆时,我们把此用户id对应的session_id的session文件删除,并重新记录当前的ses ...
- java的web项目中使用cookie保存用户登陆信息
本文转自:http://lever0066.iteye.com/blog/1735963 最近在编写论坛系统的实现,其中就涉及到用户登陆后保持会话直到浏览器关闭,同时可以使用cookie保存登陆信息以 ...
- laravel框架——保存用户登陆信息(session)
public function inlog(Request $request) { //获取表单提交的数据 $input = $request->all(); //根本获取的数据去数据库中查询 ...
- jQuery cookie插件保存用户登陆信息
通过jquery cookie插件保存用户登录信息. 代码: <html> <head> <title>cookies.html</title> ...
- 如何禁止浏览器自动填充非登陆input的账号和密码?
发现浏览器填充密码的方式,那就是,找到页面上第一个type为password的input填充.发现了这个规律后,很自然的就想到了,是不是可以在真正的password前面加一个隐藏的password,形 ...
- Ionic Cordova Sqlite 实现保存用户名登陆
1.添加sqlite 组件 cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage.git --save 2. ...
- ie浏览器多开-----同时登陆多个账号
1.在电脑桌面右键 找到 新建快捷方式 在上图输入框中输入 "C:\Program Files\Internet Explorer\iexplore.exe" -noframeme ...
- #写一个登陆的程序 ( 1.最多登录失败3次 2.登陆成功,提示欢迎XX登录,今天的日期是XXX,程序结束 3.要检验输入是否为空,账户和密码不能为空 4.账户不区分大小写)
import datetime import MySQLdb today=datetime.datetime.today() username=str(input('请输入账户:')) passwd1 ...
随机推荐
- 堆排序(C++实现)
#include<iostream> #include<vector> using namespace std; void swap(vector<int> &am ...
- hdu4511小明系列故事——女友的考验(ac自动机+最短路)
链接 预处理出来任意两点的距离,然后可以顺着trie树中的节点走,不能走到不合法的地方,另开一维表示走到了哪里,依次来更新. 注意判断一下起点是不是合法. #include <iostream& ...
- 微软2017校招笔试题2 composition
题目 Alice writes an English composition with a length of N characters. However, her teacher requires ...
- spm总结
自己尽量做出来,不要等和依靠,发挥主观能动性,所有的配置都在features模块里,一个一个的慢慢找和点击查找
- hello word
开通微博,用于记录在工作中遇到的点滴问题. 2015/08/31
- Windows7系统下JAVA运行环境下载、安装和设置(第二次更新:2012年03月14日)
1.下载 地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html,(由于Sun于2009年被oracle收购所以网址 ...
- 禁止Android 横屏竖屏切换
在Android中要让一个程序的界面始终保持一个方向,不随手机方向转动而变化的办法: 只要在AndroidManifest.xml里面配置一下就可以了. 在AndroidManifest.xml的ac ...
- 读javascript高级程序设计16-几条函数小技巧
内容概要 作用域安全的构造函数 惰性载入函数 函数绑定 函数节流 一.作用域安全的构造函数 我们知道,当使用new操作符调用构造函数时,构造函数内部的this会指向新创建对象的实例. function ...
- OC语言构造方法
OC语言构造方法 一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情,1)使用alloc方法 ...
- z-index、display、selector选择器优先级css优先级面试用到
z-index:控制元素叠放顺序,哪个z-index数值越大,那个优先被叠放在上面. relative.absolute.fixed这三种情况可以使用z-index. static不可以使用. dis ...