107 lines
2.8 KiB
C++
107 lines
2.8 KiB
C++
#pragma once
|
|
|
|
#include "cocos2d.h"
|
|
#include "HttpClient.h"
|
|
#include "HttpRequest.h"
|
|
#include "external/json/document.h"
|
|
#include "Utility.h"
|
|
|
|
USING_NS_CC;
|
|
using namespace cocos2d::network;
|
|
|
|
typedef std::map<std::string,std::string> XQ5WebReqInfo;
|
|
|
|
struct SendStructInfo
|
|
{
|
|
SendStructInfo()
|
|
{
|
|
pRequest = NULL;
|
|
m_fStrResCallBack = nullptr;
|
|
m_fJsonResCallBack = nullptr;
|
|
}
|
|
HttpRequest* pRequest;
|
|
std::function<void(std::string)> m_fStrResCallBack;
|
|
std::function<void(rapidjson::Document*)> m_fJsonResCallBack;
|
|
std::string kImagicUrlName;
|
|
};
|
|
typedef std::vector<SendStructInfo> SendStructInfoList;
|
|
|
|
class CWebReq :public Ref
|
|
{
|
|
public:
|
|
static CWebReq& getInstance(){
|
|
static CWebReq _instance;
|
|
return _instance;
|
|
}
|
|
|
|
public:
|
|
void sendRequestStr(std::string kUrl
|
|
,std::function<void(std::string)> _callBack1);
|
|
void sendRequestDocument(std::string kUrl
|
|
,std::function<void(rapidjson::Document*)> _callBack1);
|
|
void sendRequestDocumentUrl(std::string kUrl
|
|
,std::function<void(rapidjson::Document*)> _callBack1
|
|
,std::function<void(std::string)> _callBack2);
|
|
|
|
public: //request
|
|
std::string getWebIp();
|
|
std::string getSocketIP();
|
|
void CB_IP(rapidjson::Document* pDoc);
|
|
|
|
std::string getPostData(std::map<std::string,std::string>& dataMap);
|
|
std::string joinMapToData(std::map<std::string, std::string> ¶menters);
|
|
|
|
std::string parseResData(HttpResponse *response);
|
|
|
|
HttpRequest* getFile(std::string kUrl,const ccHttpRequestCallback &callback);
|
|
HttpRequest* getFileEx(std::string kUrl,const ccHttpRequestCallback &callback);
|
|
protected://resqunse
|
|
void resCallBack(HttpClient* client,HttpResponse* response);
|
|
|
|
bool parseResByJson(HttpResponse* response,rapidjson::Document &doc);
|
|
bool parseDataByJson(const std::string& data, rapidjson::Document &doc);
|
|
public:
|
|
template<typename T>
|
|
static void pushValue(std::string& kUrl,std::string kKey,T kValue)
|
|
{
|
|
pushValue(kUrl,kKey, utility::toString(kValue));
|
|
}
|
|
static void pushValue(std::string& kUrl,std::string kKey,std::string kValue);
|
|
template<typename T>
|
|
static std::string getDataValueStr(T* kValue,std::string kName)
|
|
{
|
|
if ((*kValue).HasMember(kName.c_str())&&((*kValue)[kName.c_str()].IsString()))
|
|
{
|
|
return (*kValue)[kName.c_str()].GetString();
|
|
}
|
|
return "";
|
|
}
|
|
template<typename T>
|
|
static int getDataValueInt(T* kValue,std::string kName)
|
|
{
|
|
if ((*kValue).HasMember(kName.c_str())&&((*kValue)[kName.c_str()].IsInt()))
|
|
{
|
|
return (*kValue)[kName.c_str()].GetInt();
|
|
}
|
|
return 0;
|
|
}
|
|
template<typename T>
|
|
static int getDataValueDouble(T* kValue,std::string kName)
|
|
{
|
|
if ((*kValue).HasMember(kName.c_str())&&((*kValue)[kName.c_str()].IsDouble()))
|
|
{
|
|
return (*kValue)[kName.c_str()].GetDouble();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void init();
|
|
|
|
private:
|
|
CWebReq();
|
|
|
|
std::string m_kWebIP;
|
|
std::string m_kSocketIP;
|
|
SendStructInfoList m_kCBList;
|
|
};
|