zoukankan      html  css  js  c++  java
  • 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 = 0; 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,登陆的时候刷新账号信息,把最近登陆的排在最前面(代码里面有写),然后保存到本地。

  • 相关阅读:
    echarts----实现图表联动
    echarst-----入门,实现柱状图、饼图、环形图、折线图、词云图
    软件需求---河北省重大需求进度报告08
    Tensorflow学习笔记(一)
    软件工程课程个人总结
    Android项目——用户主页实现
    Android项目——消息列表实现
    Android项目——功能更新
    第十四周学习进度总结
    Android项目——查看我的发布
  • 原文地址:https://www.cnblogs.com/abelmou/p/5041400.html
Copyright © 2011-2022 走看看