作业要求
- 实现 输入用户名和密码进行注册、登录,注册、登录后应有信息回显 (显示成功/失败,和服务端返回的信息)
- 实现 获取用户信息
- (加分项) 实现 修改个人信息
- 修改个人信息只是修改“卡组”信息,输入框中直接输入“卡组”的json
- 这一功能需要通过cookie认证身份,即在登录后需要记录cookie (enableCookies)
- 使用enableCookies非常简单,因此需要在报告中体现 “思考enableCookies的作用” 才能加分
具体实现
LoginRegisterScene.h
#ifndef __LOGIN_REGISTER_SCENE_H__
#define __LOGIN_REGISTER_SCENE_H__
#include "cocos2d.h"
#include "ui\CocosGUI.h"
#include "network/HttpClient.h"
using namespace cocos2d::network;
using namespace cocos2d::ui;
USING_NS_CC;
class LoginRegisterScene : public cocos2d::Scene {
public:
static cocos2d::Scene* createScene();
virtual bool init();
void loginButtonCallback(Ref *pSender);
void registerButtonCallback(Ref *pSender);
void onHttpRequestCompleted(HttpClient *sender, HttpResponse *response);
void onHttpRequestCompleted1(HttpClient *sender, HttpResponse *response);
CREATE_FUNC(LoginRegisterScene);
Label *messageBox;
private:
TextField *usernameInput;
TextField *passwordInput;
};
#endif // !__LOGIN_REGISTER_SCENE_H__
LoginRegisterScene.cpp
#include "LoginRegisterScene.h"
#include "ui\CocosGUI.h"
#include "network\HttpClient.h"
#include "json\document.h"
#include "Utils.h"
USING_NS_CC;
using namespace cocos2d::network;
using namespace cocos2d::ui;
cocos2d::Scene * LoginRegisterScene::createScene() {
return LoginRegisterScene::create();
}
bool LoginRegisterScene::init() {
if (!Scene::init()) {
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto loginButton = MenuItemFont::create("Login", CC_CALLBACK_1(LoginRegisterScene::loginButtonCallback, this));
if (loginButton) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + loginButton->getContentSize().height / 2;
loginButton->setPosition(Vec2(x, y));
}
auto registerButton = MenuItemFont::create("Register", CC_CALLBACK_1(LoginRegisterScene::registerButtonCallback, this));
if (registerButton) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + registerButton->getContentSize().height / 2 + 100;
registerButton->setPosition(Vec2(x, y));
}
auto backButton = MenuItemFont::create("Back", [] (Ref* pSender) {
Director::getInstance()->popScene();
});
if (backButton) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + visibleSize.height - backButton->getContentSize().height / 2;
backButton->setPosition(Vec2(x, y));
}
auto menu = Menu::create(loginButton, registerButton, backButton, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
usernameInput = TextField::create("username", "arial", 24);
if (usernameInput) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + visibleSize.height - 100.0f;
usernameInput->setPosition(Vec2(x, y));
this->addChild(usernameInput, 1);
}
passwordInput = TextField::create("password", "arial", 24);
if (passwordInput) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + visibleSize.height - 130.0f;
passwordInput->setPosition(Vec2(x, y))